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 F33A91FF187 for ; Tue, 2 Dec 2025 13:53:36 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 31CEAF26F; Tue, 2 Dec 2025 13:54:00 +0100 (CET) From: Robert Obkircher To: pve-devel@lists.proxmox.com Date: Tue, 2 Dec 2025 13:52:46 +0100 Message-ID: <20251202125343.91927-1-r.obkircher@proxmox.com> X-Mailer: git-send-email 2.47.3 MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1764679991679 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.072 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches 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. [rule.rs] Subject: [pve-devel] [PATCH v1 proxmox-firewall] fix #7068: show rule comments in nftables output 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Add rule comments from the config file to the generated nft rules. Truncate them to at most 128 bytes to match the limit in libnftnl. Signed-off-by: Robert Obkircher --- proxmox-firewall/src/rule.rs | 37 +++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/proxmox-firewall/src/rule.rs b/proxmox-firewall/src/rule.rs index b79f91c..f6c5e44 100644 --- a/proxmox-firewall/src/rule.rs +++ b/proxmox-firewall/src/rule.rs @@ -36,14 +36,19 @@ pub(crate) struct NftRule { family: Option, statements: Vec, terminal_statements: Vec, + comment: Option, } impl NftRule { + /// from NFTNL_UDATA_COMMENT_MAXLEN + pub const MAX_COMMENT_LEN: usize = 128; + pub fn from_terminal_statements(terminal_statements: Vec) -> Self { Self { family: None, statements: Vec::new(), terminal_statements, + comment: None, } } @@ -52,6 +57,7 @@ impl NftRule { family: None, statements: Vec::new(), terminal_statements: vec![terminal_statement], + comment: None, } } @@ -81,6 +87,24 @@ impl NftRule { ipfilter.to_nft_rules(&mut rules, env)?; Ok(rules) } + + pub fn set_comment(&mut self, comment: &str) { + self.comment = + Some(comment[..my_floor_char_boundary(comment, Self::MAX_COMMENT_LEN)].to_string()); + } +} + +// TODO: replace with str::floor_char_boundary once rustc 1.91.0 is available +fn my_floor_char_boundary(s: &str, index: usize) -> usize { + if index >= s.len() { + s.len() + } else { + s.char_indices() + .map(|(i, _)| i) + .take_while(|i| *i <= index) + .last() + .unwrap_or(0) + } } impl Deref for NftRule { @@ -101,7 +125,12 @@ impl NftRule { pub fn into_add_rule(self, chain: ChainPart) -> AddRule { let statements = self.statements.into_iter().chain(self.terminal_statements); - AddRule::from_statements(chain, statements) + let result = AddRule::from_statements(chain, statements); + if let Some(comment) = self.comment { + result.with_comment(comment) + } else { + result + } } pub fn family(&self) -> Option { @@ -175,11 +204,17 @@ impl ToNftRules for Rule { fn to_nft_rules(&self, rules: &mut Vec, env: &NftRuleEnv) -> Result<(), Error> { log::trace!("generating nft rules for config rule {self:?}"); + let before = rules.len(); match self.kind() { Kind::Match(rule) => rule.to_nft_rules(rules, env)?, Kind::Group(group) => group.to_nft_rules(rules, env)?, }; + if let Some(comment) = self.comment() { + for nft_rule in &mut rules[before..] { + nft_rule.set_comment(comment); + } + } Ok(()) } } -- 2.47.3 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel