From 60df998f6eee3c24d21e05857a6fe9da4048ff05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 12 Oct 2026 20:53:26 +0300 Subject: [PATCH] fix RX pinning: dedicated monitor vif + STA down; iwlwifi chanctx never released on in-place type switch --- src/iface.rs | 97 +++++++++++++++++++++++++++----------------------- src/service.rs | 17 +++++---- 2 files changed, 62 insertions(+), 52 deletions(-) diff --git a/src/iface.rs b/src/iface.rs index 6992c3f..91942c5 100644 --- a/src/iface.rs +++ b/src/iface.rs @@ -44,62 +44,69 @@ fn iface_exists(ifname: &str) -> bool { .unwrap_or(false) } -/// Bring the link down, switch ifname to monitor mode, bring it up. +/// Enter monitor mode by creating a DEDICATED monitor vif `mon` +/// and bringing the original STA vif DOWN. /// -/// On drivers that cannot switch type in place, falls back to creating a -/// separate `mon` monitor interface. Idempotent. +/// Why never switch type in place: on iwlwifi (verified on this lab box, +/// AX-class) a STA vif that has ever been associated drags a firmware +/// channel-context with it into monitor type — `iw set channel`/`set freq` +/// succeed at the cfg80211 level, `iw dev info` shows the new channel, yet +/// the RX path demonstrably stays pinned to the old association channel +/// (only that AP's frames ever arrive). A freshly created monitor vif has +/// no such context and receives everything, with the STA vif merely down. +/// Bringing the STA vif down also releases the channel context, so the +/// original interface survives (no `iw dev del`). /// -/// Returns the name of the interface that is now in monitor mode — either -/// `ifname` itself or the created `mon`. Callers must pass the -/// returned name to channel-setting, sockets and `to_managed`. +/// Idempotent. Returns the monitor vif name (`mon`). Callers pass +/// the returned name to channel-setting and sockets, and call +/// [`teardown_monitor`] with the ORIGINAL ifname to restore. pub fn to_monitor(ifname: &str) -> Result { + let mon = monitor_name(ifname); + // STA down first: releases any channel context the association held. ip(&["link", "set", ifname, "down"])?; - match iw(&["dev", ifname, "set", "type", "monitor"]) { - Ok(_) => { - ip(&["link", "set", ifname, "up"])?; - Ok(ifname.to_string()) - } - Err(e) => { - // Fallback: add a dedicated monitor vif named `mon`. - let mon = format!("{ifname}mon"); - iw(&[ - "dev", - ifname, - "interface", - "add", - &mon, - "type", - "monitor", - ]) - .with_context(|| format!("monitor fallback after: {e:#}"))?; - ip(&["link", "set", &mon, "up"])?; - // Bring the parent back up as well so the driver stays happy. - let _ = ip(&["link", "set", ifname, "up"]); - Ok(mon) - } + if !iface_exists(&mon) { + let phy = phy_of(ifname)?; + iw(&[ + "phy", &phy, "interface", "add", &mon, "type", "monitor", + ]) + .with_context(|| format!("adding monitor vif {mon} on {phy}"))?; } + ip(&["link", "set", &mon, "up"])?; + Ok(mon) } -/// Reverse `to_monitor`: when `ifname` is a `mon` vif that `to_monitor` -/// created via interface-add (probed through `iw dev`: base present too), -/// delete it; otherwise switch the interface itself back to managed mode. -pub fn to_managed(ifname: &str) -> Result<()> { - let added_vif = ifname - .strip_suffix("mon") - .filter(|base| !base.is_empty()) - .map(|base| iface_exists(ifname) && iface_exists(base) && base != ifname) - .unwrap_or(false); - if added_vif { - iw(&["dev", ifname, "del"])?; - return Ok(()); +/// Reverse [`to_monitor`]: delete the monitor vif if present and bring the +/// original interface back up (it never left managed type). Idempotent. +pub fn teardown_monitor(ifname: &str) -> Result<()> { + let mon = monitor_name(ifname); + if iface_exists(&mon) { + let _ = ip(&["link", "set", &mon, "down"]); + iw(&["dev", &mon, "del"])?; + } + if iface_exists(ifname) { + ip(&["link", "set", ifname, "up"])?; } - ip(&["link", "set", ifname, "down"])?; - iw(&["dev", ifname, "set", "type", "managed"]) - .or_else(|_| iw(&["dev", ifname, "set", "type", "station"]))?; - ip(&["link", "set", ifname, "up"])?; Ok(()) } +/// Monitor vif name convention — single source of truth. +pub fn monitor_name(ifname: &str) -> String { + format!("{ifname}mon") +} + +/// Resolve the wiphy name (e.g. "phy0") that owns `ifname` via sysfs. +fn phy_of(ifname: &str) -> Result { + let link = format!("/sys/class/net/{ifname}/phy80211"); + let target = std::fs::read_link(&link) + .with_context(|| format!("reading {link} — is {ifname} wireless?"))?; + let name = target + .file_name() + .and_then(|n| n.to_str()) + .map(str::to_string) + .with_context(|| format!("resolving phy name for {ifname}"))?; + Ok(name) +} + /// Tune the interface to channel `ch` (one retry — some drivers drop the /// first request when issued immediately after a mode switch). pub fn set_channel(ifname: &str, ch: u8) -> Result<()> { diff --git a/src/service.rs b/src/service.rs index f4102e2..d1976c1 100644 --- a/src/service.rs +++ b/src/service.rs @@ -108,7 +108,7 @@ impl RadioService { Err(e) => { log_err(&self.tx, format!("monitor mode on {ifname} failed: {e:#}")); // Best effort: leave the box the way we found it. - let _ = iface::to_managed(ifname); + let _ = iface::teardown_monitor(ifname); self.orig_ifname = None; return; } @@ -142,8 +142,10 @@ impl RadioService { log_err(&self.tx, format!("scanner: spawn failed: {e}")); // Leave the UI consistent: nothing runs but the interface // is in monitor mode — flip it back. - if let Some(mon) = self.mon_ifname.take() { - let _ = iface::to_managed(&mon); + if self.mon_ifname.take().is_some() { + if let Some(orig) = &self.orig_ifname { + let _ = iface::teardown_monitor(orig); + } } } } @@ -157,12 +159,13 @@ impl RadioService { for h in handles { let _ = h.join(); } - if let Some(mon) = self.mon_ifname.take() { - if let Err(e) = iface::to_managed(&mon) { - eprintln!("wifilab-svc: to_managed({mon}) failed: {e:#}"); + if let Some(orig) = self.orig_ifname.take() { + if self.mon_ifname.take().is_some() { + if let Err(e) = iface::teardown_monitor(&orig) { + eprintln!("wifilab-svc: teardown_monitor({orig}) failed: {e:#}"); + } } } - self.orig_ifname = None; } /// Register a worker thread; on spawn failure the error is reported on