From 911382df034de6b23d0f47373feaec6e7b9b2af5 Mon Sep 17 00:00:00 2001 From: Oussama Teffahi <70609372+oteffahi@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:29:37 +0100 Subject: [PATCH] Add interface binding to QUIC link (#1652) * Add interface binding for QUIC listener * Add interface binding for QUIC link * Add TLS and QUIC interface binding tests * Fix incorrect port number in test --- Cargo.lock | 1 + io/zenoh-links/zenoh-link-quic/Cargo.toml | 1 + io/zenoh-links/zenoh-link-quic/src/unicast.rs | 59 ++- io/zenoh-links/zenoh-link-quic/src/utils.rs | 20 +- io/zenoh-transport/tests/unicast_openclose.rs | 360 +++++++++++------- 5 files changed, 280 insertions(+), 161 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e49f2936e..3c2fa5212 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5331,6 +5331,7 @@ dependencies = [ "zenoh-link-commons", "zenoh-protocol", "zenoh-result", + "zenoh-util", ] [[package]] diff --git a/io/zenoh-links/zenoh-link-quic/Cargo.toml b/io/zenoh-links/zenoh-link-quic/Cargo.toml index 6132abeac..4cd8cee7f 100644 --- a/io/zenoh-links/zenoh-link-quic/Cargo.toml +++ b/io/zenoh-links/zenoh-link-quic/Cargo.toml @@ -51,6 +51,7 @@ zenoh-core = { workspace = true } zenoh-link-commons = { workspace = true, features = ["tls"] } zenoh-protocol = { workspace = true } zenoh-result = { workspace = true } +zenoh-util = { workspace = true } [package.metadata.cargo-machete] ignored = ["rustls-webpki"] \ No newline at end of file diff --git a/io/zenoh-links/zenoh-link-quic/src/unicast.rs b/io/zenoh-links/zenoh-link-quic/src/unicast.rs index 3618a7a62..4f69a3ae1 100644 --- a/io/zenoh-links/zenoh-link-quic/src/unicast.rs +++ b/io/zenoh-links/zenoh-link-quic/src/unicast.rs @@ -20,7 +20,10 @@ use std::{ }; use async_trait::async_trait; -use quinn::crypto::rustls::{QuicClientConfig, QuicServerConfig}; +use quinn::{ + crypto::rustls::{QuicClientConfig, QuicServerConfig}, + EndpointConfig, +}; use time::OffsetDateTime; use tokio::sync::Mutex as AsyncMutex; use tokio_util::sync::CancellationToken; @@ -270,8 +273,30 @@ impl LinkManagerUnicastTrait for LinkManagerUnicastQuic { } else { Ipv6Addr::UNSPECIFIED.into() }; - let mut quic_endpoint = quinn::Endpoint::client(SocketAddr::new(ip_addr, 0)) - .map_err(|e| zerror!("Can not create a new QUIC link bound to {}: {}", host, e))?; + + // Initialize the Endpoint + let mut quic_endpoint = if let Some(iface) = client_crypto.bind_iface { + async { + // Bind the UDP socket + let socket = tokio::net::UdpSocket::bind(SocketAddr::new(ip_addr, 0)).await?; + zenoh_util::net::set_bind_to_device_udp_socket(&socket, iface)?; + + // create the Endpoint with this socket + let runtime = quinn::default_runtime().ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::Other, "no async runtime found") + })?; + ZResult::Ok(quinn::Endpoint::new_with_abstract_socket( + EndpointConfig::default(), + None, + runtime.wrap_udp_socket(socket.into_std()?)?, + runtime, + )?) + } + .await + } else { + quinn::Endpoint::client(SocketAddr::new(ip_addr, 0)).map_err(Into::into) + } + .map_err(|e| zerror!("Can not create a new QUIC link bound to {host}: {e}"))?; let quic_config: QuicClientConfig = client_crypto .client_config @@ -324,7 +349,7 @@ impl LinkManagerUnicastTrait for LinkManagerUnicastQuic { Ok(LinkUnicast(link)) } - async fn new_listener(&self, mut endpoint: EndPoint) -> ZResult { + async fn new_listener(&self, endpoint: EndPoint) -> ZResult { let epaddr = endpoint.address(); let epconf = endpoint.config(); @@ -367,15 +392,35 @@ impl LinkManagerUnicastTrait for LinkManagerUnicastQuic { .max_concurrent_bidi_streams(1_u8.into()); // Initialize the Endpoint - let quic_endpoint = quinn::Endpoint::server(server_config, addr) - .map_err(|e| zerror!("Can not create a new QUIC listener on {}: {}", addr, e))?; + let quic_endpoint = if let Some(iface) = server_crypto.bind_iface { + async { + // Bind the UDP socket + let socket = tokio::net::UdpSocket::bind(addr).await?; + zenoh_util::net::set_bind_to_device_udp_socket(&socket, iface)?; + + // create the Endpoint with this socket + let runtime = quinn::default_runtime().ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::Other, "no async runtime found") + })?; + ZResult::Ok(quinn::Endpoint::new_with_abstract_socket( + EndpointConfig::default(), + Some(server_config), + runtime.wrap_udp_socket(socket.into_std()?)?, + runtime, + )?) + } + .await + } else { + quinn::Endpoint::server(server_config, addr).map_err(Into::into) + } + .map_err(|e| zerror!("Can not create a new QUIC listener on {}: {}", addr, e))?; let local_addr = quic_endpoint .local_addr() .map_err(|e| zerror!("Can not create a new QUIC listener on {}: {}", addr, e))?; // Update the endpoint locator address - endpoint = EndPoint::new( + let endpoint = EndPoint::new( endpoint.protocol(), local_addr.to_string(), endpoint.metadata(), diff --git a/io/zenoh-links/zenoh-link-quic/src/utils.rs b/io/zenoh-links/zenoh-link-quic/src/utils.rs index c00fb8e3d..e979f2a9c 100644 --- a/io/zenoh-links/zenoh-link-quic/src/utils.rs +++ b/io/zenoh-links/zenoh-link-quic/src/utils.rs @@ -28,7 +28,9 @@ use rustls::{ use secrecy::ExposeSecret; use webpki::anchor_from_trusted_cert; use zenoh_config::Config as ZenohConfig; -use zenoh_link_commons::{tls::WebPkiVerifierAnyServerName, ConfigurationInspector}; +use zenoh_link_commons::{ + tls::WebPkiVerifierAnyServerName, ConfigurationInspector, BIND_INTERFACE, +}; use zenoh_protocol::core::{ endpoint::{Address, Config}, parameters, @@ -151,13 +153,14 @@ impl ConfigurationInspector for TlsConfigurator { } } -pub(crate) struct TlsServerConfig { +pub(crate) struct TlsServerConfig<'a> { pub(crate) server_config: ServerConfig, pub(crate) tls_close_link_on_expiration: bool, + pub(crate) bind_iface: Option<&'a str>, } -impl TlsServerConfig { - pub async fn new(config: &Config<'_>) -> ZResult { +impl<'a> TlsServerConfig<'a> { + pub async fn new(config: &'a Config<'_>) -> ZResult { let tls_server_client_auth: bool = match config.get(TLS_ENABLE_MTLS) { Some(s) => s .parse() @@ -231,6 +234,7 @@ impl TlsServerConfig { Ok(TlsServerConfig { server_config: sc, tls_close_link_on_expiration, + bind_iface: config.get(BIND_INTERFACE), }) } @@ -255,13 +259,14 @@ impl TlsServerConfig { } } -pub(crate) struct TlsClientConfig { +pub(crate) struct TlsClientConfig<'a> { pub(crate) client_config: ClientConfig, pub(crate) tls_close_link_on_expiration: bool, + pub(crate) bind_iface: Option<&'a str>, } -impl TlsClientConfig { - pub async fn new(config: &Config<'_>) -> ZResult { +impl<'a> TlsClientConfig<'a> { + pub async fn new(config: &'a Config<'_>) -> ZResult { let tls_client_server_auth: bool = match config.get(TLS_ENABLE_MTLS) { Some(s) => s .parse() @@ -375,6 +380,7 @@ impl TlsClientConfig { Ok(TlsClientConfig { client_config: cc, tls_close_link_on_expiration, + bind_iface: config.get(BIND_INTERFACE), }) } diff --git a/io/zenoh-transport/tests/unicast_openclose.rs b/io/zenoh-transport/tests/unicast_openclose.rs index f2300a2a8..4aaec3d4a 100644 --- a/io/zenoh-transport/tests/unicast_openclose.rs +++ b/io/zenoh-transport/tests/unicast_openclose.rs @@ -562,80 +562,8 @@ async fn openclose_tls_only() { use zenoh_link::tls::config::*; zenoh_util::init_log_from_env_or("error"); - // NOTE: this an auto-generated pair of certificate and key. - // The target domain is localhost, so it has no real - // mapping to any existing domain. The certificate and key - // have been generated using: https://github.com/jsha/minica - let key = "-----BEGIN RSA PRIVATE KEY----- -MIIEpAIBAAKCAQEAsfqAuhElN4HnyeqLovSd4Qe+nNv5AwCjSO+HFiF30x3vQ1Hi -qRA0UmyFlSqBnFH3TUHm4Jcad40QfrX8f11NKGZdpvKHsMYqYjZnYkRFGS2s4fQy -aDbV5M06s3UDX8ETPgY41Y8fCKTSVdi9iHkwcVrXMxUu4IBBx0C1r2GSo3gkIBnU -cELdFdaUOSbdCipJhbnkwixEr2h7PXxwba7SIZgZtRaQWak1VE9b716qe3iMuMha -Efo/UoFmeZCPu5spfwaOZsnCsxRPk2IjbzlsHTJ09lM9wmbEFHBMVAXejLTk++Sr -Xt8jASZhNen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fAmQ/BUIQm/9 -/llJJs2jUMWzGzAfBgNVHSMEGDAWgBTWfAmQ/BUIQm/9/llJJs2jUMWzGzANBgkq -hkiG9w0BAQsFAAOCAQEAvtcZFAELKiTuOiAeYts6zeKxc+nnHCzayDeD/BDCbxGJ -e1n+xdHjLtWGd+/Anc+fvftSYBPTFQqCi84lPiUIln5z/rUxE+ke81hNPIfw2obc -yIg87xCabQpVyEh8s+MV+7YPQ1+fH4FuSi2Fck1FejxkVqN2uOZPvOYUmSTsaVr1 -8SfRnwJNZ9UMRPM2bD4Jkvj0VcL42JM3QkOClOzYW4j/vll2cSs4kx7er27cIoo1 -Ck0v2xSPAiVjg6w65rUQeW6uB5m0T2wyj+wm0At8vzhZPlgS1fKhcmT2dzOq3+oN -R+IdLiXcyIkg0m9N8I17p0ljCSkbrgGMD3bbePRTfg== ------END CERTIFICATE-----"; + let (ca, cert, key) = get_tls_certs(); let mut endpoint: EndPoint = format!("tls/localhost:{}", 13030).parse().unwrap(); endpoint @@ -659,80 +587,7 @@ R+IdLiXcyIkg0m9N8I17p0ljCSkbrgGMD3bbePRTfg== async fn openclose_quic_only() { use zenoh_link::quic::config::*; - // NOTE: this an auto-generated pair of certificate and key. - // The target domain is localhost, so it has no real - // mapping to any existing domain. The certificate and key - // have been generated using: https://github.com/jsha/minica - let key = "-----BEGIN RSA PRIVATE KEY----- -MIIEpAIBAAKCAQEAsfqAuhElN4HnyeqLovSd4Qe+nNv5AwCjSO+HFiF30x3vQ1Hi -qRA0UmyFlSqBnFH3TUHm4Jcad40QfrX8f11NKGZdpvKHsMYqYjZnYkRFGS2s4fQy -aDbV5M06s3UDX8ETPgY41Y8fCKTSVdi9iHkwcVrXMxUu4IBBx0C1r2GSo3gkIBnU -cELdFdaUOSbdCipJhbnkwixEr2h7PXxwba7SIZgZtRaQWak1VE9b716qe3iMuMha -Efo/UoFmeZCPu5spfwaOZsnCsxRPk2IjbzlsHTJ09lM9wmbEFHBMVAXejLTk++Sr -Xt8jASZhNen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WfAmQ/BUIQm/9 -/llJJs2jUMWzGzAfBgNVHSMEGDAWgBTWfAmQ/BUIQm/9/llJJs2jUMWzGzANBgkq -hkiG9w0BAQsFAAOCAQEAvtcZFAELKiTuOiAeYts6zeKxc+nnHCzayDeD/BDCbxGJ -e1n+xdHjLtWGd+/Anc+fvftSYBPTFQqCi84lPiUIln5z/rUxE+ke81hNPIfw2obc -yIg87xCabQpVyEh8s+MV+7YPQ1+fH4FuSi2Fck1FejxkVqN2uOZPvOYUmSTsaVr1 -8SfRnwJNZ9UMRPM2bD4Jkvj0VcL42JM3QkOClOzYW4j/vll2cSs4kx7er27cIoo1 -Ck0v2xSPAiVjg6w65rUQeW6uB5m0T2wyj+wm0At8vzhZPlgS1fKhcmT2dzOq3+oN -R+IdLiXcyIkg0m9N8I17p0ljCSkbrgGMD3bbePRTfg== ------END CERTIFICATE-----"; + let (ca, cert, key) = get_tls_certs(); // Define the locator let mut endpoint: EndPoint = format!("quic/localhost:{}", 13040).parse().unwrap(); @@ -834,3 +689,214 @@ async fn openclose_vsock() { let endpoint: EndPoint = "vsock/VMADDR_CID_LOCAL:17000".parse().unwrap(); openclose_lowlatency_transport(&endpoint).await; } + +#[cfg(feature = "transport_quic")] +#[cfg(target_os = "linux")] +#[should_panic(expected = "Elapsed")] +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn openclose_quic_only_connect_with_interface_restriction() { + use zenoh_link::quic::config::*; + + zenoh_util::init_log_from_env_or("error"); + let addrs = get_ipv4_ipaddrs(None); + let (ca, cert, key) = get_tls_certs(); + + let mut listen_endpoint: EndPoint = format!("quic/{}:{}", addrs[0], 13005).parse().unwrap(); + listen_endpoint + .config_mut() + .extend_from_iter( + [ + (TLS_ROOT_CA_CERTIFICATE_RAW, ca), + (TLS_LISTEN_PRIVATE_KEY_RAW, key), + (TLS_LISTEN_CERTIFICATE_RAW, cert), + ] + .iter() + .copied(), + ) + .unwrap(); + + let connect_endpoint: EndPoint = format!("quic/{}:{}#iface=lo", addrs[0], 13005) + .parse() + .unwrap(); + + // should not connect to local interface and external address + openclose_transport(&listen_endpoint, &connect_endpoint, false).await; +} + +#[cfg(feature = "transport_quic")] +#[cfg(target_os = "linux")] +#[should_panic(expected = "Elapsed")] +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn openclose_quic_only_listen_with_interface_restriction() { + use zenoh_link::quic::config::*; + + zenoh_util::init_log_from_env_or("error"); + let addrs = get_ipv4_ipaddrs(None); + let (ca, cert, key) = get_tls_certs(); + + let mut listen_endpoint: EndPoint = format!("quic/{}:{}#iface=lo", addrs[0], 13006) + .parse() + .unwrap(); + listen_endpoint + .config_mut() + .extend_from_iter( + [ + (TLS_ROOT_CA_CERTIFICATE_RAW, ca), + (TLS_LISTEN_PRIVATE_KEY_RAW, key), + (TLS_LISTEN_CERTIFICATE_RAW, cert), + ] + .iter() + .copied(), + ) + .unwrap(); + + let connect_endpoint: EndPoint = format!("quic/{}:{}", addrs[0], 13006).parse().unwrap(); + + // should not connect to local interface and external address + openclose_transport(&listen_endpoint, &connect_endpoint, false).await; +} + +#[cfg(feature = "transport_tls")] +#[cfg(target_os = "linux")] +#[should_panic(expected = "Elapsed")] +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn openclose_tls_only_connect_with_interface_restriction() { + use zenoh_link::tls::config::*; + + zenoh_util::init_log_from_env_or("error"); + let addrs = get_ipv4_ipaddrs(None); + let (ca, cert, key) = get_tls_certs(); + + let mut listen_endpoint: EndPoint = format!("tls/{}:{}", addrs[0], 13007).parse().unwrap(); + listen_endpoint + .config_mut() + .extend_from_iter( + [ + (TLS_ROOT_CA_CERTIFICATE_RAW, ca), + (TLS_LISTEN_PRIVATE_KEY_RAW, key), + (TLS_LISTEN_CERTIFICATE_RAW, cert), + ] + .iter() + .copied(), + ) + .unwrap(); + + let connect_endpoint: EndPoint = format!("tls/{}:{}#iface=lo", addrs[0], 13007) + .parse() + .unwrap(); + + // should not connect to local interface and external address + openclose_transport(&listen_endpoint, &connect_endpoint, false).await; +} + +#[cfg(feature = "transport_tls")] +#[cfg(target_os = "linux")] +#[should_panic(expected = "assertion failed: open_res.is_ok()")] +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn openclose_tls_only_listen_with_interface_restriction() { + use zenoh_link::tls::config::*; + + zenoh_util::init_log_from_env_or("error"); + let addrs = get_ipv4_ipaddrs(None); + let (ca, cert, key) = get_tls_certs(); + + let mut listen_endpoint: EndPoint = format!("tls/{}:{}#iface=lo", addrs[0], 13008) + .parse() + .unwrap(); + listen_endpoint + .config_mut() + .extend_from_iter( + [ + (TLS_ROOT_CA_CERTIFICATE_RAW, ca), + (TLS_LISTEN_PRIVATE_KEY_RAW, key), + (TLS_LISTEN_CERTIFICATE_RAW, cert), + ] + .iter() + .copied(), + ) + .unwrap(); + + let connect_endpoint: EndPoint = format!("tls/{}:{}", addrs[0], 13008).parse().unwrap(); + + // should not connect to local interface and external address + openclose_transport(&listen_endpoint, &connect_endpoint, false).await; +} + +fn get_tls_certs() -> (&'static str, &'static str, &'static str) { + // NOTE: this an auto-generated pair of certificate and key. + // The target domain is localhost, so it has no real + // mapping to any existing domain. The certificate and key + // have been generated using: https://github.com/jsha/minica + let key = "-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAsfqAuhElN4HnyeqLovSd4Qe+nNv5AwCjSO+HFiF30x3vQ1Hi +qRA0UmyFlSqBnFH3TUHm4Jcad40QfrX8f11NKGZdpvKHsMYqYjZnYkRFGS2s4fQy +aDbV5M06s3UDX8ETPgY41Y8fCKTSVdi9iHkwcVrXMxUu4IBBx0C1r2GSo3gkIBnU +cELdFdaUOSbdCipJhbnkwixEr2h7PXxwba7SIZgZtRaQWak1VE9b716qe3iMuMha +Efo/UoFmeZCPu5spfwaOZsnCsxRPk2IjbzlsHTJ09lM9wmbEFHBMVAXejLTk++Sr +Xt8jASZhNen/2GzyLQNAquGn98lCMQ6SsE9vLQIDAQABAoIBAGQkKggHm6Q20L+4 +2+b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