Skip to main content

nucleus/network/
bridge.rs

1use crate::error::{NucleusError, Result, StateTransition};
2use crate::network::config::{BridgeConfig, EgressPolicy, PortForward};
3use crate::network::NetworkState;
4use std::process::Command;
5use tracing::{debug, info, warn};
6
7/// Bridge network manager
8pub struct BridgeNetwork {
9    config: BridgeConfig,
10    container_ip: String,
11    veth_host: String,
12    container_id: String,
13    prev_ip_forward: Option<String>,
14    state: NetworkState,
15}
16
17impl BridgeNetwork {
18    /// Set up bridge networking for a container
19    ///
20    /// Creates bridge, veth pair, assigns IPs, enables NAT.
21    /// Must be called from the parent process after fork (needs host netns).
22    ///
23    /// State transitions: Unconfigured -> Configuring -> Active
24    pub fn setup(pid: u32, config: &BridgeConfig) -> Result<Self> {
25        Self::setup_for(pid, config, &format!("{:x}", pid))
26    }
27
28    /// Set up bridge networking with an explicit container ID for IP tracking.
29    pub fn setup_with_id(pid: u32, config: &BridgeConfig, container_id: &str) -> Result<Self> {
30        Self::setup_for(pid, config, container_id)
31    }
32
33    fn setup_for(pid: u32, config: &BridgeConfig, container_id: &str) -> Result<Self> {
34        // Validate all network parameters before using them in shell commands
35        config.validate()?;
36
37        let mut net_state = NetworkState::Unconfigured;
38        net_state = net_state.transition(NetworkState::Configuring)?;
39
40        let alloc_dir = Self::ip_alloc_dir();
41        let container_ip = Self::reserve_ip_in_dir(
42            &alloc_dir,
43            container_id,
44            &config.subnet,
45            config.container_ip.as_deref(),
46        )?;
47        let prefix = Self::subnet_prefix(&config.subnet);
48
49        // Linux interface names max 15 chars; truncate if needed
50        let veth_host_full = format!("veth-{:x}", pid);
51        let veth_cont_full = format!("vethc-{:x}", pid);
52        let veth_host = veth_host_full[..veth_host_full.len().min(15)].to_string();
53        let veth_container = veth_cont_full[..veth_cont_full.len().min(15)].to_string();
54        let mut rollback = SetupRollback::new(
55            veth_host.clone(),
56            config.subnet.clone(),
57            Some((alloc_dir.clone(), container_id.to_string())),
58        );
59
60        // 1. Create bridge if it doesn't exist
61        Self::ensure_bridge_for(&config.bridge_name, &config.subnet)?;
62
63        // 2. Create veth pair
64        Self::run_cmd(
65            "ip",
66            &[
67                "link",
68                "add",
69                &veth_host,
70                "type",
71                "veth",
72                "peer",
73                "name",
74                &veth_container,
75            ],
76        )?;
77        rollback.veth_created = true;
78
79        // 3. Attach host end to bridge
80        Self::run_cmd(
81            "ip",
82            &["link", "set", &veth_host, "master", &config.bridge_name],
83        )?;
84        Self::run_cmd("ip", &["link", "set", &veth_host, "up"])?;
85
86        // 4. Move container end to container's network namespace
87        Self::run_cmd(
88            "ip",
89            &["link", "set", &veth_container, "netns", &pid.to_string()],
90        )?;
91
92        // 5. Configure container interface (inside container netns via nsenter)
93        let pid_str = pid.to_string();
94        Self::run_cmd(
95            "nsenter",
96            &[
97                "-t",
98                &pid_str,
99                "-n",
100                "ip",
101                "addr",
102                "add",
103                &format!("{}/{}", container_ip, prefix),
104                "dev",
105                &veth_container,
106            ],
107        )?;
108        Self::run_cmd(
109            "nsenter",
110            &[
111                "-t",
112                &pid_str,
113                "-n",
114                "ip",
115                "link",
116                "set",
117                &veth_container,
118                "up",
119            ],
120        )?;
121        Self::run_cmd(
122            "nsenter",
123            &["-t", &pid_str, "-n", "ip", "link", "set", "lo", "up"],
124        )?;
125
126        // 6. Set default route in container
127        let gateway = Self::gateway_from_subnet(&config.subnet);
128        Self::run_cmd(
129            "nsenter",
130            &[
131                "-t", &pid_str, "-n", "ip", "route", "add", "default", "via", &gateway,
132            ],
133        )?;
134
135        // 7. Enable NAT (masquerade) on the host
136        Self::run_cmd(
137            "iptables",
138            &[
139                "-t",
140                "nat",
141                "-A",
142                "POSTROUTING",
143                "-s",
144                &config.subnet,
145                "-j",
146                "MASQUERADE",
147            ],
148        )?;
149        rollback.nat_added = true;
150
151        // 8. Enable IP forwarding (save previous value for restore on cleanup)
152        let prev_ip_forward = std::fs::read_to_string("/proc/sys/net/ipv4/ip_forward")
153            .unwrap_or_default()
154            .trim()
155            .to_string();
156        rollback.prev_ip_forward = Some(prev_ip_forward);
157        std::fs::write("/proc/sys/net/ipv4/ip_forward", "1").map_err(|e| {
158            NucleusError::NetworkError(format!("Failed to enable IP forwarding: {}", e))
159        })?;
160
161        // 9. Set up port forwarding rules
162        for pf in &config.port_forwards {
163            Self::setup_port_forward_for(&container_ip, pf)?;
164            rollback
165                .port_forwards
166                .push((container_ip.clone(), pf.clone()));
167        }
168
169        net_state = net_state.transition(NetworkState::Active)?;
170
171        info!(
172            "Bridge network configured: {} -> {} (IP: {})",
173            veth_host, veth_container, container_ip
174        );
175        let prev_ip_forward = rollback.prev_ip_forward.clone();
176        rollback.disarm();
177
178        Ok(Self {
179            config: config.clone(),
180            container_ip,
181            veth_host,
182            container_id: container_id.to_string(),
183            prev_ip_forward,
184            state: net_state,
185        })
186    }
187
188    /// Apply egress policy rules inside the container's network namespace.
189    ///
190    /// Uses iptables OUTPUT chain to restrict outbound connections.
191    /// Must be called after bridge setup while the container netns is reachable.
192    pub fn apply_egress_policy(&self, pid: u32, policy: &EgressPolicy) -> Result<()> {
193        // Validate egress CIDRs before passing to iptables
194        for cidr in &policy.allowed_cidrs {
195            crate::network::config::validate_egress_cidr(cidr)
196                .map_err(|e| NucleusError::NetworkError(format!("Invalid egress CIDR: {}", e)))?;
197        }
198
199        let pid_str = pid.to_string();
200
201        // Flush any existing OUTPUT rules to prevent duplication on repeated calls
202        Self::run_cmd(
203            "nsenter",
204            &["-t", &pid_str, "-n", "iptables", "-F", "OUTPUT"],
205        )?;
206        // Reset OUTPUT policy to ACCEPT before rebuilding rules
207        Self::run_cmd(
208            "nsenter",
209            &["-t", &pid_str, "-n", "iptables", "-P", "OUTPUT", "ACCEPT"],
210        )?;
211
212        // Default policy: drop all OUTPUT (except established/related and loopback)
213        Self::run_cmd(
214            "nsenter",
215            &[
216                "-t", &pid_str, "-n", "iptables", "-A", "OUTPUT", "-o", "lo", "-j", "ACCEPT",
217            ],
218        )?;
219
220        Self::run_cmd(
221            "nsenter",
222            &[
223                "-t",
224                &pid_str,
225                "-n",
226                "iptables",
227                "-A",
228                "OUTPUT",
229                "-m",
230                "conntrack",
231                "--ctstate",
232                "ESTABLISHED,RELATED",
233                "-j",
234                "ACCEPT",
235            ],
236        )?;
237
238        // Allow DNS to configured resolvers (only when policy permits it)
239        if policy.allow_dns {
240            for dns in &self.config.dns {
241                Self::run_cmd(
242                    "nsenter",
243                    &[
244                        "-t", &pid_str, "-n", "iptables", "-A", "OUTPUT", "-p", "udp", "-d", dns,
245                        "--dport", "53", "-j", "ACCEPT",
246                    ],
247                )?;
248                Self::run_cmd(
249                    "nsenter",
250                    &[
251                        "-t", &pid_str, "-n", "iptables", "-A", "OUTPUT", "-p", "tcp", "-d", dns,
252                        "--dport", "53", "-j", "ACCEPT",
253                    ],
254                )?;
255            }
256        }
257
258        // Allow traffic to each permitted CIDR
259        for cidr in &policy.allowed_cidrs {
260            if policy.allowed_tcp_ports.is_empty() && policy.allowed_udp_ports.is_empty() {
261                // Allow all ports to this CIDR
262                Self::run_cmd(
263                    "nsenter",
264                    &[
265                        "-t", &pid_str, "-n", "iptables", "-A", "OUTPUT", "-d", cidr, "-j",
266                        "ACCEPT",
267                    ],
268                )?;
269            } else {
270                for port in &policy.allowed_tcp_ports {
271                    Self::run_cmd(
272                        "nsenter",
273                        &[
274                            "-t",
275                            &pid_str,
276                            "-n",
277                            "iptables",
278                            "-A",
279                            "OUTPUT",
280                            "-p",
281                            "tcp",
282                            "-d",
283                            cidr,
284                            "--dport",
285                            &port.to_string(),
286                            "-j",
287                            "ACCEPT",
288                        ],
289                    )?;
290                }
291                for port in &policy.allowed_udp_ports {
292                    Self::run_cmd(
293                        "nsenter",
294                        &[
295                            "-t",
296                            &pid_str,
297                            "-n",
298                            "iptables",
299                            "-A",
300                            "OUTPUT",
301                            "-p",
302                            "udp",
303                            "-d",
304                            cidr,
305                            "--dport",
306                            &port.to_string(),
307                            "-j",
308                            "ACCEPT",
309                        ],
310                    )?;
311                }
312            }
313        }
314
315        // Log denied packets (rate-limited)
316        if policy.log_denied {
317            Self::run_cmd(
318                "nsenter",
319                &[
320                    "-t",
321                    &pid_str,
322                    "-n",
323                    "iptables",
324                    "-A",
325                    "OUTPUT",
326                    "-m",
327                    "limit",
328                    "--limit",
329                    "5/min",
330                    "-j",
331                    "LOG",
332                    "--log-prefix",
333                    "nucleus-egress-denied: ",
334                ],
335            )?;
336        }
337
338        // Drop everything else
339        Self::run_cmd(
340            "nsenter",
341            &["-t", &pid_str, "-n", "iptables", "-P", "OUTPUT", "DROP"],
342        )?;
343
344        info!(
345            "Egress policy applied: {} allowed CIDRs",
346            policy.allowed_cidrs.len()
347        );
348        debug!("Egress policy details: {:?}", policy);
349
350        Ok(())
351    }
352
353    /// Clean up bridge networking
354    ///
355    /// State transition: Active -> Cleaned
356    pub fn cleanup(mut self) -> Result<()> {
357        self.state = self.state.transition(NetworkState::Cleaned)?;
358
359        // Release the IP allocation
360        Self::release_allocated_ip(&self.container_id);
361
362        // Remove port forwarding rules
363        for pf in &self.config.port_forwards {
364            if let Err(e) = self.cleanup_port_forward(pf) {
365                warn!("Failed to cleanup port forward: {}", e);
366            }
367        }
368
369        // Remove NAT rule
370        let _ = Self::run_cmd(
371            "iptables",
372            &[
373                "-t",
374                "nat",
375                "-D",
376                "POSTROUTING",
377                "-s",
378                &self.config.subnet,
379                "-j",
380                "MASQUERADE",
381            ],
382        );
383
384        // Delete veth pair (deleting one end removes both)
385        let _ = Self::run_cmd("ip", &["link", "del", &self.veth_host]);
386
387        // Restore previous ip_forward state if we changed it
388        if let Some(ref prev) = self.prev_ip_forward {
389            if prev == "0" {
390                if let Err(e) = std::fs::write("/proc/sys/net/ipv4/ip_forward", "0") {
391                    warn!("Failed to restore ip_forward to 0: {}", e);
392                } else {
393                    info!("Restored net.ipv4.ip_forward to 0");
394                }
395            }
396        }
397
398        info!("Bridge network cleaned up");
399        Ok(())
400    }
401
402    /// Detect and remove orphaned iptables rules from previous Nucleus runs.
403    ///
404    /// Checks for stale MASQUERADE rules referencing the nucleus subnet that
405    /// have no corresponding running container. Prevents gradual degradation
406    /// of network isolation from accumulated orphaned rules.
407    pub fn cleanup_orphaned_rules(subnet: &str) {
408        // List NAT rules and look for nucleus-related MASQUERADE entries
409        let output = match Command::new("iptables")
410            .args(["-t", "nat", "-L", "POSTROUTING", "-n"])
411            .output()
412        {
413            Ok(o) => o,
414            Err(e) => {
415                debug!("Cannot check iptables for orphaned rules: {}", e);
416                return;
417            }
418        };
419
420        let stdout = String::from_utf8_lossy(&output.stdout);
421        let mut orphaned_count = 0u32;
422        for line in stdout.lines() {
423            if line.contains("MASQUERADE") && line.contains(subnet) {
424                // Try to remove it; if it fails, it may be actively used
425                let _ = Self::run_cmd(
426                    "iptables",
427                    &[
428                        "-t",
429                        "nat",
430                        "-D",
431                        "POSTROUTING",
432                        "-s",
433                        subnet,
434                        "-j",
435                        "MASQUERADE",
436                    ],
437                );
438                orphaned_count += 1;
439            }
440        }
441
442        if orphaned_count > 0 {
443            info!(
444                "Cleaned up {} orphaned iptables MASQUERADE rule(s) for subnet {}",
445                orphaned_count, subnet
446            );
447        }
448    }
449
450    fn ensure_bridge_for(bridge_name: &str, subnet: &str) -> Result<()> {
451        // Check if bridge exists
452        if Self::run_cmd("ip", &["link", "show", bridge_name]).is_ok() {
453            return Ok(());
454        }
455
456        // Create bridge
457        Self::run_cmd(
458            "ip",
459            &["link", "add", "name", bridge_name, "type", "bridge"],
460        )?;
461
462        let gateway = Self::gateway_from_subnet(subnet);
463        Self::run_cmd(
464            "ip",
465            &[
466                "addr",
467                "add",
468                &format!("{}/{}", gateway, Self::subnet_prefix(subnet)),
469                "dev",
470                bridge_name,
471            ],
472        )?;
473        Self::run_cmd("ip", &["link", "set", bridge_name, "up"])?;
474
475        info!("Created bridge {}", bridge_name);
476        Ok(())
477    }
478
479    fn setup_port_forward_for(container_ip: &str, pf: &PortForward) -> Result<()> {
480        for chain in ["PREROUTING", "OUTPUT"] {
481            let args = Self::port_forward_rule_args("-A", chain, container_ip, pf);
482            Self::run_cmd_owned("iptables", &args)?;
483        }
484
485        info!(
486            "Port forward: {}:{} -> {}:{}/{}",
487            "host", pf.host_port, container_ip, pf.container_port, pf.protocol
488        );
489        Ok(())
490    }
491
492    fn cleanup_port_forward(&self, pf: &PortForward) -> Result<()> {
493        for chain in ["OUTPUT", "PREROUTING"] {
494            let args = Self::port_forward_rule_args("-D", chain, &self.container_ip, pf);
495            Self::run_cmd_owned("iptables", &args)?;
496        }
497        Ok(())
498    }
499
500    /// Allocate a container IP from the subnet using /dev/urandom.
501    ///
502    /// Checks both host-visible interfaces (via `ip addr`) and IPs assigned to
503    /// other Nucleus containers (via state files) to avoid duplicates. Container
504    /// IPs inside network namespaces are invisible to `ip addr show` on the host.
505    fn allocate_ip_with_reserved(
506        subnet: &str,
507        reserved: &std::collections::HashSet<String>,
508    ) -> Result<String> {
509        let base = subnet.split('/').next().unwrap_or("10.0.42.0");
510        let parts: Vec<&str> = base.split('.').collect();
511        if parts.len() != 4 {
512            return Ok("10.0.42.2".to_string());
513        }
514
515        // Use rejection sampling to avoid modulo bias.
516        // Range is 2..=254 (253 values). We reject random bytes >= 253 to
517        // ensure uniform distribution, then add 2 to shift into the valid range.
518        // Open /dev/urandom once and read all randomness in a single batch.
519        // 128 bytes gives ~125 valid candidates (byte < 253), making exhaustion
520        // in a populated subnet far less likely than the previous 32-byte buffer.
521        let mut rand_buf = [0u8; 128];
522        std::fs::File::open("/dev/urandom")
523            .and_then(|mut f| std::io::Read::read_exact(&mut f, &mut rand_buf))
524            .map_err(|e| {
525                NucleusError::NetworkError(format!("Failed to read /dev/urandom: {}", e))
526            })?;
527        for &byte in &rand_buf {
528            // Rejection sampling: discard values that would cause modulo bias
529            if byte >= 253 {
530                continue;
531            }
532            let offset = byte as u32 + 2;
533            let candidate = format!("{}.{}.{}.{}", parts[0], parts[1], parts[2], offset);
534            if reserved.contains(&candidate) {
535                continue;
536            }
537            if !Self::is_ip_in_use(&candidate)? {
538                // Lock is released when lock_file is dropped
539                return Ok(candidate);
540            }
541        }
542
543        Err(NucleusError::NetworkError(format!(
544            "Failed to allocate free IP in subnet {}",
545            subnet
546        )))
547    }
548
549    fn reserve_ip_in_dir(
550        alloc_dir: &std::path::Path,
551        container_id: &str,
552        subnet: &str,
553        requested_ip: Option<&str>,
554    ) -> Result<String> {
555        std::fs::create_dir_all(alloc_dir).map_err(|e| {
556            NucleusError::NetworkError(format!("Failed to create IP alloc dir: {}", e))
557        })?;
558        let lock_path = alloc_dir.join(".lock");
559        let lock_file = std::fs::OpenOptions::new()
560            .create(true)
561            .write(true)
562            .truncate(false)
563            .open(&lock_path)
564            .map_err(|e| {
565                NucleusError::NetworkError(format!("Failed to open IP alloc lock: {}", e))
566            })?;
567        use std::os::unix::io::AsRawFd;
568        let lock_ret = unsafe { libc::flock(lock_file.as_raw_fd(), libc::LOCK_EX) };
569        if lock_ret != 0 {
570            return Err(NucleusError::NetworkError(format!(
571                "Failed to acquire IP alloc lock: {}",
572                std::io::Error::last_os_error()
573            )));
574        }
575
576        let reserved = Self::collect_reserved_ips_in_dir(alloc_dir);
577        let ip = match requested_ip {
578            Some(ip) => {
579                if reserved.contains(ip) || Self::is_ip_in_use(ip)? {
580                    return Err(NucleusError::NetworkError(format!(
581                        "Requested container IP {} is already in use",
582                        ip
583                    )));
584                }
585                ip.to_string()
586            }
587            None => Self::allocate_ip_with_reserved(subnet, &reserved)?,
588        };
589
590        Self::record_allocated_ip_in_dir(alloc_dir, container_id, &ip)?;
591        Ok(ip)
592    }
593
594    /// Scan the Nucleus IP allocation directory for IPs already assigned.
595    fn collect_reserved_ips_in_dir(
596        alloc_dir: &std::path::Path,
597    ) -> std::collections::HashSet<String> {
598        let mut ips = std::collections::HashSet::new();
599        if let Ok(entries) = std::fs::read_dir(alloc_dir) {
600            for entry in entries.flatten() {
601                if let Some(name) = entry.file_name().to_str() {
602                    if name.ends_with(".ip") {
603                        if let Ok(ip) = std::fs::read_to_string(entry.path()) {
604                            let ip = ip.trim().to_string();
605                            if !ip.is_empty() {
606                                ips.insert(ip);
607                            }
608                        }
609                    }
610                }
611            }
612        }
613        ips
614    }
615
616    /// Persist the allocated IP for this container so other containers can see it.
617    fn record_allocated_ip_in_dir(
618        alloc_dir: &std::path::Path,
619        container_id: &str,
620        ip: &str,
621    ) -> Result<()> {
622        std::fs::create_dir_all(alloc_dir).map_err(|e| {
623            NucleusError::NetworkError(format!("Failed to create IP alloc dir: {}", e))
624        })?;
625        let path = alloc_dir.join(format!("{}.ip", container_id));
626        std::fs::write(&path, ip).map_err(|e| {
627            NucleusError::NetworkError(format!("Failed to record IP allocation: {}", e))
628        })?;
629        Ok(())
630    }
631
632    /// Remove the persisted IP allocation for a container.
633    fn release_allocated_ip(container_id: &str) {
634        let alloc_dir = Self::ip_alloc_dir();
635        Self::release_allocated_ip_in_dir(&alloc_dir, container_id);
636    }
637
638    fn release_allocated_ip_in_dir(alloc_dir: &std::path::Path, container_id: &str) {
639        let path = alloc_dir.join(format!("{}.ip", container_id));
640        let _ = std::fs::remove_file(path);
641    }
642
643    fn ip_alloc_dir() -> std::path::PathBuf {
644        if nix::unistd::Uid::effective().is_root() {
645            std::path::PathBuf::from("/var/run/nucleus/ip-alloc")
646        } else {
647            dirs::runtime_dir()
648                .map(|d| d.join("nucleus/ip-alloc"))
649                .or_else(|| dirs::data_local_dir().map(|d| d.join("nucleus/ip-alloc")))
650                .unwrap_or_else(|| {
651                    dirs::home_dir()
652                        .map(|h| h.join(".nucleus/ip-alloc"))
653                        .unwrap_or_else(|| std::path::PathBuf::from("/var/run/nucleus/ip-alloc"))
654                })
655        }
656    }
657
658    /// Get gateway IP from subnet (first usable address)
659    fn gateway_from_subnet(subnet: &str) -> String {
660        let base = subnet.split('/').next().unwrap_or("10.0.42.0");
661        let parts: Vec<&str> = base.split('.').collect();
662        if parts.len() == 4 {
663            format!("{}.{}.{}.1", parts[0], parts[1], parts[2])
664        } else {
665            "10.0.42.1".to_string()
666        }
667    }
668
669    fn subnet_prefix(subnet: &str) -> u8 {
670        subnet
671            .split_once('/')
672            .and_then(|(_, p)| p.parse::<u8>().ok())
673            .filter(|p| *p <= 32)
674            .unwrap_or(24)
675    }
676
677    /// Resolve a system binary to an absolute path when running as root.
678    /// When unprivileged, falls back to bare name (PATH-based resolution).
679    fn resolve_bin(name: &str) -> String {
680        if nix::unistd::Uid::effective().is_root() {
681            let search_dirs: &[&str] = match name {
682                "ip" => &["/usr/sbin/ip", "/sbin/ip", "/usr/bin/ip"],
683                "iptables" => &["/usr/sbin/iptables", "/sbin/iptables", "/usr/bin/iptables"],
684                "nsenter" => &["/usr/bin/nsenter", "/usr/sbin/nsenter", "/bin/nsenter"],
685                _ => &[],
686            };
687            for path in search_dirs {
688                if std::path::Path::new(path).exists() {
689                    return path.to_string();
690                }
691            }
692        }
693        name.to_string()
694    }
695
696    fn run_cmd(program: &str, args: &[&str]) -> Result<()> {
697        let resolved = Self::resolve_bin(program);
698        let output = Command::new(&resolved).args(args).output().map_err(|e| {
699            NucleusError::NetworkError(format!("Failed to run {} {:?}: {}", resolved, args, e))
700        })?;
701
702        if !output.status.success() {
703            let stderr = String::from_utf8_lossy(&output.stderr);
704            return Err(NucleusError::NetworkError(format!(
705                "{} {:?} failed: {}",
706                program, args, stderr
707            )));
708        }
709
710        Ok(())
711    }
712
713    fn run_cmd_owned(program: &str, args: &[String]) -> Result<()> {
714        let refs: Vec<&str> = args.iter().map(String::as_str).collect();
715        Self::run_cmd(program, &refs)
716    }
717
718    fn port_forward_rule_args(
719        operation: &str,
720        chain: &str,
721        container_ip: &str,
722        pf: &PortForward,
723    ) -> Vec<String> {
724        let mut args = vec![
725            "-t".to_string(),
726            "nat".to_string(),
727            operation.to_string(),
728            chain.to_string(),
729            "-p".to_string(),
730            pf.protocol.as_str().to_string(),
731        ];
732
733        if chain == "OUTPUT" {
734            args.extend([
735                "-m".to_string(),
736                "addrtype".to_string(),
737                "--dst-type".to_string(),
738                "LOCAL".to_string(),
739            ]);
740        }
741
742        args.extend([
743            "--dport".to_string(),
744            pf.host_port.to_string(),
745            "-j".to_string(),
746            "DNAT".to_string(),
747            "--to-destination".to_string(),
748            format!("{}:{}", container_ip, pf.container_port),
749        ]);
750
751        args
752    }
753
754    fn is_ip_in_use(ip: &str) -> Result<bool> {
755        let ip_bin = Self::resolve_bin("ip");
756        let output = Command::new(&ip_bin)
757            .args(["-4", "addr", "show"])
758            .output()
759            .map_err(|e| {
760                NucleusError::NetworkError(format!("Failed to inspect host IPs: {}", e))
761            })?;
762
763        if !output.status.success() {
764            let stderr = String::from_utf8_lossy(&output.stderr);
765            return Err(NucleusError::NetworkError(format!(
766                "ip -4 addr show failed: {}",
767                stderr.trim()
768            )));
769        }
770
771        let stdout = String::from_utf8_lossy(&output.stdout);
772        Ok(stdout.contains(&format!(" {}/", ip)))
773    }
774
775    /// Write resolv.conf inside container (for writable /etc, e.g. agent mode)
776    pub fn write_resolv_conf(root: &std::path::Path, dns: &[String]) -> Result<()> {
777        let resolv_path = root.join("etc/resolv.conf");
778        let content: String = dns
779            .iter()
780            .map(|server| format!("nameserver {}\n", server))
781            .collect();
782        std::fs::write(&resolv_path, content).map_err(|e| {
783            NucleusError::NetworkError(format!("Failed to write resolv.conf: {}", e))
784        })?;
785        Ok(())
786    }
787
788    /// Bind-mount a resolv.conf over a read-only /etc (for production rootfs mode).
789    ///
790    /// Creates a memfd-backed resolv.conf and bind-mounts it over
791    /// /etc/resolv.conf so it works even when the rootfs /etc is read-only.
792    /// The memfd is cleaned up when the container exits.
793    pub fn bind_mount_resolv_conf(root: &std::path::Path, dns: &[String]) -> Result<()> {
794        use nix::mount::{mount, MsFlags};
795
796        let content: String = dns
797            .iter()
798            .map(|server| format!("nameserver {}\n", server))
799            .collect();
800
801        // Create a memfd-backed file to avoid leaving staging files on disk
802        let memfd_name = std::ffi::CString::new("nucleus-resolv").map_err(|e| {
803            NucleusError::NetworkError(format!("Failed to create memfd name: {}", e))
804        })?;
805        let memfd_fd = unsafe { libc::memfd_create(memfd_name.as_ptr(), 0) };
806        if memfd_fd < 0 {
807            // Fallback to staging file if memfd_create is unavailable
808            return Self::bind_mount_resolv_conf_staging(root, dns);
809        }
810
811        // Write content to memfd
812        let write_result = unsafe {
813            libc::write(
814                memfd_fd,
815                content.as_ptr() as *const libc::c_void,
816                content.len(),
817            )
818        };
819        if write_result < 0 {
820            unsafe { libc::close(memfd_fd) };
821            return Self::bind_mount_resolv_conf_staging(root, dns);
822        }
823
824        // Ensure the mount target exists
825        let target = root.join("etc/resolv.conf");
826        if !target.exists() {
827            let _ = std::fs::write(&target, "");
828        }
829
830        // Bind mount the memfd over the read-only resolv.conf
831        let memfd_path = format!("/proc/self/fd/{}", memfd_fd);
832        mount(
833            Some(memfd_path.as_str()),
834            &target,
835            None::<&str>,
836            MsFlags::MS_BIND,
837            None::<&str>,
838        )
839        .map_err(|e| {
840            unsafe { libc::close(memfd_fd) };
841            NucleusError::NetworkError(format!("Failed to bind mount resolv.conf: {}", e))
842        })?;
843
844        // Close the fd — the mount keeps the file alive
845        unsafe { libc::close(memfd_fd) };
846
847        info!("Bind-mounted resolv.conf for bridge networking (rootfs mode, memfd)");
848        Ok(())
849    }
850
851    /// Fallback: bind-mount a staging resolv.conf file.
852    fn bind_mount_resolv_conf_staging(root: &std::path::Path, dns: &[String]) -> Result<()> {
853        use nix::mount::{mount, MsFlags};
854
855        let content: String = dns
856            .iter()
857            .map(|server| format!("nameserver {}\n", server))
858            .collect();
859
860        // Write to a staging file outside /etc
861        let staging = root.join("tmp/.resolv.conf.nucleus");
862        if let Some(parent) = staging.parent() {
863            std::fs::create_dir_all(parent).map_err(|e| {
864                NucleusError::NetworkError(format!(
865                    "Failed to create resolv.conf staging parent: {}",
866                    e
867                ))
868            })?;
869        }
870        std::fs::write(&staging, content).map_err(|e| {
871            NucleusError::NetworkError(format!("Failed to write staging resolv.conf: {}", e))
872        })?;
873
874        // Ensure the mount target exists
875        let target = root.join("etc/resolv.conf");
876        if !target.exists() {
877            let _ = std::fs::write(&target, "");
878        }
879
880        // Bind mount the staging file over the read-only resolv.conf
881        mount(
882            Some(staging.as_path()),
883            &target,
884            None::<&str>,
885            MsFlags::MS_BIND,
886            None::<&str>,
887        )
888        .map_err(|e| {
889            NucleusError::NetworkError(format!("Failed to bind mount resolv.conf: {}", e))
890        })?;
891
892        // The bind mount holds a reference to the inode, so we can safely
893        // unlink the staging path to avoid leaking DNS server info on disk.
894        if let Err(e) = std::fs::remove_file(&staging) {
895            warn!("Failed to remove staging resolv.conf {:?}: {}", staging, e);
896        }
897
898        info!("Bind-mounted resolv.conf for bridge networking (rootfs mode, staging)");
899        Ok(())
900    }
901}
902
903struct SetupRollback {
904    veth_host: String,
905    subnet: String,
906    veth_created: bool,
907    nat_added: bool,
908    port_forwards: Vec<(String, PortForward)>,
909    prev_ip_forward: Option<String>,
910    reserved_ip: Option<(std::path::PathBuf, String)>,
911    armed: bool,
912}
913
914impl SetupRollback {
915    fn new(
916        veth_host: String,
917        subnet: String,
918        reserved_ip: Option<(std::path::PathBuf, String)>,
919    ) -> Self {
920        Self {
921            veth_host,
922            subnet,
923            veth_created: false,
924            nat_added: false,
925            port_forwards: Vec::new(),
926            prev_ip_forward: None,
927            reserved_ip,
928            armed: true,
929        }
930    }
931
932    fn disarm(&mut self) {
933        self.armed = false;
934    }
935}
936
937impl Drop for SetupRollback {
938    fn drop(&mut self) {
939        if !self.armed {
940            return;
941        }
942
943        for (container_ip, pf) in self.port_forwards.iter().rev() {
944            for chain in ["OUTPUT", "PREROUTING"] {
945                let args = BridgeNetwork::port_forward_rule_args("-D", chain, container_ip, pf);
946                if let Err(e) = BridgeNetwork::run_cmd_owned("iptables", &args) {
947                    warn!(
948                        "Rollback: failed to remove iptables {} rule for {}: {}",
949                        chain, container_ip, e
950                    );
951                }
952            }
953        }
954
955        if self.nat_added {
956            if let Err(e) = BridgeNetwork::run_cmd(
957                "iptables",
958                &[
959                    "-t",
960                    "nat",
961                    "-D",
962                    "POSTROUTING",
963                    "-s",
964                    &self.subnet,
965                    "-j",
966                    "MASQUERADE",
967                ],
968            ) {
969                warn!("Rollback: failed to remove NAT rule: {}", e);
970            }
971        }
972
973        if self.veth_created {
974            if let Err(e) = BridgeNetwork::run_cmd("ip", &["link", "del", &self.veth_host]) {
975                warn!("Rollback: failed to delete veth {}: {}", self.veth_host, e);
976            }
977        }
978
979        if let Some((alloc_dir, container_id)) = &self.reserved_ip {
980            BridgeNetwork::release_allocated_ip_in_dir(alloc_dir, container_id);
981        }
982    }
983}
984
985#[cfg(test)]
986mod tests {
987    use super::*;
988
989    #[test]
990    fn test_ip_allocation_rejection_sampling_range() {
991        // H-5: Verify that rejection sampling produces values in 2..=254
992        // and that values >= 253 are rejected (no modulo bias).
993        for byte in 0u8..253 {
994            let offset = byte as u32 + 2;
995            assert!(
996                (2..=254).contains(&offset),
997                "offset {} out of range",
998                offset
999            );
1000        }
1001        // Values 253, 254, 255 must be rejected
1002        for byte in [253u8, 254, 255] {
1003            assert!(byte >= 253);
1004        }
1005    }
1006
1007    #[test]
1008    fn test_reserve_ip_blocks_duplicate_requested_address() {
1009        let temp = tempfile::tempdir().unwrap();
1010        BridgeNetwork::record_allocated_ip_in_dir(temp.path(), "one", "10.0.42.2").unwrap();
1011
1012        let err =
1013            BridgeNetwork::reserve_ip_in_dir(temp.path(), "two", "10.0.42.0/24", Some("10.0.42.2"))
1014                .unwrap_err();
1015        assert!(
1016            err.to_string().contains("already in use"),
1017            "second reservation of the same IP must fail"
1018        );
1019    }
1020
1021    #[test]
1022    fn test_setup_rollback_releases_reserved_ip() {
1023        let temp = tempfile::tempdir().unwrap();
1024        BridgeNetwork::record_allocated_ip_in_dir(temp.path(), "rollback", "10.0.42.3").unwrap();
1025
1026        let rollback = SetupRollback {
1027            veth_host: "veth-test".to_string(),
1028            subnet: "10.0.42.0/24".to_string(),
1029            veth_created: false,
1030            nat_added: false,
1031            port_forwards: Vec::new(),
1032            prev_ip_forward: None,
1033            reserved_ip: Some((temp.path().to_path_buf(), "rollback".to_string())),
1034            armed: true,
1035        };
1036
1037        drop(rollback);
1038
1039        assert!(
1040            !temp.path().join("rollback.ip").exists(),
1041            "rollback must release reserved IP files on setup failure"
1042        );
1043    }
1044
1045    #[test]
1046    fn test_port_forward_rules_include_output_chain_for_local_host_clients() {
1047        let pf = PortForward {
1048            host_port: 8080,
1049            container_port: 80,
1050            protocol: crate::network::config::Protocol::Tcp,
1051        };
1052
1053        let prerouting =
1054            BridgeNetwork::port_forward_rule_args("-A", "PREROUTING", "10.0.42.2", &pf);
1055        let output = BridgeNetwork::port_forward_rule_args("-A", "OUTPUT", "10.0.42.2", &pf);
1056
1057        assert!(prerouting.iter().any(|arg| arg == "PREROUTING"));
1058        assert!(output.iter().any(|arg| arg == "OUTPUT"));
1059        assert!(
1060            output
1061                .windows(2)
1062                .any(|pair| pair[0] == "--dst-type" && pair[1] == "LOCAL"),
1063            "OUTPUT rule must target local-destination traffic"
1064        );
1065    }
1066}