openwrt fixes

This commit is contained in:
ari committed 2026-08-27 12:09:26 -04:00
1 parent 027552a3db
commit c1c17ec8e6
12 files changed
+207 -82

No files matched your search

+27
View File
@@ -38,6 +38,9 @@ pub struct Device {
pub domains: Option<Vec<String>>,
#[serde(rename = "justMac", default)]
pub just_mac: bool,
/// Restrict this single device to internet-only access (matched by MAC).
#[serde(rename = "internetOnly", default)]
pub internet_only: bool,
}
#[cfg(test)]
@@ -99,4 +102,28 @@ mod tests {
let config: Config = serde_json::from_str(json).unwrap();
assert!(config.internet_only.is_empty());
}
#[test]
fn test_device_internet_only_flag() {
let json = r#"{
"generalMappings": [],
"networks": {
"lab": [{"mac": "aa:bb:cc:dd:ee:ff", "name": "printer", "internetOnly": true}]
}
}"#;
let config: Config = serde_json::from_str(json).unwrap();
assert!(config.networks["lab"][0].internet_only);
}
#[test]
fn test_device_internet_only_defaults_false() {
let json = r#"{
"generalMappings": [],
"networks": {
"lab": [{"mac": "aa:bb:cc:dd:ee:ff", "name": "printer"}]
}
}"#;
let config: Config = serde_json::from_str(json).unwrap();
assert!(!config.networks["lab"][0].internet_only);
}
}
@@ -59,6 +59,7 @@ mod tests {
id: None,
domains,
just_mac: false,
internet_only: false,
}]);
Config { general_mappings: vec![], networks, dns_resolvers: vec![], internet_only: vec![] }
}
@@ -39,6 +39,7 @@ mod tests {
id: None,
domains: None,
just_mac,
internet_only: false,
}]);
Config { general_mappings: vec![], networks, dns_resolvers: vec![], internet_only: vec![] }
}
@@ -1,17 +1,18 @@
use crate::config::Config;
use crate::network::{prefix_for, process_network, ULA_PREFIX};
/// Renders UCI firewall rules (in `uci batch` format) that restrict the networks
/// listed in `config.internet_only` to internet-only access.
/// Renders UCI firewall rules (in `uci batch` format) that restrict devices to
/// internet-only access.
///
/// For each restricted network:
/// - IPv4: a single DROP rule matching the whole subnet (catches all devices on it).
/// - IPv6: a DROP rule per known device MAC (all subnets share one /64, so subnets
/// are not distinguishable in IPv6).
/// Two sources of restriction:
/// - **Network-level** (`config.internet_only`): for each restricted network,
/// - IPv4: a single DROP rule matching the whole subnet (catches all devices on it).
/// - IPv6: a DROP rule per known device MAC (all subnets share one /64, so subnets
/// are not distinguishable in IPv6).
/// - **Device-level** (a device with `internetOnly: true`): IPv4 + IPv6 DROP rules
/// matched by that device's MAC, on any network.
///
/// Rules are named `skyg_<network>_drop_lan[_v6]` so the deploy script can manage
/// exactly the rules it owns. They are inserted before port-specific ACCEPT rules
/// (e.g. the existing `sambasharelan`) so the DROPs win.
/// Rules are named `skyg_*` so the deploy script can manage exactly the rules it owns.
pub fn generate(config: &Config) -> String {
let mut out = String::new();
out.push_str("# Managed by skyg openwrt — do not edit by hand\n");
@@ -56,9 +57,49 @@ pub fn generate(config: &Config) -> String {
}
}
// Device-level: any device flagged internetOnly, regardless of network.
for (network, devices) in &config.networks {
for pd in process_network(network, devices) {
if !pd.device.internet_only {
continue;
}
let slug = mac_slug(&pd.device.mac);
out.push_str(&format!(
"config rule\n\
\toption name 'skyg_mac_{slug}_drop_lan'\n\
\toption src 'lan'\n\
\toption dest 'lan'\n\
\toption src_mac '{mac}'\n\
\toption dest_ip '10.0.0.0/16'\n\
\toption target 'DROP'\n\
\toption family 'ipv4'\n",
slug = slug,
mac = pd.device.mac,
));
out.push_str(&format!(
"config rule\n\
\toption name 'skyg_mac_{slug}_drop_lan_v6'\n\
\toption src 'lan'\n\
\toption dest 'lan'\n\
\toption src_mac '{mac}'\n\
\toption dest_ip '{ula}'\n\
\toption target 'DROP'\n\
\toption family 'ipv6'\n",
slug = slug,
mac = pd.device.mac,
ula = ULA_PREFIX,
));
}
}
out
}
/// MAC address without separators, for use in a UCI rule name.
fn mac_slug(mac: &str) -> String {
mac.replace(':', "")
}
#[cfg(test)]
mod tests {
use super::*;
@@ -76,6 +117,7 @@ mod tests {
id: None,
domains: None,
just_mac: false,
internet_only: false,
},
Device {
mac: "aa:bb:cc:dd:ee:02".to_string(),
@@ -83,6 +125,7 @@ mod tests {
id: None,
domains: None,
just_mac: false,
internet_only: false,
},
],
);
@@ -129,4 +172,35 @@ mod tests {
let result = generate(&config);
assert!(result.lines().count() == 1); // only the header comment
}
#[test]
fn test_device_level_internet_only() {
let mut config = config_with_devices(vec![]);
// Flag one device on a non-restricted network as internet-only.
config
.networks
.entry("lab".to_string())
.or_insert_with(Vec::new)
.push(Device {
mac: "11:22:33:44:55:66".to_string(),
name: "printer".to_string(),
id: None,
domains: None,
just_mac: false,
internet_only: true,
});
let result = generate(&config);
assert!(result.contains("skyg_mac_112233445566_drop_lan"));
assert!(result.contains("skyg_mac_112233445566_drop_lan_v6"));
assert!(result.contains("option src_mac '11:22:33:44:55:66'"));
assert!(result.contains("option dest_ip '10.0.0.0/16'"));
assert!(result.contains("option dest_ip 'fd59:de0a:bff5::/48'"));
// No subnet rules were generated (no network-level internetOnly).
assert!(!result.contains("skyg_lab_drop_lan\n"));
}
#[test]
fn test_mac_slug_strips_colons() {
assert_eq!(mac_slug("aa:bb:cc:dd:ee:ff"), "aabbccddeeff");
}
}
+1
View File
@@ -71,6 +71,7 @@ mod tests {
id,
domains: None,
just_mac,
internet_only: false,
}
}