Keep concurrent overlays working and select faster data carriers
A second subnet on macOS could send with the first utun alias. Reconcile and verify each owned subnet route with its actual source, retry partially applied plans and expose host errors to clients. On Windows verify the alias and prefix on the intended adapter. Do not restart underlay sockets when the only network change is a local overlay address. Compare mutually enabled carriers using authenticated data probes while the incumbent carries traffic. The dialler coordinates a generation-scoped decision after a sustained median RTT improvement. Old peers retain ordered fallback. Local IPC moves to v24 to report data RTT and the physical path independently of control RTT. The mesh wire versions, keys and saved network settings stay compatible. Validated formatting, workspace Clippy and the workspace test suite on macOS, including bidirectional dual-network traffic and a live TLS-to-QUIC switch on loopback. Added focused native CI tests for macOS and Windows. The elevated utun test requires administrative access unavailable on the development host.
This commit is contained in:
1 parent
8d107cf0e9
commit
27e29cf8a7
34 files changed
+1569
-253
No files matched your search
@@ -153,9 +153,7 @@ pub struct Interface {
|
||||
routes: Arc<RoutingTable>,
|
||||
carrier: Arc<dyn PacketCarrier>,
|
||||
tally: Arc<Tally>,
|
||||
/// What was last applied to the host, so an unchanged table is not
|
||||
/// re-applied on every pass.
|
||||
applied: std::sync::Mutex<Vec<Cidr>>,
|
||||
addresses: std::sync::Mutex<AddressState>,
|
||||
task: std::sync::Mutex<Option<JoinHandle<()>>>,
|
||||
vip_task: std::sync::Mutex<Option<JoinHandle<()>>>,
|
||||
sync_lock: Arc<tokio::sync::Mutex<()>>,
|
||||
@@ -171,6 +169,13 @@ pub struct Interface {
|
||||
underlay: std::sync::Mutex<std::collections::HashMap<NetworkId, Underlay>>,
|
||||
}
|
||||
|
||||
/// Addresses and subnet routes are applied together. A partial change
|
||||
/// invalidates the cached plan until the whole interface reconciles again.
|
||||
struct AddressState {
|
||||
applied: Option<Vec<Cidr>>,
|
||||
last_error: Option<String>,
|
||||
}
|
||||
|
||||
/// Underlay destinations one network's runtime currently depends on.
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
struct Underlay {
|
||||
@@ -336,7 +341,10 @@ impl Interface {
|
||||
routes,
|
||||
carrier,
|
||||
tally,
|
||||
applied: std::sync::Mutex::new(Vec::new()),
|
||||
addresses: std::sync::Mutex::new(AddressState {
|
||||
applied: Some(Vec::new()),
|
||||
last_error: None,
|
||||
}),
|
||||
task: std::sync::Mutex::new(Some(task)),
|
||||
vip_task: std::sync::Mutex::new(Some(vip_task)),
|
||||
sync_lock: Arc::new(tokio::sync::Mutex::new(())),
|
||||
@@ -700,13 +708,13 @@ impl Interface {
|
||||
let _guard = self.sync_lock.lock().await;
|
||||
let wanted = self.wanted_addresses();
|
||||
{
|
||||
let applied = match self.applied.lock() {
|
||||
Ok(guard) => guard.clone(),
|
||||
Err(poisoned) => poisoned.into_inner().clone(),
|
||||
};
|
||||
if applied == wanted {
|
||||
let mut addresses = self.addresses.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if addresses.applied.as_ref() == Some(&wanted) {
|
||||
return Ok(());
|
||||
}
|
||||
// A failed or cancelled reconfiguration can leave some aliases or
|
||||
// routes installed. Even a return to the previous plan must retry.
|
||||
addresses.applied = None;
|
||||
}
|
||||
|
||||
// Every address this agent holds, in every network it is in. The
|
||||
@@ -717,12 +725,25 @@ impl Interface {
|
||||
addresses: wanted.clone(),
|
||||
mtu: self.mtu,
|
||||
};
|
||||
self.factory.reconfigure(request).await?;
|
||||
match self.applied.lock() {
|
||||
Ok(mut guard) => *guard = wanted,
|
||||
Err(poisoned) => *poisoned.into_inner() = wanted,
|
||||
let result = self.factory.reconfigure(request).await;
|
||||
let mut addresses = self.addresses.lock().unwrap_or_else(|e| e.into_inner());
|
||||
match &result {
|
||||
Ok(()) => {
|
||||
addresses.applied = Some(wanted);
|
||||
addresses.last_error = None;
|
||||
}
|
||||
Err(err) => addresses.last_error = Some(format!("{}: {err}", self.name)),
|
||||
}
|
||||
Ok(())
|
||||
result
|
||||
}
|
||||
|
||||
/// Last error applying the shared interface's addresses and subnet routes.
|
||||
pub fn address_error(&self) -> Option<String> {
|
||||
self.addresses
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.last_error
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// The interface name the operating system gave.
|
||||
@@ -1244,6 +1265,83 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dropping_a_network_after_partial_provisioning_removes_its_alias() {
|
||||
#[derive(Debug, Default)]
|
||||
struct PartialFactory {
|
||||
devices: MemoryTunFactory,
|
||||
addresses: std::sync::Mutex<Vec<Cidr>>,
|
||||
}
|
||||
|
||||
impl TunFactory for PartialFactory {
|
||||
fn name(&self) -> &str {
|
||||
"partial"
|
||||
}
|
||||
|
||||
fn on_host(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn create<'a>(
|
||||
&'a self,
|
||||
request: TunRequest,
|
||||
) -> crate::BoxFuture<'a, Result<Arc<dyn TunDevice>, OverlayError>> {
|
||||
self.devices.create(request)
|
||||
}
|
||||
|
||||
fn reconfigure<'a>(
|
||||
&'a self,
|
||||
request: TunRequest,
|
||||
) -> crate::BoxFuture<'a, Result<(), OverlayError>> {
|
||||
Box::pin(async move {
|
||||
let mut host = self.addresses.lock().unwrap();
|
||||
*host = request.addresses;
|
||||
if !host.is_empty() {
|
||||
return Err(OverlayError::Unavailable("subnet route failed".into()));
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let routes = Arc::new(RoutingTable::new());
|
||||
let network = network("partial");
|
||||
routes
|
||||
.set_network(
|
||||
network,
|
||||
NetworkRoutes {
|
||||
local: Some(addr(1)),
|
||||
range: Some("10.13.37.0/24".parse().unwrap()),
|
||||
..NetworkRoutes::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let factory = Arc::new(PartialFactory::default());
|
||||
let interface = Interface::start(
|
||||
factory.clone(),
|
||||
"tsunpartial",
|
||||
1280,
|
||||
routes.clone(),
|
||||
Arc::new(Recorder::default()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(interface.address_error().is_none());
|
||||
assert!(interface.sync_addresses().await.is_err());
|
||||
assert!(
|
||||
interface
|
||||
.address_error()
|
||||
.unwrap()
|
||||
.contains("subnet route failed")
|
||||
);
|
||||
assert_eq!(factory.addresses.lock().unwrap().len(), 1);
|
||||
routes.remove_network(network);
|
||||
interface.sync_addresses().await.unwrap();
|
||||
assert!(interface.address_error().is_none());
|
||||
assert!(factory.addresses.lock().unwrap().is_empty());
|
||||
interface.remove().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_wanted_addresses_follow_the_table() {
|
||||
let (interface, _device, routes, _carrier, id) = interface(false).await;
|
||||
|
||||
@@ -39,7 +39,8 @@
|
||||
//! `getifaddrs`. An interface is matched by its real `utunN` name, which only
|
||||
//! matters on reconfiguration: the first create always makes a fresh device.
|
||||
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
use netdev::ipnet::IpNet;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::BoxFuture;
|
||||
@@ -115,7 +116,7 @@ impl InterfaceProvisioner for UtunProvisioner {
|
||||
if self.is_ours(&plan.name) {
|
||||
let current = observe(&plan.name).await;
|
||||
let changes = plan_changes(¤t, plan, true)?;
|
||||
configure(&plan.name, &changes).await?;
|
||||
configure(&plan.name, &changes, &plan.addresses).await?;
|
||||
return Ok(Provisioned {
|
||||
changes,
|
||||
device: None,
|
||||
@@ -136,7 +137,7 @@ impl InterfaceProvisioner for UtunProvisioner {
|
||||
// A failure here leaves an interface that exists but cannot carry
|
||||
// traffic. Drop the device so the utun closes, and report, rather
|
||||
// than leave a trap behind.
|
||||
if let Err(err) = configure(&real, &changes).await {
|
||||
if let Err(err) = configure(&real, &changes, &plan.addresses).await {
|
||||
drop(device);
|
||||
return Err(err);
|
||||
}
|
||||
@@ -225,28 +226,25 @@ async fn observe(name: &str) -> InterfaceState {
|
||||
}
|
||||
|
||||
/// Applies MTU, link state, addresses and the on-link routes they imply.
|
||||
async fn configure(name: &str, changes: &super::Changes) -> Result<(), OverlayError> {
|
||||
async fn configure(
|
||||
name: &str,
|
||||
changes: &super::Changes,
|
||||
wanted: &[Cidr],
|
||||
) -> Result<(), OverlayError> {
|
||||
if let Some(mtu) = changes.set_mtu {
|
||||
set_mtu(name, mtu).await?;
|
||||
}
|
||||
for cidr in &changes.remove {
|
||||
// Best effort: an address that is already gone is the outcome wanted.
|
||||
if let Err(err) = address_del(name, *cidr).await {
|
||||
tracing::debug!(interface = %name, %cidr, %err, "could not remove address");
|
||||
}
|
||||
if let Err(err) = route_del(name, *cidr).await {
|
||||
tracing::debug!(interface = %name, %cidr, %err, "could not remove route");
|
||||
let network = subnet(*cidr)?;
|
||||
if !is_host_route(*cidr)
|
||||
&& matches!(subnet_route(network).await?, SubnetRoute::Present { interface, .. } if interface == name)
|
||||
{
|
||||
run("/sbin/route", route_args("delete", name, network)).await?;
|
||||
}
|
||||
address_del(name, *cidr).await?;
|
||||
}
|
||||
for cidr in &changes.add {
|
||||
address_add(name, *cidr).await?;
|
||||
// The subnet route makes the whole overlay range reach the interface;
|
||||
// a point-to-point utun does not install one from the address alone. A
|
||||
// host address (/32 or /128) needs none. Best effort: a route that is
|
||||
// already there is the outcome wanted.
|
||||
if let Err(err) = route_add(name, *cidr).await {
|
||||
tracing::debug!(interface = %name, %cidr, %err, "could not add route");
|
||||
}
|
||||
}
|
||||
if changes.bring_up {
|
||||
// Best effort: a utun with a descriptor open is up already, and a
|
||||
@@ -255,31 +253,22 @@ async fn configure(name: &str, changes: &super::Changes) -> Result<(), OverlayEr
|
||||
tracing::debug!(interface = %name, %err, "could not enable the interface");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The network address of a CIDR, with the host bits cleared.
|
||||
fn network_of(cidr: Cidr) -> IpAddr {
|
||||
match cidr.addr {
|
||||
IpAddr::V4(addr) => {
|
||||
let bits = u32::from(addr);
|
||||
let mask = if cidr.prefix_len == 0 {
|
||||
0
|
||||
} else {
|
||||
u32::MAX << (32 - u32::from(cidr.prefix_len))
|
||||
};
|
||||
IpAddr::V4(Ipv4Addr::from(bits & mask))
|
||||
}
|
||||
IpAddr::V6(addr) => {
|
||||
let bits = u128::from(addr);
|
||||
let mask = if cidr.prefix_len == 0 {
|
||||
0
|
||||
} else {
|
||||
u128::MAX << (128 - u32::from(cidr.prefix_len))
|
||||
};
|
||||
IpAddr::V6(Ipv6Addr::from(bits & mask))
|
||||
// An alias may already be present after a partial reconcile, or its old
|
||||
// subnet route may still select the first alias on this utun.
|
||||
let mut result = Ok(());
|
||||
for &cidr in wanted {
|
||||
if !is_host_route(cidr) {
|
||||
// Finish the other networks even when one subnet conflicts.
|
||||
let update = ensure_subnet_route(name, subnet(cidr)?).await;
|
||||
result = result.and(update);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn subnet(cidr: Cidr) -> Result<IpNet, OverlayError> {
|
||||
IpNet::new(cidr.addr, cidr.prefix_len)
|
||||
.map_err(|err| OverlayError::Unavailable(format!("invalid overlay prefix {cidr}: {err}")))
|
||||
}
|
||||
|
||||
/// Whether a CIDR is a single host address, which needs no subnet route.
|
||||
@@ -326,40 +315,26 @@ fn address_del_args(name: &str, cidr: Cidr) -> Vec<String> {
|
||||
]
|
||||
}
|
||||
|
||||
/// The `route` arguments that point a CIDR's subnet at the interface.
|
||||
fn route_add_args(name: &str, cidr: Cidr) -> Vec<String> {
|
||||
let family = match cidr.addr {
|
||||
fn route_args(command: &str, name: &str, subnet: IpNet) -> Vec<String> {
|
||||
let family = match subnet.addr() {
|
||||
IpAddr::V4(_) => "-inet",
|
||||
IpAddr::V6(_) => "-inet6",
|
||||
};
|
||||
vec![
|
||||
"-q".into(),
|
||||
let mut args = vec![
|
||||
if command == "get" { "-v" } else { "-q" }.into(),
|
||||
"-n".into(),
|
||||
"add".into(),
|
||||
command.into(),
|
||||
family.into(),
|
||||
"-net".into(),
|
||||
format!("{}/{}", network_of(cidr), cidr.prefix_len),
|
||||
"-interface".into(),
|
||||
name.into(),
|
||||
]
|
||||
}
|
||||
|
||||
/// The `route` arguments that remove that subnet route.
|
||||
fn route_del_args(name: &str, cidr: Cidr) -> Vec<String> {
|
||||
let family = match cidr.addr {
|
||||
IpAddr::V4(_) => "-inet",
|
||||
IpAddr::V6(_) => "-inet6",
|
||||
};
|
||||
vec![
|
||||
"-q".into(),
|
||||
"-n".into(),
|
||||
"delete".into(),
|
||||
family.into(),
|
||||
"-net".into(),
|
||||
format!("{}/{}", network_of(cidr), cidr.prefix_len),
|
||||
"-interface".into(),
|
||||
name.into(),
|
||||
]
|
||||
subnet.trunc().to_string(),
|
||||
];
|
||||
if command != "get" {
|
||||
args.extend(["-interface".into(), name.into()]);
|
||||
}
|
||||
if matches!(command, "add" | "change") {
|
||||
args.extend(["-ifa".into(), subnet.addr().to_string()]);
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
async fn address_add(name: &str, cidr: Cidr) -> Result<(), OverlayError> {
|
||||
@@ -370,18 +345,92 @@ async fn address_del(name: &str, cidr: Cidr) -> Result<(), OverlayError> {
|
||||
ifconfig(address_del_args(name, cidr)).await
|
||||
}
|
||||
|
||||
async fn route_add(name: &str, cidr: Cidr) -> Result<(), OverlayError> {
|
||||
if is_host_route(cidr) {
|
||||
return Ok(());
|
||||
}
|
||||
route(route_add_args(name, cidr)).await
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum SubnetRoute {
|
||||
Absent,
|
||||
Present { interface: String, source: IpAddr },
|
||||
}
|
||||
|
||||
async fn route_del(name: &str, cidr: Cidr) -> Result<(), OverlayError> {
|
||||
if is_host_route(cidr) {
|
||||
return Ok(());
|
||||
impl SubnetRoute {
|
||||
fn action(&self, name: &str, subnet: IpNet) -> Result<Option<&'static str>, OverlayError> {
|
||||
match self {
|
||||
Self::Absent => Ok(Some("add")),
|
||||
Self::Present { interface, .. } if interface != name => {
|
||||
Err(OverlayError::Unavailable(format!(
|
||||
"route for {}/{} belongs to {interface}; cannot install it on {name}",
|
||||
subnet.network(),
|
||||
subnet.prefix_len()
|
||||
)))
|
||||
}
|
||||
Self::Present { source, .. } if *source == subnet.addr() => Ok(None),
|
||||
Self::Present { .. } => Ok(Some("change")),
|
||||
}
|
||||
}
|
||||
route(route_del_args(name, cidr)).await
|
||||
}
|
||||
|
||||
// A verbose RTM_GET prints two sockaddr lists (request and reply). In the
|
||||
// kernel's reply IFA follows the variable-width link address of the interface.
|
||||
fn parse_subnet_route(output: &str, subnet: IpNet) -> Result<SubnetRoute, OverlayError> {
|
||||
let invalid =
|
||||
|| OverlayError::Unavailable(format!("could not read the kernel route for {subnet}"));
|
||||
let field = |key: &str| {
|
||||
output
|
||||
.lines()
|
||||
.find_map(|line| line.trim().strip_prefix(key).map(str::trim))
|
||||
};
|
||||
let destination = field("destination:").ok_or_else(invalid)?;
|
||||
let matches = |text: &str, expected: IpAddr| {
|
||||
text.parse::<IpAddr>() == Ok(expected) || (text == "default" && expected.is_unspecified())
|
||||
};
|
||||
if !matches(destination, subnet.network())
|
||||
|| !field("mask:").is_some_and(|mask| matches(mask, subnet.netmask()))
|
||||
{
|
||||
return Ok(SubnetRoute::Absent);
|
||||
}
|
||||
let interface = field("interface:").ok_or_else(invalid)?;
|
||||
let (_, reply) = output.rsplit_once("sockaddrs:").ok_or_else(invalid)?;
|
||||
let (fields, addresses) = reply.trim().split_once('\n').ok_or_else(invalid)?;
|
||||
let after = fields
|
||||
.trim()
|
||||
.trim_matches(['<', '>'])
|
||||
.split(',')
|
||||
.rev()
|
||||
.position(|field| field == "IFA")
|
||||
.ok_or_else(invalid)?;
|
||||
let source = addresses
|
||||
.split_whitespace()
|
||||
.rev()
|
||||
.nth(after)
|
||||
.and_then(|ip| ip.parse().ok())
|
||||
.ok_or_else(invalid)?;
|
||||
Ok(SubnetRoute::Present {
|
||||
interface: interface.into(),
|
||||
source,
|
||||
})
|
||||
}
|
||||
|
||||
async fn subnet_route(subnet: IpNet) -> Result<SubnetRoute, OverlayError> {
|
||||
match run("/sbin/route", route_args("get", "", subnet)).await {
|
||||
Ok(output) => parse_subnet_route(&output, subnet),
|
||||
Err(OverlayError::Unavailable(message)) if message.ends_with("not in table") => {
|
||||
Ok(SubnetRoute::Absent)
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
async fn ensure_subnet_route(name: &str, subnet: IpNet) -> Result<(), OverlayError> {
|
||||
if let Some(command) = subnet_route(subnet).await?.action(name, subnet)? {
|
||||
run("/sbin/route", route_args(command, name, subnet)).await?;
|
||||
let observed = subnet_route(subnet).await?;
|
||||
if observed.action(name, subnet)?.is_some() {
|
||||
return Err(OverlayError::Unavailable(format!(
|
||||
"route for {subnet} on {name} did not select source {} (observed {observed:?})",
|
||||
subnet.addr()
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_mtu(name: &str, mtu: u32) -> Result<(), OverlayError> {
|
||||
@@ -394,27 +443,26 @@ async fn set_up(name: &str) -> Result<(), OverlayError> {
|
||||
|
||||
/// Runs `/sbin/ifconfig`, turning a non-zero exit into an error with its text.
|
||||
async fn ifconfig(args: Vec<String>) -> Result<(), OverlayError> {
|
||||
run("/sbin/ifconfig", args).await
|
||||
}
|
||||
|
||||
/// Runs `/sbin/route`, turning a non-zero exit into an error with its text.
|
||||
async fn route(args: Vec<String>) -> Result<(), OverlayError> {
|
||||
run("/sbin/route", args).await
|
||||
run("/sbin/ifconfig", args).await.map(|_| ())
|
||||
}
|
||||
|
||||
/// Runs a system tool by absolute path, so nothing on `PATH` can stand in for
|
||||
/// it, and turns a non-zero exit into an error carrying what it said.
|
||||
async fn run(program: &'static str, args: Vec<String>) -> Result<(), OverlayError> {
|
||||
async fn run(program: &'static str, args: Vec<String>) -> Result<String, OverlayError> {
|
||||
let display = format!("{program} {}", args.join(" "));
|
||||
let output = tokio::task::spawn_blocking(move || {
|
||||
std::process::Command::new(program).args(&args).output()
|
||||
std::process::Command::new(program)
|
||||
.env("LC_ALL", "C")
|
||||
.args(&args)
|
||||
.output()
|
||||
})
|
||||
.await
|
||||
.map_err(|err| OverlayError::Other(format!("could not run {display}: {err}")))?
|
||||
.map_err(|err| OverlayError::Unavailable(format!("could not run {display}: {err}")))?;
|
||||
|
||||
if output.status.success() {
|
||||
return Ok(());
|
||||
// Darwin route can exit successfully after a routing-socket write failed.
|
||||
if output.status.success() && (program != "/sbin/route" || output.stderr.is_empty()) {
|
||||
return Ok(String::from_utf8_lossy(&output.stdout).into_owned());
|
||||
}
|
||||
let message = {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
@@ -439,6 +487,7 @@ mod tests {
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use super::*;
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
|
||||
fn v4(last: u8, prefix: u8) -> Cidr {
|
||||
Cidr::new(IpAddr::V4(Ipv4Addr::new(10, 13, 37, last)), prefix).unwrap()
|
||||
@@ -476,8 +525,8 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_subnet_route_points_the_masked_network_at_the_interface() {
|
||||
let args = route_add_args("utun5", v4(69, 24));
|
||||
fn a_subnet_route_names_its_own_overlay_source() {
|
||||
let args = route_args("add", "utun5", subnet(v4(69, 24)).unwrap());
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
@@ -488,11 +537,93 @@ mod tests {
|
||||
"-net",
|
||||
"10.13.37.0/24",
|
||||
"-interface",
|
||||
"utun5"
|
||||
"utun5",
|
||||
"-ifa",
|
||||
"10.13.37.69"
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wrong_source_is_repaired_only_on_the_owned_interface() {
|
||||
let cidr = subnet(v4(69, 24)).unwrap();
|
||||
let observed = "destination: 10.13.37.0\n mask: 255.255.255.0\n interface: utun5\n\
|
||||
sockaddrs: <DST,GATEWAY,NETMASK,IFP,IFA>\n 10.13.37.0 index: 20 255.255.255.0 index: 20 utun5 10.99.0.2\n";
|
||||
let route = parse_subnet_route(observed, cidr).unwrap();
|
||||
assert_eq!(route.action("utun5", cidr).unwrap(), Some("change"));
|
||||
assert!(route.action("utun9", cidr).is_err());
|
||||
let corrected = observed.replace("10.99.0.2", "10.13.37.69");
|
||||
assert_eq!(
|
||||
parse_subnet_route(&corrected, cidr)
|
||||
.unwrap()
|
||||
.action("utun5", cidr)
|
||||
.unwrap(),
|
||||
None
|
||||
);
|
||||
assert!(parse_subnet_route(&observed.replace(",IFA>", ">"), cidr).is_err());
|
||||
assert!(parse_subnet_route("", cidr).is_err());
|
||||
let broader = observed.replace("255.255.255.0", "255.255.0.0");
|
||||
assert_eq!(
|
||||
parse_subnet_route(&broader, cidr).unwrap(),
|
||||
SubnetRoute::Absent
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires root and creates a temporary macOS utun with two subnet routes"]
|
||||
async fn unbound_sockets_choose_each_networks_source_on_one_real_utun() {
|
||||
let provisioner = UtunProvisioner::new().unwrap();
|
||||
let first = Cidr::new("198.18.251.1".parse().unwrap(), 30).unwrap();
|
||||
let second = Cidr::new("198.19.251.1".parse().unwrap(), 30).unwrap();
|
||||
let created = provisioner
|
||||
.reconcile(&InterfacePlan::new("tsuntest", 1280, vec![first]))
|
||||
.await
|
||||
.unwrap();
|
||||
let device = created.device.unwrap();
|
||||
let plan = InterfacePlan::new(device.name(), 1280, vec![first, second]);
|
||||
let before = netwatch::netmon::State::new().await;
|
||||
provisioner.reconcile(&plan).await.unwrap();
|
||||
let after = netwatch::netmon::State::new().await;
|
||||
assert!(crate::net::only_owned_interface_changed(
|
||||
&before,
|
||||
&after,
|
||||
device.name()
|
||||
));
|
||||
for (peer, expected) in [("198.18.251.2", first), ("198.19.251.2", second)] {
|
||||
let socket = std::net::UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0)).unwrap();
|
||||
socket
|
||||
.connect((peer.parse::<Ipv4Addr>().unwrap(), 9))
|
||||
.unwrap();
|
||||
assert_eq!(socket.local_addr().unwrap().ip(), expected.addr);
|
||||
}
|
||||
// Simulate a route left by an earlier release, with both aliases still present.
|
||||
let second_subnet = subnet(second).unwrap();
|
||||
let mut stale = route_args("change", device.name(), second_subnet);
|
||||
*stale.last_mut().unwrap() = first.addr.to_string();
|
||||
run("/sbin/route", stale).await.unwrap();
|
||||
assert_eq!(
|
||||
subnet_route(second_subnet)
|
||||
.await
|
||||
.unwrap()
|
||||
.action(device.name(), second_subnet)
|
||||
.unwrap(),
|
||||
Some("change")
|
||||
);
|
||||
provisioner.reconcile(&plan).await.unwrap();
|
||||
let socket = std::net::UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0)).unwrap();
|
||||
socket.connect(("198.19.251.2", 9)).unwrap();
|
||||
assert_eq!(socket.local_addr().unwrap().ip(), second.addr);
|
||||
provisioner
|
||||
.reconcile(&InterfacePlan::new(device.name(), 1280, vec![first]))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
subnet_route(second_subnet).await.unwrap(),
|
||||
SubnetRoute::Absent
|
||||
);
|
||||
provisioner.remove(device.name()).await.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_host_address_installs_no_subnet_route() {
|
||||
assert!(is_host_route(v4(69, 32)));
|
||||
@@ -501,22 +632,6 @@ mod tests {
|
||||
assert!(!is_host_route(v6(1, 64)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_network_address_clears_the_host_bits() {
|
||||
assert_eq!(
|
||||
network_of(v4(69, 24)),
|
||||
IpAddr::V4(Ipv4Addr::new(10, 13, 37, 0))
|
||||
);
|
||||
assert_eq!(
|
||||
network_of(Cidr::new(IpAddr::V4(Ipv4Addr::new(10, 13, 37, 69)), 16).unwrap()),
|
||||
IpAddr::V4(Ipv4Addr::new(10, 13, 0, 0))
|
||||
);
|
||||
assert_eq!(
|
||||
network_of(v6(0x1234, 64)),
|
||||
IpAddr::V6(Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 0))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn link_local_addresses_are_not_ours_to_manage() {
|
||||
assert!(is_link_local(IpAddr::V6(Ipv6Addr::new(
|
||||
|
||||
@@ -131,10 +131,14 @@ impl InterfaceProvisioner for WintunProvisioner {
|
||||
};
|
||||
|
||||
for cidr in flush.iter().chain(changes.remove.iter()) {
|
||||
// Best effort: an address that is already gone is the outcome
|
||||
// wanted, and refusing to start over one is not worth it.
|
||||
if let Err(err) = address_del(&plan.name, *cidr).await {
|
||||
tracing::debug!(interface = %plan.name, %cidr, %err, "could not remove address");
|
||||
if let Err(err) = address_del(&plan.name, *cidr).await
|
||||
&& observe(&plan.name)
|
||||
.await
|
||||
.addresses
|
||||
.iter()
|
||||
.any(|a| a.addr == cidr.addr)
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,11 +266,12 @@ async fn configure(plan: &InterfacePlan, changes: &super::Changes) -> Result<(),
|
||||
set_mtu(&plan.name, mtu).await?;
|
||||
}
|
||||
for cidr in &changes.add {
|
||||
if let Err(err) = address_add(&plan.name, *cidr).await {
|
||||
let err_str = err.to_string();
|
||||
if !err_str.contains("already exists") && !err_str.contains("0x80071392") {
|
||||
return Err(err);
|
||||
}
|
||||
// An already-installed alias is idempotent, including on localized
|
||||
// Windows. A duplicate on a different adapter cannot satisfy the plan.
|
||||
if let Err(err) = address_add(&plan.name, *cidr).await
|
||||
&& !observe(&plan.name).await.addresses.contains(cidr)
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
if changes.bring_up {
|
||||
@@ -300,6 +305,7 @@ fn address_add_args(name: &str, cidr: Cidr) -> Vec<String> {
|
||||
format!("address={addr}"),
|
||||
format!("mask={}", ipv4_mask(cidr.prefix_len)),
|
||||
"store=active".into(),
|
||||
"skipassource=false".into(),
|
||||
],
|
||||
IpAddr::V6(addr) => vec![
|
||||
"interface".into(),
|
||||
@@ -466,6 +472,70 @@ mod tests {
|
||||
assert!(args.contains(&"name=tsun0".to_string()));
|
||||
assert!(args.contains(&"address=10.13.37.69".to_string()));
|
||||
assert!(args.contains(&"mask=255.255.255.0".to_string()));
|
||||
assert!(args.contains(&"skipassource=false".to_string()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires an elevated Windows host with Wintun"]
|
||||
async fn two_windows_subnets_choose_their_own_source_and_survive_removal() {
|
||||
let addresses = [
|
||||
(
|
||||
Ipv4Addr::new(198, 18, 251, 1),
|
||||
Ipv4Addr::new(198, 18, 251, 2),
|
||||
),
|
||||
(
|
||||
Ipv4Addr::new(198, 19, 251, 1),
|
||||
Ipv4Addr::new(198, 19, 251, 2),
|
||||
),
|
||||
];
|
||||
for iface in netdev::get_interfaces() {
|
||||
for prefix in iface.ipv4 {
|
||||
assert!(
|
||||
addresses.iter().all(|(ip, destination)| {
|
||||
!prefix.contains(ip) && !prefix.contains(destination)
|
||||
}),
|
||||
"test subnets already in use"
|
||||
);
|
||||
}
|
||||
}
|
||||
let adapter = format!("tsun-test-{:08x}", rand::random::<u32>());
|
||||
let provisioner = WintunProvisioner::new().unwrap();
|
||||
let cidrs: Vec<_> = addresses
|
||||
.iter()
|
||||
.map(|(ip, _)| Cidr::new((*ip).into(), 30).unwrap())
|
||||
.collect();
|
||||
let mut device = None;
|
||||
for wanted in [&cidrs[..1], &cidrs[..], &cidrs[1..], &cidrs[..]] {
|
||||
let plan = InterfacePlan::new(&adapter, 1280, wanted.to_vec());
|
||||
let result = provisioner.reconcile(&plan).await.unwrap();
|
||||
if let Some(created) = result.device {
|
||||
assert!(device.replace(created).is_none());
|
||||
}
|
||||
// Allow the Windows address to finish duplicate address detection.
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
||||
loop {
|
||||
let ready = addresses
|
||||
.iter()
|
||||
.filter(|(ip, _)| wanted.iter().any(|c| c.addr == IpAddr::V4(*ip)))
|
||||
.all(|(ip, destination)| {
|
||||
std::net::UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))
|
||||
.and_then(|socket| {
|
||||
socket.connect((*destination, 9))?;
|
||||
socket.local_addr()
|
||||
})
|
||||
.is_ok_and(|source| source.ip() == IpAddr::V4(*ip))
|
||||
});
|
||||
if ready {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("Windows selected the wrong source");
|
||||
assert_eq!(observe(&adapter).await.addresses, plan.addresses);
|
||||
}
|
||||
provisioner.remove(&adapter).await.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user