diff --git a/Cargo.lock b/Cargo.lock index dff1adbf08..a3ec4b90cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6657,6 +6657,17 @@ dependencies = [ "thiserror", ] +[[package]] +name = "nym-ip-packet-requests" +version = "0.1.0" +dependencies = [ + "bincode", + "bytes", + "nym-service-providers-common", + "nym-sphinx", + "serde", +] + [[package]] name = "nym-ip-packet-router" version = "0.1.0" @@ -7465,6 +7476,18 @@ dependencies = [ "wasm-utils", ] +[[package]] +name = "nym-tun" +version = "0.1.0" +dependencies = [ + "boringtun", + "etherparse", + "log", + "thiserror", + "tokio", + "tokio-tun", +] + [[package]] name = "nym-types" version = "1.0.0" @@ -7593,9 +7616,9 @@ dependencies = [ "ip_network", "ip_network_table", "log", - "nym-service-providers-common", "nym-sphinx", "nym-task", + "nym-tun", "nym-wireguard-types", "rand 0.8.5", "serde", diff --git a/Cargo.toml b/Cargo.toml index 01195764d5..a7f5365711 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ members = [ "common/exit-policy", "common/http-api-client", "common/inclusion-probability", + "common/ip-packet-requests", "common/ledger", "common/mixnode-common", "common/network-defaults", @@ -74,6 +75,7 @@ members = [ "common/store-cipher", "common/task", "common/topology", + "common/tun", "common/types", "common/wasm/client-core", "common/wasm/storage", diff --git a/common/ip-packet-requests/Cargo.toml b/common/ip-packet-requests/Cargo.toml new file mode 100644 index 0000000000..242464a5c6 --- /dev/null +++ b/common/ip-packet-requests/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "nym-ip-packet-requests" +version = "0.1.0" +authors.workspace = true +repository.workspace = true +homepage.workspace = true +documentation.workspace = true +edition.workspace = true +license.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bincode = "1.3.3" +bytes = "1.5.0" +nym-service-providers-common = { path = "../../service-providers/common" } +nym-sphinx = { path = "../nymsphinx" } +serde = { workspace = true, features = ["derive"] } diff --git a/common/ip-packet-requests/src/lib.rs b/common/ip-packet-requests/src/lib.rs new file mode 100644 index 0000000000..104e0336d2 --- /dev/null +++ b/common/ip-packet-requests/src/lib.rs @@ -0,0 +1,33 @@ +use nym_service_providers_common::interface; + +pub type IpPacketRouterRequest = interface::Request; +pub type IpPacketRouterResponse = interface::Response; + +#[derive(serde::Serialize, serde::Deserialize)] +pub struct TaggedIpPacket { + pub packet: bytes::Bytes, + pub return_address: nym_sphinx::addressing::clients::Recipient, + pub return_mix_hops: Option, + pub return_mix_delays: Option, +} + +#[derive(serde::Serialize, serde::Deserialize)] +pub struct IpPacket { + pub packet: bytes::Bytes, +} + +impl TaggedIpPacket { + pub fn from_message( + message: &nym_sphinx::receiver::ReconstructedMessage, + ) -> Result { + use bincode::Options; + make_bincode_serializer().deserialize(&message.message) + } +} + +fn make_bincode_serializer() -> impl bincode::Options { + use bincode::Options; + bincode::DefaultOptions::new() + .with_big_endian() + .with_varint_encoding() +} diff --git a/common/tun/Cargo.toml b/common/tun/Cargo.toml new file mode 100644 index 0000000000..a674d4fde4 --- /dev/null +++ b/common/tun/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "nym-tun" +version = "0.1.0" +authors.workspace = true +repository.workspace = true +homepage.workspace = true +documentation.workspace = true +edition.workspace = true +license.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +thiserror.workspace = true +tokio = { workspace = true, features = ["rt-multi-thread", "net", "io-util", "time", "sync", "macros"] } +etherparse = "0.13.0" +log.workspace = true +boringtun = { workspace = true } +nym-wireguard = { path = "../wireguard", optional = true } + +[target.'cfg(target_os = "linux")'.dependencies] +tokio-tun = "0.9.0" + +[features] +wireguard = [] diff --git a/common/tun/src/lib.rs b/common/tun/src/lib.rs new file mode 100644 index 0000000000..2b8336d071 --- /dev/null +++ b/common/tun/src/lib.rs @@ -0,0 +1,7 @@ +#[cfg(target_os = "linux")] +mod linux; + +pub mod tun_task_channel; + +#[cfg(target_os = "linux")] +pub use linux::tun_device; diff --git a/common/wireguard/src/platform/linux/mod.rs b/common/tun/src/linux/mod.rs similarity index 100% rename from common/wireguard/src/platform/linux/mod.rs rename to common/tun/src/linux/mod.rs diff --git a/common/wireguard/src/platform/linux/tun_device.rs b/common/tun/src/linux/tun_device.rs similarity index 93% rename from common/wireguard/src/platform/linux/tun_device.rs rename to common/tun/src/linux/tun_device.rs index eab3102162..6bb594bba4 100644 --- a/common/wireguard/src/platform/linux/tun_device.rs +++ b/common/tun/src/linux/tun_device.rs @@ -1,7 +1,6 @@ use std::{ collections::HashMap, net::{IpAddr, Ipv4Addr}, - sync::Arc, time::Duration, }; @@ -11,16 +10,15 @@ use tokio::{ time::timeout, }; -use crate::{ - active_peers::PeerEventSenderError, - event::Event, - tun_task_channel::{ - tun_task_channel, tun_task_response_channel, TunTaskPayload, TunTaskResponseRx, - TunTaskResponseSendError, TunTaskResponseTx, TunTaskRx, TunTaskTx, - }, - udp_listener::PeersByIp, +use crate::tun_task_channel::{ + tun_task_channel, tun_task_response_channel, TunTaskPayload, TunTaskResponseRx, + TunTaskResponseSendError, TunTaskResponseTx, TunTaskRx, TunTaskTx, }; +#[cfg(feature = "wireguard")] +use nym_wireguard::{active_peers::PeerEventSenderError, event::Event, udp_listener::PPeersByIp}; + +#[cfg(feature = "wireguard")] const MUTEX_LOCK_TIMEOUT_MS: u64 = 200; const TUN_WRITE_TIMEOUT_MS: u64 = 1000; @@ -32,6 +30,7 @@ pub enum TunDeviceError { #[error("error writing to tun device: {source}")] TunWriteError { source: std::io::Error }, + #[cfg(feature = "wireguard")] #[error("failed forwarding packet to peer: {source}")] ForwardToPeerFailed { #[from] @@ -94,6 +93,7 @@ pub struct TunDevice { pub enum RoutingMode { // The routing table, as how wireguard does it + #[cfg(feature = "wireguard")] AllowedIps(AllowedIpsInner), // This is an alternative to the routing table, where we just match outgoing source IP with @@ -108,15 +108,18 @@ impl RoutingMode { }) } + #[cfg(feature = "wireguard")] pub fn new_allowed_ips(peers_by_ip: Arc>) -> Self { RoutingMode::AllowedIps(AllowedIpsInner { peers_by_ip }) } } +#[cfg(feature = "wireguard")] pub struct AllowedIpsInner { peers_by_ip: Arc>, } +#[cfg(feature = "wireguard")] impl AllowedIpsInner { async fn lock(&self) -> Result, TunDeviceError> { timeout( @@ -180,6 +183,7 @@ impl TunDevice { ); // TODO: expire old entries + #[allow(irrefutable_let_patterns)] if let RoutingMode::Nat(nat_table) = &mut self.routing_mode { nat_table.nat_table.insert(src_addr, tag); } @@ -207,6 +211,7 @@ impl TunDevice { match self.routing_mode { // This is how wireguard does it, by consulting the AllowedIPs table. + #[cfg(feature = "wireguard")] RoutingMode::AllowedIps(ref peers_by_ip) => { let peers = peers_by_ip.lock().await?; if let Some(peer_tx) = peers.longest_match(dst_addr).map(|(_, tx)| tx) { diff --git a/common/wireguard/src/tun_task_channel.rs b/common/tun/src/tun_task_channel.rs similarity index 71% rename from common/wireguard/src/tun_task_channel.rs rename to common/tun/src/tun_task_channel.rs index e096323d14..2821c744d5 100644 --- a/common/wireguard/src/tun_task_channel.rs +++ b/common/tun/src/tun_task_channel.rs @@ -1,6 +1,5 @@ use std::time::Duration; -use nym_service_providers_common::interface; use tokio::sync::mpsc::{ self, error::{SendError, SendTimeoutError, TrySendError}, @@ -81,35 +80,3 @@ pub(crate) fn tun_task_response_channel() -> (TunTaskResponseTx, TunTaskResponse TunTaskResponseRx(tun_task_rx), ) } - -pub type IpPacketRouterRequest = interface::Request; -pub type IpPacketRouterResponse = interface::Response; - -#[derive(serde::Serialize, serde::Deserialize)] -pub struct TaggedIpPacket { - pub packet: bytes::Bytes, - pub return_address: nym_sphinx::addressing::clients::Recipient, - pub return_mix_hops: Option, - pub return_mix_delays: Option, -} - -#[derive(serde::Serialize, serde::Deserialize)] -pub struct IpPacket { - pub packet: bytes::Bytes, -} - -impl TaggedIpPacket { - pub fn from_message( - message: &nym_sphinx::receiver::ReconstructedMessage, - ) -> Result { - use bincode::Options; - make_bincode_serializer().deserialize(&message.message) - } -} - -fn make_bincode_serializer() -> impl bincode::Options { - use bincode::Options; - bincode::DefaultOptions::new() - .with_big_endian() - .with_varint_encoding() -} diff --git a/common/wireguard/Cargo.toml b/common/wireguard/Cargo.toml index 6b76c605c2..f2ac47abdc 100644 --- a/common/wireguard/Cargo.toml +++ b/common/wireguard/Cargo.toml @@ -29,14 +29,12 @@ log.workspace = true nym-task = { path = "../task" } nym-wireguard-types = { path = "../wireguard-types" } nym-sphinx = { path = "../nymsphinx" } +nym-tun = { path = "../tun" , features = ["wireguard"] } rand.workspace = true serde = { workspace = true, features = ["derive"] } tap.workspace = true thiserror.workspace = true tokio = { workspace = true, features = ["rt-multi-thread", "net", "io-util"] } -# WIP(JON) we'll move this when we move the tun stuff to its own crate -nym-service-providers-common = { path = "../../service-providers/common" } - [target.'cfg(target_os = "linux")'.dependencies] tokio-tun = "0.9.0" diff --git a/common/wireguard/src/lib.rs b/common/wireguard/src/lib.rs index 2c7096bcf8..a03c6d96f4 100644 --- a/common/wireguard/src/lib.rs +++ b/common/wireguard/src/lib.rs @@ -8,10 +8,10 @@ mod error; mod event; mod network_table; mod packet_relayer; -mod platform; +// mod platform; mod registered_peers; pub mod setup; -pub mod tun_task_channel; +// pub mod tun_task_channel; mod udp_listener; mod wg_tunnel; @@ -20,7 +20,9 @@ use std::sync::Arc; // Currently the module related to setting up the virtual network device is platform specific. #[cfg(target_os = "linux")] -pub use platform::linux::tun_device; +use nym_tun::tun_device; + +use nym_tun::tun_task_channel; /// Start wireguard UDP listener and TUN device ///