mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-18 22:16:18 -05:00
feat: prepare 3.0.0 release
This commit is contained in:
252 files changed
+26210
-7457
No files matched your search
@@ -0,0 +1,223 @@
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
|
||||
use anyhow::ensure;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
|
||||
use super::super::ClientAddresses;
|
||||
|
||||
/// Physical network context bound to the authenticated non-tunnel endpoint. Wire input is validated;
|
||||
/// runtime transports use this context without performing another DNS lookup.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
|
||||
#[serde(try_from = "BindingFields", rename_all = "camelCase")]
|
||||
pub struct GatewayBinding {
|
||||
endpoint: SocketAddr,
|
||||
source: SocketAddr,
|
||||
interface: String,
|
||||
interface_index: u32,
|
||||
addresses: ClientAddresses,
|
||||
network_fingerprint: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct BindingFields {
|
||||
endpoint: SocketAddr,
|
||||
source: SocketAddr,
|
||||
interface: String,
|
||||
interface_index: u32,
|
||||
addresses: ClientAddresses,
|
||||
network_fingerprint: Option<String>,
|
||||
}
|
||||
|
||||
impl TryFrom<BindingFields> for GatewayBinding {
|
||||
type Error = anyhow::Error;
|
||||
fn try_from(fields: BindingFields) -> Result<Self, Self::Error> {
|
||||
Self::new(
|
||||
fields.endpoint,
|
||||
fields.source,
|
||||
fields.interface,
|
||||
fields.interface_index,
|
||||
fields.addresses,
|
||||
fields.network_fingerprint,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl GatewayBinding {
|
||||
pub fn new(
|
||||
endpoint: SocketAddr,
|
||||
source: SocketAddr,
|
||||
interface: String,
|
||||
interface_index: u32,
|
||||
addresses: ClientAddresses,
|
||||
network_fingerprint: impl Into<Option<String>>,
|
||||
) -> anyhow::Result<Self> {
|
||||
let network_fingerprint = network_fingerprint.into();
|
||||
ensure!(
|
||||
network_fingerprint
|
||||
.as_ref()
|
||||
.is_none_or(|value| value.len() == 64 && value.bytes().all(|byte| byte.is_ascii_hexdigit())),
|
||||
"Physical network fingerprint is invalid"
|
||||
);
|
||||
ensure!(
|
||||
endpoint.port() != 0 && !endpoint.ip().is_unspecified() && !endpoint.ip().is_multicast(),
|
||||
"Gateway endpoint is invalid"
|
||||
);
|
||||
ensure!(
|
||||
source.port() == 0 && !source.ip().is_unspecified() && !source.ip().is_multicast(),
|
||||
"Gateway source is invalid"
|
||||
);
|
||||
ensure!(
|
||||
endpoint.is_ipv4() == source.is_ipv4(),
|
||||
"Gateway address families differ"
|
||||
);
|
||||
ensure!(
|
||||
!interface.is_empty() && interface.len() < 64 && !interface.contains('\0') && interface_index != 0,
|
||||
"Gateway interface is invalid"
|
||||
);
|
||||
for address in [endpoint, source] {
|
||||
if let SocketAddr::V6(address) = address {
|
||||
ensure!(address.flowinfo() == 0, "Gateway IPv6 flow information is invalid");
|
||||
ensure!(
|
||||
address.scope_id() == 0 || address.scope_id() == interface_index,
|
||||
"Gateway IPv6 scope differs from its interface"
|
||||
);
|
||||
ensure!(
|
||||
!address.ip().is_unicast_link_local() || address.scope_id() == interface_index,
|
||||
"Gateway IPv6 scope is missing"
|
||||
);
|
||||
}
|
||||
}
|
||||
match source.ip() {
|
||||
IpAddr::V4(ip) => ensure!(
|
||||
addresses.ipv4 == Some(ip),
|
||||
"Gateway IPv4 source differs from its identity"
|
||||
),
|
||||
IpAddr::V6(ip) => ensure!(
|
||||
addresses.ipv6 == Some(ip),
|
||||
"Gateway IPv6 source differs from its identity"
|
||||
),
|
||||
}
|
||||
Ok(Self {
|
||||
endpoint,
|
||||
source,
|
||||
interface,
|
||||
interface_index,
|
||||
addresses,
|
||||
network_fingerprint,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn endpoint(&self) -> SocketAddr {
|
||||
self.endpoint
|
||||
}
|
||||
pub fn source(&self) -> SocketAddr {
|
||||
self.source
|
||||
}
|
||||
pub fn interface(&self) -> &str {
|
||||
&self.interface
|
||||
}
|
||||
pub fn interface_index(&self) -> u32 {
|
||||
self.interface_index
|
||||
}
|
||||
pub fn addresses(&self) -> ClientAddresses {
|
||||
self.addresses
|
||||
}
|
||||
|
||||
pub fn network_fingerprint(&self) -> Option<&str> {
|
||||
self.network_fingerprint.as_deref()
|
||||
}
|
||||
|
||||
pub fn validate_server(&self, server: &str) -> anyhow::Result<()> {
|
||||
let origin = super::super::transport::gateway_origin(server)?;
|
||||
ensure!(
|
||||
origin.port_or_known_default() == Some(self.endpoint.port()),
|
||||
"Gateway endpoint port differs"
|
||||
);
|
||||
let literal = match origin.host() {
|
||||
Some(url::Host::Ipv4(ip)) => Some(IpAddr::V4(ip)),
|
||||
Some(url::Host::Ipv6(ip)) => Some(IpAddr::V6(ip)),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(ip) = literal {
|
||||
ensure!(self.endpoint.ip() == ip, "Literal gateway endpoint differs");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn ipv4_binding() -> GatewayBinding {
|
||||
GatewayBinding::new(
|
||||
"192.0.2.10:443".parse().unwrap(),
|
||||
"192.0.2.5:0".parse().unwrap(),
|
||||
"fixture".into(),
|
||||
7,
|
||||
ClientAddresses {
|
||||
ipv4: Some("192.0.2.5".parse().unwrap()),
|
||||
ipv6: None,
|
||||
},
|
||||
"a".repeat(64),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wire_input_rejects_inconsistent_network_identity() {
|
||||
let original = serde_json::to_value(ipv4_binding()).unwrap();
|
||||
for (field, value) in [
|
||||
("source", serde_json::json!("192.0.2.6:0")),
|
||||
("source", serde_json::json!("192.0.2.5:1234")),
|
||||
("endpoint", serde_json::json!("[2001:db8::10]:443")),
|
||||
("interfaceIndex", serde_json::json!(0)),
|
||||
("networkFingerprint", serde_json::json!("not-a-fingerprint")),
|
||||
] {
|
||||
let mut invalid = original.clone();
|
||||
invalid[field] = value;
|
||||
assert!(
|
||||
serde_json::from_value::<GatewayBinding>(invalid).is_err(),
|
||||
"accepted {field}"
|
||||
);
|
||||
}
|
||||
assert!(serde_json::from_value::<GatewayBinding>(original).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn link_local_scope_survives_wire_round_trip_and_must_match_interface() {
|
||||
let addresses = ClientAddresses {
|
||||
ipv4: None,
|
||||
ipv6: Some("fe80::5".parse().unwrap()),
|
||||
};
|
||||
let binding = GatewayBinding::new(
|
||||
"[fe80::10%7]:443".parse().unwrap(),
|
||||
"[fe80::5%7]:0".parse().unwrap(),
|
||||
"fixture".into(),
|
||||
7,
|
||||
addresses,
|
||||
"a".repeat(64),
|
||||
)
|
||||
.unwrap();
|
||||
let value = serde_json::to_value(&binding).unwrap();
|
||||
let restored: GatewayBinding = serde_json::from_value(value.clone()).unwrap();
|
||||
assert_eq!(restored.source(), binding.source());
|
||||
assert_eq!(restored.endpoint(), binding.endpoint());
|
||||
for source in ["[fe80::5]:0", "[fe80::5%8]:0"] {
|
||||
let mut invalid = value.clone();
|
||||
invalid["source"] = serde_json::json!(source);
|
||||
assert!(serde_json::from_value::<GatewayBinding>(invalid).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_origin_must_match_endpoint_port_and_literal_address() {
|
||||
let binding = ipv4_binding();
|
||||
assert!(binding.validate_server("gateway.example.com").is_ok());
|
||||
assert!(binding.validate_server("192.0.2.10").is_ok());
|
||||
assert!(binding.validate_server("192.0.2.11").is_err());
|
||||
assert!(binding.validate_server("gateway.example.com:444").is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
pub(super) use super::bsd_routes::routes;
|
||||
use super::*;
|
||||
|
||||
pub(super) fn scope_resolver_socket(
|
||||
_socket: &tokio::net::UdpSocket,
|
||||
_interface: &Interface,
|
||||
_endpoint: SocketAddr,
|
||||
) -> anyhow::Result<()> {
|
||||
// resolv.conf has no per-interface resolver contexts. IPv6 zones are carried
|
||||
// in the destination address instead.
|
||||
anyhow::bail!("Interface-scoped resolver contexts are unsupported on BSD")
|
||||
}
|
||||
|
||||
pub(super) fn resolvers(control: &dyn CollectionControl) -> anyhow::Result<Vec<ResolverContext>> {
|
||||
resolv_conf(control)
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
use super::*;
|
||||
|
||||
#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "macos"))]
|
||||
pub(super) fn routes(control: &dyn CollectionControl) -> anyhow::Result<Vec<RouteContext>> {
|
||||
let mut routes = Vec::new();
|
||||
for family in ["inet", "inet6"] {
|
||||
routes.extend(parse_routes(&command("netstat", &["-rn", "-f", family], control)?)?);
|
||||
}
|
||||
Ok(routes)
|
||||
}
|
||||
|
||||
fn parse_routes(contents: &str) -> anyhow::Result<Vec<RouteContext>> {
|
||||
let mut interface_column = None;
|
||||
let mut routes = Vec::new();
|
||||
for line in contents.lines() {
|
||||
let fields = line.split_whitespace().collect::<Vec<_>>();
|
||||
if fields.first() == Some(&"Destination") {
|
||||
interface_column = Some(
|
||||
fields
|
||||
.iter()
|
||||
.position(|field| matches!(*field, "Netif" | "Iface"))
|
||||
.context("Route table has no interface column")?,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
let Some(column) = interface_column else {
|
||||
continue;
|
||||
};
|
||||
if fields.len() <= column {
|
||||
continue;
|
||||
}
|
||||
// L/W entries are neighbor-cache or cloned host routes. Their presence,
|
||||
// gateway MAC and expiry depend on traffic and must not cause rediscovery.
|
||||
if fields[2].contains('L') || fields[2].contains('W') {
|
||||
continue;
|
||||
}
|
||||
routes.push(RouteContext {
|
||||
destination: fields[0].into(),
|
||||
gateway: fields[1].into(),
|
||||
interface: fields[column].into(),
|
||||
table: String::new(),
|
||||
metric: String::new(),
|
||||
});
|
||||
}
|
||||
ensure!(interface_column.is_some(), "Route table header is missing");
|
||||
Ok(routes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn ignores_route_use_expiry_and_platform_column_positions() {
|
||||
let darwin = parse_routes("Routing tables\nInternet:\nDestination Gateway Flags Netif Expire\ndefault 192.0.2.1 UGScg en0\n192.0.2.5 aa:bb:cc:dd:ee:ff UHLWI en0 123").unwrap();
|
||||
let bsd = parse_routes("Destination Gateway Flags Refs Use Mtu Netif Expire\ndefault 192.0.2.1 UGS 4 22 1500 en0\n192.0.2.5 aa:bb:cc:dd:ee:ff UHL 0 33 1500 en0 99").unwrap();
|
||||
assert_eq!(darwin, bsd);
|
||||
assert_eq!(darwin.len(), 1);
|
||||
assert!(parse_routes("unrecognized table").is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
use std::net::IpAddr;
|
||||
|
||||
use anyhow::ensure;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
|
||||
#[serde(try_from = "DetectionFields")]
|
||||
pub struct InternalHostDetection {
|
||||
targets: Vec<DetectionTarget>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
|
||||
struct DetectionTarget {
|
||||
address: IpAddr,
|
||||
hostname: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct DetectionFields {
|
||||
targets: Vec<DetectionTarget>,
|
||||
}
|
||||
|
||||
impl TryFrom<DetectionFields> for InternalHostDetection {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(fields: DetectionFields) -> anyhow::Result<Self> {
|
||||
Self::new(
|
||||
fields
|
||||
.targets
|
||||
.into_iter()
|
||||
.map(|target| (target.address, target.hostname))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl InternalHostDetection {
|
||||
pub fn new(targets: Vec<(IpAddr, String)>) -> anyhow::Result<Self> {
|
||||
ensure!(
|
||||
!targets.is_empty() && targets.len() <= 2,
|
||||
"Invalid internal detection target count"
|
||||
);
|
||||
let targets = targets
|
||||
.into_iter()
|
||||
.map(|(address, hostname)| {
|
||||
validate_target(address, &hostname)?;
|
||||
Ok(DetectionTarget { address, hostname })
|
||||
})
|
||||
.collect::<anyhow::Result<Vec<_>>>()?;
|
||||
Ok(Self { targets })
|
||||
}
|
||||
|
||||
pub async fn detect(&self, cancellation: &CancellationToken) -> anyhow::Result<bool> {
|
||||
for target in &self.targets {
|
||||
if super::native::detect(target.address, &target.hostname, cancellation).await? {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn validate_target(address: IpAddr, hostname: &str) -> anyhow::Result<()> {
|
||||
ensure!(
|
||||
!address.is_unspecified() && !address.is_loopback() && !address.is_multicast(),
|
||||
"Invalid internal detection address"
|
||||
);
|
||||
ensure!(
|
||||
!hostname.is_empty()
|
||||
&& hostname.len() <= 253
|
||||
&& hostname.is_ascii()
|
||||
&& hostname
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b'_')),
|
||||
"Invalid internal detection hostname"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn detection_inputs_are_retained_and_validated_on_deserialization() {
|
||||
let detection = InternalHostDetection::new(vec![("192.0.2.10".parse().unwrap(), "host.example".into())]).unwrap();
|
||||
let value = serde_json::to_value(&detection).unwrap();
|
||||
assert!(serde_json::from_value::<InternalHostDetection>(value).is_ok());
|
||||
assert!(InternalHostDetection::new(vec![]).is_err());
|
||||
assert!(InternalHostDetection::new(vec![("127.0.0.1".parse().unwrap(), "host.example".into())]).is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
use super::*;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(super) fn scope_resolver_socket(
|
||||
socket: &tokio::net::UdpSocket,
|
||||
interface: &Interface,
|
||||
_endpoint: SocketAddr,
|
||||
) -> anyhow::Result<()> {
|
||||
socket2::SockRef::from(socket).bind_device(Some(interface.name.as_bytes()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(super) fn routes(control: &dyn CollectionControl) -> anyhow::Result<Vec<RouteContext>> {
|
||||
let mut routes = Vec::new();
|
||||
for family in ["-4", "-6"] {
|
||||
routes.extend(parse_routes(&command(
|
||||
"ip",
|
||||
&[family, "-j", "route", "show", "table", "all"],
|
||||
control,
|
||||
)?)?);
|
||||
}
|
||||
Ok(routes)
|
||||
}
|
||||
|
||||
fn parse_routes(contents: &str) -> anyhow::Result<Vec<RouteContext>> {
|
||||
let value: Vec<serde_json::Value> = serde_json::from_str(contents)?;
|
||||
let mut routes = Vec::new();
|
||||
for route in value {
|
||||
// Kernel cache and neighbor lifetimes are traffic, not network changes.
|
||||
if route.get("cache").is_some() {
|
||||
continue;
|
||||
}
|
||||
let Some(interface) = route["dev"].as_str() else {
|
||||
continue;
|
||||
};
|
||||
let field = |name| {
|
||||
route
|
||||
.get(name)
|
||||
.map(|value| value.as_str().map(str::to_owned).unwrap_or_else(|| value.to_string()))
|
||||
.unwrap_or_default()
|
||||
};
|
||||
routes.push(RouteContext {
|
||||
destination: field("dst"),
|
||||
gateway: field("gateway"),
|
||||
interface: interface.into(),
|
||||
table: field("table"),
|
||||
metric: field("metric"),
|
||||
});
|
||||
}
|
||||
Ok(routes)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(super) fn resolvers(control: &dyn CollectionControl) -> anyhow::Result<Vec<ResolverContext>> {
|
||||
let path = std::fs::canonicalize("/etc/resolv.conf")?;
|
||||
if path.starts_with("/run/systemd/resolve") {
|
||||
let dns = command("resolvectl", &["dns"], control)?;
|
||||
let domains = command("resolvectl", &["domain"], control)?;
|
||||
return parse_resolved(&dns, &domains);
|
||||
}
|
||||
resolv_conf(control)
|
||||
}
|
||||
|
||||
fn parse_resolved(dns: &str, domains: &str) -> anyhow::Result<Vec<ResolverContext>> {
|
||||
let mut resolvers = std::collections::BTreeMap::<Option<String>, ResolverContext>::new();
|
||||
for (contents, is_dns) in [(dns, true), (domains, false)] {
|
||||
for line in contents.lines().filter(|line| !line.trim().is_empty()) {
|
||||
let (label, values) = line.split_once(':').context("Invalid systemd resolver output")?;
|
||||
let interface = if label == "Global" {
|
||||
None
|
||||
} else {
|
||||
let (_, name) = label.split_once('(').context("Invalid systemd resolver interface")?;
|
||||
Some(
|
||||
name
|
||||
.strip_suffix(')')
|
||||
.context("Invalid systemd resolver interface")?
|
||||
.to_owned(),
|
||||
)
|
||||
};
|
||||
let resolver = resolvers.entry(interface.clone()).or_insert_with(|| ResolverContext {
|
||||
interface,
|
||||
servers: Vec::new(),
|
||||
domains: Vec::new(),
|
||||
});
|
||||
if is_dns {
|
||||
for server in values.split_whitespace() {
|
||||
server
|
||||
.split('%')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.parse::<IpAddr>()
|
||||
.context("Invalid systemd resolver address")?;
|
||||
resolver.servers.push(server.to_owned());
|
||||
}
|
||||
} else {
|
||||
resolver
|
||||
.domains
|
||||
.extend(values.split_whitespace().map(|domain| domain.to_ascii_lowercase()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(resolvers.into_values().collect())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn route_identity_retains_ipv6_and_ignores_cache() {
|
||||
let routes = parse_routes(r#"[{"dst":"default","gateway":"2001:db8::1","dev":"eth0","metric":600,"table":"main"},{"dst":"2001:db8::5","dev":"eth0","cache":[]}]"#).unwrap();
|
||||
assert_eq!(routes.len(), 1);
|
||||
assert_eq!(routes[0].gateway, "2001:db8::1");
|
||||
assert_eq!(routes[0].metric, "600");
|
||||
}
|
||||
#[test]
|
||||
fn resolved_preserves_interface_domains_and_empty_global_context() {
|
||||
let resolvers = parse_resolved(
|
||||
"Global:\nLink 2 (eth0): 192.0.2.53\n\nLink 3 (tun0): 198.51.100.53\n",
|
||||
"Global:\nLink 2 (eth0): CORP.EXAMPLE\nLink 3 (tun0): ~.\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(resolvers.len(), 3);
|
||||
assert!(
|
||||
resolvers
|
||||
.iter()
|
||||
.any(|resolver| resolver.interface.as_deref() == Some("eth0")
|
||||
&& resolver.domains.iter().any(|domain| domain == "corp.example"))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
pub(super) use super::bsd_routes::routes;
|
||||
use super::*;
|
||||
|
||||
pub(super) fn scope_resolver_socket(
|
||||
socket: &tokio::net::UdpSocket,
|
||||
interface: &Interface,
|
||||
endpoint: SocketAddr,
|
||||
) -> anyhow::Result<()> {
|
||||
let index = std::num::NonZeroU32::new(interface.index).context("Resolver interface index is missing")?;
|
||||
let socket = socket2::SockRef::from(socket);
|
||||
if endpoint.is_ipv4() {
|
||||
socket.bind_device_by_index_v4(Some(index))?;
|
||||
} else {
|
||||
socket.bind_device_by_index_v6(Some(index))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn resolvers(control: &dyn CollectionControl) -> anyhow::Result<Vec<ResolverContext>> {
|
||||
parse_resolvers(&command("scutil", &["--dns"], control)?)
|
||||
}
|
||||
|
||||
fn parse_resolvers(contents: &str) -> anyhow::Result<Vec<ResolverContext>> {
|
||||
let mut resolvers = Vec::new();
|
||||
let mut current: Option<ResolverContext> = None;
|
||||
for line in contents.lines().map(str::trim) {
|
||||
if line.starts_with("resolver #") {
|
||||
if let Some(resolver) = current.take() {
|
||||
resolvers.push(resolver);
|
||||
}
|
||||
current = Some(ResolverContext {
|
||||
interface: None,
|
||||
servers: Vec::new(),
|
||||
domains: Vec::new(),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
let Some(resolver) = &mut current else {
|
||||
continue;
|
||||
};
|
||||
let Some((field, value)) = line.split_once(':') else {
|
||||
continue;
|
||||
};
|
||||
let field = field.trim();
|
||||
let value = value.trim();
|
||||
if field.starts_with("nameserver[") {
|
||||
value
|
||||
.split('%')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.parse::<IpAddr>()
|
||||
.context("Invalid macOS resolver address")?;
|
||||
resolver.servers.push(value.to_owned());
|
||||
} else if field == "domain" || field.starts_with("search domain[") {
|
||||
resolver.domains.push(value.to_ascii_lowercase());
|
||||
} else if field == "if_index" {
|
||||
if let Some((_, name)) = value.split_once('(') {
|
||||
resolver.interface = Some(
|
||||
name
|
||||
.strip_suffix(')')
|
||||
.context("Invalid macOS resolver interface")?
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(resolver) = current {
|
||||
resolvers.push(resolver);
|
||||
}
|
||||
Ok(resolvers)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn resolver_identity_is_scoped_and_ignores_reachability_counters() {
|
||||
let a = parse_resolvers(
|
||||
"resolver #1\n nameserver[0] : 192.0.2.53\n if_index : 4 (en0)\n search domain[0] : CORP.EXAMPLE\n reach : 2",
|
||||
)
|
||||
.unwrap();
|
||||
let b = parse_resolvers(
|
||||
"resolver #9\n search domain[0] : corp.example\n if_index : 4 (en0)\n nameserver[0] : 192.0.2.53\n reach : 3",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(a, b);
|
||||
assert_eq!(a[0].interface.as_deref(), Some("en0"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,680 @@
|
||||
use std::{
|
||||
collections::BTreeSet,
|
||||
io,
|
||||
net::{IpAddr, SocketAddr},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use anyhow::{Context, ensure};
|
||||
use netdev::Interface;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::ClientAddresses;
|
||||
use crate::process::collection::{CollectionBudget, CollectionControl, CollectorCommands};
|
||||
|
||||
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
|
||||
mod bsd;
|
||||
#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "macos", test))]
|
||||
mod bsd_routes;
|
||||
#[cfg(any(target_os = "linux", test))]
|
||||
mod linux;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod macos;
|
||||
|
||||
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
|
||||
use bsd as platform;
|
||||
#[cfg(target_os = "linux")]
|
||||
use linux as platform;
|
||||
#[cfg(target_os = "macos")]
|
||||
use macos as platform;
|
||||
|
||||
mod binding;
|
||||
mod detection;
|
||||
pub mod native;
|
||||
pub use binding::GatewayBinding;
|
||||
pub use detection::InternalHostDetection;
|
||||
|
||||
const INSPECTION_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
const POLL_INTERVAL: Duration = Duration::from_secs(5);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]
|
||||
struct InterfaceIdentity {
|
||||
index: u32,
|
||||
name: String,
|
||||
mac: Option<String>,
|
||||
flags: u32,
|
||||
addresses: BTreeSet<(String, u32)>,
|
||||
}
|
||||
|
||||
impl From<&Interface> for InterfaceIdentity {
|
||||
fn from(interface: &Interface) -> Self {
|
||||
let mut addresses = interface
|
||||
.ipv4
|
||||
.iter()
|
||||
.map(|ip| (ip.to_string(), 0))
|
||||
.collect::<BTreeSet<_>>();
|
||||
addresses.extend(interface.ipv6.iter().enumerate().map(|(index, ip)| {
|
||||
(
|
||||
ip.to_string(),
|
||||
interface.ipv6_scope_ids.get(index).copied().unwrap_or_default(),
|
||||
)
|
||||
}));
|
||||
Self {
|
||||
index: interface.index,
|
||||
name: interface.name.clone(),
|
||||
mac: interface.mac_addr.map(|mac| mac.to_string()),
|
||||
flags: interface.flags,
|
||||
addresses,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]
|
||||
struct RouteContext {
|
||||
destination: String,
|
||||
gateway: String,
|
||||
interface: String,
|
||||
table: String,
|
||||
metric: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]
|
||||
struct ResolverContext {
|
||||
interface: Option<String>,
|
||||
servers: Vec<String>,
|
||||
domains: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
|
||||
struct NetworkSnapshot {
|
||||
interfaces: BTreeSet<InterfaceIdentity>,
|
||||
routes: BTreeSet<RouteContext>,
|
||||
resolvers: Vec<ResolverContext>,
|
||||
}
|
||||
|
||||
/// Captured before this attempt installs a tunnel. Interface counters and
|
||||
/// timestamps do not participate in identity; routes and resolver context do.
|
||||
#[derive(Clone)]
|
||||
pub struct PhysicalNetwork {
|
||||
snapshot: NetworkSnapshot,
|
||||
interfaces: Vec<Interface>,
|
||||
}
|
||||
|
||||
impl PhysicalNetwork {
|
||||
pub async fn bind_authenticated_endpoint(
|
||||
&self,
|
||||
server: &str,
|
||||
endpoint: SocketAddr,
|
||||
disable_ipv6: bool,
|
||||
cancellation: &CancellationToken,
|
||||
) -> anyhow::Result<GatewayBinding> {
|
||||
self.ensure_current(cancellation).await?;
|
||||
let binding = resolve_binding(
|
||||
&self.interfaces,
|
||||
Some(self.fingerprint()?),
|
||||
server,
|
||||
disable_ipv6,
|
||||
cancellation,
|
||||
endpoint,
|
||||
)
|
||||
.await?;
|
||||
self.ensure_current(cancellation).await?;
|
||||
Ok(binding)
|
||||
}
|
||||
pub async fn validate_internal_binding(
|
||||
&self,
|
||||
binding: &GatewayBinding,
|
||||
detection: Option<&InternalHostDetection>,
|
||||
cancellation: &CancellationToken,
|
||||
) -> anyhow::Result<()> {
|
||||
self.validate_binding(binding, cancellation).await?;
|
||||
physical_interface_for_source(&self.interfaces, binding.source(), binding.endpoint())?;
|
||||
self.validate_internal(detection, cancellation).await
|
||||
}
|
||||
|
||||
pub async fn validate_internal(
|
||||
&self,
|
||||
detection: Option<&InternalHostDetection>,
|
||||
cancellation: &CancellationToken,
|
||||
) -> anyhow::Result<()> {
|
||||
self.ensure_current(cancellation).await?;
|
||||
let mut resolver_count = 0;
|
||||
for resolver in &self.snapshot.resolvers {
|
||||
if resolver.servers.is_empty() {
|
||||
continue;
|
||||
}
|
||||
for server in &resolver.servers {
|
||||
let endpoint = resolver_endpoint(server, resolver.interface.as_deref(), &self.interfaces)?;
|
||||
ensure!(
|
||||
!endpoint.ip().is_loopback() && !endpoint.ip().is_unspecified(),
|
||||
"Unattributed local DNS stub is unsupported"
|
||||
);
|
||||
let socket = tokio::net::UdpSocket::bind(if endpoint.is_ipv4() { "0.0.0.0:0" } else { "[::]:0" }).await?;
|
||||
if let Some(name) = &resolver.interface {
|
||||
let interface = self
|
||||
.interfaces
|
||||
.iter()
|
||||
.find(|interface| interface.name == *name)
|
||||
.context("Configured resolver interface is missing")?;
|
||||
platform::scope_resolver_socket(&socket, interface, endpoint)?;
|
||||
}
|
||||
socket.connect(endpoint).await?;
|
||||
let interface = physical_interface_for_source(&self.interfaces, socket.local_addr()?, endpoint)?;
|
||||
ensure!(
|
||||
resolver.interface.as_deref().is_none_or(|name| name == interface.name),
|
||||
"Resolver route differs from its configured interface"
|
||||
);
|
||||
resolver_count += 1;
|
||||
}
|
||||
}
|
||||
ensure!(resolver_count > 0, "Physical resolver context is unavailable");
|
||||
if let Some(detection) = detection {
|
||||
ensure!(
|
||||
detection.detect(cancellation).await?,
|
||||
"Internal host detection no longer matches"
|
||||
);
|
||||
}
|
||||
self.ensure_current(cancellation).await
|
||||
}
|
||||
pub async fn capture_controlled(cancellation: &CancellationToken) -> anyhow::Result<Self> {
|
||||
let cancellation = cancellation.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let budget = CollectionBudget::new(INSPECTION_TIMEOUT);
|
||||
let check = || {
|
||||
if cancellation.is_cancelled() {
|
||||
return Err(io::Error::new(io::ErrorKind::Interrupted, "Network capture cancelled"));
|
||||
}
|
||||
budget.check()
|
||||
};
|
||||
Self::capture(&check)
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
pub async fn validate_binding(
|
||||
&self,
|
||||
binding: &GatewayBinding,
|
||||
cancellation: &CancellationToken,
|
||||
) -> anyhow::Result<()> {
|
||||
self.ensure_current(cancellation).await?;
|
||||
ensure!(
|
||||
binding.network_fingerprint() == Some(self.fingerprint()?.as_str()),
|
||||
"Physical network changed since authentication"
|
||||
);
|
||||
let interface = self
|
||||
.interfaces
|
||||
.iter()
|
||||
.find(|interface| interface.index == binding.interface_index() && interface.name == binding.interface())
|
||||
.context("Authenticated gateway interface is no longer present")?;
|
||||
let addresses = binding.addresses();
|
||||
ensure!(
|
||||
addresses
|
||||
.ipv4
|
||||
.is_none_or(|ip| interface.ipv4.iter().any(|address| address.addr() == ip)),
|
||||
"Authenticated IPv4 address changed"
|
||||
);
|
||||
ensure!(
|
||||
addresses
|
||||
.ipv6
|
||||
.is_none_or(|ip| interface.ipv6.iter().any(|address| address.addr() == ip)),
|
||||
"Authenticated IPv6 address changed"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fingerprint(&self) -> anyhow::Result<String> {
|
||||
Ok(sha256::digest(serde_json::to_vec(&self.snapshot)?))
|
||||
}
|
||||
|
||||
pub fn capture(control: &dyn CollectionControl) -> anyhow::Result<Self> {
|
||||
control.check()?;
|
||||
let mut interfaces = netdev::get_interfaces();
|
||||
for interface in &mut interfaces {
|
||||
interface.ipv4.sort();
|
||||
// The address and its zone must move together when native enumeration
|
||||
// order changes between captures.
|
||||
let mut ipv6 = interface
|
||||
.ipv6
|
||||
.iter()
|
||||
.copied()
|
||||
.enumerate()
|
||||
.map(|(index, address)| {
|
||||
(
|
||||
address,
|
||||
interface.ipv6_scope_ids.get(index).copied().unwrap_or_default(),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
ipv6.sort();
|
||||
interface.ipv6 = ipv6.iter().map(|(address, _)| *address).collect();
|
||||
interface.ipv6_scope_ids = ipv6.into_iter().map(|(_, scope)| scope).collect();
|
||||
}
|
||||
let snapshot = snapshot(&interfaces, control)?;
|
||||
control.check()?;
|
||||
Ok(Self { snapshot, interfaces })
|
||||
}
|
||||
|
||||
/// Validate endpoint selection against the captured network. The inspection
|
||||
/// is joined even when cancellation occurs, so native collectors cannot outlive
|
||||
/// the connection attempt.
|
||||
pub async fn ensure_current(&self, cancellation: &CancellationToken) -> anyhow::Result<()> {
|
||||
let current = inspect(cancellation.clone()).await?;
|
||||
ensure!(
|
||||
current == self.snapshot,
|
||||
"Physical network changed during gateway setup"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The owner must cancel and await this monitor, including its blocking
|
||||
/// inspection. Dropping a selected future would detach the native collector.
|
||||
pub async fn wait_for_change(
|
||||
&self,
|
||||
detection: Option<InternalHostDetection>,
|
||||
cancellation: CancellationToken,
|
||||
) -> anyhow::Result<bool> {
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = cancellation.cancelled() => return Ok(false),
|
||||
_ = tokio::time::sleep(POLL_INTERVAL) => {},
|
||||
}
|
||||
let current = self.validate_internal(detection.as_ref(), &cancellation).await;
|
||||
if cancellation.is_cancelled() {
|
||||
return Ok(false);
|
||||
}
|
||||
if current.is_err() {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn resolve_binding(
|
||||
interfaces: &[Interface],
|
||||
fingerprint: Option<String>,
|
||||
server: &str,
|
||||
disable_ipv6: bool,
|
||||
cancellation: &CancellationToken,
|
||||
authenticated_endpoint: SocketAddr,
|
||||
) -> anyhow::Result<GatewayBinding> {
|
||||
let origin = super::transport::gateway_origin(server)?;
|
||||
let host = origin.host_str().context("Gateway host is missing")?;
|
||||
let port = origin.port_or_known_default().context("Gateway port is missing")?;
|
||||
let literal = literal_gateway_address(&origin)?;
|
||||
let endpoints = if let Some(ip) = literal {
|
||||
vec![SocketAddr::new(ip, port)]
|
||||
} else {
|
||||
native::resolve(host, port, disable_ipv6, cancellation).await?
|
||||
};
|
||||
ensure!(
|
||||
endpoints.contains(&authenticated_endpoint),
|
||||
"Authenticated peer differs from the gateway's current DNS addresses; check for a proxy or changed DNS answer"
|
||||
);
|
||||
let endpoint = authenticated_endpoint;
|
||||
ensure!(
|
||||
!(disable_ipv6 && endpoint.is_ipv6()),
|
||||
"Authenticated gateway uses disabled IPv6"
|
||||
);
|
||||
let bind = if endpoint.is_ipv4() { "0.0.0.0:0" } else { "[::]:0" };
|
||||
let socket = tokio::net::UdpSocket::bind(bind).await?;
|
||||
socket.connect(endpoint).await?;
|
||||
let mut source_address = socket.local_addr()?;
|
||||
source_address.set_port(0);
|
||||
let source = source_address.ip();
|
||||
let interface = physical_interface_for_source(interfaces, source_address, endpoint)?;
|
||||
let ipv4 = match source {
|
||||
IpAddr::V4(ip) => Some(ip),
|
||||
_ => interface
|
||||
.ipv4
|
||||
.iter()
|
||||
.map(|ip| ip.addr())
|
||||
.find(|ip| !ip.is_loopback() && !ip.is_unspecified()),
|
||||
};
|
||||
let ipv6 = if disable_ipv6 {
|
||||
None
|
||||
} else {
|
||||
match source {
|
||||
IpAddr::V6(ip) => Some(ip),
|
||||
_ => interface
|
||||
.ipv6
|
||||
.iter()
|
||||
.map(|ip| ip.addr())
|
||||
.find(|ip| !ip.is_loopback() && !ip.is_unspecified() && !ip.is_unicast_link_local()),
|
||||
}
|
||||
};
|
||||
if let std::net::SocketAddr::V6(address) = &mut source_address {
|
||||
if address.ip().is_unicast_link_local() && address.scope_id() == 0 {
|
||||
address.set_scope_id(interface.index);
|
||||
}
|
||||
}
|
||||
GatewayBinding::new(
|
||||
endpoint,
|
||||
source_address,
|
||||
interface.name.clone(),
|
||||
interface.index,
|
||||
ClientAddresses { ipv4, ipv6 },
|
||||
fingerprint.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
// Default-route inventories include scoped, backup, and unrelated address-family
|
||||
// routes. Attribute each actual destination by its selected source instead.
|
||||
fn physical_interface_for_source(
|
||||
interfaces: &[Interface],
|
||||
source: SocketAddr,
|
||||
endpoint: SocketAddr,
|
||||
) -> anyhow::Result<&Interface> {
|
||||
let mut scope = None;
|
||||
for address in [source, endpoint] {
|
||||
if let SocketAddr::V6(address) = address {
|
||||
if address.scope_id() != 0 {
|
||||
ensure!(
|
||||
scope.is_none_or(|scope| scope == address.scope_id()),
|
||||
"Network source and destination have different interface scopes"
|
||||
);
|
||||
scope = Some(address.scope_id());
|
||||
}
|
||||
}
|
||||
}
|
||||
let source = source.ip();
|
||||
ensure!(
|
||||
!source.is_unspecified() && !source.is_loopback(),
|
||||
"Network route has no physical source address"
|
||||
);
|
||||
let mut matches = interfaces.iter().filter(|interface| {
|
||||
scope.is_none_or(|scope| scope == interface.index)
|
||||
&& (interface.ipv4.iter().any(|ip| IpAddr::V4(ip.addr()) == source)
|
||||
|| interface.ipv6.iter().any(|ip| IpAddr::V6(ip.addr()) == source))
|
||||
});
|
||||
let interface = matches
|
||||
.next()
|
||||
.context("Network route has no matching physical interface")?;
|
||||
ensure!(
|
||||
matches.next().is_none(),
|
||||
"Network route has ambiguous physical interfaces"
|
||||
);
|
||||
ensure!(
|
||||
matches!(
|
||||
interface.if_type,
|
||||
netdev::prelude::InterfaceType::Ethernet
|
||||
| netdev::prelude::InterfaceType::Wireless80211
|
||||
| netdev::prelude::InterfaceType::FastEthernetT
|
||||
| netdev::prelude::InterfaceType::FastEthernetFx
|
||||
| netdev::prelude::InterfaceType::GigabitEthernet
|
||||
),
|
||||
"Network route uses an unsupported physical interface: {}",
|
||||
interface.name
|
||||
);
|
||||
Ok(interface)
|
||||
}
|
||||
|
||||
fn resolver_endpoint(server: &str, interface: Option<&str>, interfaces: &[Interface]) -> anyhow::Result<SocketAddr> {
|
||||
let (address, zone) = server
|
||||
.split_once('%')
|
||||
.map_or((server, None), |(ip, zone)| (ip, Some(zone)));
|
||||
let ip: IpAddr = address.parse().context("Invalid resolver address")?;
|
||||
let mut endpoint = SocketAddr::new(ip, 53);
|
||||
if let SocketAddr::V6(address) = &mut endpoint {
|
||||
let zone = if zone.is_none() && address.ip().is_unicast_link_local() {
|
||||
interface
|
||||
} else {
|
||||
zone
|
||||
};
|
||||
if let Some(zone) = zone {
|
||||
let index = interfaces
|
||||
.iter()
|
||||
.find(|candidate| candidate.name == zone || candidate.index.to_string() == zone)
|
||||
.context("Resolver IPv6 zone has no matching interface")?
|
||||
.index;
|
||||
address.set_scope_id(index);
|
||||
}
|
||||
ensure!(
|
||||
!address.ip().is_unicast_link_local() || address.scope_id() != 0,
|
||||
"Link-local resolver has no interface scope"
|
||||
);
|
||||
} else {
|
||||
ensure!(zone.is_none(), "IPv4 resolver cannot have an interface scope");
|
||||
}
|
||||
Ok(endpoint)
|
||||
}
|
||||
|
||||
async fn inspect(cancellation: CancellationToken) -> anyhow::Result<NetworkSnapshot> {
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let budget = CollectionBudget::new(INSPECTION_TIMEOUT);
|
||||
let check = || {
|
||||
if cancellation.is_cancelled() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Interrupted,
|
||||
"Network inspection cancelled",
|
||||
));
|
||||
}
|
||||
budget.check()
|
||||
};
|
||||
let result = snapshot(&netdev::get_interfaces(), &check)?;
|
||||
check()?;
|
||||
Ok(result)
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
fn literal_gateway_address(origin: &url::Url) -> anyhow::Result<Option<IpAddr>> {
|
||||
Ok(match origin.host().context("Gateway host is missing")? {
|
||||
url::Host::Ipv4(ip) => Some(IpAddr::V4(ip)),
|
||||
url::Host::Ipv6(ip) => Some(IpAddr::V6(ip)),
|
||||
url::Host::Domain(_) => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn snapshot(interfaces: &[Interface], control: &dyn CollectionControl) -> anyhow::Result<NetworkSnapshot> {
|
||||
control.check()?;
|
||||
Ok(NetworkSnapshot {
|
||||
interfaces: interfaces.iter().map(InterfaceIdentity::from).collect(),
|
||||
routes: platform::routes(control)?.into_iter().collect(),
|
||||
resolvers: platform::resolvers(control)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn command(name: &str, args: &[&str], control: &dyn CollectionControl) -> anyhow::Result<String> {
|
||||
let output = CollectorCommands::new(control)
|
||||
.run(name, args)?
|
||||
.with_context(|| format!("Required network tool {name} is unavailable"))?;
|
||||
ensure!(output.status.success(), "Network tool {name} failed");
|
||||
String::from_utf8(output.stdout).context("Network tool output is not UTF-8")
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
fn resolv_conf(control: &dyn CollectionControl) -> anyhow::Result<Vec<ResolverContext>> {
|
||||
control.check()?;
|
||||
let file = std::fs::File::open("/etc/resolv.conf")?;
|
||||
use std::io::Read;
|
||||
let mut contents = String::new();
|
||||
file.take(64 * 1024 + 1).read_to_string(&mut contents)?;
|
||||
ensure!(contents.len() <= 64 * 1024, "Resolver configuration is too large");
|
||||
control.check()?;
|
||||
Ok(vec![parse_resolv_conf(&contents)?])
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))]
|
||||
fn parse_resolv_conf(contents: &str) -> anyhow::Result<ResolverContext> {
|
||||
let mut resolver = ResolverContext {
|
||||
interface: None,
|
||||
servers: Vec::new(),
|
||||
domains: Vec::new(),
|
||||
};
|
||||
for line in contents.lines() {
|
||||
let line = line.split(['#', ';']).next().unwrap_or_default();
|
||||
let mut fields = line.split_whitespace();
|
||||
match fields.next() {
|
||||
Some("nameserver") => {
|
||||
if let Some(server) = fields.next() {
|
||||
// IPv6 zone identifiers are retained for scoped physical resolvers.
|
||||
server
|
||||
.split('%')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.parse::<IpAddr>()
|
||||
.context("Invalid resolver address")?;
|
||||
resolver.servers.push(server.to_owned());
|
||||
}
|
||||
}
|
||||
Some("search" | "domain") => resolver
|
||||
.domains
|
||||
.extend(fields.map(|domain| domain.to_ascii_lowercase())),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(resolver)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn ethernet(name: &str, index: u32, address: &str) -> Interface {
|
||||
let mut interface = Interface::dummy();
|
||||
interface.name = name.into();
|
||||
interface.index = index;
|
||||
interface.if_type = netdev::prelude::InterfaceType::Ethernet;
|
||||
interface.ipv4 = vec![address.parse().unwrap()];
|
||||
interface.ipv6 = vec!["fe80::1/64".parse().unwrap()];
|
||||
interface
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attributes_each_destination_without_requiring_a_single_uplink() {
|
||||
let ethernet = ethernet("en0", 7, "192.0.2.10/24");
|
||||
let mut wifi = ethernet.clone();
|
||||
wifi.name = "en1".into();
|
||||
wifi.index = 8;
|
||||
wifi.if_type = netdev::prelude::InterfaceType::Wireless80211;
|
||||
wifi.ipv4 = vec!["198.51.100.10/24".parse().unwrap()];
|
||||
let mut tunnel = Interface::dummy();
|
||||
tunnel.name = "utun0".into();
|
||||
tunnel.index = 9;
|
||||
tunnel.ipv4 = vec!["10.0.0.1/24".parse().unwrap()];
|
||||
let interfaces = [ethernet, wifi, tunnel];
|
||||
for (source, endpoint, expected) in [
|
||||
("192.0.2.10:0", "203.0.113.1:443", "en0"),
|
||||
("198.51.100.10:0", "198.51.100.53:53", "en1"),
|
||||
("[fe80::1%8]:0", "[fe80::53%8]:53", "en1"),
|
||||
] {
|
||||
assert_eq!(
|
||||
physical_interface_for_source(&interfaces, source.parse().unwrap(), endpoint.parse().unwrap())
|
||||
.unwrap()
|
||||
.name,
|
||||
expected
|
||||
);
|
||||
}
|
||||
// Virtual interfaces may coexist, but must never supply the selected source.
|
||||
assert!(
|
||||
physical_interface_for_source(
|
||||
&interfaces,
|
||||
"10.0.0.1:0".parse().unwrap(),
|
||||
"10.0.0.53:53".parse().unwrap()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
// Identical link-local addresses without scope are ambiguous.
|
||||
assert!(
|
||||
physical_interface_for_source(
|
||||
&interfaces,
|
||||
"[fe80::1]:0".parse().unwrap(),
|
||||
"[fe80::53]:53".parse().unwrap()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
physical_interface_for_source(
|
||||
&interfaces,
|
||||
"[fe80::1%7]:0".parse().unwrap(),
|
||||
"[fe80::53%8]:53".parse().unwrap()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_ambiguous_or_unattributed_sources() {
|
||||
let interfaces = [ethernet("en0", 7, "192.0.2.10/24"), ethernet("en1", 8, "192.0.2.10/24")];
|
||||
for source in ["192.0.2.10:0", "192.0.2.11:0", "127.0.0.1:0", "0.0.0.0:0"] {
|
||||
assert!(
|
||||
physical_interface_for_source(&interfaces, source.parse().unwrap(), "192.0.2.53:53".parse().unwrap()).is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolver_zones_support_native_names_and_numeric_indices() {
|
||||
let interfaces = [ethernet("en0", 7, "192.0.2.10/24")];
|
||||
let expected: SocketAddr = "[fe80::53%7]:53".parse().unwrap();
|
||||
assert_eq!(resolver_endpoint("fe80::53%en0", None, &interfaces).unwrap(), expected);
|
||||
assert_eq!(resolver_endpoint("fe80::53%7", None, &interfaces).unwrap(), expected);
|
||||
assert_eq!(
|
||||
resolver_endpoint("fe80::53", Some("en0"), &interfaces).unwrap(),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
resolver_endpoint("192.0.2.53", Some("en0"), &interfaces).unwrap(),
|
||||
"192.0.2.53:53".parse().unwrap()
|
||||
);
|
||||
for server in ["fe80::53", "fe80::53%missing", "fe80::53%8", "192.0.2.53%en0"] {
|
||||
assert!(
|
||||
resolver_endpoint(server, None, &interfaces).is_err(),
|
||||
"accepted {server}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bracketed_ipv6_gateway_is_a_literal_endpoint() {
|
||||
let origin = super::super::transport::gateway_origin("https://[2001:db8::1]:444").unwrap();
|
||||
assert_eq!(
|
||||
literal_gateway_address(&origin).unwrap(),
|
||||
Some("2001:db8::1".parse().unwrap())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolver_order_comments_and_case_do_not_change_network_identity() {
|
||||
assert_eq!(
|
||||
parse_resolv_conf("nameserver 192.0.2.53\nsearch CORP.EXAMPLE\n# comment").unwrap(),
|
||||
parse_resolv_conf("search corp.example\nnameserver 192.0.2.53 # DHCP\n").unwrap()
|
||||
);
|
||||
assert_ne!(
|
||||
parse_resolv_conf("nameserver 192.0.2.53").unwrap(),
|
||||
parse_resolv_conf("nameserver 192.0.2.54").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interface_identity_ignores_counters_and_address_enumeration_order() {
|
||||
let mut interface = Interface::dummy();
|
||||
interface.index = 7;
|
||||
interface.name = "bridge0".into();
|
||||
interface.ipv4 = vec!["192.0.2.10/24".parse().unwrap(), "192.0.2.11/24".parse().unwrap()];
|
||||
let original = InterfaceIdentity::from(&interface);
|
||||
interface.ipv4.reverse();
|
||||
interface.mtu = Some(1200);
|
||||
interface.default = !interface.default;
|
||||
assert_eq!(original, InterfaceIdentity::from(&interface));
|
||||
interface.index = 8;
|
||||
assert_ne!(original, InterfaceIdentity::from(&interface));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn test_binding() -> GatewayBinding {
|
||||
GatewayBinding::new(
|
||||
"192.0.2.10:443".parse().unwrap(),
|
||||
"192.0.2.5:0".parse().unwrap(),
|
||||
"fixture".into(),
|
||||
1,
|
||||
ClientAddresses {
|
||||
ipv4: Some("192.0.2.5".parse().unwrap()),
|
||||
ipv6: None,
|
||||
},
|
||||
"0".repeat(64),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
use std::{net::SocketAddr, path::PathBuf};
|
||||
|
||||
use anyhow::{Context, ensure};
|
||||
use dns_lookup::{AddrFamily, AddrInfoHints, SockType, getaddrinfo};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::*;
|
||||
|
||||
const MAX_ADDRESSES: usize = 64;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn detect_in_helper(address: std::net::IpAddr, hostname: &str) -> anyhow::Result<bool> {
|
||||
super::detection::validate_target(address, hostname)?;
|
||||
Ok(dns_lookup::lookup_addr(&address).is_ok_and(|name| name.eq_ignore_ascii_case(hostname)))
|
||||
}
|
||||
|
||||
pub(super) async fn detect(
|
||||
address: std::net::IpAddr,
|
||||
hostname: &str,
|
||||
cancellation: &CancellationToken,
|
||||
) -> anyhow::Result<bool> {
|
||||
super::detection::validate_target(address, hostname)?;
|
||||
let args = vec![
|
||||
"detect-internal-host".into(),
|
||||
"--address".into(),
|
||||
address.to_string(),
|
||||
"--hostname".into(),
|
||||
hostname.into(),
|
||||
];
|
||||
let output = run_helper(common::binary_paths::gpclient(), args, cancellation.clone()).await?;
|
||||
serde_json::from_slice(&output).context("Invalid internal detection helper response")
|
||||
}
|
||||
|
||||
/// Entry point for gpclient's isolated native resolver helper. This blocking
|
||||
/// function must run in the child process, never a connection runtime thread.
|
||||
#[doc(hidden)]
|
||||
pub fn lookup_in_helper(host: &str, port: u16, disable_ipv6: bool) -> anyhow::Result<Vec<SocketAddr>> {
|
||||
validate_host(host, port)?;
|
||||
let hints = AddrInfoHints {
|
||||
address: if disable_ipv6 { AddrFamily::Inet.into() } else { 0 },
|
||||
socktype: SockType::Stream.into(),
|
||||
..AddrInfoHints::default()
|
||||
};
|
||||
let service = port.to_string();
|
||||
let mut addresses = Vec::new();
|
||||
for result in getaddrinfo(Some(host), Some(&service), Some(hints)).map_err(io::Error::from)? {
|
||||
let address = result?.sockaddr;
|
||||
if !addresses.contains(&address) {
|
||||
ensure!(
|
||||
addresses.len() < MAX_ADDRESSES,
|
||||
"Native resolver returned too many gateway addresses"
|
||||
);
|
||||
addresses.push(address);
|
||||
}
|
||||
}
|
||||
validate_addresses(&addresses, port, disable_ipv6)?;
|
||||
Ok(addresses)
|
||||
}
|
||||
|
||||
pub(super) async fn resolve(
|
||||
host: &str,
|
||||
port: u16,
|
||||
disable_ipv6: bool,
|
||||
cancellation: &CancellationToken,
|
||||
) -> anyhow::Result<Vec<SocketAddr>> {
|
||||
validate_host(host, port)?;
|
||||
let mut args = vec!["resolve-gateway".into(), host.into(), "--port".into(), port.to_string()];
|
||||
if disable_ipv6 {
|
||||
args.push("--disable-ipv6".into());
|
||||
}
|
||||
run_resolver(
|
||||
common::binary_paths::gpclient(),
|
||||
args,
|
||||
port,
|
||||
disable_ipv6,
|
||||
cancellation.clone(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn run_resolver(
|
||||
executable: PathBuf,
|
||||
args: Vec<String>,
|
||||
port: u16,
|
||||
disable_ipv6: bool,
|
||||
cancellation: CancellationToken,
|
||||
) -> anyhow::Result<Vec<SocketAddr>> {
|
||||
let output = run_helper(executable, args, cancellation).await?;
|
||||
let addresses: Vec<SocketAddr> = serde_json::from_slice(&output).context("Invalid resolver helper response")?;
|
||||
validate_addresses(&addresses, port, disable_ipv6)?;
|
||||
Ok(addresses)
|
||||
}
|
||||
|
||||
async fn run_helper(
|
||||
executable: PathBuf,
|
||||
args: Vec<String>,
|
||||
cancellation: CancellationToken,
|
||||
) -> anyhow::Result<Vec<u8>> {
|
||||
// Await the owned worker even on cancellation. CollectorCommands kills and
|
||||
// waits for the helper process and any descendants before returning.
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let budget = CollectionBudget::new(INSPECTION_TIMEOUT);
|
||||
let check = || {
|
||||
if cancellation.is_cancelled() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Interrupted,
|
||||
"Gateway resolution cancelled",
|
||||
));
|
||||
}
|
||||
budget.check()
|
||||
};
|
||||
let args = args.iter().map(String::as_str).collect::<Vec<_>>();
|
||||
let output = CollectorCommands::new(&check)
|
||||
.run_executable(&executable, &args)?
|
||||
.context("Native resolver helper is unavailable")?;
|
||||
check()?;
|
||||
ensure!(output.status.success(), "Native gateway resolution failed");
|
||||
Ok(output.stdout)
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
fn validate_host(host: &str, port: u16) -> anyhow::Result<()> {
|
||||
ensure!(port != 0, "Gateway port is zero");
|
||||
ensure!(
|
||||
!host.is_empty() && host.len() <= 253 && !host.contains('\0') && !host.contains(['/', ':']),
|
||||
"Invalid gateway hostname"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_addresses(addresses: &[SocketAddr], port: u16, disable_ipv6: bool) -> anyhow::Result<()> {
|
||||
ensure!(
|
||||
!addresses.is_empty() && addresses.len() <= MAX_ADDRESSES,
|
||||
"Invalid gateway address count"
|
||||
);
|
||||
ensure!(
|
||||
addresses.iter().all(|address| {
|
||||
address.port() == port && !address.ip().is_unspecified() && (!disable_ipv6 || address.is_ipv4())
|
||||
}),
|
||||
"Invalid gateway address from resolver helper"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn actual_helper_output_is_validated_before_admission() {
|
||||
let addresses = run_resolver(
|
||||
"/bin/sh".into(),
|
||||
vec!["-c".into(), "printf '[\"192.0.2.1:443\"]'".into()],
|
||||
443,
|
||||
true,
|
||||
CancellationToken::new(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(addresses, vec!["192.0.2.1:443".parse::<SocketAddr>().unwrap()]);
|
||||
assert!(
|
||||
run_resolver(
|
||||
"/bin/sh".into(),
|
||||
vec!["-c".into(), "printf '[\"192.0.2.1:444\"]'".into()],
|
||||
443,
|
||||
true,
|
||||
CancellationToken::new(),
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancellation_kills_and_joins_the_running_resolver_process() {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
let path = file.path().to_owned();
|
||||
let cancellation = CancellationToken::new();
|
||||
let worker = tokio::spawn(run_resolver(
|
||||
"/bin/sh".into(),
|
||||
vec![
|
||||
"-c".into(),
|
||||
"printf '%s' \"$$\" > \"$1\"; sleep 30 & wait".into(),
|
||||
"resolver-test".into(),
|
||||
path.to_string_lossy().into_owned(),
|
||||
],
|
||||
443,
|
||||
false,
|
||||
cancellation.clone(),
|
||||
));
|
||||
let pid = tokio::time::timeout(Duration::from_secs(2), async {
|
||||
loop {
|
||||
if let Ok(pid) = std::fs::read_to_string(&path).unwrap().parse::<i32>() {
|
||||
break pid;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
cancellation.cancel();
|
||||
let error = tokio::time::timeout(Duration::from_secs(1), worker)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.unwrap_err();
|
||||
assert_eq!(
|
||||
error.downcast::<io::Error>().unwrap().kind(),
|
||||
io::ErrorKind::Interrupted
|
||||
);
|
||||
assert_eq!(
|
||||
nix::sys::signal::kill(nix::unistd::Pid::from_raw(pid), None),
|
||||
Err(nix::errno::Errno::ESRCH)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_lookup_preserves_port_and_ipv4_policy() {
|
||||
let addresses = lookup_in_helper("localhost", 444, true).unwrap();
|
||||
assert!(
|
||||
addresses
|
||||
.iter()
|
||||
.all(|address| address.is_ipv4() && address.ip().is_loopback() && address.port() == 444)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn helper_response_validation_retains_ipv6_scope_and_rejects_wrong_port() {
|
||||
let addresses = vec!["[fe80::1%7]:443".parse().unwrap(), "192.0.2.1:443".parse().unwrap()];
|
||||
validate_addresses(&addresses, 443, false).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Vec<SocketAddr>>(&serde_json::to_string(&addresses).unwrap()).unwrap(),
|
||||
addresses
|
||||
);
|
||||
assert!(validate_addresses(&addresses, 444, false).is_err());
|
||||
assert!(validate_addresses(&addresses, 443, true).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_helper_requests_before_lookup() {
|
||||
assert!(lookup_in_helper("gateway.example/path", 443, false).is_err());
|
||||
assert!(lookup_in_helper("gateway.example", 0, false).is_err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user