69e3a9377d
A firewall sees an address and a port, never a hostname, so a road to photos.home.arpa compiled to home-server:443 and also opened git.home.arpa there. Rules are now per service: - devices.json services may carry their own mesh address (same rules as a device address, unique among all of them). A road to such a service compiles to that address only; the hosting device's plan lists it in the new field services[] so it answers on it, and DNS points there. - A road to a service that shares its address and a port with another service is refused (service.shared), naming the others, instead of opening both. Narrowing ports: to a port nobody else serves on that address is allowed. - A device: road reaches the whole device: its own address and its services' own addresses. No Ianitor identity check is needed for this. The home fixture gives photos its own address (100.96.1.2); tests that encoded the old host:port model are updated, three new snapshot fixtures cover the address rules, and the golden plans and pinned digest are regenerated. Mutation-checked: dropping the refusal, or ignoring the service address, each fails an F-01 test.
177 lines
5.4 KiB
Rust
177 lines
5.4 KiB
Rust
//! The compiled plan: what one device needs (docs/FORMATS.md, "The plan"). Field order is the wire order.
|
|
|
|
use crate::id::RoadId;
|
|
use crate::ids::{AppId, DeviceKey, Hostname};
|
|
use crate::net::{Addr, Net};
|
|
use crate::ports::Proto;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
pub const FORMAT: u32 = 1;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Plan {
|
|
pub format: u32,
|
|
pub device: DeviceKey,
|
|
pub name: String,
|
|
pub address: Addr,
|
|
/// The services this device hosts on an address of their own: it answers on these too (sorted by host).
|
|
pub services: Vec<ServiceAddress>,
|
|
/// This device is a classic WireGuard peer: not post-quantum.
|
|
pub classic: bool,
|
|
/// The earliest `until` of the roads this plan was built from, UTC: compile again by then.
|
|
pub expires: Option<String>,
|
|
pub peers: Vec<Peer>,
|
|
pub firewall: Firewall,
|
|
pub dns: Vec<Dns>,
|
|
pub exit: Option<Exit>,
|
|
pub kill_switch: Option<KillSwitch>,
|
|
pub direct: Vec<Direct>,
|
|
pub custom: Vec<Custom>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Peer {
|
|
pub key: DeviceKey,
|
|
pub name: String,
|
|
pub address: Addr,
|
|
/// A classic WireGuard peer: **not post-quantum**.
|
|
pub classic: bool,
|
|
pub allowed_ips: Vec<Net>,
|
|
/// A path hint (a relay or an exit) when every road to this peer names the same one.
|
|
pub via: Option<DeviceKey>,
|
|
pub roads: Vec<RoadId>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Firewall {
|
|
/// Always `deny`: only the rules below let traffic in or through.
|
|
pub default: Action,
|
|
pub rules: Vec<Rule>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "kebab-case")]
|
|
pub enum Action {
|
|
Deny,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
|
#[serde(rename_all = "kebab-case")]
|
|
pub enum Direction {
|
|
/// To this device's own address.
|
|
In,
|
|
/// Through this device: to a subnet it routes, or to the internet as an exit.
|
|
Forward,
|
|
}
|
|
|
|
/// Where a rule lets traffic go.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub enum Dst {
|
|
Net(Net),
|
|
/// Every address except the private ranges (`net::NOT_INTERNET`).
|
|
Internet,
|
|
}
|
|
|
|
impl Serialize for Dst {
|
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
|
match self {
|
|
Dst::Net(n) => n.serialize(s),
|
|
Dst::Internet => s.serialize_str("internet"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'de> Deserialize<'de> for Dst {
|
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
|
let s = String::deserialize(d)?;
|
|
if s == "internet" {
|
|
return Ok(Dst::Internet);
|
|
}
|
|
Net::parse_canonical(&s)
|
|
.map(Dst::Net)
|
|
.map_err(|_| serde::de::Error::custom(format!("`{s}` is not a network or `internet`")))
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Rule {
|
|
pub direction: Direction,
|
|
pub src: Net,
|
|
pub dst: Dst,
|
|
pub proto: Proto,
|
|
pub ports: Option<[u16; 2]>,
|
|
pub roads: Vec<RoadId>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct ServiceAddress {
|
|
pub host: Hostname,
|
|
pub address: Addr,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Dns {
|
|
pub host: Hostname,
|
|
pub address: Addr,
|
|
pub roads: Vec<RoadId>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Exit {
|
|
pub through: DeviceKey,
|
|
pub roads: Vec<RoadId>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct KillSwitch {
|
|
/// `None` when kill-switch roads disagree: nothing leaves but the mesh (fails closed).
|
|
pub through: Option<DeviceKey>,
|
|
pub roads: Vec<RoadId>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Direct {
|
|
/// `None`: all traffic to `subnet`; `Some`: only that app's.
|
|
pub app: Option<AppId>,
|
|
pub subnet: Net,
|
|
/// The nets inside `subnet` that this plan sends through the tunnel: they stay in it (QA F-02). A
|
|
/// platform that cannot keep them in (an app-level bypass, say) must not apply this entry at all.
|
|
pub except: Vec<Net>,
|
|
pub roads: Vec<RoadId>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Custom {
|
|
pub road: RoadId,
|
|
/// For a classic device (or a road that says so): not post-quantum.
|
|
pub classic: bool,
|
|
/// The raw block, byte for byte. **Never checked by the compiler.**
|
|
pub raw: String,
|
|
}
|
|
|
|
impl Plan {
|
|
/// Every allowed IP of every peer, with the peer.
|
|
pub fn allowed_ips(&self) -> impl Iterator<Item = (&DeviceKey, &Net)> {
|
|
self.peers
|
|
.iter()
|
|
.flat_map(|p| p.allowed_ips.iter().map(move |n| (&p.key, n)))
|
|
}
|
|
|
|
/// Pretty JSON, two spaces, fields in wire order, a final newline: the same input, the same bytes.
|
|
pub fn to_json(&self) -> String {
|
|
let mut s = serde_json::to_string_pretty(self).expect("a plan always serialises");
|
|
s.push('\n');
|
|
s
|
|
}
|
|
}
|