1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! Epair interface management for VNET jails
//!
//! Epairs are virtual Ethernet pairs used to connect VNET jails to bridges.
//! One end stays on the host (and is added to a bridge), the other is moved
//! into the jail.
use crate::error::{Error, Result};
use crate::jail::jexec_with_output;
use crate::network::ioctl;
/// An epair interface pair for connecting VNET jails to bridges
#[derive(Debug, Clone)]
pub struct EpairInterface {
/// Host-side interface name (e.g., "epair0a")
host_side: String,
/// Jail-side interface name (e.g., "epair0b")
jail_side: String,
}
impl EpairInterface {
pub(crate) const fn from_existing(host_side: String, jail_side: String) -> Self {
Self {
host_side,
jail_side,
}
}
/// Create a new epair interface pair using native ioctl syscalls
///
/// The epair is created with system-assigned names (epairNa, epairNb).
pub fn create() -> Result<Self> {
// Use native SIOCIFCREATE ioctl instead of spawning ifconfig process
let host_side = ioctl::create_interface("epair", None)?;
// The jail side is the same but with 'b' instead of 'a'
let jail_side = host_side
.strip_suffix('a')
.map(|s| format!("{s}b"))
.ok_or_else(|| Error::Network(format!("Unexpected epair name format: {host_side}")))?;
// Bring host side up using native ioctl
if let Err(e) = ioctl::set_interface_up(&host_side, true) {
// Rollback; keep the original error.
let _ = ioctl::destroy_interface(&host_side);
return Err(e);
}
// Ownership marker: only the host side keeps groups (VNET transfer
// strips them from the jail side), so cleanup checks the a-end.
// Untagged epairs are never reclaimed by cleanup_state.
if let Err(e) = crate::sys::tag_interface(&host_side) {
// Rollback; keep the original error.
let _ = ioctl::destroy_interface(&host_side);
return Err(e);
}
Ok(Self::from_existing(host_side, jail_side))
}
/// Create an epair for a jail.
///
/// Blackship intentionally keeps the kernel-assigned epair names instead
/// of renaming them. Renaming based on a process-local counter collides
/// across separate CLI invocations and breaks detached/cleanup workflows.
pub fn create_for_jail(_jail_name: &str) -> Result<Self> {
Self::create()
}
/// Get the host-side interface name
pub fn host_side(&self) -> &str {
&self.host_side
}
/// Get the jail-side interface name
pub fn jail_side(&self) -> &str {
&self.jail_side
}
/// Set MAC address on the jail-side interface using ioctl
///
/// Must be called before moving the interface into the jail.
/// MAC format: "02:00:00:00:00:01" (locally administered addresses start with 02)
pub fn set_mac_address(&self, mac: &str) -> Result<()> {
ioctl::set_mac_address(&self.jail_side, mac)
}
/// Move the jail-side interface into a VNET jail using ioctl
///
/// This is done via SIOCSIFVNET ioctl syscall.
pub fn move_to_jail(&self, jid: i32) -> Result<()> {
ioctl::move_to_vnet(&self.jail_side, jid)
}
/// Configure the jail-side interface with an IP address (inside the jail)
pub fn configure_in_jail(
jid: i32,
interface: &str,
addr: &str,
gateway: Option<&str>,
) -> Result<()> {
// Configure IP address inside jail using native jexec syscall
let (exit_code, _stdout, stderr) =
jexec_with_output(jid, &["ifconfig", interface, addr])
.map_err(|e| Error::Network(format!("Failed to configure interface: {e}")))?;
if exit_code != 0 {
let stderr_str = String::from_utf8_lossy(&stderr);
return Err(Error::Network(format!(
"Failed to configure {interface} in jail {jid}: {stderr_str}"
)));
}
// Bring interface up using native jexec syscall
let (exit_code, _stdout, stderr) =
jexec_with_output(jid, &["ifconfig", interface, "up"])
.map_err(|e| Error::Network(format!("Failed to bring up interface: {e}")))?;
if exit_code != 0 {
let stderr_str = String::from_utf8_lossy(&stderr);
return Err(Error::Network(format!(
"Failed to bring up {interface} in jail {jid}: {stderr_str}"
)));
}
// Set default route if gateway provided using native jexec syscall
if let Some(gw) = gateway {
let (exit_code, _stdout, stderr) =
jexec_with_output(jid, &["route", "add", "default", gw])
.map_err(|e| Error::Network(format!("Failed to add route: {e}")))?;
if exit_code != 0 {
let stderr_str = String::from_utf8_lossy(&stderr);
// Don't fail if route already exists
if !stderr_str.contains("File exists") {
return Err(Error::Network(format!(
"Failed to add default route in jail {jid}: {stderr_str}"
)));
}
}
}
Ok(())
}
/// Destroy the epair using ioctl (destroys both ends)
pub fn destroy(&self) -> Result<()> {
// Destroying either end destroys both
// Ignore error if already destroyed
ioctl::destroy_interface(&self.host_side).or_else(|e| {
if e.to_string().contains("does not exist") {
Ok(())
} else {
Err(e)
}
})
}
}