2020-04-20 12:15:57 +00:00
|
|
|
use std::io::{Write};
|
|
|
|
use std::collections::{HashSet, HashMap};
|
|
|
|
|
2020-04-22 08:54:07 +00:00
|
|
|
use anyhow::{Error, format_err, bail};
|
2020-04-20 12:15:57 +00:00
|
|
|
|
2020-04-21 10:55:33 +00:00
|
|
|
use proxmox::tools::{fs::replace_file, fs::CreateOptions};
|
|
|
|
|
2020-04-20 12:15:57 +00:00
|
|
|
mod helper;
|
2020-04-20 16:10:15 +00:00
|
|
|
pub use helper::*;
|
2020-04-20 12:15:57 +00:00
|
|
|
|
|
|
|
mod lexer;
|
|
|
|
pub use lexer::*;
|
|
|
|
|
|
|
|
mod parser;
|
|
|
|
pub use parser::*;
|
|
|
|
|
2020-04-23 06:45:03 +00:00
|
|
|
use crate::api2::types::{Interface, NetworkConfigMethod, NetworkInterfaceType};
|
2020-04-20 12:15:57 +00:00
|
|
|
|
|
|
|
impl Interface {
|
|
|
|
|
2020-04-20 16:10:15 +00:00
|
|
|
pub fn new(name: String) -> Self {
|
2020-04-21 09:06:22 +00:00
|
|
|
Self {
|
2020-04-20 12:15:57 +00:00
|
|
|
name,
|
2020-04-23 06:45:03 +00:00
|
|
|
interface_type: NetworkInterfaceType::Unknown,
|
2020-04-22 14:46:46 +00:00
|
|
|
auto: false,
|
2020-04-21 09:46:56 +00:00
|
|
|
active: false,
|
2020-04-20 16:10:15 +00:00
|
|
|
method_v4: None,
|
|
|
|
method_v6: None,
|
2020-04-22 06:45:13 +00:00
|
|
|
cidr_v4: None,
|
2020-04-20 12:15:57 +00:00
|
|
|
gateway_v4: None,
|
2020-04-22 06:45:13 +00:00
|
|
|
cidr_v6: None,
|
2020-04-20 12:15:57 +00:00
|
|
|
gateway_v6: None,
|
|
|
|
options_v4: Vec::new(),
|
|
|
|
options_v6: Vec::new(),
|
2020-04-23 05:07:14 +00:00
|
|
|
mtu: None,
|
2020-04-23 07:24:17 +00:00
|
|
|
bridge_ports: None,
|
2020-04-23 07:31:10 +00:00
|
|
|
bond_slaves: None,
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:17:57 +00:00
|
|
|
fn set_method_v4(&mut self, method: NetworkConfigMethod) -> Result<(), Error> {
|
2020-04-20 16:10:15 +00:00
|
|
|
if self.method_v4.is_none() {
|
|
|
|
self.method_v4 = Some(method);
|
|
|
|
} else {
|
|
|
|
bail!("inet configuration method already set.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:17:57 +00:00
|
|
|
fn set_method_v6(&mut self, method: NetworkConfigMethod) -> Result<(), Error> {
|
2020-04-20 16:10:15 +00:00
|
|
|
if self.method_v6.is_none() {
|
|
|
|
self.method_v6 = Some(method);
|
|
|
|
} else {
|
|
|
|
bail!("inet6 configuration method already set.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-22 06:45:13 +00:00
|
|
|
fn set_cidr_v4(&mut self, address: String) -> Result<(), Error> {
|
|
|
|
if self.cidr_v4.is_none() {
|
|
|
|
self.cidr_v4 = Some(address);
|
2020-04-20 12:15:57 +00:00
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv4 address.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_gateway_v4(&mut self, gateway: String) -> Result<(), Error> {
|
|
|
|
if self.gateway_v4.is_none() {
|
|
|
|
self.gateway_v4 = Some(gateway);
|
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv4 gateway.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-22 06:45:13 +00:00
|
|
|
fn set_cidr_v6(&mut self, address: String) -> Result<(), Error> {
|
|
|
|
if self.cidr_v6.is_none() {
|
|
|
|
self.cidr_v6 = Some(address);
|
2020-04-20 12:15:57 +00:00
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv6 address.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_gateway_v6(&mut self, gateway: String) -> Result<(), Error> {
|
|
|
|
if self.gateway_v6.is_none() {
|
|
|
|
self.gateway_v6 = Some(gateway);
|
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv4 gateway.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:43:38 +00:00
|
|
|
fn set_interface_type(&mut self, interface_type: NetworkInterfaceType) -> Result<(), Error> {
|
|
|
|
if self.interface_type == NetworkInterfaceType::Unknown {
|
|
|
|
self.interface_type = interface_type;
|
|
|
|
} else if self.interface_type != interface_type {
|
|
|
|
bail!("interface type already defined - cannot change from {:?} to {:?}", self.interface_type, interface_type);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:15:57 +00:00
|
|
|
fn push_addon_option(&mut self, text: String) {
|
2020-04-20 16:10:15 +00:00
|
|
|
if self.method_v4.is_none() && self.method_v6.is_some() {
|
|
|
|
self.options_v6.push(text);
|
|
|
|
} else {
|
|
|
|
self.options_v4.push(text);
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 09:06:22 +00:00
|
|
|
|
2020-04-23 05:07:14 +00:00
|
|
|
/// Write attributes not dependening on address family
|
|
|
|
fn write_iface_attributes(&self, w: &mut dyn Write) -> Result<(), Error> {
|
2020-04-23 07:24:17 +00:00
|
|
|
|
|
|
|
match self.interface_type {
|
|
|
|
NetworkInterfaceType::Bridge => {
|
|
|
|
if let Some(ref ports) = self.bridge_ports {
|
|
|
|
if ports.is_empty() {
|
|
|
|
writeln!(w, " bridge-ports none")?;
|
|
|
|
} else {
|
|
|
|
writeln!(w, " bridge-ports {}", ports.join(" "))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 07:31:10 +00:00
|
|
|
NetworkInterfaceType::Bond => {
|
|
|
|
if let Some(ref slaves) = self.bond_slaves {
|
|
|
|
if slaves.is_empty() {
|
|
|
|
writeln!(w, " bond-slaves none")?;
|
|
|
|
} else {
|
|
|
|
writeln!(w, " bond-slaves {}", slaves.join(" "))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 07:24:17 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:07:14 +00:00
|
|
|
if let Some(mtu) = self.mtu {
|
|
|
|
writeln!(w, " mtu {}", mtu)?;
|
|
|
|
}
|
2020-04-23 07:24:17 +00:00
|
|
|
|
2020-04-23 05:07:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write attributes dependening on address family inet (IPv4)
|
2020-04-22 10:42:09 +00:00
|
|
|
fn write_iface_attributes_v4(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> {
|
|
|
|
if method == NetworkConfigMethod::Static {
|
|
|
|
if let Some(address) = &self.cidr_v4 {
|
|
|
|
writeln!(w, " address {}", address)?;
|
|
|
|
}
|
|
|
|
if let Some(gateway) = &self.gateway_v4 {
|
|
|
|
writeln!(w, " gateway {}", gateway)?;
|
|
|
|
}
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
2020-04-22 10:42:09 +00:00
|
|
|
|
2020-04-21 09:06:22 +00:00
|
|
|
for option in &self.options_v4 {
|
|
|
|
writeln!(w, " {}", option)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:07:14 +00:00
|
|
|
/// Write attributes dependening on address family inet6 (IPv6)
|
2020-04-22 10:42:09 +00:00
|
|
|
fn write_iface_attributes_v6(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> {
|
|
|
|
if method == NetworkConfigMethod::Static {
|
|
|
|
if let Some(address) = &self.cidr_v6 {
|
|
|
|
writeln!(w, " address {}", address)?;
|
|
|
|
}
|
|
|
|
if let Some(gateway) = &self.gateway_v6 {
|
|
|
|
writeln!(w, " gateway {}", gateway)?;
|
|
|
|
}
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
2020-04-22 10:42:09 +00:00
|
|
|
|
2020-04-21 09:06:22 +00:00
|
|
|
for option in &self.options_v6 {
|
|
|
|
writeln!(w, " {}", option)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:54:12 +00:00
|
|
|
/// Return whether we can write a single entry for inet and inet6
|
|
|
|
fn combine_entry(&self) -> bool {
|
|
|
|
// Note: use match to make sure we considered all values at compile time
|
|
|
|
match self {
|
|
|
|
Interface {
|
|
|
|
method_v4,
|
|
|
|
method_v6,
|
|
|
|
options_v4,
|
|
|
|
options_v6,
|
|
|
|
// the rest does not matter
|
|
|
|
name: _name,
|
2020-04-23 06:45:03 +00:00
|
|
|
interface_type: _interface_type,
|
2020-04-23 05:54:12 +00:00
|
|
|
auto: _auto,
|
|
|
|
active: _active,
|
|
|
|
cidr_v4: _cidr_v4,
|
|
|
|
cidr_v6: _cidr_v6,
|
|
|
|
gateway_v4: _gateway_v4,
|
|
|
|
gateway_v6: _gateway_v6,
|
|
|
|
mtu: _mtu,
|
2020-04-23 07:24:17 +00:00
|
|
|
bridge_ports: _bridge_ports,
|
2020-04-23 07:31:10 +00:00
|
|
|
bond_slaves: _bond_slaves,
|
2020-04-23 05:54:12 +00:00
|
|
|
} => {
|
|
|
|
method_v4 == method_v6
|
|
|
|
&& options_v4.is_empty()
|
|
|
|
&& options_v6.is_empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:06:22 +00:00
|
|
|
fn write_iface(&self, w: &mut dyn Write) -> Result<(), Error> {
|
|
|
|
|
2020-04-21 15:17:57 +00:00
|
|
|
fn method_to_str(method: NetworkConfigMethod) -> &'static str {
|
2020-04-21 09:06:22 +00:00
|
|
|
match method {
|
2020-04-21 15:17:57 +00:00
|
|
|
NetworkConfigMethod::Static => "static",
|
|
|
|
NetworkConfigMethod::Loopback => "loopback",
|
|
|
|
NetworkConfigMethod::Manual => "manual",
|
|
|
|
NetworkConfigMethod::DHCP => "dhcp",
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 10:42:09 +00:00
|
|
|
if self.method_v4.is_none() && self.method_v6.is_none() { return Ok(()); }
|
|
|
|
|
2020-04-22 14:46:46 +00:00
|
|
|
if self.auto {
|
2020-04-21 09:06:22 +00:00
|
|
|
writeln!(w, "auto {}", self.name)?;
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:54:12 +00:00
|
|
|
if self.combine_entry() {
|
2020-04-22 10:42:09 +00:00
|
|
|
if let Some(method) = self.method_v4 {
|
|
|
|
writeln!(w, "iface {} {}", self.name, method_to_str(method))?;
|
|
|
|
self.write_iface_attributes_v4(w, method)?;
|
|
|
|
self.write_iface_attributes_v6(w, method)?;
|
2020-04-23 05:07:14 +00:00
|
|
|
self.write_iface_attributes(w)?;
|
2020-04-22 10:42:09 +00:00
|
|
|
writeln!(w)?;
|
|
|
|
}
|
2020-04-21 09:06:22 +00:00
|
|
|
} else {
|
|
|
|
if let Some(method) = self.method_v4 {
|
|
|
|
writeln!(w, "iface {} inet {}", self.name, method_to_str(method))?;
|
2020-04-22 10:42:09 +00:00
|
|
|
self.write_iface_attributes_v4(w, method)?;
|
2020-04-21 09:06:22 +00:00
|
|
|
writeln!(w)?;
|
|
|
|
}
|
|
|
|
if let Some(method) = self.method_v6 {
|
|
|
|
writeln!(w, "iface {} inet6 {}", self.name, method_to_str(method))?;
|
2020-04-22 10:42:09 +00:00
|
|
|
self.write_iface_attributes_v6(w, method)?;
|
2020-04-21 09:06:22 +00:00
|
|
|
writeln!(w)?;
|
|
|
|
}
|
2020-04-23 05:07:14 +00:00
|
|
|
self.write_iface_attributes(w)?;
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum NetworkOrderEntry {
|
|
|
|
Iface(String),
|
|
|
|
Comment(String),
|
|
|
|
Option(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NetworkConfig {
|
2020-04-21 12:28:26 +00:00
|
|
|
pub interfaces: HashMap<String, Interface>,
|
2020-04-20 12:15:57 +00:00
|
|
|
order: Vec<NetworkOrderEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkConfig {
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
interfaces: HashMap::new(),
|
|
|
|
order: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 08:54:07 +00:00
|
|
|
pub fn lookup(&self, name: &str) -> Result<&Interface, Error> {
|
|
|
|
let interface = self.interfaces.get(name).ok_or_else(|| {
|
|
|
|
format_err!("interface '{}' does not exist.", name)
|
|
|
|
})?;
|
|
|
|
Ok(interface)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lookup_mut(&mut self, name: &str) -> Result<&mut Interface, Error> {
|
|
|
|
let interface = self.interfaces.get_mut(name).ok_or_else(|| {
|
|
|
|
format_err!("interface '{}' does not exist.", name)
|
|
|
|
})?;
|
|
|
|
Ok(interface)
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:15:57 +00:00
|
|
|
pub fn write_config(&self, w: &mut dyn Write) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut done = HashSet::new();
|
|
|
|
|
|
|
|
let mut last_entry_was_comment = false;
|
|
|
|
|
|
|
|
for entry in self.order.iter() {
|
2020-04-21 09:06:22 +00:00
|
|
|
match entry {
|
2020-04-20 12:15:57 +00:00
|
|
|
NetworkOrderEntry::Comment(comment) => {
|
|
|
|
writeln!(w, "#{}", comment)?;
|
|
|
|
last_entry_was_comment = true;
|
|
|
|
}
|
|
|
|
NetworkOrderEntry::Option(option) => {
|
|
|
|
if last_entry_was_comment { writeln!(w)?; }
|
|
|
|
last_entry_was_comment = false;
|
|
|
|
writeln!(w, "{}", option)?;
|
|
|
|
writeln!(w)?;
|
|
|
|
}
|
|
|
|
NetworkOrderEntry::Iface(name) => {
|
|
|
|
let interface = match self.interfaces.get(name) {
|
|
|
|
Some(interface) => interface,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
if last_entry_was_comment { writeln!(w)?; }
|
|
|
|
last_entry_was_comment = false;
|
|
|
|
|
|
|
|
if done.contains(name) { continue; }
|
|
|
|
done.insert(name);
|
|
|
|
|
2020-04-21 09:06:22 +00:00
|
|
|
interface.write_iface(w)?;
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (name, interface) in &self.interfaces {
|
|
|
|
if done.contains(name) { continue; }
|
2020-04-21 09:06:22 +00:00
|
|
|
interface.write_iface(w)?;
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 10:55:33 +00:00
|
|
|
|
|
|
|
pub const NETWORK_INTERFACES_FILENAME: &str = "/etc/network/interfaces";
|
2020-04-23 05:19:29 +00:00
|
|
|
pub const NETWORK_INTERFACES_NEW_FILENAME: &str = "/etc/network/interfaces.new";
|
2020-04-21 10:55:33 +00:00
|
|
|
pub const NETWORK_LOCKFILE: &str = "/var/lock/pve-network.lck";
|
|
|
|
|
2020-04-23 05:19:29 +00:00
|
|
|
|
2020-04-21 10:55:33 +00:00
|
|
|
pub fn config() -> Result<(NetworkConfig, [u8;32]), Error> {
|
2020-04-23 05:19:29 +00:00
|
|
|
let content = std::fs::read(NETWORK_INTERFACES_NEW_FILENAME)
|
|
|
|
.or_else(|err| {
|
2020-04-21 10:55:33 +00:00
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
2020-04-23 05:19:29 +00:00
|
|
|
std::fs::read(NETWORK_INTERFACES_FILENAME)
|
|
|
|
.or_else(|err| {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
Ok(Vec::new())
|
|
|
|
} else {
|
|
|
|
bail!("unable to read '{}' - {}", NETWORK_INTERFACES_FILENAME, err);
|
|
|
|
}
|
|
|
|
})
|
2020-04-21 10:55:33 +00:00
|
|
|
} else {
|
2020-04-23 05:19:29 +00:00
|
|
|
bail!("unable to read '{}' - {}", NETWORK_INTERFACES_NEW_FILENAME, err);
|
2020-04-21 10:55:33 +00:00
|
|
|
}
|
2020-04-23 05:19:29 +00:00
|
|
|
})?;
|
|
|
|
|
2020-04-21 10:55:33 +00:00
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(&content);
|
|
|
|
|
|
|
|
let mut parser = NetworkParser::new(&content[..]);
|
|
|
|
let data = parser.parse_interfaces()?;
|
|
|
|
|
|
|
|
Ok((data, digest))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_config(config: &NetworkConfig) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut raw = Vec::new();
|
|
|
|
config.write_config(&mut raw)?;
|
|
|
|
|
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = root, group(r)=root, others(r)
|
|
|
|
let options = CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(nix::unistd::ROOT)
|
|
|
|
.group(nix::unistd::Gid::from_raw(0));
|
|
|
|
|
2020-04-23 05:19:29 +00:00
|
|
|
replace_file(NETWORK_INTERFACES_NEW_FILENAME, &raw, options)?;
|
2020-04-21 10:55:33 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-22 08:54:07 +00:00
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
pub fn complete_interface_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.interfaces.keys().map(|id| id.to_string()).collect(),
|
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|