From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 221701FF0AA for ; Tue, 17 Nov 2026 19:35:52 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 75D9621731; Tue, 17 Nov 2026 19:35:14 +0100 (CET) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-ve-rs v2 05/11] ve-config: sdn: wireguard: add helper for resolving the peers of an interface Date: Tue, 17 Nov 2026 19:34:52 +0100 Message-ID: <20261117183500.750636-6-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261117183500.750636-1-s.hanreich@proxmox.com> References: <20261117183500.750636-1-s.hanreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794940507426 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.261 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 65PY6XL4DAWWM5HN4H37XQ7M5IWQLSFF X-Message-ID-Hash: 65PY6XL4DAWWM5HN4H37XQ7M5IWQLSFF X-MailFrom: s.hanreich@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: This function will be used by the status reporting, which requires the ability to match a peer from the `wg show` output to the respective node in the section config, in order to include the corresponding node/interface in its informational output. This helps users matching peers from the running WireGuard configuration to their respective section config entry. It also allows checking whether the running WireGuard configuration contains exactly the configured peers. Signed-off-by: Stefan Hanreich --- proxmox-ve-config/src/sdn/fabric/mod.rs | 88 ++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/proxmox-ve-config/src/sdn/fabric/mod.rs b/proxmox-ve-config/src/sdn/fabric/mod.rs index 79bb1fd9..d4687fb1 100644 --- a/proxmox-ve-config/src/sdn/fabric/mod.rs +++ b/proxmox-ve-config/src/sdn/fabric/mod.rs @@ -7,6 +7,7 @@ use std::marker::PhantomData; use std::ops::Deref; use anyhow::Error; +use proxmox_wireguard::PublicKey; use section_config::protocol::wireguard::WireGuardProperties; use serde::{Deserialize, Serialize}; @@ -34,7 +35,8 @@ use crate::sdn::fabric::section_config::protocol::ospf::{ OspfNodePropertiesUpdater, OspfProperties, OspfPropertiesUpdater, }; use crate::sdn::fabric::section_config::protocol::wireguard::{ - WireGuardDeletableProperties, WireGuardNode, WireGuardNodeDeletableProperties, + ExternalWireGuardNode, InternalWireGuardNode, WireGuardDeletableProperties, + WireGuardInterfaceProperties, WireGuardNode, WireGuardNodeDeletableProperties, WireGuardNodePeer, WireGuardNodeUpdater, WireGuardPropertiesUpdater, }; use crate::sdn::fabric::section_config::{FabricOrNode, Section}; @@ -215,6 +217,90 @@ impl_entry!(Ospf, OspfProperties, OspfNodeProperties); impl_entry!(WireGuard, WireGuardProperties, WireGuardNode); impl_entry!(Bgp, BgpProperties, BgpNode); +/// A configured peer of a WireGuard interface, resolved to the node it refers to. +pub enum WireGuardInterfacePeer<'a> { + /// A peer that refers to an interface of an internal node. + Internal { + node_id: &'a NodeId, + node: &'a InternalWireGuardNode, + interface: &'a WireGuardInterfaceProperties, + }, + /// A peer that refers to an external node. + External { + node_id: &'a NodeId, + node: &'a ExternalWireGuardNode, + }, +} + +impl WireGuardInterfacePeer<'_> { + /// Returns the public key of the peer. + pub fn public_key(&self) -> &PublicKey { + match self { + Self::Internal { interface, .. } => interface.public_key(), + Self::External { node, .. } => &node.public_key, + } + } +} + +impl Entry { + /// Resolve the configured peers of an interface on a node to the nodes they refer to. + /// + /// These are the same peers with the same public keys as in the generated WireGuard + /// configuration of the interface. + pub fn interface_peers( + &self, + local_node_id: &NodeId, + interface: &str, + ) -> Result>, Error> { + let Node::WireGuard(wireguard_node) = self.get_node(local_node_id)? else { + anyhow::bail!("no wireguard node with id {local_node_id} found"); + }; + + let WireGuardNode::Internal(internal_node) = wireguard_node.properties() else { + anyhow::bail!("wireguard node with id {local_node_id} is not an internal node"); + }; + + let mut peers = Vec::new(); + + for peer in internal_node + .peers() + .filter(|peer| peer.iface().as_ref() == interface) + { + let referenced_wireguard_node = self.node_section(peer.node())?; + + match (referenced_wireguard_node.properties(), peer) { + ( + WireGuardNode::Internal(referenced_internal_node), + WireGuardNodePeer::Internal(internal_peer), + ) => { + let referenced_interface = referenced_internal_node + .interfaces() + .find(|interface| interface.name() == &internal_peer.node_iface) + .ok_or_else(|| anyhow::anyhow!("referenced interface does not exist"))?; + + peers.push(WireGuardInterfacePeer::Internal { + node_id: peer.node(), + node: referenced_internal_node, + interface: referenced_interface, + }); + } + ( + WireGuardNode::External(referenced_external_node), + WireGuardNodePeer::External(_), + ) => { + peers.push(WireGuardInterfacePeer::External { + node_id: peer.node(), + node: referenced_external_node, + }); + } + _ => anyhow::bail!("invalid combination of peer / node types"), + } + } + + Ok(peers) + } +} + /// All possible entries in a [`FabricConfig`]. /// /// It utilizes the [`Entry`] struct to validate proper combinations of [`FabricSection`] and -- 2.47.3