2020-04-20 12:15:57 +00:00
|
|
|
use std::io::{Write};
|
|
|
|
use std::collections::{HashSet, HashMap};
|
|
|
|
|
|
|
|
use anyhow::{Error, bail};
|
2020-04-21 12:28:26 +00:00
|
|
|
use serde::{Serialize, Deserialize};
|
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-21 12:28:26 +00:00
|
|
|
use proxmox::api::api;
|
2020-04-21 10:55:33 +00:00
|
|
|
|
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-21 12:28:26 +00:00
|
|
|
#[api()]
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
/// Interface configuration method
|
2020-04-21 15:17:57 +00:00
|
|
|
pub enum NetworkConfigMethod {
|
2020-04-21 12:28:26 +00:00
|
|
|
/// Configuration is done manually using other tools
|
2020-04-20 16:10:15 +00:00
|
|
|
Manual,
|
2020-04-21 12:28:26 +00:00
|
|
|
/// Define interfaces with statically allocated addresses.
|
2020-04-20 16:10:15 +00:00
|
|
|
Static,
|
2020-04-21 12:28:26 +00:00
|
|
|
/// Obtain an address via DHCP
|
2020-04-20 16:10:15 +00:00
|
|
|
DHCP,
|
2020-04-21 12:28:26 +00:00
|
|
|
/// Define the loopback interface.
|
2020-04-20 16:10:15 +00:00
|
|
|
Loopback,
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 12:28:26 +00:00
|
|
|
#[api(
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
type: String,
|
|
|
|
min_length: 1,
|
|
|
|
max_length: libc::IFNAMSIZ-1,
|
|
|
|
},
|
|
|
|
method_v4: {
|
2020-04-21 15:17:57 +00:00
|
|
|
type: NetworkConfigMethod,
|
2020-04-21 12:28:26 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
method_v6: {
|
2020-04-21 15:17:57 +00:00
|
|
|
type: NetworkConfigMethod,
|
2020-04-21 12:28:26 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
options_v4: {
|
|
|
|
description: "Option list (inet)",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
description: "Optional attribute.",
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options_v6: {
|
|
|
|
description: "Option list (inet6)",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
description: "Optional attribute.",
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
/// Network Interface configuration
|
2020-04-20 12:15:57 +00:00
|
|
|
pub struct Interface {
|
2020-04-21 12:28:26 +00:00
|
|
|
/// Autostart interface
|
2020-04-21 09:06:22 +00:00
|
|
|
pub autostart: bool,
|
2020-04-21 12:28:26 +00:00
|
|
|
/// Interface is a physical network device
|
2020-04-21 09:46:56 +00:00
|
|
|
pub exists: bool,
|
2020-04-21 12:28:26 +00:00
|
|
|
/// Interface is active (UP)
|
2020-04-21 09:46:56 +00:00
|
|
|
pub active: bool,
|
2020-04-21 12:28:26 +00:00
|
|
|
/// Interface name
|
2020-04-20 12:15:57 +00:00
|
|
|
pub name: String,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-04-21 15:17:57 +00:00
|
|
|
pub method_v4: Option<NetworkConfigMethod>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-04-21 15:17:57 +00:00
|
|
|
pub method_v6: Option<NetworkConfigMethod>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
/// IPv4 address
|
2020-04-20 12:15:57 +00:00
|
|
|
pub address_v4: Option<String>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
/// IPv4 gateway
|
2020-04-20 12:15:57 +00:00
|
|
|
pub gateway_v4: Option<String>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
/// IPv4 netmask
|
2020-04-20 12:15:57 +00:00
|
|
|
pub netmask_v4: Option<u8>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
/// IPv6 address
|
2020-04-20 12:15:57 +00:00
|
|
|
pub address_v6: Option<String>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
/// IPv6 gateway
|
2020-04-20 12:15:57 +00:00
|
|
|
pub gateway_v6: Option<String>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
/// IPv6 netmask
|
2020-04-20 12:15:57 +00:00
|
|
|
pub netmask_v6: Option<u8>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Vec::is_empty")]
|
2020-04-20 12:15:57 +00:00
|
|
|
pub options_v4: Vec<String>,
|
2020-04-21 12:28:26 +00:00
|
|
|
#[serde(skip_serializing_if="Vec::is_empty")]
|
2020-04-20 12:15:57 +00:00
|
|
|
pub options_v6: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 09:06:22 +00:00
|
|
|
autostart: false,
|
2020-04-21 09:46:56 +00:00
|
|
|
exists: false,
|
|
|
|
active: false,
|
2020-04-20 16:10:15 +00:00
|
|
|
method_v4: None,
|
|
|
|
method_v6: None,
|
2020-04-20 12:15:57 +00:00
|
|
|
address_v4: None,
|
|
|
|
gateway_v4: None,
|
|
|
|
netmask_v4: None,
|
|
|
|
address_v6: None,
|
|
|
|
gateway_v6: None,
|
|
|
|
netmask_v6: None,
|
|
|
|
options_v4: Vec::new(),
|
|
|
|
options_v6: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-20 12:15:57 +00:00
|
|
|
fn set_address_v4(&mut self, address: String) -> Result<(), Error> {
|
|
|
|
if self.address_v4.is_none() {
|
|
|
|
self.address_v4 = Some(address);
|
|
|
|
} 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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_netmask_v4(&mut self, mask: u8) -> Result<(), Error> {
|
|
|
|
if self.netmask_v4.is_none() {
|
|
|
|
if mask > 0 && mask <= 32 {
|
|
|
|
self.netmask_v4 = Some(mask);
|
|
|
|
} else {
|
|
|
|
bail!("invalid ipv4 netmaks '{}'", mask);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv4 netmask.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_address_v6(&mut self, address: String) -> Result<(), Error> {
|
|
|
|
if self.address_v6.is_none() {
|
|
|
|
self.address_v6 = Some(address);
|
|
|
|
} 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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_netmask_v6(&mut self, mask: u8) -> Result<(), Error> {
|
|
|
|
if self.netmask_v6.is_none() {
|
|
|
|
if mask > 0 && mask <= 128 {
|
|
|
|
self.netmask_v6 = Some(mask);
|
|
|
|
} else {
|
|
|
|
bail!("invalid ipv6 netmaks '{}'", mask);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv6 netmask.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
fn write_iface_attributes_v4(&self, w: &mut dyn Write) -> Result<(), Error> {
|
|
|
|
if let Some(address) = &self.address_v4 {
|
|
|
|
if let Some(netmask) = self.netmask_v4 {
|
|
|
|
writeln!(w, " address {}/{}", address, netmask)?;
|
|
|
|
} else {
|
|
|
|
writeln!(w, " address {}", address)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(gateway) = &self.gateway_v4 {
|
|
|
|
writeln!(w, " gateway {}", gateway)?;
|
|
|
|
}
|
|
|
|
for option in &self.options_v4 {
|
|
|
|
writeln!(w, " {}", option)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_iface_attributes_v6(&self, w: &mut dyn Write) -> Result<(), Error> {
|
|
|
|
if let Some(address) = &self.address_v6 {
|
|
|
|
if let Some(netmask) = self.netmask_v6 {
|
|
|
|
writeln!(w, " address {}/{}", address, netmask)?;
|
|
|
|
} else {
|
|
|
|
writeln!(w, " address {}", address)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(gateway) = &self.gateway_v6 {
|
|
|
|
writeln!(w, " gateway {}", gateway)?;
|
|
|
|
}
|
|
|
|
for option in &self.options_v6 {
|
|
|
|
writeln!(w, " {}", option)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.autostart {
|
|
|
|
writeln!(w, "auto {}", self.name)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.method_v4 == self.method_v6 {
|
2020-04-21 15:17:57 +00:00
|
|
|
let method = self.method_v4.unwrap_or(NetworkConfigMethod::Static);
|
2020-04-21 09:06:22 +00:00
|
|
|
writeln!(w, "iface {} {}", self.name, method_to_str(method))?;
|
|
|
|
self.write_iface_attributes_v4(w)?;
|
|
|
|
self.write_iface_attributes_v6(w)?;
|
|
|
|
writeln!(w)?;
|
|
|
|
} else {
|
|
|
|
if let Some(method) = self.method_v4 {
|
|
|
|
writeln!(w, "iface {} inet {}", self.name, method_to_str(method))?;
|
|
|
|
self.write_iface_attributes_v4(w)?;
|
|
|
|
writeln!(w)?;
|
|
|
|
}
|
|
|
|
if let Some(method) = self.method_v6 {
|
|
|
|
writeln!(w, "iface {} inet6 {}", self.name, method_to_str(method))?;
|
|
|
|
self.write_iface_attributes_v6(w)?;
|
|
|
|
writeln!(w)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
pub const NETWORK_LOCKFILE: &str = "/var/lock/pve-network.lck";
|
|
|
|
|
|
|
|
pub fn config() -> Result<(NetworkConfig, [u8;32]), Error> {
|
|
|
|
let content = match std::fs::read(NETWORK_INTERFACES_FILENAME) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
Vec::new()
|
|
|
|
} else {
|
|
|
|
bail!("unable to read '{}' - {}", NETWORK_INTERFACES_FILENAME, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
replace_file(NETWORK_INTERFACES_FILENAME, &raw, options)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|