From: Dietmar Maurer <dietmar@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [RFC proxmox 15/22] firewall-api-types: add FirewallRule type
Date: Mon, 16 Feb 2026 11:43:53 +0100 [thread overview]
Message-ID: <20260216104401.3959270-16-dietmar@proxmox.com> (raw)
In-Reply-To: <20260216104401.3959270-1-dietmar@proxmox.com>
Extracted from Perl API and migrated to Rust with type-safe implementations.
Replaced string-based types with strongly-typed Rust counterparts including:
- FirewallAction (Accept, Reject, Drop)
- FirewallLogLevel
- FirewallDirection (In, Out, Group)
- FirewallAddressMatch for source/destination addresses
- FirewallPortList for port specifications
- FirewallIcmpType for ICMP type matching
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
---
proxmox-firewall-api-types/src/lib.rs | 3 +
proxmox-firewall-api-types/src/rule.rs | 192 +++++++++++++++++++++++++
2 files changed, 195 insertions(+)
create mode 100644 proxmox-firewall-api-types/src/rule.rs
diff --git a/proxmox-firewall-api-types/src/lib.rs b/proxmox-firewall-api-types/src/lib.rs
index c6f00250..25e260c3 100644
--- a/proxmox-firewall-api-types/src/lib.rs
+++ b/proxmox-firewall-api-types/src/lib.rs
@@ -37,3 +37,6 @@ mod port;
pub use port::{
FirewallPortList, FirewallPortListEntry, FIREWALL_DPORT_API_SCHEMA, FIREWALL_SPORT_API_SCHEMA,
};
+
+mod rule;
+pub use rule::{FirewallRule, FirewallRuleType};
diff --git a/proxmox-firewall-api-types/src/rule.rs b/proxmox-firewall-api-types/src/rule.rs
new file mode 100644
index 00000000..a2298609
--- /dev/null
+++ b/proxmox-firewall-api-types/src/rule.rs
@@ -0,0 +1,192 @@
+use serde::{Deserialize, Serialize};
+
+#[cfg(feature = "enum-fallback")]
+use proxmox_fixed_string::FixedString;
+use proxmox_schema::{api, const_regex, ApiStringFormat};
+
+use crate::{FirewallAddressMatch, FirewallIcmpType, FirewallPortList};
+use crate::{FIREWALL_DPORT_API_SCHEMA, FIREWALL_SPORT_API_SCHEMA};
+use super::FirewallLogLevel;
+
+const_regex! {
+ FIREWALL_RULE_IFACE_RE = r##"^[a-zA-Z][a-zA-Z0-9_]{1,20}([:\.]\d+)?$"##;
+ FIREWALL_SECURITY_GROUP_RE = r##"^[A-Za-z][A-Za-z0-9\-\_]+$"##;
+}
+
+#[api(
+ properties: {
+ action: {
+ format: &ApiStringFormat::Pattern(&FIREWALL_SECURITY_GROUP_RE),
+ max_length: 20,
+ min_length: 2,
+ type: String,
+ },
+ comment: {
+ optional: true,
+ type: String,
+ },
+ dest: {
+ optional: true,
+ },
+ digest: {
+ max_length: 64,
+ optional: true,
+ type: String,
+ },
+ dport: {
+ optional: true,
+ schema: FIREWALL_DPORT_API_SCHEMA,
+ },
+ enable: {
+ minimum: 0,
+ optional: true,
+ type: Integer,
+ },
+ "icmp-type": {
+ optional: true,
+ },
+ iface: {
+ format: &ApiStringFormat::Pattern(&FIREWALL_RULE_IFACE_RE),
+ max_length: 20,
+ min_length: 2,
+ optional: true,
+ type: String,
+ },
+ log: {
+ optional: true,
+ },
+ "macro": {
+ max_length: 128,
+ optional: true,
+ type: String,
+ },
+ pos: {
+ minimum: 0,
+ optional: true,
+ type: Integer,
+ },
+ proto: {
+ max_length: 64, // arbitrary limit, much longer than anything in /etc/protocols
+ optional: true,
+ type: String,
+ },
+ source: {
+ optional: true,
+ },
+ sport: {
+ optional: true,
+ schema: FIREWALL_SPORT_API_SCHEMA,
+ },
+ type: {
+ type: FirewallRuleType,
+ },
+ },
+)]
+/// Firewall Rule.
+#[derive(Debug, serde::Deserialize, serde::Serialize)]
+pub struct FirewallRule {
+ /// Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.
+ pub action: String,
+
+ /// Descriptive comment.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub comment: Option<String>,
+
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub dest: Option<FirewallAddressMatch>,
+
+ /// Prevent changes if current configuration file has a different digest.
+ /// This can be used to prevent concurrent modifications.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub digest: Option<String>,
+
+ /// Restrict TCP/UDP destination port.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub dport: Option<FirewallPortList>,
+
+ /// Flag to enable/disable a rule.
+ #[serde(deserialize_with = "proxmox_serde::perl::deserialize_u64")]
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub enable: Option<u64>,
+
+ /// Specify icmp-type. Only valid if proto equals 'icmp' or
+ /// 'icmpv6'/'ipv6-icmp'.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ #[serde(rename = "icmp-type")]
+ pub icmp_type: Option<FirewallIcmpType>,
+
+ /// Network interface name. You have to use network configuration key names
+ /// for VMs and containers ('net\d+'). Host related rules can use arbitrary
+ /// strings.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub iface: Option<String>,
+
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub log: Option<FirewallLogLevel>,
+
+ /// Use predefined standard macro.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ #[serde(rename = "macro")]
+ pub r#macro: Option<String>,
+
+ /// Update rule at position <pos>.
+ #[serde(deserialize_with = "proxmox_serde::perl::deserialize_u64")]
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub pos: Option<u64>,
+
+ /// IP protocol. You can use protocol names ('tcp'/'udp') or simple numbers,
+ /// as defined in '/etc/protocols'.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub proto: Option<String>,
+
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub source: Option<FirewallAddressMatch>,
+
+ /// Restrict TCP/UDP source port.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub sport: Option<FirewallPortList>,
+
+ #[serde(rename = "type")]
+ pub ty: FirewallRuleType,
+}
+
+#[api]
+/// Rule type.
+#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
+pub enum FirewallRuleType {
+ #[serde(rename = "in")]
+ /// in.
+ In,
+ #[serde(rename = "out")]
+ /// out.
+ Out,
+ #[serde(rename = "forward")]
+ /// forward.
+ Forward,
+ #[serde(rename = "group")]
+ /// group.
+ Group,
+ /// Unknown variants for forward compatibility.
+ #[cfg(feature = "enum-fallback")]
+ #[serde(untagged)]
+ UnknownEnumValue(FixedString),
+}
+
+serde_plain::derive_display_from_serialize!(FirewallRuleType);
+serde_plain::derive_fromstr_from_deserialize!(FirewallRuleType);
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn test_regex_compilation_firewall_rule_iface_re() {
+ use regex::Regex;
+ let _: &Regex = &FIREWALL_RULE_IFACE_RE;
+ }
+ #[test]
+ fn test_regex_compilation_firewall_security_group_re() {
+ use regex::Regex;
+ let _: &Regex = &FIREWALL_SECURITY_GROUP_RE;
+ }
+}
--
2.47.3
next prev parent reply other threads:[~2026-02-16 10:45 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-16 10:43 [RFC proxmox 00/22] New crate for firewall api types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 01/22] firewall-api-types: add new " Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 02/22] firewall-api-types: add README.md Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 03/22] firewall-api-types: add firewall policy types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 04/22] firewall-api-types: add logging types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 05/22] firewall-api-types: add FirewallClusterOptions Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 06/22] firewall-api-types: add FirewallGuestOptions Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 07/22] firewall-api-types: add FirewallConntrackHelper enum Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 08/22] firewall-api-types: add FirewallNodeOptions struct Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 09/22] firewall-api-types: add FirewallRef type Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 10/22] firewall-api-types: add FirewallPortList types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 11/22] firewall-api-types: add FirewallIcmpType Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 12/22] firewall-api-types: add FirewallIpsetReference type Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 13/22] firewall-api-types: add FirewallAliasReference type Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 14/22] firewall-api-types: add firewall address types Dietmar Maurer
2026-02-16 10:43 ` Dietmar Maurer [this message]
2026-02-16 10:43 ` [RFC proxmox 16/22] firewall-api-types: use ConfigDigest from proxmox-config-digest crate Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 17/22] firewall-api-types: use COMMENT_SCHEMA from proxmox-schema crate Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 18/22] firewall-api-types: add FirewallRuleUpdater type Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 19/22] firewall-api-types: refactor FirewallRule and add FirewallRuleListEntry Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 20/22] firewall-api-types: add DeletableFirewallRuleProperty enum Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 21/22] firewall-api-types: add FirewallAliasEntry API type Dietmar Maurer
2026-02-16 10:44 ` [RFC proxmox 22/22] firewall-api-types: add FirewallIpsetListEntry and FirewallIpsetEntry api types Dietmar Maurer
2026-02-17 6:17 ` [RFC proxmox 00/22] New crate for firewall " Hannes Laimer
2026-02-17 6:39 ` Dietmar Maurer
2026-02-17 8:17 ` Hannes Laimer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260216104401.3959270-16-dietmar@proxmox.com \
--to=dietmar@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox