use crate::error::{Error, Result};
use crate::jail::jexec_with_output;
use crate::network::ioctl;
use std::sync::atomic::{AtomicU32, Ordering};
static EPAIR_COUNTER: AtomicU32 = AtomicU32::new(0);
#[derive(Debug, Clone)]
pub struct EpairInterface {
host_side: String,
jail_side: String,
}
impl EpairInterface {
pub fn create() -> Result<Self> {
let host_side = ioctl::create_interface("epair", None)?;
let jail_side = host_side
.strip_suffix('a')
.map(|s| format!("{}b", s))
.ok_or_else(|| {
Error::Network(format!("Unexpected epair name format: {}", host_side))
})?;
ioctl::set_interface_up(&host_side, true)?;
Ok(Self {
host_side,
jail_side,
})
}
pub fn create_for_jail(jail_name: &str) -> Result<Self> {
let epair = Self::create()?;
let counter = EPAIR_COUNTER.fetch_add(1, Ordering::SeqCst);
let new_host_name = format!("e{}a_{}", counter, Self::sanitize_name(jail_name));
let new_jail_name = format!("e{}b_{}", counter, Self::sanitize_name(jail_name));
if let Err(e) = ioctl::rename_interface(&epair.host_side, &new_host_name) {
let _ = ioctl::destroy_interface(&epair.host_side);
return Err(e);
}
if let Err(e) = ioctl::rename_interface(&epair.jail_side, &new_jail_name) {
let _ = ioctl::destroy_interface(&new_host_name);
return Err(e);
}
Ok(Self {
host_side: new_host_name,
jail_side: new_jail_name,
})
}
pub fn host_side(&self) -> &str {
&self.host_side
}
pub fn jail_side(&self) -> &str {
&self.jail_side
}
pub fn set_mac_address(&self, mac: &str) -> Result<()> {
ioctl::set_mac_address(&self.jail_side, mac)
}
pub fn move_to_jail(&self, jid: i32) -> Result<()> {
ioctl::move_to_vnet(&self.jail_side, jid)
}
pub fn configure_in_jail(
jid: i32,
interface: &str,
addr: &str,
gateway: Option<&str>,
) -> Result<()> {
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 {} in jail {}: {}",
interface, jid, stderr_str
)));
}
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 {} in jail {}: {}",
interface, jid, stderr_str
)));
}
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);
if !stderr_str.contains("File exists") {
return Err(Error::Network(format!(
"Failed to add default route in jail {}: {}",
jid, stderr_str
)));
}
}
}
Ok(())
}
pub fn destroy(&self) -> Result<()> {
ioctl::destroy_interface(&self.host_side).or_else(|e| {
if e.to_string().contains("does not exist") {
Ok(())
} else {
Err(e)
}
})
}
fn sanitize_name(name: &str) -> String {
let sanitized: String = name
.chars()
.filter(|c| c.is_ascii_alphanumeric() || *c == '_')
.take(10) .collect();
if sanitized.is_empty() {
"jail".to_string()
} else {
sanitized
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sanitize_name() {
assert_eq!(EpairInterface::sanitize_name("myjail"), "myjail");
assert_eq!(EpairInterface::sanitize_name("my-jail"), "myjail");
assert_eq!(
EpairInterface::sanitize_name("verylongjailname"),
"verylongja"
);
assert_eq!(EpairInterface::sanitize_name(""), "jail");
}
}