← Back to Pull requests

fix: allow broadcast and multicast traffic to reach container via por… #1494

Open
Rickydama3 opened this pull request with 112 lines diff across 3 files
View on GitHub
Rickydama3 Rickydama3 commented on Aug 06, 2026 22:51 UTC
Author

Fixes #1466

Context & Problem Statement


Currently, when a system broadcasts UDP packets on a network, rootful containers cannot receive that traffic via port forwarding (e.g., -p 10600:10600/udp). This occurs because the netavark nftables rules use fib daddr type local for the PREROUTING and OUTPUT chains, which drops broadcast and multicast traffic before it reaches the DNAT chain for evaluation.

Proposed Solution


This PR updates the FIB match rule to use an anonymous set { local, broadcast, multicast }. This allows broadcast and multicast packets to successfully route to the NETAVARK-HOSTPORT-DNAT chain. Security isolation is fully preserved because the DNAT chain acts as the primary filter—only packets that map to an explicitly published container port will be forwarded.

Impact & Benefits


Containers can now natively receive broadcast and multicast UDP traffic via standard port publishing. This aligns rootful container behavior with user expectations and eliminates the need for complex host-level workarounds (such as modifying route_localnet sysctls or relying on socat for unicast redirection).

Diff: 112 lines (+86 / -26) across 3 files Reviewer: Luap99 Review duration: 170569m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.41) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.41 · 112 lines diff across 3 files
0.41
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.94 · 5 substantive comment(s)
0.94
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.283
Real PR Conversation Timeline
15 items
mheon mheon commented on Aug 07, 2026 12:08 UTC
Contributor

Code LGTM. Any reasonable way of testing this?

Rickydama3 Rickydama3 commented on Aug 07, 2026 13:32 UTC
Author

@mheon It looks like the CI is failing on the podman-* integration tests now.

I checked into it, and it seems Podman's system tests are strictly asserting the exact output of the nftables rules. Since the PR now generates three independent rules (local, broadcast, and multicast) in the PREROUTING and OUTPUT chains to remain compatible with older nftables on CentOS 10, it's causing the Podman tests to fail because they expect the old single-rule output.

Since netavark's own tests pass with this change, how would you like to proceed? Should we get a companion PR opened in the podman repo to update their expected test outputs?

Luap99 Luap99 commented on Aug 07, 2026 16:36 UTC
Contributor

This has nothing to do with podman test. netavark tests fail in the same way.

Error: Basetype of type fib address type is not bitmask

Did you actually test this end to end locally?

Regardless this requires a proper integration test in test/ that verifies this works with broadcast and multicast traffic, just comparing the rule is not good enough for testing

Rickydama3 Rickydama3 commented on Aug 07, 2026 23:36 UTC
Author

Apologies The CI checks are finally all green, but here is what happened with those failures:

First, those integration test failures were actually because of the new E2E broadcast test I added! I originally wrote it using socat, but it turns out socat isn't installed on the Fedora and CentOS test nodes, which caused the script to fail. To fix this and keep things native, I removed the socat dependency entirely. Instead, I updated our built-in netavark-connection-tester to set the SO_BROADCAST socket option, and I am now using that to natively verify the broadcast traffic!

Secondly, that single validate check failure was just me accidentally leaving a stray debug statement in nft.rs which ended up breaking cargo fmt.

I've pushed the fixes for both—the code is properly formatted, the syntax errors are gone, and the E2E broadcast test runs flawlessly without external dependencies. Let me know if everything looks good to merge.

