From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 7CC991FF348 for ; Wed, 17 Apr 2024 16:03:42 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E4EC099AD; Wed, 17 Apr 2024 16:03:36 +0200 (CEST) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Date: Wed, 17 Apr 2024 15:53:49 +0200 Message-Id: <20240417135404.573490-25-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240417135404.573490-1-s.hanreich@proxmox.com> References: <20240417135404.573490-1-s.hanreich@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.305 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [types.rs] Subject: [pve-devel] [PATCH proxmox-firewall v2 24/39] nftables: types: add conversion traits X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Cc: Wolfgang Bumiller Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Some parts of the firewall config map directly to nftables objects, so we introduce conversion traits for convenient conversion into the respective nftables objects / types. They are guarded behind a feature, so the nftables crate can be used standalone without depending on the proxmox-ve-config crate. Reviewed-by: Lukas Wagner Reviewed-by: Max Carrara Co-authored-by: Wolfgang Bumiller Signed-off-by: Stefan Hanreich --- proxmox-nftables/src/types.rs | 80 ++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/proxmox-nftables/src/types.rs b/proxmox-nftables/src/types.rs index 90d3466..a83e958 100644 --- a/proxmox-nftables/src/types.rs +++ b/proxmox-nftables/src/types.rs @@ -7,6 +7,12 @@ use crate::{Expression, Statement}; use serde::{Deserialize, Serialize}; +#[cfg(feature = "config-ext")] +use proxmox_ve_config::firewall::types::address::Family; + +#[cfg(feature = "config-ext")] +use proxmox_ve_config::firewall::types::ipset::IpsetName; + #[cfg(feature = "config-ext")] use proxmox_ve_config::guest::types::Vmid; @@ -33,6 +39,15 @@ impl TableFamily { _ => vec![IpFamily::Ip, IpFamily::Ip6], } } + + #[cfg(feature = "config-ext")] + pub fn families(&self) -> Vec { + match self { + TableFamily::Ip => vec![Family::V4], + TableFamily::Ip6 => vec![Family::V6], + _ => vec![Family::V4, Family::V6], + } + } } #[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)] @@ -157,6 +172,21 @@ pub enum RateTimescale { Day, } +#[cfg(feature = "config-ext")] +use proxmox_ve_config::firewall::types::log::LogRateLimitTimescale; + +#[cfg(feature = "config-ext")] +impl From for RateTimescale { + fn from(value: LogRateLimitTimescale) -> Self { + match value { + LogRateLimitTimescale::Second => RateTimescale::Second, + LogRateLimitTimescale::Minute => RateTimescale::Minute, + LogRateLimitTimescale::Hour => RateTimescale::Hour, + LogRateLimitTimescale::Day => RateTimescale::Day, + } + } +} + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TableName { family: TableFamily, @@ -586,6 +616,44 @@ impl SetName { name: name.into(), } } + + pub fn name(&self) -> &str { + self.name.as_ref() + } + + #[cfg(feature = "config-ext")] + pub fn ipset_name( + family: Family, + name: &IpsetName, + vmid: Option, + nomatch: bool, + ) -> String { + use proxmox_ve_config::firewall::types::ipset::IpsetScope; + + let prefix = match family { + Family::V4 => "v4", + Family::V6 => "v6", + }; + + let name = match name.scope() { + IpsetScope::Datacenter => name.to_string(), + IpsetScope::Guest => { + if let Some(vmid) = vmid { + format!("guest-{vmid}/{}", name.name()) + } else { + log::warn!("Creating IPSet for guest without vmid parameter!"); + name.to_string() + } + } + }; + + let suffix = match nomatch { + true => "-nomatch", + false => "", + }; + + format!("{prefix}-{name}{suffix}") + } } #[derive(Clone, Debug, Deserialize, Serialize)] @@ -788,7 +856,17 @@ pub enum L3Protocol { Ip6, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg(feature = "config-ext")] +impl From for L3Protocol { + fn from(value: Family) -> Self { + match value { + Family::V4 => L3Protocol::Ip, + Family::V6 => L3Protocol::Ip6, + } + } +} + +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] pub enum CtHelperProtocol { TCP, -- 2.39.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel