From 85502506087a9553eca92032f0c0150978187a6a Mon Sep 17 00:00:00 2001 From: sneurlax Date: Sun, 22 Nov 2026 12:48:36 -0600 Subject: [PATCH] feat(owly-radio): prefer tested, non-disruptive 5 GHz cards when station is unset --- crates/owly-radio/src/probe.rs | 346 ++++++++++++++++++++++++++++++++- 1 file changed, 340 insertions(+), 6 deletions(-) diff --git a/crates/owly-radio/src/probe.rs b/crates/owly-radio/src/probe.rs index 80a53d2..01be653 100644 --- a/crates/owly-radio/src/probe.rs +++ b/crates/owly-radio/src/probe.rs @@ -67,17 +67,27 @@ impl Station { } } -/// The station: the one `/etc/owly/radio.toml` names, else the first -/// managed Wi-Fi interface. Refuses a card the quirks table doesn't mark -/// tested unless allowed. +/// The station: the one `/etc/owly/radio.toml` names, else the best managed +/// Wi-Fi interface (preferring tested drivers, non-disruptive links such as an +/// unassociated adapter over an active AP station, and 5 GHz social-channel +/// support while preserving OS interface order on ties). +/// +/// Refuses a card the quirks table doesn't mark tested unless allowed. pub fn find_station(ops: &dyn Ops, settings: &Settings) -> Result { let ours = [AWDL_IFACE, MONITOR, IBSS, AP]; let name = match &settings.station { Some(s) if ops.exists(s) => Some(s.clone()), Some(_) => None, - None => ops.wifi_interfaces().into_iter().find(|i| { - !ours.contains(&i.as_str()) && ops.iface_type(i).as_deref() == Some("managed") - }), + None => { + let candidates: Vec = ops + .wifi_interfaces() + .into_iter() + .filter(|i| { + !ours.contains(&i.as_str()) && ops.iface_type(i).as_deref() == Some("managed") + }) + .collect(); + select_best_station(ops, settings, &candidates) + } }; let Some(name) = name else { return Err(missing( @@ -151,6 +161,52 @@ pub fn find_station(ops: &dyn Ops, settings: &Settings) -> Result Option { + if candidates.len() <= 1 { + return candidates.first().cloned(); + } + candidates + .iter() + .enumerate() + .max_by_key(|(idx, iface)| { + let driver = ops.driver(iface).unwrap_or_default(); + let device = ops.device_id(iface); + let (quirk, _) = settings.quirks.get(&driver, device.as_deref()); + let status_rank: u8 = match quirk.status { + Status::Tested => 2, + Status::Untested => 1, + Status::Broken => 0, + }; + let non_disruptive = match quirk.mode { + Mode::Shared => ops + .link_freq(iface) + .is_none_or(|f| social_channel(f).is_some()), + Mode::Exclusive | Mode::ActiveMonitor => ops.link_freq(iface).is_none(), + }; + let wiphy = ops.phy(iface).and_then(|p| ops.wiphy(&p)); + let order = settings.channels.as_ref().unwrap_or(&quirk.channels); + let has_5ghz_social = order.iter().any(|&c| { + (c == 44 || c == 149) + && social_freq(c).is_some_and(|f| { + wiphy + .as_ref() + .is_some_and(|w| w.channel(f).is_some_and(|ch| ch.may_transmit())) + }) + }); + ( + status_rank, + non_disruptive, + has_5ghz_social, + std::cmp::Reverse(*idx), + ) + }) + .map(|(_, iface)| iface.clone()) +} + /// A card's abilities, from nl80211. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Card { @@ -371,6 +427,7 @@ pub fn wrong_channel(freq: u32) -> String { #[cfg(test)] mod tests { use super::*; + use crate::quirks::Quirks; // spec: AWDL-11 #[test] @@ -384,4 +441,281 @@ mod tests { assert_eq!(social_freq(149), Some(5745)); assert_eq!(social_freq(36), None); } + + struct MockStation { + name: &'static str, + driver: &'static str, + device: Option<&'static str>, + phy: &'static str, + link_freq: Option, + wiphy: Option, + } + + struct MockOps { + stations: Vec, + } + + impl Ops for MockOps { + fn wifi_interfaces(&self) -> Vec { + self.stations.iter().map(|s| s.name.into()).collect() + } + fn iface_type(&self, iface: &str) -> Option { + self.exists(iface).then(|| "managed".into()) + } + fn driver(&self, iface: &str) -> Option { + self.stations + .iter() + .find(|s| s.name == iface) + .map(|s| s.driver.into()) + } + fn device_id(&self, iface: &str) -> Option { + self.stations + .iter() + .find(|s| s.name == iface) + .and_then(|s| s.device.map(str::to_string)) + } + fn phy(&self, iface: &str) -> Option { + self.stations + .iter() + .find(|s| s.name == iface) + .map(|s| s.phy.into()) + } + fn wiphy(&self, phy: &str) -> Option { + self.stations + .iter() + .find(|s| s.phy == phy) + .and_then(|s| s.wiphy.clone()) + } + fn mac(&self, _iface: &str) -> Option { + None + } + fn index(&self, _iface: &str) -> Option { + None + } + fn exists(&self, iface: &str) -> bool { + self.stations.iter().any(|s| s.name == iface) + } + fn link_freq(&self, iface: &str) -> Option { + self.stations + .iter() + .find(|s| s.name == iface) + .and_then(|s| s.link_freq) + } + fn gateway_reachable(&self, _iface: &str) -> bool { + false + } + fn regdom(&self) -> Option { + None + } + fn set_regdom(&self, _alpha2: &str) -> std::io::Result<()> { + Ok(()) + } + fn add_iface( + &self, + _phy: &str, + _name: &str, + _kind: crate::nl80211::Kind, + ) -> std::io::Result<()> { + Ok(()) + } + fn del_iface(&self, _name: &str) -> std::io::Result<()> { + Ok(()) + } + fn set_kind(&self, _iface: &str, _kind: crate::nl80211::Kind) -> std::io::Result<()> { + Ok(()) + } + fn set_mac(&self, _iface: &str, _mac: crate::radio::MacAddr) -> std::io::Result<()> { + Ok(()) + } + fn set_up(&self, _iface: &str, _up: bool) -> std::io::Result<()> { + Ok(()) + } + fn set_freq(&self, _iface: &str, _freq: u32) -> std::io::Result<()> { + Ok(()) + } + fn join_ibss(&self, _iface: &str, _freq: u32) -> std::io::Result<()> { + Ok(()) + } + fn leave_ibss(&self, _iface: &str) -> std::io::Result<()> { + Ok(()) + } + fn sysctl(&self, _key: &str, _value: &str) -> std::io::Result<()> { + Ok(()) + } + fn mt76_dir(&self, phy: &str) -> std::path::PathBuf { + std::path::PathBuf::from(phy) + } + fn read(&self, _path: &Path) -> Option { + None + } + fn write(&self, _path: &Path, _value: &str) -> std::io::Result<()> { + Ok(()) + } + fn set_managed(&self, _iface: &str, _managed: bool) {} + fn nm_connection(&self, _station: &str) -> Option { + None + } + fn connection_up(&self, _uuid: &str, _station: &str) -> std::io::Result<()> { + Ok(()) + } + fn reconnect(&self, _station: &str) {} + fn sleep(&self, _d: std::time::Duration) {} + } + + fn ch(freq: u32) -> crate::nl80211::Channel { + crate::nl80211::Channel { + freq, + disabled: false, + no_ir: false, + radar: false, + } + } + + fn settings_with(quirks: Quirks, station: Option<&str>) -> Settings { + Settings { + station: station.map(str::to_string), + allow_untested: false, + reconnect_after: None, + country: None, + channels: None, + aggregation: None, + tx_rate: None, + tx_amsdu: false, + hostapd: std::path::PathBuf::from("/usr/bin/hostapd"), + quirks, + run_dir: RunDir::new(Path::new("/tmp")), + } + } + + #[test] + fn prefers_unassociated_5ghz_usb_dongle_over_associated_onboard_card() { + let ops = MockOps { + stations: vec![ + MockStation { + name: "wlo1", + driver: "iwlwifi", + device: Some("8086:7af0"), + phy: "phy0", + link_freq: Some(5220), + wiphy: Some(Wiphy { + channels: vec![ch(2437), ch(5220), ch(5745)], + ..Wiphy::default() + }), + }, + MockStation { + name: "wlx9cefd5f73ae2", + driver: "mt76x2u", + device: Some("0e8d:7612"), + phy: "phy3", + link_freq: None, + wiphy: Some(Wiphy { + active_monitor: true, + channels: vec![ch(2437), ch(5220), ch(5745)], + ..Wiphy::default() + }), + }, + ], + }; + let quirks = Quirks::load(Path::new("/nonexistent-owly-overrides")).unwrap(); + let chosen = find_station(&ops, &settings_with(quirks.clone(), None)).unwrap(); + assert_eq!(chosen.name, "wlx9cefd5f73ae2"); + + // Explicit station override still wins unconditionally. + let forced = find_station(&ops, &settings_with(quirks, Some("wlo1"))).unwrap(); + assert_eq!(forced.name, "wlo1"); + } + + #[test] + fn prefers_tested_dongle_over_untested_or_broken_first_card() { + let quirks = Quirks::parse( + r#" + [drivers.broken_first] + status = "broken" + mode = "exclusive" + [drivers.mt76x2u] + status = "tested" + mode = "active-monitor" + channels = [149, 44, 6] + [drivers."*"] + status = "untested" + mode = "shared" + "#, + ) + .unwrap(); + let ops = MockOps { + stations: vec![ + MockStation { + name: "wlan0", + driver: "broken_first", + device: None, + phy: "phy0", + link_freq: None, + wiphy: Some(Wiphy { + channels: vec![ch(2437)], + ..Wiphy::default() + }), + }, + MockStation { + name: "wlan1", + driver: "mt76x2u", + device: Some("0e8d:7612"), + phy: "phy1", + link_freq: Some(5745), + wiphy: Some(Wiphy { + active_monitor: true, + channels: vec![ch(2437), ch(5745)], + ..Wiphy::default() + }), + }, + ], + }; + let chosen = find_station(&ops, &settings_with(quirks, None)).unwrap(); + assert_eq!(chosen.name, "wlan1"); + } + + #[test] + fn unreadable_wiphy_does_not_get_a_5ghz_bonus() { + let quirks = Quirks::parse( + r#" + [drivers.known] + status = "tested" + mode = "active-monitor" + channels = [149, 6] + [drivers.unreadable] + status = "tested" + mode = "active-monitor" + channels = [149, 6] + [drivers."*"] + status = "untested" + mode = "shared" + "#, + ) + .unwrap(); + let ops = MockOps { + stations: vec![ + MockStation { + name: "wlan0", + driver: "known", + device: None, + phy: "phy0", + link_freq: None, + wiphy: Some(Wiphy { + channels: vec![ch(2437)], + ..Wiphy::default() + }), + }, + MockStation { + name: "wlan1", + driver: "unreadable", + device: None, + phy: "phy1", + link_freq: None, + wiphy: None, + }, + ], + }; + + let chosen = find_station(&ops, &settings_with(quirks, None)).unwrap(); + assert_eq!(chosen.name, "wlan0"); + } }