Luap99 Luap99 commented on code on Aug 11, 2026 11:15 UTC
Code Review
src/firewall/nft.rs:1
@@ -197,34 +197,27 @@ impl firewall::FirewallDriver for Nftables {
         // We need rules in Prerouting and Output pointing to our dnat chain.
         // But only if they do not exist.
         let match_jump_dnat = get_rule_matcher_jump_to(DNATCHAIN.to_string());
-        // Prerouting: fib daddr type local jump <dnat_chain>
-        // Output: fib daddr type local jump <dnat_chain>
+        // Prerouting: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
+        // Output: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
         let mut rules_hash: HashSet<expr::FibFlag> = HashSet::new();
         rules_hash.insert(expr::FibFlag::Daddr);
-        let base_conditions = [
-            stmt::Statement::Match(stmt::Match {
-                left: expr::Expression::Named(expr::NamedExpression::Fib(expr::Fib {
-                    result: expr::FibResult::Type,
-                    flags: rules_hash,
-                })),
-                right: expr::Expression::String(Cow::Borrowed("local")),
-                op: stmt::Operator::EQ,
-            }),
-            get_jump_action(Cow::Borrowed(DNATCHAIN)),
-        ];
-        if get_matching_rules_in_chain(&existing_rules, PREROUTINGCHAIN, &match_jump_dnat)
-            .is_empty()
-        {
-            batch.add(make_rule(
-                Cow::Borrowed(PREROUTINGCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
-        }
-        if get_matching_rules_in_chain(&existing_rules, OUTPUTCHAIN, &match_jump_dnat).is_empty() {
-            batch.add(make_rule(
-                Cow::Borrowed(OUTPUTCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
+        for chain in [PREROUTINGCHAIN, OUTPUTCHAIN] {
+            if get_matching_rules_in_chain(&existing_rules, chain, &match_jump_dnat).is_empty() {
+                for fib_type in ["local", "broadcast", "multicast"] {

this is adding three separate rules which I guess is less effient that using the proper map match of { local, broadcast, multicast } At the very least these rule do not match the statement above.

I think we should be using the map syntax as it is less verbose

Luap99 Luap99 commented on code on Aug 11, 2026 11:17 UTC
Code Review
src/test/main.rs:1
@@ -103,6 +103,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
     )
     .expect("connect socket");
 
+    if sock_type == socket::SockType::Datagram {
+        socket::setsockopt(&connect_sock, socket::sockopt::Broadcast, &true)
+            .expect("set SO_BROADCAST");
+    }

this needs a proper opt in option so we do not use broadcast when we should not

Luap99 Luap99 requested changes on Aug 11, 2026 11:19 UTC
Changes Requested Reviewer

also please squash the commits

Rickydama3 Rickydama3 commented on code on Aug 13, 2026 21:32 UTC
Code Review
src/firewall/nft.rs:1
@@ -197,34 +197,27 @@ impl firewall::FirewallDriver for Nftables {
         // We need rules in Prerouting and Output pointing to our dnat chain.
         // But only if they do not exist.
         let match_jump_dnat = get_rule_matcher_jump_to(DNATCHAIN.to_string());
-        // Prerouting: fib daddr type local jump <dnat_chain>
-        // Output: fib daddr type local jump <dnat_chain>
+        // Prerouting: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
+        // Output: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
         let mut rules_hash: HashSet<expr::FibFlag> = HashSet::new();
         rules_hash.insert(expr::FibFlag::Daddr);
-        let base_conditions = [
-            stmt::Statement::Match(stmt::Match {
-                left: expr::Expression::Named(expr::NamedExpression::Fib(expr::Fib {
-                    result: expr::FibResult::Type,
-                    flags: rules_hash,
-                })),
-                right: expr::Expression::String(Cow::Borrowed("local")),
-                op: stmt::Operator::EQ,
-            }),
-            get_jump_action(Cow::Borrowed(DNATCHAIN)),
-        ];
-        if get_matching_rules_in_chain(&existing_rules, PREROUTINGCHAIN, &match_jump_dnat)
-            .is_empty()
-        {
-            batch.add(make_rule(
-                Cow::Borrowed(PREROUTINGCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
-        }
-        if get_matching_rules_in_chain(&existing_rules, OUTPUTCHAIN, &match_jump_dnat).is_empty() {
-            batch.add(make_rule(
-                Cow::Borrowed(OUTPUTCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
+        for chain in [PREROUTINGCHAIN, OUTPUTCHAIN] {
+            if get_matching_rules_in_chain(&existing_rules, chain, &match_jump_dnat).is_empty() {
+                for fib_type in ["local", "broadcast", "multicast"] {

Yeah I agree the { local, broadcast, multicast } syntax is way cleaner. I actually tried doing exactly that at first, but it ended up failing the centos 10 CI tests. It threw a Basetype of type fib address type is not bitmask error.

Looks like the older nftables version on centos 10 doesn't flag fib address type as a bitmask, so it just rejects the set matching entirely. That's why I had to split it into three separate rules as a workaround. Let me know if there's a better way to handle it for centos 10 though

Rickydama3 Rickydama3 commented on code on Aug 13, 2026 21:33 UTC
Code Review
src/test/main.rs:1
@@ -103,6 +103,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
     )
     .expect("connect socket");
 
+    if sock_type == socket::SockType::Datagram {
+        socket::setsockopt(&connect_sock, socket::sockopt::Broadcast, &true)
+            .expect("set SO_BROADCAST");
+    }

Thanks for your guidance, I've added a --broadcast flag to the connection tester so it's fully opt-in now.

ajoshua2004 ajoshua2004 commented on code on Oct 07, 2026 18:46 UTC
Code Review
src/firewall/nft.rs:1
@@ -197,34 +197,27 @@ impl firewall::FirewallDriver for Nftables {
         // We need rules in Prerouting and Output pointing to our dnat chain.
         // But only if they do not exist.
         let match_jump_dnat = get_rule_matcher_jump_to(DNATCHAIN.to_string());
-        // Prerouting: fib daddr type local jump <dnat_chain>
-        // Output: fib daddr type local jump <dnat_chain>
+        // Prerouting: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
+        // Output: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
         let mut rules_hash: HashSet<expr::FibFlag> = HashSet::new();
         rules_hash.insert(expr::FibFlag::Daddr);
-        let base_conditions = [
-            stmt::Statement::Match(stmt::Match {
-                left: expr::Expression::Named(expr::NamedExpression::Fib(expr::Fib {
-                    result: expr::FibResult::Type,
-                    flags: rules_hash,
-                })),
-                right: expr::Expression::String(Cow::Borrowed("local")),
-                op: stmt::Operator::EQ,
-            }),
-            get_jump_action(Cow::Borrowed(DNATCHAIN)),
-        ];
-        if get_matching_rules_in_chain(&existing_rules, PREROUTINGCHAIN, &match_jump_dnat)
-            .is_empty()
-        {
-            batch.add(make_rule(
-                Cow::Borrowed(PREROUTINGCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
-        }
-        if get_matching_rules_in_chain(&existing_rules, OUTPUTCHAIN, &match_jump_dnat).is_empty() {
-            batch.add(make_rule(
-                Cow::Borrowed(OUTPUTCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
+        for chain in [PREROUTINGCHAIN, OUTPUTCHAIN] {
+            if get_matching_rules_in_chain(&existing_rules, chain, &match_jump_dnat).is_empty() {
+                for fib_type in ["local", "broadcast", "multicast"] {
+                    let base_conditions = vec![
+                        stmt::Statement::Match(stmt::Match {
+                            left: expr::Expression::Named(expr::NamedExpression::Fib(expr::Fib {
+                                result: expr::FibResult::Type,
+                                flags: rules_hash.clone(),
+                            })),
+                            right: expr::Expression::String(Cow::Borrowed(fib_type)),

The CentOS 10 bitmask error is probably from serializing a bare JSON array, you could try NamedExpression::Set(vec![...]) so this becomes one fib daddr type { local, broadcast, multicast } rule instead of 3?

ajoshua2004 ajoshua2004 commented on code on Oct 07, 2026 18:48 UTC
Code Review
src/firewall/nft.rs:1
@@ -197,34 +197,27 @@ impl firewall::FirewallDriver for Nftables {
         // We need rules in Prerouting and Output pointing to our dnat chain.
         // But only if they do not exist.
         let match_jump_dnat = get_rule_matcher_jump_to(DNATCHAIN.to_string());
-        // Prerouting: fib daddr type local jump <dnat_chain>
-        // Output: fib daddr type local jump <dnat_chain>
+        // Prerouting: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
+        // Output: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
         let mut rules_hash: HashSet<expr::FibFlag> = HashSet::new();
         rules_hash.insert(expr::FibFlag::Daddr);
-        let base_conditions = [
-            stmt::Statement::Match(stmt::Match {
-                left: expr::Expression::Named(expr::NamedExpression::Fib(expr::Fib {
-                    result: expr::FibResult::Type,
-                    flags: rules_hash,
-                })),
-                right: expr::Expression::String(Cow::Borrowed("local")),
-                op: stmt::Operator::EQ,
-            }),
-            get_jump_action(Cow::Borrowed(DNATCHAIN)),
-        ];
-        if get_matching_rules_in_chain(&existing_rules, PREROUTINGCHAIN, &match_jump_dnat)
-            .is_empty()
-        {
-            batch.add(make_rule(
-                Cow::Borrowed(PREROUTINGCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
-        }
-        if get_matching_rules_in_chain(&existing_rules, OUTPUTCHAIN, &match_jump_dnat).is_empty() {
-            batch.add(make_rule(
-                Cow::Borrowed(OUTPUTCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
+        for chain in [PREROUTINGCHAIN, OUTPUTCHAIN] {
+            if get_matching_rules_in_chain(&existing_rules, chain, &match_jump_dnat).is_empty() {

This matches any existing jump to the DNAT chain, so hosts that already have the old local only rule never get the new match until the table is flushed

ajoshua2004 ajoshua2004 commented on code on Oct 07, 2026 18:49 UTC
Code Review
src/firewall/nft.rs:1
@@ -197,34 +197,27 @@ impl firewall::FirewallDriver for Nftables {
         // We need rules in Prerouting and Output pointing to our dnat chain.
         // But only if they do not exist.
         let match_jump_dnat = get_rule_matcher_jump_to(DNATCHAIN.to_string());
-        // Prerouting: fib daddr type local jump <dnat_chain>
-        // Output: fib daddr type local jump <dnat_chain>
+        // Prerouting: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
+        // Output: fib daddr type { local, broadcast, multicast } jump <dnat_chain>
         let mut rules_hash: HashSet<expr::FibFlag> = HashSet::new();
         rules_hash.insert(expr::FibFlag::Daddr);
-        let base_conditions = [
-            stmt::Statement::Match(stmt::Match {
-                left: expr::Expression::Named(expr::NamedExpression::Fib(expr::Fib {
-                    result: expr::FibResult::Type,
-                    flags: rules_hash,
-                })),
-                right: expr::Expression::String(Cow::Borrowed("local")),
-                op: stmt::Operator::EQ,
-            }),
-            get_jump_action(Cow::Borrowed(DNATCHAIN)),
-        ];
-        if get_matching_rules_in_chain(&existing_rules, PREROUTINGCHAIN, &match_jump_dnat)
-            .is_empty()
-        {
-            batch.add(make_rule(
-                Cow::Borrowed(PREROUTINGCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
-        }
-        if get_matching_rules_in_chain(&existing_rules, OUTPUTCHAIN, &match_jump_dnat).is_empty() {
-            batch.add(make_rule(
-                Cow::Borrowed(OUTPUTCHAIN),
-                Cow::Borrowed(&base_conditions),
-            ));
+        for chain in [PREROUTINGCHAIN, OUTPUTCHAIN] {

is multicast needed in OUTPUT? With it, the host's own multicast to a forwarded port gets DNATed to the container

ajoshua2004 ajoshua2004 commented on code on Oct 07, 2026 18:52 UTC
Code Review
test/250-bridge-nftables.bats:1
@@ -517,6 +517,62 @@ export NETAVARK_FW=nftables
     test_port_fw ip=dual proto=udp
 }
 
+@test "$fw_driver - port forwarding ipv4 - udp broadcast" {
+    local host_port=$(random_port)
+    local container_port=$(random_port)
+    local container_id=$(random_string 64)
+    local container_name="name-$(random_string 10)"
+    local ipv4_subnet=$(random_subnet)
+    local ipv4_gateway=$(gateway_from_subnet $ipv4_subnet)
+    local ipv4_container_ip=$(random_ip_in_subnet $ipv4_subnet)
+    local broadcast_ip=$(echo $ipv4_subnet | sed "s/\.0\/24/.255/")
+
+    read -r -d '\0' config <<EOF

I feel like it would be cleaner if you made this into a json file in test/testfiles and be loaded in

ajoshua2004 ajoshua2004 requested changes on Oct 07, 2026 18:55 UTC
Changes Requested Reviewer

Requested changes.

Reviewers
Assignees
Rickydama3 Rickydama3
Labels
risk: low confidence: 90.6% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 41.2% (MEDIUM) · Higher = larger blast radius
41.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 90.6% · Lower = rushed or shallow review
90.6%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 3.9% · Safe to merge
3.9%