#[cfg(test)]
use super::windows::adapter_property_probe;
use super::windows::{
POWERSHELL, WindowsHyperV, powershell, powershell_script, ps_literal, route_prefix, windows_mac,
};
use super::{Platform, ipv4_netmask};
use crate::config::{Interface, Profile, Route};
use crate::net::{Cmd, CommandRunner};
use anyhow::{Result, bail};
use ipnet::IpNet;
const VLAN_KEYWORD: &str = "VlanID";
const RESTART_WAIT_POLLS: u32 = 40;
pub struct WindowsDriverVlan;
impl Platform for WindowsDriverVlan {
fn name(&self) -> &'static str {
"windows-driver-vlan"
}
fn iface_name(&self, _interface: &Interface, device: &str) -> String {
device.to_string()
}
fn bringup_commands(&self, interface: &Interface, device: &str) -> Vec<Cmd> {
let mut cmds = vec![powershell(&set_vlan_script(
interface.vlan.unwrap_or(0),
device,
))];
let addr = interface.address.addr().to_string();
let mask = ipv4_netmask(interface.address.prefix_len());
cmds.push(Cmd::new(
"netsh",
&[
"interface",
"ipv4",
"set",
"address",
device,
"static",
&addr,
&mask,
],
));
if let Some(mtu) = interface.mtu {
cmds.push(Cmd::new(
"netsh",
&[
"interface",
"ipv4",
"set",
"subinterface",
device,
&format!("mtu={mtu}"),
"store=persistent",
],
));
}
cmds
}
fn teardown_commands(&self, iface: &str) -> Vec<Cmd> {
vec![
powershell(&clear_routes_script(iface)),
Cmd::new(
"netsh",
&["interface", "ipv4", "set", "address", iface, "dhcp"],
),
powershell(&set_vlan_script(0, iface)),
]
}
fn route_commands(&self, route: &Route, _in_subnet: bool, iface: &str) -> Vec<Cmd> {
let mut cmds = vec![powershell(&set_route_script(route, iface))];
if route.gateway.is_none()
&& let Some(mac) = &route.mac
&& let Ok(net) = route.destination.parse::<IpNet>()
{
cmds.push(powershell(&set_neighbor_script(
&net.addr().to_string(),
&windows_mac(mac),
iface,
)));
}
cmds
}
fn existing_vlans(
&self,
_runner: &mut dyn CommandRunner,
_parent: &str,
) -> Result<Vec<(u16, String)>> {
Ok(Vec::new())
}
fn is_candidate_device(&self, name: &str) -> bool {
WindowsHyperV.is_candidate_device(name)
}
fn reverts_parent_config(&self) -> bool {
true
}
fn claims_parent_exclusively(&self) -> bool {
true
}
fn teardown_removes_interface(&self) -> bool {
false
}
fn validate_profile(&self, profile: &Profile) -> Result<()> {
if profile.interfaces.len() > 1 {
bail!(
"profile '{}' has {} interface entries, and the driver VLAN backend \
configures one VLAN per adapter (the `VlanID` keyword holds a single \
id); apply one VLAN at a time as separate profiles, or use the Hyper-V \
backend for several VLANs on one adapter",
profile.name,
profile.interfaces.len()
);
}
Ok(())
}
fn list_devices(&self, runner: &mut dyn CommandRunner) -> Result<Vec<String>> {
WindowsHyperV.list_devices(runner)
}
fn addresses_on(&self, runner: &mut dyn CommandRunner, device: &str) -> Result<Vec<IpNet>> {
WindowsHyperV.addresses_on(runner, device)
}
fn is_wireless(&self, runner: &mut dyn CommandRunner, device: &str) -> Result<bool> {
WindowsHyperV.is_wireless(runner, device)
}
fn link_is_active(&self, runner: &mut dyn CommandRunner, device: &str) -> Result<bool> {
WindowsHyperV.link_is_active(runner, device)
}
fn records_created_interface(&self, cmd: &Cmd) -> bool {
cmd.program == POWERSHELL
&& powershell_script(cmd).is_some_and(|s| {
s.contains("Set-NetAdapterAdvancedProperty") && !s.contains("-RegistryValue '0'")
})
}
}
fn set_vlan_script(vlan: u16, device: &str) -> String {
let dev = ps_literal(device);
let value = ps_literal(&vlan.to_string());
format!(
"Set-NetAdapterAdvancedProperty -Name {dev} -RegistryKeyword {kw} -RegistryValue {value}; \
$tries = 0; \
while (-not ((Get-NetAdapterAdvancedProperty -Name {dev} -RegistryKeyword {kw} \
-ErrorAction SilentlyContinue).RegistryValue -eq {value} -and \
(Get-NetAdapter | Where-Object {{ $_.Name -eq {dev} }}))) {{ \
if (++$tries -gt {RESTART_WAIT_POLLS}) {{ throw 'adapter ' + {dev} + ' did not come back after setting {VLAN_KEYWORD}' }}; \
Start-Sleep -Milliseconds 500 }}",
kw = ps_literal(VLAN_KEYWORD),
)
}
fn set_route_script(route: &Route, iface: &str) -> String {
let prefix = ps_literal(&route_prefix(&route.destination));
let dev = ps_literal(iface);
let next_hop = match &route.gateway {
Some(gateway) => format!(" -NextHop {}", ps_literal(&gateway.to_string())),
None => String::new(),
};
format!(
"Remove-NetRoute -DestinationPrefix {prefix} -InterfaceAlias {dev} -Confirm:$false \
-ErrorAction SilentlyContinue; \
New-NetRoute -DestinationPrefix {prefix} -InterfaceAlias {dev}{next_hop} | Out-Null"
)
}
fn set_neighbor_script(host: &str, mac: &str, iface: &str) -> String {
let host = ps_literal(host);
let mac = ps_literal(mac);
let dev = ps_literal(iface);
format!(
"Remove-NetNeighbor -InterfaceAlias {dev} -IPAddress {host} -Confirm:$false \
-ErrorAction SilentlyContinue; \
New-NetNeighbor -InterfaceAlias {dev} -IPAddress {host} -LinkLayerAddress {mac} \
-State Permanent | Out-Null"
)
}
fn clear_routes_script(iface: &str) -> String {
let dev = ps_literal(iface);
format!(
"Get-NetRoute -InterfaceAlias {dev} -AddressFamily IPv4 -ErrorAction SilentlyContinue | \
Where-Object {{ $_.Protocol -eq 'NetMgmt' }} | \
Remove-NetRoute -Confirm:$false -ErrorAction SilentlyContinue; \
Get-NetNeighbor -InterfaceAlias {dev} -AddressFamily IPv4 -State Permanent \
-ErrorAction SilentlyContinue | \
Where-Object {{ $_.LinkLayerAddress -notlike '01-00-5E-*' -and \
$_.LinkLayerAddress -ne 'FF-FF-FF-FF-FF-FF' }} | \
Remove-NetNeighbor -Confirm:$false -ErrorAction SilentlyContinue; exit 0"
)
}
#[cfg(test)]
fn keyword_probe(device: &str) -> Cmd {
adapter_property_probe(device, "Status")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::net::RecordingRunner;
use crate::plan::bringup_commands_for;
fn tagged(id: u16, cidr: &str) -> Interface {
Interface {
vlan: Some(id),
address: cidr.parse().unwrap(),
mtu: None,
routes: vec![],
}
}
fn render(cmds: &[Cmd]) -> Vec<String> {
cmds.iter().map(Cmd::display).collect()
}
#[test]
fn the_interface_is_the_parent_adapter_itself() {
assert_eq!(
WindowsDriverVlan.iface_name(&tagged(11, "192.168.11.87/24"), "Ethernet 2"),
"Ethernet 2"
);
}
#[test]
fn rendering_of_a_tagged_entry_is_pinned() {
let i = Interface {
vlan: Some(11),
address: "192.168.11.87/24".parse().unwrap(),
mtu: Some(1500),
routes: vec![Route {
destination: "239.255.0.255/32".to_string(),
gateway: None,
mac: None,
}],
};
let cmds = bringup_commands_for(&WindowsDriverVlan, &i, "Ethernet 2");
assert_eq!(
powershell_script(&cmds[0]).unwrap(),
"$ErrorActionPreference = 'Stop'; \
Set-NetAdapterAdvancedProperty -Name 'Ethernet 2' -RegistryKeyword 'VlanID' -RegistryValue '11'; \
$tries = 0; \
while (-not ((Get-NetAdapterAdvancedProperty -Name 'Ethernet 2' -RegistryKeyword 'VlanID' \
-ErrorAction SilentlyContinue).RegistryValue -eq '11' -and \
(Get-NetAdapter | Where-Object { $_.Name -eq 'Ethernet 2' }))) { \
if (++$tries -gt 40) { throw 'adapter ' + 'Ethernet 2' + ' did not come back after setting VlanID' }; \
Start-Sleep -Milliseconds 500 }"
);
assert_eq!(
render(&cmds[1..3]),
vec![
"netsh interface ipv4 set address Ethernet 2 static 192.168.11.87 255.255.255.0",
"netsh interface ipv4 set subinterface Ethernet 2 mtu=1500 store=persistent",
]
);
assert_eq!(
powershell_script(&cmds[3]).unwrap(),
"$ErrorActionPreference = 'Stop'; \
Remove-NetRoute -DestinationPrefix '239.255.0.255/32' -InterfaceAlias 'Ethernet 2' \
-Confirm:$false -ErrorAction SilentlyContinue; \
New-NetRoute -DestinationPrefix '239.255.0.255/32' -InterfaceAlias 'Ethernet 2' | Out-Null"
);
assert_eq!(cmds.len(), 4);
}
#[test]
fn routes_and_neighbors_replace_rather_than_add() {
let via = Route {
destination: "10.0.0.0/8".to_string(),
gateway: Some("192.168.11.1".parse().unwrap()),
mac: None,
};
let cmds = WindowsDriverVlan.route_commands(&via, false, "Ethernet");
assert_eq!(cmds.len(), 1);
let script = powershell_script(&cmds[0]).unwrap();
assert!(
script.contains("; Remove-NetRoute -DestinationPrefix '10.0.0.0/8'")
&& script.contains(
"New-NetRoute -DestinationPrefix '10.0.0.0/8' -InterfaceAlias 'Ethernet' \
-NextHop '192.168.11.1'"
),
"{script}"
);
let on_link = Route {
destination: "192.168.11.102/32".to_string(),
gateway: None,
mac: Some("3a:42:f7:79:32:2e".to_string()),
};
let cmds = WindowsDriverVlan.route_commands(&on_link, true, "Ethernet");
let scripts: Vec<&str> = cmds.iter().map(|c| powershell_script(c).unwrap()).collect();
assert_eq!(scripts.len(), 2);
assert_eq!(
scripts[1],
"$ErrorActionPreference = 'Stop'; \
Remove-NetNeighbor -InterfaceAlias 'Ethernet' -IPAddress '192.168.11.102' \
-Confirm:$false -ErrorAction SilentlyContinue; \
New-NetNeighbor -InterfaceAlias 'Ethernet' -IPAddress '192.168.11.102' \
-LinkLayerAddress '3a-42-f7-79-32-2e' -State Permanent | Out-Null"
);
assert!(cmds.iter().all(|c| c.program != "netsh"));
}
#[test]
fn teardown_clears_routes_then_returns_the_adapter_to_dhcp_and_no_vlan() {
let cmds = WindowsDriverVlan.teardown_commands("Ethernet 2");
assert_eq!(cmds.len(), 3);
let clear = powershell_script(&cmds[0]).unwrap();
assert!(
clear.contains("Get-NetRoute -InterfaceAlias 'Ethernet 2'")
&& clear.contains("$_.Protocol -eq 'NetMgmt'")
&& clear.contains("Remove-NetRoute")
&& clear.contains("Get-NetNeighbor -InterfaceAlias 'Ethernet 2'")
&& clear.contains("-State Permanent")
&& clear.contains("$_.LinkLayerAddress -notlike '01-00-5E-*'")
&& clear.contains("$_.LinkLayerAddress -ne 'FF-FF-FF-FF-FF-FF'")
&& clear.contains("Remove-NetNeighbor")
&& clear.ends_with("; exit 0"),
"{clear}"
);
assert_eq!(
cmds[1].display(),
"netsh interface ipv4 set address Ethernet 2 dhcp"
);
assert!(
powershell_script(&cmds[2])
.unwrap()
.contains("-RegistryKeyword 'VlanID' -RegistryValue '0';")
);
}
#[test]
fn only_the_apply_direction_of_the_keyword_is_recorded() {
let cmds = bringup_commands_for(
&WindowsDriverVlan,
&tagged(11, "192.168.11.87/24"),
"Ethernet 2",
);
let recorded: Vec<bool> = cmds
.iter()
.map(|c| WindowsDriverVlan.records_created_interface(c))
.collect();
assert_eq!(recorded, vec![true, false]);
for cmd in WindowsDriverVlan.teardown_commands("Ethernet 2") {
assert!(
!WindowsDriverVlan.records_created_interface(&cmd),
"{}",
cmd.display()
);
}
}
#[test]
fn a_profile_with_two_entries_is_refused_before_anything_runs() {
let one: Profile =
toml::from_str("name=\"live\"\n[[interface]]\nvlan=11\naddress=\"192.168.11.87/24\"\n")
.unwrap();
assert!(WindowsDriverVlan.validate_profile(&one).is_ok());
let two: Profile = toml::from_str(
"name=\"lab\"\n[[interface]]\nvlan=10\naddress=\"192.168.10.90/24\"\n\
[[interface]]\nvlan=11\naddress=\"192.168.11.87/24\"\n",
)
.unwrap();
let err = WindowsDriverVlan
.validate_profile(&two)
.unwrap_err()
.to_string();
assert!(
err.contains("one VLAN per adapter") && err.contains("Hyper-V"),
"{err}"
);
}
#[test]
fn the_keyword_script_carries_no_double_quotes_and_escapes_the_device() {
let script = set_vlan_script(11, "x'; Remove-VMSwitch -Name 'vlanctl");
assert!(!script.contains('"'));
assert!(script.contains("-Name 'x''; Remove-VMSwitch -Name ''vlanctl'"));
}
#[test]
fn host_probes_are_the_hyperv_backends_and_stay_read_only() {
let mut runner = RecordingRunner::default();
let _ = WindowsDriverVlan.list_devices(&mut runner);
let _ = WindowsDriverVlan.addresses_on(&mut runner, "Ethernet 2");
let _ = WindowsDriverVlan.link_is_active(&mut runner, "Ethernet 2");
let _ = WindowsDriverVlan.is_wireless(&mut runner, "Ethernet 2");
assert_eq!(runner.commands.len(), 4);
for cmd in &runner.commands {
assert!(WindowsHyperV::is_read_only_probe(cmd), "{}", cmd.display());
}
assert!(WindowsHyperV::is_read_only_probe(&keyword_probe(
"Ethernet 2"
)));
let set = powershell(&set_vlan_script(11, "Ethernet 2"));
assert!(!WindowsHyperV::is_read_only_probe(&set));
}
}