diff --git a/.github/workflows/ci-netns.yml b/.github/workflows/ci-netns.yml index 5e69d7fa42..a2897541e0 100644 --- a/.github/workflows/ci-netns.yml +++ b/.github/workflows/ci-netns.yml @@ -83,3 +83,21 @@ jobs: # these tests the filter matches nothing: a green no-op that still compiles # the affected crates' test targets. run: cargo test -p minimald -p sandbox2 -p minimald-rpc netns -- --include-ignored --nocapture + - name: Run WireGuard mesh netns proof (UC7) + env: + MINIMALD_NETNS_TEST: "1" + GVPROXY_BIN: ${{ runner.temp }}/gvproxy + # The UC7 two-namespace mesh proof lives in the `networking-wg`-gated + # `mesh_uc7.rs` (feature is minimald-only, cannot span the multi-package + # command above). Unlike the sudo-per-command UC6 tests, mesh_uc7 binds + # sockets inside the namespaces via in-process `setns`, so the test + # *binary* must run as root. Build unprivileged, then run the binary + # under the runner's passwordless sudo. + run: | + set -euo pipefail + bin=$(cargo test -p minimald --features networking-wg --test mesh_uc7 \ + --no-run --message-format=json \ + | jq -r 'select(.executable != null and (.target.name? == "mesh_uc7")) | .executable' \ + | tail -1) + echo "mesh_uc7 test binary: $bin" + sudo -E "$bin" --include-ignored --nocapture diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e4cdaeafee..d230c054e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -85,6 +85,13 @@ jobs: # (R4.5 mtls_missing_cert_returns_401_with_no_topology, UC2b # mtls_valid_cert_routes_to_backend). Exercise them explicitly. run: cargo nextest run -p minimald --features networking-proxy + - name: Run WireGuard mesh tests + # The WireGuard mesh peer is behind the non-default `networking-wg` + # feature, so `--workspace` above does not compile or run its proofs. + # Exercise them explicitly (R-WG: two_meshes_handshake_and_relay_a_packet, + # rpc get_mesh_status). The `#[ignore]` two-namespace mesh_uc7 proof runs + # in the netns lane. + run: cargo test -p minimald --features networking-wg - name: Run doctests run: cargo test --workspace --doc diff --git a/Cargo.lock b/Cargo.lock index 2f7e115c6a..9d96287cb2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,16 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common 0.1.7", + "generic-array 0.14.7", +] + [[package]] name = "aead" version = "0.6.1" @@ -24,7 +34,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99" dependencies = [ "crypto-common 0.2.2", - "inout", + "inout 0.2.2", ] [[package]] @@ -33,7 +43,7 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1fc76eaeac4c9164506c466d4ffdd8ec9d0c5bf57ee97177c4d8eceb3a0e138" dependencies = [ - "cipher", + "cipher 0.5.2", "cpubits", "cpufeatures 0.3.0", "zeroize", @@ -45,9 +55,9 @@ version = "0.11.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da8c919c118108f144adecad74b425b804ad075580d605d9b33c2d6d1c62a2f8" dependencies = [ - "aead", + "aead 0.6.1", "aes", - "cipher", + "cipher 0.5.2", "ctr", "ghash", "subtle", @@ -141,7 +151,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7af50940b73bf4e16c15c448a2b121c63f2d68e3e54b6a8731673cb4aa0cdff5" dependencies = [ "base64ct", - "blake2", + "blake2 0.11.0-rc.6", "cpufeatures 0.3.0", "password-hash", ] @@ -398,6 +408,15 @@ version = "3.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d084b0137aaa901caf9f1e8b21daa6aa24d41cd806e111335541eff9683bd6" +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest 0.10.7", +] + [[package]] name = "blake2" version = "0.11.0-rc.6" @@ -457,7 +476,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62ce3946557b35e71d1bbe07ec385073ce9eda05043f95de134eb578fcf1a298" dependencies = [ "byteorder", - "cipher", + "cipher 0.5.2", +] + +[[package]] +name = "boringtun" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15dd6a8a89cbe8997f37ca0cf035e6ea4d64cd2ecea4aed83ffb9f99f7126939" +dependencies = [ + "aead 0.5.2", + "base64", + "blake2 0.10.6", + "chacha20poly1305", + "hex", + "hmac 0.12.1", + "ip_network", + "ip_network_table", + "libc", + "nix 0.31.3", + "parking_lot", + "portable-atomic", + "rand_core 0.6.4", + "ring", + "tracing", + "untrusted 0.9.0", + "x25519-dalek", ] [[package]] @@ -540,7 +584,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce2dc9ee5f88d11e0beb842c88b33c8a5cf0d1329c4b19494af42b07dbfe8896" dependencies = [ - "cipher", + "cipher 0.5.2", ] [[package]] @@ -573,6 +617,17 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "chacha20" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +dependencies = [ + "cfg-if", + "cipher 0.4.4", + "cpufeatures 0.2.17", +] + [[package]] name = "chacha20" version = "0.10.0" @@ -580,12 +635,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601" dependencies = [ "cfg-if", - "cipher", + "cipher 0.5.2", "cpufeatures 0.3.0", "rand_core 0.10.1", "zeroize", ] +[[package]] +name = "chacha20poly1305" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" +dependencies = [ + "aead 0.5.2", + "chacha20 0.9.1", + "cipher 0.4.4", + "poly1305 0.8.0", + "zeroize", +] + [[package]] name = "check" version = "0.0.1" @@ -650,6 +718,17 @@ dependencies = [ "half", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout 0.1.4", + "zeroize", +] + [[package]] name = "cipher" version = "0.5.2" @@ -658,7 +737,7 @@ checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" dependencies = [ "block-buffer 0.12.1", "crypto-common 0.2.2", - "inout", + "inout 0.2.2", "zeroize", ] @@ -1054,6 +1133,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array 0.14.7", + "rand_core 0.6.4", "typenum", ] @@ -1084,7 +1164,7 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baaca1c4b237092596f64d571e9db6ce4109c4ef9742e27590f1709594461f21" dependencies = [ - "cipher", + "cipher 0.5.2", ] [[package]] @@ -1097,6 +1177,21 @@ dependencies = [ "subtle", ] +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "curve25519-dalek-derive", + "fiat-crypto 0.2.9", + "rustc_version", + "subtle", + "zeroize", +] + [[package]] name = "curve25519-dalek" version = "5.0.0-rc.0" @@ -1107,7 +1202,7 @@ dependencies = [ "cpufeatures 0.3.0", "curve25519-dalek-derive", "digest 0.11.3", - "fiat-crypto", + "fiat-crypto 0.3.0", "rustc_version", "subtle", "zeroize", @@ -1304,7 +1399,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "916a94e407b54f9034d71dd748234cd1e516ced6284009906ae246f177eafe5a" dependencies = [ - "cipher", + "cipher 0.5.2", ] [[package]] @@ -1321,6 +1416,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer 0.10.4", "crypto-common 0.1.7", + "subtle", ] [[package]] @@ -1416,7 +1512,7 @@ version = "3.0.0-rc.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b011170fe4f04665565b4110afef66774fe9ffff278f3eb5b81cc73d26e27d60" dependencies = [ - "curve25519-dalek", + "curve25519-dalek 5.0.0-rc.0", "ed25519", "rand_core 0.10.1", "serde", @@ -1537,6 +1633,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + [[package]] name = "fiat-crypto" version = "0.3.0" @@ -2221,7 +2323,16 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" dependencies = [ - "hmac", + "hmac 0.13.0", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", ] [[package]] @@ -2550,6 +2661,15 @@ dependencies = [ "rustversion", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array 0.14.7", +] + [[package]] name = "inout" version = "0.2.2" @@ -2572,6 +2692,28 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "ip_network" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa2f047c0a98b2f299aa5d6d7088443570faae494e9ae1305e48be000c9e0eb1" + +[[package]] +name = "ip_network_table" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4099b7cfc5c5e2fe8c5edf3f6f7adf7a714c9cc697534f63a5a5da30397cb2c0" +dependencies = [ + "ip_network", + "ip_network_table-deps-treebitmap", +] + +[[package]] +name = "ip_network_table-deps-treebitmap" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e537132deb99c0eb4b752f0346b6a836200eaaa3516dd7e5514b63930a09e5d" + [[package]] name = "ipnet" version = "2.12.0" @@ -3347,6 +3489,8 @@ dependencies = [ "args", "async-compression", "async-tar", + "base64", + "boringtun", "bytes", "camino", "check", @@ -3368,6 +3512,7 @@ dependencies = [ "ot", "path-absolutize", "paths", + "rand_core 0.6.4", "rcgen", "russh", "russh-sftp", @@ -3793,6 +3938,12 @@ dependencies = [ "walkdir", ] +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "openssl-probe" version = "0.2.1" @@ -3893,7 +4044,7 @@ checksum = "9bd5333afa5ae0347f39e6a0f2c9c155da431583fd71fe5555bd0521b4ccaf02" dependencies = [ "ecdsa", "elliptic-curve", - "fiat-crypto", + "fiat-crypto 0.3.0", "primefield", "primeorder", "sha2 0.11.0", @@ -4024,7 +4175,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "112d82ceb8c5bf524d9af484d4e4970c9fd5a0cc15ba14ad93dccd28873b0629" dependencies = [ "digest 0.11.3", - "hmac", + "hmac 0.13.0", ] [[package]] @@ -4159,6 +4310,17 @@ version = "0.3.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" +[[package]] +name = "poly1305" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" +dependencies = [ + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash 0.5.1", +] + [[package]] name = "poly1305" version = "0.9.0" @@ -4166,7 +4328,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a00baa632505d05512f48a963e16051c54fda9a95cc9acea1a4e3c90991c4a2e" dependencies = [ "cpufeatures 0.3.0", - "universal-hash", + "universal-hash 0.6.1", "zeroize", ] @@ -4178,7 +4340,7 @@ checksum = "7dfc63250416fea14f5749b90725916a6c903f599d51cb635aa7a52bfd03eede" dependencies = [ "cpubits", "cpufeatures 0.3.0", - "universal-hash", + "universal-hash 0.6.1", ] [[package]] @@ -4565,7 +4727,7 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2e8e8bcc7961af1fdac401278c6a831614941f6164ee3bf4ce61b7edb162207" dependencies = [ - "chacha20", + "chacha20 0.10.0", "getrandom 0.4.3", "rand_core 0.10.1", ] @@ -4827,7 +4989,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5236ce872cac07e0fb3969b0cbf468c7d2f37d432f1b627dcb7b8d34563fb0c3" dependencies = [ - "hmac", + "hmac 0.13.0", "subtle", ] @@ -4877,10 +5039,10 @@ dependencies = [ "byteorder", "bytes", "cbc", - "cipher", + "cipher 0.5.2", "crypto-bigint", "ctr", - "curve25519-dalek", + "curve25519-dalek 5.0.0-rc.0", "data-encoding", "delegate", "der", @@ -4895,8 +5057,8 @@ dependencies = [ "getrandom 0.4.3", "ghash", "hex-literal", - "hmac", - "inout", + "hmac 0.13.0", + "inout 0.2.2", "internal-russh-num-bigint", "keccak 0.2.0", "log", @@ -4932,7 +5094,7 @@ dependencies = [ "thiserror 2.0.18", "tokio", "typenum", - "universal-hash", + "universal-hash 0.6.1", "zeroize", ] @@ -5130,7 +5292,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f874456e72520ff1375a06c588eaf074b0f01f9e9e1aada45bd9b7954a6e42c" dependencies = [ "cfg-if", - "cipher", + "cipher 0.5.2", ] [[package]] @@ -5612,16 +5774,16 @@ version = "0.3.0-rc.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10db6f219196a8528f9ec904d9d45cdad692d65b0e57e72be4dedd1c5fddce36" dependencies = [ - "aead", + "aead 0.6.1", "aes", "aes-gcm", "cbc", - "chacha20", - "cipher", + "chacha20 0.10.0", + "cipher 0.5.2", "ctr", "ctutils", "des", - "poly1305", + "poly1305 0.9.0", "ssh-encoding", "zeroize", ] @@ -5652,7 +5814,7 @@ dependencies = [ "ctutils", "ed25519-dalek", "hex", - "hmac", + "hmac 0.13.0", "p256", "p384", "p521", @@ -6439,6 +6601,16 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81e544489bf3d8ef66c953931f56617f423cd4b5494be343d9b9d3dda037b9a3" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common 0.1.7", + "subtle", +] + [[package]] name = "universal-hash" version = "0.6.1" @@ -7101,6 +7273,18 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ffae5123b2d3fc086436f8834ae3ab053a283cfac8fe0a0b8eaae044768a4c4" +[[package]] +name = "x25519-dalek" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7e468321c81fb07fa7f4c636c3972b9100f0346e5b6a9f2bd0603a52f7ed277" +dependencies = [ + "curve25519-dalek 4.1.3", + "rand_core 0.6.4", + "serde", + "zeroize", +] + [[package]] name = "xattr" version = "1.6.1" diff --git a/Cargo.toml b/Cargo.toml index c29b83b7f0..44feaf9258 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,11 @@ codegen-units = 16 anyhow = "1.0" async-compression = { version = "0.4", features = ["tokio", "zstd"] } async-tar = { version = "0.6", default-features = false, features = ["runtime-tokio"] } +base64 = "0.22" blake3 = { version = "1", features = ["serde"] } +# WireGuard mesh peer (Unit 4, behind the `networking-wg` feature). Pure-Rust +# WireGuard avoids a Go/cgo toolchain dependency (spec Design Considerations). +boringtun = { version = "0.7", default-features = false } bytes = "1.11" # keep in sync with reqwest bzip2 = "0.6" camino = { version = "1", features = ["serde1"] } @@ -87,6 +91,9 @@ object = { version = "0.39", default-features = false, features = ["archive", "e oci-spec = "0.10" path-absolutize = "3.1" rand = "0.10" +# rand_core 0.6 (not 0.10) to match boringtun/x25519-dalek's RNG traits for +# WireGuard keypair generation (`networking-wg`). +rand_core = { version = "0.6", features = ["getrandom"] } rayon = "1.12" rcgen = { version = "0.13", default-features = false, features = ["crypto", "pem", "ring"] } regex = "1" # keep in sync with nickel-lang-core diff --git a/crates/minimal2/src/main.rs b/crates/minimal2/src/main.rs index b0ae17b815..c66f7cfc60 100644 --- a/crates/minimal2/src/main.rs +++ b/crates/minimal2/src/main.rs @@ -33,6 +33,8 @@ enum Command { Destroy(DestroyArgs), /// Session inspection subcommands Session(SessionArgs), + /// WireGuard mesh: join, leave, and inspect remote-access state + Mesh(MeshArgs), /// Proxy stdio to a daemon UDS socket (used as an SSH ProxyCommand). #[command(hide = true)] Proxy(ProxyArgs), @@ -96,6 +98,56 @@ struct PolicyArgs { session: String, } +/// WireGuard mesh subcommands for authenticated remote PTask access (UC7 / +/// UC2b). The mesh lets a laptop, or another host's PTasks, reach this host's +/// PTasks over an encrypted tunnel. +#[derive(Debug, Args)] +struct MeshArgs { + #[command(subcommand)] + command: MeshCommand, +} + +#[derive(Debug, Subcommand)] +enum MeshCommand { + /// Enrol this machine into a remote minimald's WireGuard mesh + /// + /// v1 uses manual key exchange: this records the target and prints the + /// steps to swap public keys. Once enrolled you can reach the remote + /// host's own-IP PTasks by their switch IPs over the tunnel. + /// + /// Example: + /// + /// minimal mesh join mesh.example.com:51820 + #[command(verbatim_doc_comment)] + Join(MeshJoinArgs), + /// Leave the WireGuard mesh and drop this machine's local enrolment + /// + /// Removes the local enrolment record written by `minimal mesh join`. + /// Peer entries on the remote minimald must be removed there (manual v1). + /// + /// Example: + /// + /// minimal mesh leave + #[command(verbatim_doc_comment)] + Leave, + /// Show this minimald's mesh status: public key, advertised subnets, peers + /// + /// Queries the local minimald for its WireGuard public key, the switch + /// subnets it advertises to the mesh, and each peer's last handshake. + /// + /// Example: + /// + /// minimal mesh status + #[command(verbatim_doc_comment)] + Status, +} + +#[derive(Debug, Args)] +struct MeshJoinArgs { + /// Address of the remote minimald exposing the mesh (`host:port`) + address: String, +} + /// Shared arguments all subcommands #[derive(Debug, Args)] pub struct GlobalArgs { @@ -198,6 +250,11 @@ async fn main() -> Result<(), ()> { Command::Session(SessionArgs { command: SessionCommand::Policy(args), }) => cmd_session_policy(&cli.global_args, args).await, + Command::Mesh(MeshArgs { command }) => match command { + MeshCommand::Status => cmd_mesh_status(&cli.global_args).await, + MeshCommand::Join(args) => cmd_mesh_join(&cli.global_args, args), + MeshCommand::Leave => cmd_mesh_leave(&cli.global_args), + }, Command::Proxy(args) => cmd_proxy(args).await, Command::SshForward(args) => cmd_ssh_forward(&cli.global_args, args).await, Command::Login(args) => cmd_login(&cli.global_args, args).await, @@ -487,6 +544,126 @@ async fn cmd_session_policy(global: &GlobalArgs, args: PolicyArgs) -> Result<(), } } +/// The local mesh-enrolment record path. Honors `--minimal-dir`, else falls +/// back to the user config dir. +fn mesh_enrolment_path(global: &GlobalArgs) -> Result { + let base = match &global.minimal_dir { + Some(dir) => dir.clone(), + None => dirs::config_dir() + .map(|c| c.join("minimal")) + .ok_or_else(|| eprintln!("cannot determine config directory; set --minimal-dir"))?, + }; + Ok(base.join("mesh-enrolment")) +} + +/// Show this minimald's WireGuard mesh status (R4.6): own public key, the +/// switch subnets it advertises, and each peer's last handshake. +async fn cmd_mesh_status(global: &GlobalArgs) -> Result<(), ()> { + if let Err(e) = autospawn::ensure_minvmd_running() { + eprintln!("Failed to ensure minvmd is running: {e}"); + return Err(()); + } + + let mut client = connect_daemon(global).await?; + + use minimald_rpc::GetMeshStatus; + let resp = client + .oneshot_rpc::(()) + .await + .map_err(|e| eprintln!("GetMeshStatus RPC failed: {e}"))?; + + if !resp.configured { + println!("No WireGuard mesh is configured on this minimald."); + return Ok(()); + } + + println!( + "public key: {}", + resp.own_public_key.as_deref().unwrap_or("-") + ); + if resp.advertised_subnets.is_empty() { + println!("advertised: (none)"); + } else { + println!("advertised: {}", resp.advertised_subnets.join(", ")); + } + + if resp.peers.is_empty() { + println!("peers: (none)"); + return Ok(()); + } + + println!("peers:"); + println!(" {:<20} {:<46} LAST HANDSHAKE", "NAME", "PUBLIC KEY"); + for p in &resp.peers { + let handshake = match p.last_handshake_secs { + Some(secs) => format!("{secs}s ago"), + None => "never".to_string(), + }; + println!(" {:<20} {:<46} {handshake}", p.name, p.public_key); + } + + Ok(()) +} + +/// Record this machine's enrolment into a remote minimald's mesh (R4.3, v1 +/// manual key exchange) and print the steps to complete the key swap. +fn cmd_mesh_join(global: &GlobalArgs, args: MeshJoinArgs) -> Result<(), ()> { + // Validate the endpoint at the point of entry so a typo never lands a bad + // enrolment on disk for a later consumer to choke on. The CLI contract is + // `host:port`; require a non-empty host and a parseable u16 port. + let Some((host, port)) = args.address.rsplit_once(':') else { + eprintln!("mesh join address must be host:port, e.g. mesh.example.com:51820"); + return Err(()); + }; + if host.is_empty() || port.parse::().map(|p| p == 0).unwrap_or(true) { + eprintln!("mesh join address must include a non-empty host and a valid non-zero port"); + return Err(()); + } + + let path = mesh_enrolment_path(global)?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent) + .map_err(|e| eprintln!("creating {}: {e}", parent.display()))?; + } + std::fs::write(&path, format!("{}\n", args.address)) + .map_err(|e| eprintln!("writing {}: {e}", path.display()))?; + + println!( + "Recorded mesh enrolment for {} at {}.", + args.address, + path.display() + ); + println!(); + println!("v1 uses manual key exchange. To complete the join:"); + println!(" 1. Run `minimal mesh status` on the remote host to read its public key."); + println!(" 2. Add this machine's WireGuard public key to the remote minimald's peers."); + println!(" 3. Add the remote's public key and endpoint to this machine's mesh config."); + Ok(()) +} + +/// Drop this machine's local mesh enrolment (R4.3). Remote peer entries are +/// removed on the remote host (manual v1). +fn cmd_mesh_leave(global: &GlobalArgs) -> Result<(), ()> { + let path = mesh_enrolment_path(global)?; + match std::fs::remove_file(&path) { + Ok(()) => { + println!( + "Left the mesh; removed local enrolment at {}.", + path.display() + ); + Ok(()) + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + println!("No local mesh enrolment to remove."); + Ok(()) + } + Err(e) => { + eprintln!("removing {}: {e}", path.display()); + Err(()) + } + } +} + /// Destroy (terminate) a session. async fn cmd_destroy(global: &GlobalArgs, args: DestroyArgs) -> Result<(), ()> { if let Err(e) = autospawn::ensure_minvmd_running() { diff --git a/crates/minimald-rpc/src/lib.rs b/crates/minimald-rpc/src/lib.rs index 3df971ec00..a7f062c545 100644 --- a/crates/minimald-rpc/src/lib.rs +++ b/crates/minimald-rpc/src/lib.rs @@ -377,6 +377,106 @@ impl OneshotSshRpc for IssueClientCert { type Response = Errorable; } +// --------------------------------------------------------------------------- +// WireGuard mesh status (Unit 4: R4.6). +// +// These types are the wire contract for `minimal mesh status` and carry no +// WireGuard dependency, so they compile in every build regardless of the +// daemon's `networking-wg` feature. A daemon built without the feature answers +// with `configured = false`. +// --------------------------------------------------------------------------- + +/// One peer's entry in a [`MeshStatus`]. +#[non_exhaustive] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct MeshPeerStatus { + /// The peer's configured name. + pub name: String, + /// The peer's WireGuard public key, base64-encoded. + pub public_key: String, + /// The peer's UDP endpoint (`host:port`), if known. + pub endpoint: Option, + /// Seconds since the last completed handshake with this peer, or `None` if + /// no handshake has completed. + pub last_handshake_secs: Option, +} + +impl MeshPeerStatus { + /// Builds a peer status entry. The struct is `#[non_exhaustive]`, so the + /// daemon (a different crate) constructs it through this constructor. + #[must_use] + pub fn new( + name: String, + public_key: String, + endpoint: Option, + last_handshake_secs: Option, + ) -> Self { + Self { + name, + public_key, + endpoint, + last_handshake_secs, + } + } +} + +/// The current WireGuard mesh state, as returned by [`GetMeshStatus`] (R4.6). +#[non_exhaustive] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct MeshStatus { + /// Whether a WireGuard mesh is configured and running. `false` when the + /// daemon was built without the `networking-wg` feature or no mesh config + /// is present. + pub configured: bool, + /// This node's WireGuard public key, base64-encoded; `None` when not + /// configured. + pub own_public_key: Option, + /// The subnets this node advertises to the mesh (subnet-router model), + /// rendered as CIDR strings. + pub advertised_subnets: Vec, + /// The configured peers and their last-handshake state. + pub peers: Vec, +} + +impl MeshStatus { + /// Builds a configured mesh status. The struct is `#[non_exhaustive]`, so + /// the daemon constructs it through this constructor. + #[must_use] + pub fn new( + own_public_key: String, + advertised_subnets: Vec, + peers: Vec, + ) -> Self { + Self { + configured: true, + own_public_key: Some(own_public_key), + advertised_subnets, + peers, + } + } + + /// The status reported when no mesh is configured, or the daemon was built + /// without the `networking-wg` feature. + #[must_use] + pub fn unconfigured() -> Self { + Self { + configured: false, + own_public_key: None, + advertised_subnets: Vec::new(), + peers: Vec::new(), + } + } +} + +/// An RPC to read the current WireGuard mesh status (R4.6). +pub struct GetMeshStatus; + +impl OneshotSshRpc for GetMeshStatus { + const NAME: &'static str = constcat::concat!(RPC_SUBSYSTEM_PREFIX, "GetMeshStatus"); + type Request<'a> = (); + type Response = MeshStatus; +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/minimald/Cargo.toml b/crates/minimald/Cargo.toml index 37e335be89..2b2bbf4daf 100644 --- a/crates/minimald/Cargo.toml +++ b/crates/minimald/Cargo.toml @@ -8,6 +8,10 @@ publish.workspace = true # Enables the B8 HTTPS reverse proxy with mTLS termination (R4.4–R4.7) in front # of the shared B5 routing core. Requires the rustls/rcgen/tokio-rustls deps. networking-proxy = ["dep:rcgen", "dep:rustls", "dep:tokio-rustls"] +# WireGuard mesh peer and its CLI/RPC plumbing (Unit 4). Off by default so the +# default `minimald` build pulls in no WireGuard code and pays no binary-size or +# startup cost (R4.7); enable with `--features networking-wg`. +networking-wg = ["dep:boringtun", "dep:base64", "dep:rand_core"] [dependencies] async-compression.workspace = true @@ -36,6 +40,11 @@ tokio.workspace = true tracing.workspace = true tracing-subscriber.workspace = true +# WireGuard mesh peer (Unit 4, R4.1/R4.7): compiled only under `networking-wg`. +base64 = { workspace = true, optional = true } +boringtun = { workspace = true, optional = true } +rand_core = { workspace = true, optional = true } + args.workspace = true check.workspace = true common.workspace = true @@ -54,6 +63,12 @@ rcgen = { workspace = true, optional = true } rustls = { workspace = true, optional = true } tokio-rustls = { workspace = true, optional = true } +[dev-dependencies] +# The two-namespace UC7 mesh integration test (`tests/mesh_uc7.rs`) drives a +# tokio runtime and `setns(2)` directly. +libc.workspace = true +tokio.workspace = true + # The guest (initramfs pid-1) boot path is Linux-only. [target.'cfg(target_os = "linux")'.dependencies] tokio-vsock.workspace = true diff --git a/crates/minimald/src/net/mod.rs b/crates/minimald/src/net/mod.rs index fcf518e256..81f6a5e851 100644 --- a/crates/minimald/src/net/mod.rs +++ b/crates/minimald/src/net/mod.rs @@ -21,6 +21,11 @@ pub mod policy; pub mod proxy; pub mod switch; +// WireGuard mesh peer (Unit 4). Compiled only under `networking-wg` so the +// default build carries no WireGuard code (R4.7). +#[cfg(feature = "networking-wg")] +pub mod wg; + use std::fmt; use std::io; use std::net::Ipv4Addr; diff --git a/crates/minimald/src/net/wg.rs b/crates/minimald/src/net/wg.rs new file mode 100644 index 0000000000..51e3e28533 --- /dev/null +++ b/crates/minimald/src/net/wg.rs @@ -0,0 +1,924 @@ +//! WireGuard mesh peer for authenticated remote PTask-to-PTask access (Unit 4). +//! +//! `minimald` joins a WireGuard mesh as a **subnet-router** peer: it advertises +//! its local gvproxy switch subnet (see [`super`]) as the AllowedIPs other +//! peers route to, so a packet for a remote PTask's switch IP is carried over +//! the encrypted tunnel to the `minimald` that owns that switch and injected +//! onto it (Tailscale-style subnet-router model, R4.1/R4.2). +//! +//! The WireGuard data plane is [`boringtun`] — a pure-Rust implementation, so +//! no Go/cgo toolchain is pulled into the build (spec Design Considerations). +//! Every per-peer [`boringtun::noise::Tunn`] is driven by one async pump task +//! ([`Mesh::start`]) over a single UDP socket: it encrypts outbound IP packets, +//! decrypts inbound datagrams onto a tunnel sink, completes handshakes, and +//! keeps a live [status snapshot](MeshHandle::status) for the `GetMeshStatus` +//! RPC (R4.6). +//! +//! The whole module is compiled only under the `networking-wg` feature, so the +//! default `minimald` build carries no WireGuard code (R4.7). +//! +//! Covers R4.1 (subnet-router peer, key generation), R4.2 (remote PTask reach +//! over the tunnel), R4.5 (auth-failure logging with no topology leak), R4.6 +//! (mesh-status snapshot), and R4.7 (feature-gated). + +use std::fmt; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; +use std::str::FromStr; +use std::sync::Arc; +use std::time::Duration; + +use base64::Engine as _; +use boringtun::noise::{Tunn, TunnResult}; +use boringtun::x25519::{PublicKey, StaticSecret}; +use tokio::net::UdpSocket; +use tokio::sync::{mpsc, watch}; +use tokio::task::JoinHandle; + +/// Largest datagram the pump reads or writes. A WireGuard data packet is the +/// inner MTU plus the fixed transport overhead; 1500 + 80 covers it. +const MAX_DATAGRAM: usize = 1580; + +/// How often the pump services per-peer timers (keepalive, rekey, handshake +/// retransmit). WireGuard's own resolution is one second; a quarter-second tick +/// keeps handshake latency low without busy-spinning. +const TIMER_TICK: Duration = Duration::from_millis(250); + +/// Standard WireGuard persistent-keepalive interval (seconds) for a configured +/// peer, so an established session survives NAT/firewall idle timeouts. +const PERSISTENT_KEEPALIVE: u16 = 25; + +/// Errors produced while configuring or running the WireGuard mesh. +#[derive(Debug, thiserror::Error)] +#[non_exhaustive] +pub enum WgError { + /// A base64 WireGuard key was malformed or not 32 bytes. + #[error("invalid WireGuard key: {0}")] + InvalidKey(String), + /// A CIDR string (`a.b.c.d/n`) for an advertised subnet or AllowedIPs entry + /// could not be parsed. + #[error("invalid CIDR {value:?}: {reason}")] + InvalidCidr { value: String, reason: String }, + /// Binding the mesh UDP listener failed. + #[error("binding WireGuard UDP listener on {addr}: {source}")] + Bind { + addr: SocketAddr, + #[source] + source: std::io::Error, + }, +} + +/// A 32-byte WireGuard public key, rendered as standard base64 (the format +/// `wg`/`wg-quick` use, so v1 manual key exchange is copy-paste compatible). +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct WgPublicKey([u8; 32]); + +impl WgPublicKey { + /// The raw key bytes. + #[must_use] + pub fn as_bytes(&self) -> &[u8; 32] { + &self.0 + } + + /// Standard-base64 rendering (44 characters). + #[must_use] + pub fn to_base64(&self) -> String { + base64::engine::general_purpose::STANDARD.encode(self.0) + } +} + +impl From for WgPublicKey { + fn from(pk: PublicKey) -> Self { + Self(pk.to_bytes()) + } +} + +impl From for PublicKey { + fn from(k: WgPublicKey) -> Self { + PublicKey::from(k.0) + } +} + +impl FromStr for WgPublicKey { + type Err = WgError; + + fn from_str(s: &str) -> Result { + let bytes = base64::engine::general_purpose::STANDARD + .decode(s.trim()) + .map_err(|e| WgError::InvalidKey(e.to_string()))?; + let arr: [u8; 32] = bytes + .try_into() + .map_err(|_| WgError::InvalidKey("expected 32 bytes".to_string()))?; + Ok(Self(arr)) + } +} + +impl fmt::Display for WgPublicKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.to_base64()) + } +} + +// A public key is not a secret, but its Debug is still the opaque base64 rather +// than 32 raw bytes — keeps logs readable and consistent with Display. +impl fmt::Debug for WgPublicKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "WgPublicKey({})", self.to_base64()) + } +} + +/// A WireGuard private key plus its derived public key. +/// +/// The [`StaticSecret`] is never logged, serialized through [`Debug`], or +/// exposed; only [`public`](Self::public) and an explicit +/// [`private_base64`](Self::private_base64) (for persisting a generated key to +/// a config the operator controls) leave the type. +pub struct Keypair { + secret: StaticSecret, + public: WgPublicKey, +} + +impl Keypair { + /// Generates a fresh keypair from the OS CSPRNG (R4.1 key generation). + #[must_use] + pub fn generate() -> Self { + let secret = StaticSecret::random_from_rng(rand_core::OsRng); + let public = WgPublicKey::from(PublicKey::from(&secret)); + Self { secret, public } + } + + /// Rebuilds a keypair from a base64-encoded private key (manual v1 key + /// exchange / config load). + /// + /// # Errors + /// + /// Returns [`WgError::InvalidKey`] if the input is not 32 base64 bytes. + pub fn from_base64(private: &str) -> Result { + let bytes = base64::engine::general_purpose::STANDARD + .decode(private.trim()) + .map_err(|e| WgError::InvalidKey(e.to_string()))?; + let arr: [u8; 32] = bytes + .try_into() + .map_err(|_| WgError::InvalidKey("expected 32 bytes".to_string()))?; + let secret = StaticSecret::from(arr); + let public = WgPublicKey::from(PublicKey::from(&secret)); + Ok(Self { secret, public }) + } + + /// This node's public key, the identity peers reference and the mesh-status + /// RPC reports. + #[must_use] + pub fn public(&self) -> WgPublicKey { + self.public + } + + /// The base64 private key, for persisting a freshly [generated](Self::generate) + /// key into operator-owned config. Never logged by this module. + #[must_use] + pub fn private_base64(&self) -> String { + base64::engine::general_purpose::STANDARD.encode(self.secret.to_bytes()) + } +} + +/// An IPv4 CIDR (`addr/prefix`) used for advertised subnets and per-peer +/// AllowedIPs. A `/32` names a single host; the mesh is IPv4-only (spec +/// non-goal: IPv6 within the switch). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Ipv4Cidr { + addr: Ipv4Addr, + prefix: u8, +} + +impl Ipv4Cidr { + /// Builds a CIDR, validating the prefix is `0..=32`. + /// + /// # Errors + /// + /// Returns [`WgError::InvalidCidr`] for a prefix above 32. + pub fn new(addr: Ipv4Addr, prefix: u8) -> Result { + if prefix > 32 { + return Err(WgError::InvalidCidr { + value: format!("{addr}/{prefix}"), + reason: "prefix must be 0..=32".to_string(), + }); + } + Ok(Self { addr, prefix }) + } + + fn mask(self) -> u32 { + if self.prefix == 0 { + 0 + } else { + u32::MAX << (32 - self.prefix) + } + } + + /// Whether `ip` falls within this CIDR — the AllowedIPs routing test. + #[must_use] + pub fn contains(self, ip: Ipv4Addr) -> bool { + u32::from(ip) & self.mask() == u32::from(self.addr) & self.mask() + } +} + +impl fmt::Display for Ipv4Cidr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}/{}", self.addr, self.prefix) + } +} + +impl FromStr for Ipv4Cidr { + type Err = WgError; + + fn from_str(s: &str) -> Result { + let invalid = |reason: &str| WgError::InvalidCidr { + value: s.to_string(), + reason: reason.to_string(), + }; + let (addr, prefix) = s + .split_once('/') + .ok_or_else(|| invalid("missing '/prefix'"))?; + let addr = addr + .parse::() + .map_err(|e| invalid(&e.to_string()))?; + let prefix = prefix.parse::().map_err(|e| invalid(&e.to_string()))?; + Ipv4Cidr::new(addr, prefix) + } +} + +/// A configured mesh peer (R4.1 — manual key exchange in v1). +#[derive(Debug, Clone)] +pub struct PeerConfig { + /// Human-readable peer name, reported in mesh status. Never sent over the + /// wire and never revealed to an unauthenticated caller (R4.5). + pub name: String, + /// The peer's WireGuard public key. + pub public_key: WgPublicKey, + /// The peer's UDP endpoint, if known. A peer with no endpoint can only be + /// reached after it initiates (roaming initiator). + pub endpoint: Option, + /// Subnets this peer routes for — its advertised switch subnet (and/or a + /// `/32` for the peer itself). Outbound packets are routed to the peer + /// whose AllowedIPs contain the destination. + pub allowed_ips: Vec, +} + +/// The full mesh configuration for this node. +pub struct MeshConfig { + /// This node's keypair. + pub keypair: Keypair, + /// UDP port the mesh listens on for WireGuard traffic. + pub listen_port: u16, + /// Subnets this node advertises to the mesh — typically its gvproxy switch + /// subnet (R4.1 subnet-router advertisement). + pub advertised_subnets: Vec, + /// The configured peers. + pub peers: Vec, +} + +impl MeshConfig { + /// Builds a config for a fresh node with a newly generated keypair. + #[must_use] + pub fn generate(listen_port: u16, advertised_subnets: Vec) -> Self { + Self { + keypair: Keypair::generate(), + listen_port, + advertised_subnets, + peers: Vec::new(), + } + } +} + +/// A point-in-time view of one peer for the mesh-status RPC. +#[derive(Debug, Clone)] +pub struct PeerSnapshot { + pub name: String, + pub public_key: WgPublicKey, + pub endpoint: Option, + /// Seconds since the last completed handshake, or `None` if no handshake + /// has completed yet. + pub last_handshake_secs: Option, +} + +/// A handle to a running mesh: read [status](Self::peers), [inject outbound +/// packets](Self::send_outbound), and keep the pump alive (dropping the handle +/// aborts the pump). +#[derive(Debug)] +pub struct MeshHandle { + own_public_key: WgPublicKey, + advertised_subnets: Vec, + outbound: mpsc::Sender>, + status: watch::Receiver>, + pump: JoinHandle<()>, +} + +impl MeshHandle { + /// This node's public key (the identity reported as `own_public_key`). + #[must_use] + pub fn own_public_key(&self) -> WgPublicKey { + self.own_public_key + } + + /// The subnets this node advertises to the mesh. + #[must_use] + pub fn advertised_subnets(&self) -> &[Ipv4Cidr] { + &self.advertised_subnets + } + + /// The current per-peer status (name, key, endpoint, last handshake). + #[must_use] + pub fn peers(&self) -> Vec { + self.status.borrow().clone() + } + + /// Whether the pump task is still running. The pump `break`s out of its + /// loop on a fatal socket error, after which the watch snapshot is frozen + /// and stale; callers (e.g. `GetMeshStatus`) use this to report the mesh as + /// no longer configured rather than serving dead peer state. + #[must_use] + pub fn is_alive(&self) -> bool { + !self.pump.is_finished() + } + + /// Queues an outbound IPv4 packet for encryption and routing to the peer + /// whose AllowedIPs contain its destination. A packet with no matching peer + /// is dropped by the pump. + /// + /// # Errors + /// + /// Returns the packet back if the pump has stopped. + pub fn send_outbound(&self, packet: Vec) -> Result<(), Vec> { + self.outbound.try_send(packet).map_err(|e| e.into_inner()) + } +} + +impl Drop for MeshHandle { + fn drop(&mut self) { + self.pump.abort(); + } +} + +/// One peer's live runtime state inside the pump. +struct PeerRuntime { + cfg: PeerConfig, + tunn: Tunn, + /// The endpoint we last heard from / send to. Learned from the source of an + /// inbound datagram when the config carried none. + endpoint: Option, + /// Whether a handshake has completed, so the transition is logged once. + handshook: bool, +} + +/// Starts the mesh: binds the UDP socket, builds a [`Tunn`] per peer, and +/// spawns the pump task. Decrypted inbound IP packets are delivered to +/// `tunnel_sink`; outbound packets are injected via [`MeshHandle::send_outbound`]. +/// +/// # Errors +/// +/// Returns [`WgError::Bind`] if the UDP listener cannot bind. +pub async fn start( + config: MeshConfig, + tunnel_sink: mpsc::Sender>, +) -> Result { + let bind_addr = SocketAddr::from((Ipv4Addr::UNSPECIFIED, config.listen_port)); + let socket = UdpSocket::bind(bind_addr) + .await + .map_err(|source| WgError::Bind { + addr: bind_addr, + source, + })?; + Ok(build_handle(config, Arc::new(socket), tunnel_sink)) +} + +/// Starts the mesh on a UDP socket that was already bound by the caller. +/// +/// Used when the socket must be created inside a specific network namespace — +/// the two-namespace UC7 integration test binds in each namespace (via `setns`) +/// before handing the socket here. The socket is switched to non-blocking and +/// adopted by the tokio reactor. +/// +/// # Errors +/// +/// Returns [`WgError::Bind`] if the socket cannot be made non-blocking or +/// adopted by the runtime. +pub fn start_with_socket( + config: MeshConfig, + socket: std::net::UdpSocket, + tunnel_sink: mpsc::Sender>, +) -> Result { + let addr = socket + .local_addr() + .unwrap_or_else(|_| SocketAddr::from((Ipv4Addr::UNSPECIFIED, config.listen_port))); + socket + .set_nonblocking(true) + .map_err(|source| WgError::Bind { addr, source })?; + let socket = UdpSocket::from_std(socket).map_err(|source| WgError::Bind { addr, source })?; + Ok(build_handle(config, Arc::new(socket), tunnel_sink)) +} + +/// Builds the peer runtimes, channels, and status watch, then spawns the pump. +/// Shared by [`start`] and [`start_with_socket`]. +fn build_handle( + config: MeshConfig, + socket: Arc, + tunnel_sink: mpsc::Sender>, +) -> MeshHandle { + let local_addr = socket.local_addr().ok(); + + let own_public_key = config.keypair.public(); + let advertised_subnets = config.advertised_subnets.clone(); + + // Build a Tunn per peer. The local index base must be unique per peer; a + // simple incrementing counter suffices (boringtun shifts it internally). + let peers: Vec = config + .peers + .into_iter() + .enumerate() + .map(|(i, cfg)| { + let tunn = Tunn::new( + StaticSecret::from(config.keypair.secret.to_bytes()), + PublicKey::from(cfg.public_key), + None, + Some(PERSISTENT_KEEPALIVE), + (i as u32) + 1, + None, + ); + PeerRuntime { + endpoint: cfg.endpoint, + cfg, + tunn, + handshook: false, + } + }) + .collect(); + + let (outbound_tx, outbound_rx) = mpsc::channel::>(256); + let (status_tx, status_rx) = watch::channel(initial_snapshot(&peers)); + + tracing::info!( + own_public_key = %own_public_key, + listen_port = config.listen_port, + advertised = advertised_subnets.len(), + peers = peers.len(), + "starting WireGuard mesh peer" + ); + + let pump = tokio::spawn(pump( + socket, + local_addr, + peers, + outbound_rx, + tunnel_sink, + status_tx, + )); + + MeshHandle { + own_public_key, + advertised_subnets, + outbound: outbound_tx, + status: status_rx, + pump, + } +} + +fn initial_snapshot(peers: &[PeerRuntime]) -> Vec { + peers + .iter() + .map(|p| PeerSnapshot { + name: p.cfg.name.clone(), + public_key: p.cfg.public_key, + endpoint: p.endpoint, + last_handshake_secs: None, + }) + .collect() +} + +/// The async pump: one task owns every [`Tunn`] and the single UDP socket, so +/// no `Tunn` is shared across tasks (it is `!Sync`-friendly this way). +async fn pump( + socket: Arc, + local_addr: Option, + mut peers: Vec, + mut outbound_rx: mpsc::Receiver>, + tunnel_sink: mpsc::Sender>, + status_tx: watch::Sender>, +) { + // Kick off a handshake with every peer that has a known endpoint, so the + // tunnel is warm before the first packet (R4.1 mesh join). + let mut buf = vec![0u8; MAX_DATAGRAM]; + for peer in &mut peers { + if let Some(endpoint) = peer.endpoint + && let TunnResult::WriteToNetwork(packet) = + peer.tunn.format_handshake_initiation(&mut buf, false) + { + let out = packet.to_vec(); + let _ = socket.send_to(&out, endpoint).await; + } + } + + let mut ticker = tokio::time::interval(TIMER_TICK); + let mut recv_buf = vec![0u8; MAX_DATAGRAM]; + + loop { + tokio::select! { + // Inbound encrypted datagrams from peers. + r = socket.recv_from(&mut recv_buf) => { + match r { + Ok((n, src)) => { + let datagram: Vec = recv_buf[..n].to_vec(); + handle_inbound(&socket, &mut peers, src, &datagram, &tunnel_sink).await; + publish_status(&mut peers, &status_tx); + } + Err(e) => { + tracing::warn!(error = %e, "WireGuard UDP receive failed; stopping mesh pump"); + break; + } + } + } + // Outbound plaintext IP packets to encrypt and route. + pkt = outbound_rx.recv() => { + match pkt { + Some(packet) => handle_outbound(&socket, &mut peers, &packet, local_addr).await, + None => break, // handle dropped + } + } + // Periodic per-peer timer servicing (keepalive, rekey, retransmit). + _ = ticker.tick() => { + service_timers(&socket, &mut peers).await; + publish_status(&mut peers, &status_tx); + } + } + } +} + +/// Routes one inbound datagram to its peer, drives `decapsulate` until the +/// network-write queue drains, and forwards any decrypted IP packet to the +/// tunnel sink. +async fn handle_inbound( + socket: &UdpSocket, + peers: &mut [PeerRuntime], + src: SocketAddr, + datagram: &[u8], + tunnel_sink: &mpsc::Sender>, +) { + // Try each candidate peer in priority order until one authenticates the + // datagram. boringtun returns `TunnResult::Err` when the datagram does not + // belong to a peer's session, so a clean `Err` on the first `decapsulate` + // is the signal to move to the next candidate; any other result means this + // peer owns the datagram and we drive it to completion. Probing this way — + // exact endpoint match first, then every endpoint-less peer — lets a + // roaming peer (new source address) and any of several endpoint-less peers + // still be matched, instead of stopping at the single first candidate. Each + // datagram is decapsulated exactly once for the owning peer (the prior + // `Err` probes leave no lasting session state and never consume it). + let mut buf = vec![0u8; MAX_DATAGRAM]; + for idx in route_candidates(peers, src) { + let mut result = peers[idx] + .tunn + .decapsulate(Some(src.ip()), datagram, &mut buf); + // The first probe of a non-owning peer returns `Err`: skip to the next + // candidate. (A decode error on the *owning* peer is handled in the + // drain loop below, where it is logged against the authenticated peer.) + if matches!(result, TunnResult::Err(_)) { + continue; + } + loop { + match result { + TunnResult::Done => break, + TunnResult::Err(e) => { + // An authenticated peer's decode error: log the peer name (it + // is already authenticated, so this leaks nothing new) and + // the error kind, but no addresses (R4.5). + tracing::warn!(peer = %peers[idx].cfg.name, error = ?e, "WireGuard decapsulation error"); + break; + } + TunnResult::WriteToNetwork(packet) => { + let out = packet.to_vec(); + // Learn/refresh the peer endpoint from where this arrived. + peers[idx].endpoint = Some(src); + let _ = socket.send_to(&out, src).await; + // Per boringtun: repeat decapsulate with an empty datagram + // until Done to flush any further queued network writes. + result = peers[idx].tunn.decapsulate(None, &[], &mut buf); + } + TunnResult::WriteToTunnelV4(packet, _src_ip) => { + let pkt = packet.to_vec(); + let _ = tunnel_sink.send(pkt).await; + break; + } + TunnResult::WriteToTunnelV6(_, _) => break, // IPv4-only mesh (spec non-goal) + } + } + return; + } + // No candidate authenticated this datagram. Log the bare source only — + // never a switch IP or PTask name (R4.5: no topology in auth failures). + tracing::warn!(remote = %src, "WireGuard datagram from unrecognised source dropped"); +} + +/// The ordered list of peer indices to try for an inbound datagram: the exact +/// endpoint match first (cheapest, the common case), then every endpoint-less +/// peer (each a possible roaming initiator whose endpoint was not +/// preconfigured). With v1 manual config and known endpoints the first entry +/// almost always authenticates. +fn route_candidates(peers: &[PeerRuntime], src: SocketAddr) -> Vec { + let mut candidates: Vec = Vec::new(); + if let Some(i) = peers.iter().position(|p| p.endpoint == Some(src)) { + candidates.push(i); + } + for (i, p) in peers.iter().enumerate() { + if p.endpoint.is_none() && !candidates.contains(&i) { + candidates.push(i); + } + } + candidates +} + +/// Encrypts one outbound IP packet and sends it to the peer whose AllowedIPs +/// contain the destination address (subnet-router routing, R4.2). +async fn handle_outbound( + socket: &UdpSocket, + peers: &mut [PeerRuntime], + packet: &[u8], + local_addr: Option, +) { + let Some(IpAddr::V4(dst)) = Tunn::dst_address(packet) else { + return; // non-IPv4 or unparsable: dropped (IPv4-only mesh) + }; + let Some(idx) = peers + .iter() + .position(|p| p.cfg.allowed_ips.iter().any(|c| c.contains(dst))) + else { + tracing::debug!(%dst, "no mesh peer advertises a route for destination; dropping"); + return; + }; + let Some(endpoint) = peers[idx].endpoint else { + tracing::debug!(peer = %peers[idx].cfg.name, "peer endpoint unknown; cannot send yet"); + return; + }; + // Don't loop a packet back to ourselves if a misconfig points a peer at our + // own listener. + if local_addr == Some(endpoint) { + return; + } + + let mut buf = vec![0u8; MAX_DATAGRAM]; + match peers[idx].tunn.encapsulate(packet, &mut buf) { + TunnResult::WriteToNetwork(out) => { + let out = out.to_vec(); + let _ = socket.send_to(&out, endpoint).await; + } + TunnResult::Done => {} + TunnResult::Err(e) => { + tracing::warn!(peer = %peers[idx].cfg.name, error = ?e, "WireGuard encapsulation error"); + } + _ => {} + } +} + +/// Services every peer's WireGuard timers, sending any keepalive/handshake the +/// timer state machine produces. +async fn service_timers(socket: &UdpSocket, peers: &mut [PeerRuntime]) { + let mut buf = vec![0u8; MAX_DATAGRAM]; + for peer in peers.iter_mut() { + if let TunnResult::WriteToNetwork(packet) = peer.tunn.update_timers(&mut buf) + && let Some(endpoint) = peer.endpoint + { + let out = packet.to_vec(); + let _ = socket.send_to(&out, endpoint).await; + } + } +} + +/// Rebuilds the status snapshot from live `Tunn` stats and publishes it, +/// logging the first completed handshake per peer (R4.1 peer-change events). +fn publish_status(peers: &mut [PeerRuntime], status_tx: &watch::Sender>) { + let snapshots = peers + .iter_mut() + .map(|peer| { + // stats().0 is the time since the last completed handshake. + let since = peer.tunn.stats().0; + if since.is_some() && !peer.handshook { + peer.handshook = true; + tracing::info!( + peer = %peer.cfg.name, + public_key = %peer.cfg.public_key, + "WireGuard handshake completed" + ); + } + PeerSnapshot { + name: peer.cfg.name.clone(), + public_key: peer.cfg.public_key, + endpoint: peer.endpoint, + last_handshake_secs: since.map(|d| d.as_secs()), + } + }) + .collect::>(); + // Ignore send error: a closed receiver means the handle was dropped and the + // pump is about to be aborted. + let _ = status_tx.send(snapshots); +} + +/// Builds the wire-level [`minimald_rpc::MeshStatus`] from a running mesh handle +/// (or the disabled status when there is no mesh). +#[must_use] +pub fn status_response(mesh: Option<&MeshHandle>) -> minimald_rpc::MeshStatus { + let Some(mesh) = mesh else { + return minimald_rpc::MeshStatus::unconfigured(); + }; + let peers = mesh + .peers() + .into_iter() + .map(|p| { + minimald_rpc::MeshPeerStatus::new( + p.name, + p.public_key.to_base64(), + p.endpoint.map(|e| e.to_string()), + p.last_handshake_secs, + ) + }) + .collect(); + minimald_rpc::MeshStatus::new( + mesh.own_public_key().to_base64(), + mesh.advertised_subnets() + .iter() + .map(ToString::to_string) + .collect(), + peers, + ) +} + +/// Builds a minimal, valid IPv4 packet with a UDP-ish payload for routing the +/// destination address through `dst_address`. Used by the data-plane tests and +/// by callers that need a probe packet; the payload is opaque to WireGuard. +#[must_use] +pub fn ipv4_test_packet(src: Ipv4Addr, dst: Ipv4Addr, payload: &[u8]) -> Vec { + let total_len = 20 + payload.len(); + let mut pkt = Vec::with_capacity(total_len); + pkt.push(0x45); // version 4, IHL 5 + pkt.push(0); // DSCP/ECN + pkt.extend_from_slice(&(total_len as u16).to_be_bytes()); + pkt.extend_from_slice(&[0, 0]); // identification + pkt.extend_from_slice(&[0x40, 0]); // flags: don't fragment + pkt.push(64); // TTL + pkt.push(17); // protocol: UDP + pkt.extend_from_slice(&[0, 0]); // header checksum (unchecked by boringtun) + pkt.extend_from_slice(&src.octets()); + pkt.extend_from_slice(&dst.octets()); + pkt.extend_from_slice(payload); + pkt +} + +#[cfg(test)] +mod tests { + use super::*; + use std::net::Ipv4Addr; + use tokio::time::{Duration, timeout}; + + #[test] + fn keypair_public_round_trips_through_base64() { + let kp = Keypair::generate(); + let b64 = kp.public().to_base64(); + let parsed: WgPublicKey = b64.parse().unwrap(); + assert_eq!(parsed, kp.public()); + assert_eq!(b64.len(), 44); // 32 bytes in standard base64 + } + + #[test] + fn keypair_from_base64_recovers_the_same_public_key() { + let kp = Keypair::generate(); + let recovered = Keypair::from_base64(&kp.private_base64()).unwrap(); + assert_eq!(recovered.public(), kp.public()); + } + + #[test] + fn invalid_public_key_is_rejected() { + assert!("not-base64!!".parse::().is_err()); + // Valid base64 but wrong length. + let short = base64::engine::general_purpose::STANDARD.encode([0u8; 16]); + assert!(short.parse::().is_err()); + } + + #[test] + fn cidr_contains_matches_only_addresses_in_range() { + let net: Ipv4Cidr = "100.64.0.0/16".parse().unwrap(); + assert!(net.contains(Ipv4Addr::new(100, 64, 0, 2))); + assert!(net.contains(Ipv4Addr::new(100, 64, 255, 254))); + assert!(!net.contains(Ipv4Addr::new(100, 65, 0, 1))); + let host: Ipv4Cidr = "10.0.0.5/32".parse().unwrap(); + assert!(host.contains(Ipv4Addr::new(10, 0, 0, 5))); + assert!(!host.contains(Ipv4Addr::new(10, 0, 0, 6))); + } + + #[test] + fn cidr_rejects_bad_prefix_and_shape() { + assert!("10.0.0.0/33".parse::().is_err()); + assert!("10.0.0.0".parse::().is_err()); + assert!("nope/8".parse::().is_err()); + } + + #[test] + fn disabled_status_reports_unconfigured() { + let s = status_response(None); + assert!(!s.configured); + assert!(s.own_public_key.is_none()); + assert!(s.peers.is_empty()); + } + + /// Brings up two meshes on loopback, each advertising a `/32` for a fake + /// switch host and pointing at the other, then drives a real WireGuard + /// handshake and relays one IP packet A→B through the encrypted tunnel. + /// + /// This exercises the full data plane this module owns — key exchange, + /// handshake, AllowedIPs routing, encapsulate/decapsulate — over real UDP, + /// and is the runnable counterpart to the `#[ignore]` two-namespace UC7 + /// integration test. + #[tokio::test] + async fn two_meshes_handshake_and_relay_a_packet() { + // Fake "switch" hosts each mesh advertises and routes for. + let a_switch = Ipv4Addr::new(100, 64, 0, 2); + let b_switch = Ipv4Addr::new(100, 65, 0, 2); + + let a_kp = Keypair::generate(); + let b_kp = Keypair::generate(); + let a_pub = a_kp.public(); + let b_pub = b_kp.public(); + + // Pre-bind two ephemeral loopback sockets so the test never collides with + // an occupied port or a parallel run; each peer needs the other's bound + // address up front, which `start_with_socket` lets us supply. + let a_sock = std::net::UdpSocket::bind("127.0.0.1:0").unwrap(); + let b_sock = std::net::UdpSocket::bind("127.0.0.1:0").unwrap(); + let a_port = a_sock.local_addr().unwrap().port(); + let b_port = b_sock.local_addr().unwrap().port(); + + let (a_sink_tx, mut a_sink_rx) = mpsc::channel::>(8); + let (b_sink_tx, mut b_sink_rx) = mpsc::channel::>(8); + + let a_cfg = MeshConfig { + keypair: a_kp, + listen_port: a_port, + advertised_subnets: vec![Ipv4Cidr::new(a_switch, 32).unwrap()], + peers: vec![PeerConfig { + name: "b".to_string(), + public_key: b_pub, + endpoint: Some(SocketAddr::from((Ipv4Addr::LOCALHOST, b_port))), + allowed_ips: vec![Ipv4Cidr::new(b_switch, 32).unwrap()], + }], + }; + let b_cfg = MeshConfig { + keypair: b_kp, + listen_port: b_port, + advertised_subnets: vec![Ipv4Cidr::new(b_switch, 32).unwrap()], + peers: vec![PeerConfig { + name: "a".to_string(), + public_key: a_pub, + endpoint: Some(SocketAddr::from((Ipv4Addr::LOCALHOST, a_port))), + allowed_ips: vec![Ipv4Cidr::new(a_switch, 32).unwrap()], + }], + }; + + let a = start_with_socket(a_cfg, a_sock, a_sink_tx).unwrap(); + let b = start_with_socket(b_cfg, b_sock, b_sink_tx).unwrap(); + + // Wait for the handshake to complete (status reports a last-handshake). + let handshook = timeout(Duration::from_secs(5), async { + loop { + if a.peers().iter().any(|p| p.last_handshake_secs.is_some()) + && b.peers().iter().any(|p| p.last_handshake_secs.is_some()) + { + break; + } + tokio::time::sleep(Duration::from_millis(50)).await; + } + }) + .await; + assert!(handshook.is_ok(), "mesh handshake did not complete in time"); + + // Send an IP packet from A destined to B's advertised switch host; it + // must arrive, decrypted, on B's tunnel sink. + let payload = b"uc7-remote-ptask"; + let packet = ipv4_test_packet(a_switch, b_switch, payload); + a.send_outbound(packet.clone()).unwrap(); + + let received = timeout(Duration::from_secs(5), b_sink_rx.recv()) + .await + .expect("timed out waiting for tunneled packet") + .expect("tunnel sink closed"); + assert_eq!( + &received, &packet, + "decrypted packet must match what was sent" + ); + + // Nothing should have leaked onto A's own sink. + assert!(a_sink_rx.try_recv().is_err()); + + // Status reflects the established peer. + let status = status_response(Some(&a)); + assert!(status.configured); + assert_eq!( + status.own_public_key.as_deref(), + Some(a_pub.to_base64()).as_deref() + ); + assert_eq!(status.peers.len(), 1); + assert_eq!(status.peers[0].name, "b"); + } +} diff --git a/crates/minimald/src/rpc.rs b/crates/minimald/src/rpc.rs index 6ce47091b8..bebcc79ddd 100644 --- a/crates/minimald/src/rpc.rs +++ b/crates/minimald/src/rpc.rs @@ -2,10 +2,10 @@ use minimald_rpc::IssueClientCertResponse; use minimald_rpc::{ CreateSession, CreateSessionResponse, DestroySession, DestroySessionResponse, Errorable, - GetSessionPolicy, GetSessionPolicyRequest, GetSessionRecord, GetSessionRecordRequest, - GetSessionRecordResponse, GetVersion, GetVersionResponse, IssueClientCert, - IssueClientCertRequest, ListSessions, ListSessionsEntry, ListSessionsResponse, OneshotSshRpc, - RPC_SUBSYSTEM_PREFIX, RenameSession, RenameSessionResponse, + GetMeshStatus, GetSessionPolicy, GetSessionPolicyRequest, GetSessionRecord, + GetSessionRecordRequest, GetSessionRecordResponse, GetVersion, GetVersionResponse, + IssueClientCert, IssueClientCertRequest, ListSessions, ListSessionsEntry, ListSessionsResponse, + OneshotSshRpc, RPC_SUBSYSTEM_PREFIX, RenameSession, RenameSessionResponse, }; use russh::{ Channel as RuChannel, ChannelId, @@ -271,6 +271,15 @@ async fn serve_issue_client_cert_unavailable(c: RuChannel) { } } +async fn serve_get_mesh_status(s: ServerStateHandle, c: RuChannel) { + let res = GetMeshStatus + .handle_channel(c, async |_req| Ok(s.mesh_status().await)) + .await; + if let Err(e) = res { + tracing::warn!("RPC handler for {} failed: {}", GetMeshStatus::NAME, e); + } +} + pub(crate) const STREAM_WORKSPACE_FILES: &str = constcat::concat!(RPC_SUBSYSTEM_PREFIX, "WorkspaceFilesTarZst"); @@ -346,6 +355,7 @@ pub async fn handle_ssh_rpc( | RenameSession::NAME | DestroySession::NAME | GetSessionPolicy::NAME + | GetMeshStatus::NAME | STREAM_WORKSPACE_FILES | IssueClientCert::NAME => { let mut conn_lock = c.lock().await; @@ -379,6 +389,7 @@ pub async fn handle_ssh_rpc( RenameSession::NAME => drop(spawn(serve_rename_session(s, channel))), DestroySession::NAME => drop(spawn(serve_destroy_session(s, channel))), GetSessionPolicy::NAME => drop(spawn(serve_get_session_policy(s, channel))), + GetMeshStatus::NAME => drop(spawn(serve_get_mesh_status(s, channel))), STREAM_WORKSPACE_FILES => drop(spawn(serve_stream_workspace_files(s, config, channel))), IssueClientCert::NAME => { #[cfg(feature = "networking-proxy")] @@ -857,4 +868,57 @@ mod tests { "expected an unknown-id error", ); } + + #[tokio::test] + async fn get_mesh_status_is_unconfigured_by_default() { + // Without a mesh installed, the RPC answers cleanly with `configured = + // false` rather than erroring (this is also the answer a daemon built + // without the `networking-wg` feature gives). + let server = TestServer::new().await; + let mut client = server.connect().await; + + let resp = client.call::(&()).await; + + assert!(!resp.configured); + assert!(resp.own_public_key.is_none()); + assert!(resp.peers.is_empty()); + } + + #[cfg(feature = "networking-wg")] + #[tokio::test] + async fn get_mesh_status_reports_own_key_and_peers() { + use crate::net::wg::{Keypair, MeshConfig, PeerConfig}; + + let server = TestServer::new().await; + + // Stand up a real mesh peer with one configured peer and install it. + let remote = Keypair::generate(); + let cfg = MeshConfig { + keypair: Keypair::generate(), + listen_port: 0, // ephemeral; no traffic is sent in this test + advertised_subnets: vec!["100.64.0.0/16".parse().unwrap()], + peers: vec![PeerConfig { + name: "remote".to_string(), + public_key: remote.public(), + endpoint: None, + allowed_ips: vec!["100.65.0.0/16".parse().unwrap()], + }], + }; + let (sink_tx, _sink_rx) = tokio::sync::mpsc::channel(1); + let mesh = std::sync::Arc::new(crate::net::wg::start(cfg, sink_tx).await.unwrap()); + let own_pub = mesh.own_public_key().to_base64(); + server.state.set_mesh(mesh).await; + + let mut client = server.connect().await; + let resp = client.call::(&()).await; + + assert!(resp.configured); + assert_eq!(resp.own_public_key.as_deref(), Some(own_pub.as_str())); + assert_eq!(resp.advertised_subnets, vec!["100.64.0.0/16".to_string()]); + assert_eq!(resp.peers.len(), 1); + assert_eq!(resp.peers[0].name, "remote"); + assert_eq!(resp.peers[0].public_key, remote.public().to_base64()); + // Keep the sink alive until the assertions complete. + drop(_sink_rx); + } } diff --git a/crates/minimald/src/server.rs b/crates/minimald/src/server.rs index fb7e7d8064..2151d36980 100644 --- a/crates/minimald/src/server.rs +++ b/crates/minimald/src/server.rs @@ -101,6 +101,12 @@ pub struct ServerState { /// restart. #[cfg(feature = "networking-proxy")] pub cert_authority: Arc, + + /// The running WireGuard mesh peer, when one is configured (Unit 4). Only + /// present under the `networking-wg` feature; the `GetMeshStatus` RPC reads + /// it through [`ServerStateHandle::mesh_status`]. + #[cfg(feature = "networking-wg")] + mesh: Option>, } impl ServerState { @@ -132,6 +138,8 @@ impl ServerState { host_key: None, #[cfg(feature = "networking-proxy")] cert_authority, + #[cfg(feature = "networking-wg")] + mesh: None, }) } } @@ -172,6 +180,35 @@ impl ServerStateHandle { pub async fn cert_authority(&self) -> Arc { Arc::clone(&self.0.lock().await.cert_authority) } + + /// Builds the current WireGuard mesh status for the `GetMeshStatus` RPC + /// (R4.6). On a build without the `networking-wg` feature, or with no mesh + /// configured, this reports `configured = false`. + pub async fn mesh_status(&self) -> minimald_rpc::MeshStatus { + #[cfg(feature = "networking-wg")] + { + let s = self.0.lock().await; + // A populated mesh slot is not proof the mesh is live: the pump can + // exit on a fatal socket error without clearing the slot, leaving a + // frozen, stale snapshot. Treat a dead pump as unconfigured so + // `GetMeshStatus` never advertises stale peer state. + match s.mesh.as_deref() { + Some(mesh) if mesh.is_alive() => crate::net::wg::status_response(Some(mesh)), + _ => minimald_rpc::MeshStatus::unconfigured(), + } + } + #[cfg(not(feature = "networking-wg"))] + { + minimald_rpc::MeshStatus::unconfigured() + } + } + + /// Installs a running mesh handle. Used by the daemon's mesh-join path and, + /// in tests, to stand up a configured mesh for the `GetMeshStatus` RPC. + #[cfg(feature = "networking-wg")] + pub async fn set_mesh(&self, mesh: Arc) { + self.0.lock().await.mesh = Some(mesh); + } } /// A transport that accepts byte-stream connections for the SSH server. diff --git a/crates/minimald/tests/mesh_uc7.rs b/crates/minimald/tests/mesh_uc7.rs new file mode 100644 index 0000000000..5f1f7d9d1a --- /dev/null +++ b/crates/minimald/tests/mesh_uc7.rs @@ -0,0 +1,221 @@ +//! UC7 — remote PTask-to-PTask over the WireGuard mesh tunnel (R4.2). +//! +//! This is proof artifact 1 of the Unit 4 task. It stands up two `minimald` +//! WireGuard peers in two separate Linux network namespaces joined by a veth +//! pair, completes a handshake via manual key exchange, and asserts that an IP +//! packet for one peer's advertised switch subnet, injected on the other peer, +//! crosses the encrypted tunnel between the namespaces and is decrypted on the +//! destination peer's tunnel sink. +//! +//! It is `#[ignore]` and gated on `MINIMALD_NETNS_TEST=1`: it needs root (to +//! create namespaces and a veth pair, and to `setns`), so it runs only in the +//! privileged netns CI lane (`ci-netns.yml`), never in the default +//! `cargo test` gate. The whole file compiles to nothing unless the +//! `networking-wg` feature is enabled. +//! +//! Scope note: this harness exercises the mesh tunnel data path end-to-end +//! across two real network namespaces — the transport that carries UC7. Wiring +//! the tunnel sink into a live gvproxy switch so a literal `TCP connect` between +//! two `minimald`-managed PTasks traverses it is the follow-up that completes +//! the full six-hop UC7 path (spec R4.2); it builds directly on the data path +//! asserted here. + +#![cfg(feature = "networking-wg")] + +use std::net::{Ipv4Addr, SocketAddr}; +use std::os::fd::AsRawFd; +use std::process::Command; +use std::time::Duration; + +use minimald::net::wg::{Ipv4Cidr, Keypair, MeshConfig, PeerConfig, start_with_socket}; +use tokio::sync::mpsc; + +/// The two namespaces and the veth addresses used to join them. +const NS_A: &str = "minimald_mesh_a"; +const NS_B: &str = "minimald_mesh_b"; +const VETH_A_IP: Ipv4Addr = Ipv4Addr::new(10, 99, 0, 1); +const VETH_B_IP: Ipv4Addr = Ipv4Addr::new(10, 99, 0, 2); +const WG_PORT: u16 = 51_820; + +/// Advertised "switch" subnets each side routes for (one `/32` host each). +const A_SWITCH: Ipv4Addr = Ipv4Addr::new(100, 64, 0, 2); +const B_SWITCH: Ipv4Addr = Ipv4Addr::new(100, 65, 0, 2); + +fn run(args: &[&str]) -> std::io::Result<()> { + let status = Command::new(args[0]).args(&args[1..]).status()?; + if !status.success() { + return Err(std::io::Error::other(format!( + "command failed ({status}): {}", + args.join(" ") + ))); + } + Ok(()) +} + +/// Creates the two namespaces and the veth pair joining them. +fn setup_namespaces() -> std::io::Result<()> { + run(&["ip", "netns", "add", NS_A])?; + run(&["ip", "netns", "add", NS_B])?; + run(&[ + "ip", "link", "add", "veth_a", "type", "veth", "peer", "name", "veth_b", + ])?; + run(&["ip", "link", "set", "veth_a", "netns", NS_A])?; + run(&["ip", "link", "set", "veth_b", "netns", NS_B])?; + run(&[ + "ip", + "netns", + "exec", + NS_A, + "ip", + "addr", + "add", + &format!("{VETH_A_IP}/24"), + "dev", + "veth_a", + ])?; + run(&[ + "ip", + "netns", + "exec", + NS_B, + "ip", + "addr", + "add", + &format!("{VETH_B_IP}/24"), + "dev", + "veth_b", + ])?; + run(&[ + "ip", "netns", "exec", NS_A, "ip", "link", "set", "veth_a", "up", + ])?; + run(&[ + "ip", "netns", "exec", NS_B, "ip", "link", "set", "veth_b", "up", + ])?; + run(&["ip", "netns", "exec", NS_A, "ip", "link", "set", "lo", "up"])?; + run(&["ip", "netns", "exec", NS_B, "ip", "link", "set", "lo", "up"])?; + Ok(()) +} + +fn teardown_namespaces() { + let _ = run(&["ip", "netns", "del", NS_A]); + let _ = run(&["ip", "netns", "del", NS_B]); +} + +/// Binds a UDP socket on `addr:port` inside the named network namespace by +/// `setns`-ing this thread to it for the duration of the bind, then restoring +/// the original namespace. Socket namespace membership is fixed at creation, so +/// the returned socket stays in the target namespace. +fn bind_in_netns(ns: &str, addr: SocketAddr) -> std::io::Result { + let original = std::fs::File::open("/proc/self/ns/net")?; + let target = std::fs::File::open(format!("/var/run/netns/{ns}"))?; + + // SAFETY: setns(fd, CLONE_NEWNET) moves the calling thread into the network + // namespace referenced by the open nsfs fd. Both fds are valid open files + // for the duration of these calls; the original is restored before return. + let enter = unsafe { libc::setns(target.as_raw_fd(), libc::CLONE_NEWNET) }; + if enter != 0 { + return Err(std::io::Error::last_os_error()); + } + let socket = std::net::UdpSocket::bind(addr); + // SAFETY: restore the original network namespace regardless of bind outcome. + let restored = unsafe { libc::setns(original.as_raw_fd(), libc::CLONE_NEWNET) }; + if restored != 0 { + return Err(std::io::Error::last_os_error()); + } + socket +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires root + two network namespaces; runs in the netns CI lane"] +async fn remote_ptask_packet_crosses_the_mesh_tunnel() { + if std::env::var("MINIMALD_NETNS_TEST").as_deref() != Ok("1") { + eprintln!("skipping: set MINIMALD_NETNS_TEST=1 (and run as root) to exercise UC7"); + return; + } + + teardown_namespaces(); // clear any leftovers from a prior run + + // RAII guard: any `expect` between setup and the explicit teardown below can + // panic, leaving root-created netns/veth state behind. The guard runs + // `teardown_namespaces()` on success, failure, and timeout paths alike. + struct NamespaceCleanup; + impl Drop for NamespaceCleanup { + fn drop(&mut self) { + teardown_namespaces(); + } + } + let _cleanup = NamespaceCleanup; + + setup_namespaces().expect("namespace + veth setup (needs root)"); + + let a_kp = Keypair::generate(); + let b_kp = Keypair::generate(); + let a_pub = a_kp.public(); + let b_pub = b_kp.public(); + + let a_sock = bind_in_netns(NS_A, SocketAddr::from((VETH_A_IP, WG_PORT))) + .expect("bind WireGuard socket in namespace A"); + let b_sock = bind_in_netns(NS_B, SocketAddr::from((VETH_B_IP, WG_PORT))) + .expect("bind WireGuard socket in namespace B"); + + let (a_sink_tx, _a_sink_rx) = mpsc::channel::>(8); + let (b_sink_tx, mut b_sink_rx) = mpsc::channel::>(8); + + // A advertises its switch host and points at B's veth endpoint; B mirrors. + let a_cfg = MeshConfig { + keypair: a_kp, + listen_port: WG_PORT, + advertised_subnets: vec![Ipv4Cidr::new(A_SWITCH, 32).unwrap()], + peers: vec![PeerConfig { + name: "b".to_string(), + public_key: b_pub, + endpoint: Some(SocketAddr::from((VETH_B_IP, WG_PORT))), + allowed_ips: vec![Ipv4Cidr::new(B_SWITCH, 32).unwrap()], + }], + }; + let b_cfg = MeshConfig { + keypair: b_kp, + listen_port: WG_PORT, + advertised_subnets: vec![Ipv4Cidr::new(B_SWITCH, 32).unwrap()], + peers: vec![PeerConfig { + name: "a".to_string(), + public_key: a_pub, + endpoint: Some(SocketAddr::from((VETH_A_IP, WG_PORT))), + allowed_ips: vec![Ipv4Cidr::new(A_SWITCH, 32).unwrap()], + }], + }; + + let a = start_with_socket(a_cfg, a_sock, a_sink_tx).expect("start mesh A"); + let b = start_with_socket(b_cfg, b_sock, b_sink_tx).expect("start mesh B"); + + // Wait for the handshake to complete across the veth. + let handshook = tokio::time::timeout(Duration::from_secs(10), async { + loop { + if a.peers().iter().any(|p| p.last_handshake_secs.is_some()) + && b.peers().iter().any(|p| p.last_handshake_secs.is_some()) + { + break; + } + tokio::time::sleep(Duration::from_millis(100)).await; + } + }) + .await; + + let result = if handshook.is_err() { + Err("handshake across namespaces did not complete".to_string()) + } else { + let packet = minimald::net::wg::ipv4_test_packet(A_SWITCH, B_SWITCH, b"uc7"); + a.send_outbound(packet.clone()) + .expect("queue outbound packet"); + match tokio::time::timeout(Duration::from_secs(10), b_sink_rx.recv()).await { + Ok(Some(got)) if got == packet => Ok(()), + Ok(Some(_)) => Err("tunneled packet did not match what was sent".to_string()), + Ok(None) => Err("tunnel sink closed before delivery".to_string()), + Err(_) => Err("timed out waiting for the tunneled packet".to_string()), + } + }; + + // `_cleanup` tears the namespaces down when it drops at end of scope — + // including the panic path of the assertion below. + result.expect("UC7 packet must cross the mesh tunnel"); +} diff --git a/crates/minvmd/src/net.rs b/crates/minvmd/src/net.rs index 08e6a2fd1f..288d1a03ae 100644 --- a/crates/minvmd/src/net.rs +++ b/crates/minvmd/src/net.rs @@ -613,12 +613,18 @@ impl GvproxySwitch { .await .is_err() { - if !already_claimed { - #[cfg(target_os = "linux")] - signal_via_pidfd(&self.pidfd, libc::SIGKILL, "SIGKILL"); - #[cfg(not(target_os = "linux"))] - signal_child(self.pid as libc::pid_t, libc::SIGKILL, "SIGKILL"); - } + // Always escalate to SIGKILL on timeout, even when another path + // already claimed teardown and sent SIGTERM. If the SIGKILL were + // gated on `!already_claimed`, a gvproxy that ignores SIGTERM would + // never be killed here and `supervisor.await` below would block + // daemon shutdown forever. On Linux the fd-based signal targets the + // exact process instance (ESRCH after exit is benign), so this never + // lands on a recycled PID — no more dangerous than the SIGTERM + // already sent. + #[cfg(target_os = "linux")] + signal_via_pidfd(&self.pidfd, libc::SIGKILL, "SIGKILL"); + #[cfg(not(target_os = "linux"))] + signal_child(self.pid as libc::pid_t, libc::SIGKILL, "SIGKILL"); let _ = supervisor.await; } }