forge
Explore
Log in

Don't you dare go Hollow

fix RX pinning: dedicated monitor vif + STA down; iwlwifi chanctx never released on in-place type switch

Browse files at this commit

60df998f6eee3c24d21e05857a6fe9da4048ff05Ahmet Kaan GümüşParent: aaca50b
src/iface.rsmodified+52 -45
@@ -44,62 +44,69 @@ fn iface_exists(ifname: &str) -> bool {
44 .unwrap_or(false)44 .unwrap_or(false)
45 }45 }
46 46
47-/// Bring the link down, switch ifname to monitor mode, bring it up.47+/// Enter monitor mode by creating a DEDICATED monitor vif `<ifname>mon`
48+/// and bringing the original STA vif DOWN.
48 ///49 ///
49-/// On drivers that cannot switch type in place, falls back to creating a50+/// Why never switch type in place: on iwlwifi (verified on this lab box,
50-/// separate `<ifname>mon` monitor interface. Idempotent.51+/// AX-class) a STA vif that has ever been associated drags a firmware
52+/// channel-context with it into monitor type — `iw set channel`/`set freq`
53+/// succeed at the cfg80211 level, `iw dev info` shows the new channel, yet
54+/// the RX path demonstrably stays pinned to the old association channel
55+/// (only that AP's frames ever arrive). A freshly created monitor vif has
56+/// no such context and receives everything, with the STA vif merely down.
57+/// Bringing the STA vif down also releases the channel context, so the
58+/// original interface survives (no `iw dev del`).
51 ///59 ///
52-/// Returns the name of the interface that is now in monitor mode — either60+/// Idempotent. Returns the monitor vif name (`<ifname>mon`). Callers pass
53-/// `ifname` itself or the created `<ifname>mon`. Callers must pass the61+/// the returned name to channel-setting and sockets, and call
54-/// returned name to channel-setting, sockets and `to_managed`.62+/// [`teardown_monitor`] with the ORIGINAL ifname to restore.
55 pub fn to_monitor(ifname: &str) -> Result<String> {63 pub fn to_monitor(ifname: &str) -> Result<String> {
64+ let mon = monitor_name(ifname);
65+ // STA down first: releases any channel context the association held.
56 ip(&["link", "set", ifname, "down"])?;66 ip(&["link", "set", ifname, "down"])?;
57- match iw(&["dev", ifname, "set", "type", "monitor"]) {67+ if !iface_exists(&mon) {
58- Ok(_) => {68+ let phy = phy_of(ifname)?;
59- ip(&["link", "set", ifname, "up"])?;69+ iw(&[
60- Ok(ifname.to_string())70+ "phy", &phy, "interface", "add", &mon, "type", "monitor",
61- }71+ ])
62- Err(e) => {72+ .with_context(|| format!("adding monitor vif {mon} on {phy}"))?;
63- // Fallback: add a dedicated monitor vif named `<ifname>mon`.
64- let mon = format!("{ifname}mon");
65- iw(&[
66- "dev",
67- ifname,
68- "interface",
69- "add",
70- &mon,
71- "type",
72- "monitor",
73- ])
74- .with_context(|| format!("monitor fallback after: {e:#}"))?;
75- ip(&["link", "set", &mon, "up"])?;
76- // Bring the parent back up as well so the driver stays happy.
77- let _ = ip(&["link", "set", ifname, "up"]);
78- Ok(mon)
79- }
80 }73 }
74+ ip(&["link", "set", &mon, "up"])?;
75+ Ok(mon)
81 }76 }
82 77
83-/// Reverse `to_monitor`: when `ifname` is a `<base>mon` vif that `to_monitor`78+/// Reverse [`to_monitor`]: delete the monitor vif if present and bring the
84-/// created via interface-add (probed through `iw dev`: base present too),79+/// original interface back up (it never left managed type). Idempotent.
85-/// delete it; otherwise switch the interface itself back to managed mode.80+pub fn teardown_monitor(ifname: &str) -> Result<()> {
86-pub fn to_managed(ifname: &str) -> Result<()> {81+ let mon = monitor_name(ifname);
87- let added_vif = ifname82+ if iface_exists(&mon) {
88- .strip_suffix("mon")83+ let _ = ip(&["link", "set", &mon, "down"]);
89- .filter(|base| !base.is_empty())84+ iw(&["dev", &mon, "del"])?;
90- .map(|base| iface_exists(ifname) && iface_exists(base) && base != ifname)85+ }
91- .unwrap_or(false);86+ if iface_exists(ifname) {
92- if added_vif {87+ ip(&["link", "set", ifname, "up"])?;
93- iw(&["dev", ifname, "del"])?;
94- return Ok(());
95 }88 }
96- ip(&["link", "set", ifname, "down"])?;
97- iw(&["dev", ifname, "set", "type", "managed"])
98- .or_else(|_| iw(&["dev", ifname, "set", "type", "station"]))?;
99- ip(&["link", "set", ifname, "up"])?;
100 Ok(())89 Ok(())
101 }90 }
102 91
92+/// Monitor vif name convention — single source of truth.
93+pub fn monitor_name(ifname: &str) -> String {
94+ format!("{ifname}mon")
95+}
96+
97+/// Resolve the wiphy name (e.g. "phy0") that owns `ifname` via sysfs.
98+fn phy_of(ifname: &str) -> Result<String> {
99+ let link = format!("/sys/class/net/{ifname}/phy80211");
100+ let target = std::fs::read_link(&link)
101+ .with_context(|| format!("reading {link} — is {ifname} wireless?"))?;
102+ let name = target
103+ .file_name()
104+ .and_then(|n| n.to_str())
105+ .map(str::to_string)
106+ .with_context(|| format!("resolving phy name for {ifname}"))?;
107+ Ok(name)
108+}
109+
103 /// Tune the interface to channel `ch` (one retry — some drivers drop the110 /// Tune the interface to channel `ch` (one retry — some drivers drop the
104 /// first request when issued immediately after a mode switch).111 /// first request when issued immediately after a mode switch).
105 pub fn set_channel(ifname: &str, ch: u8) -> Result<()> {112 pub fn set_channel(ifname: &str, ch: u8) -> Result<()> {
src/service.rsmodified+10 -7
@@ -108,7 +108,7 @@ impl RadioService {
108 Err(e) => {108 Err(e) => {
109 log_err(&self.tx, format!("monitor mode on {ifname} failed: {e:#}"));109 log_err(&self.tx, format!("monitor mode on {ifname} failed: {e:#}"));
110 // Best effort: leave the box the way we found it.110 // Best effort: leave the box the way we found it.
111- let _ = iface::to_managed(ifname);111+ let _ = iface::teardown_monitor(ifname);
112 self.orig_ifname = None;112 self.orig_ifname = None;
113 return;113 return;
114 }114 }
@@ -142,8 +142,10 @@ impl RadioService {
142 log_err(&self.tx, format!("scanner: spawn failed: {e}"));142 log_err(&self.tx, format!("scanner: spawn failed: {e}"));
143 // Leave the UI consistent: nothing runs but the interface143 // Leave the UI consistent: nothing runs but the interface
144 // is in monitor mode — flip it back.144 // is in monitor mode — flip it back.
145- if let Some(mon) = self.mon_ifname.take() {145+ if self.mon_ifname.take().is_some() {
146- let _ = iface::to_managed(&mon);146+ if let Some(orig) = &self.orig_ifname {
147+ let _ = iface::teardown_monitor(orig);
148+ }
147 }149 }
148 }150 }
149 }151 }
@@ -157,12 +159,13 @@ impl RadioService {
157 for h in handles {159 for h in handles {
158 let _ = h.join();160 let _ = h.join();
159 }161 }
160- if let Some(mon) = self.mon_ifname.take() {162+ if let Some(orig) = self.orig_ifname.take() {
161- if let Err(e) = iface::to_managed(&mon) {163+ if self.mon_ifname.take().is_some() {
162- eprintln!("wifilab-svc: to_managed({mon}) failed: {e:#}");164+ if let Err(e) = iface::teardown_monitor(&orig) {
165+ eprintln!("wifilab-svc: teardown_monitor({orig}) failed: {e:#}");
166+ }
163 }167 }
164 }168 }
165- self.orig_ifname = None;
166 }169 }
167 170
168 /// Register a worker thread; on spawn failure the error is reported on171 /// Register a worker thread; on spawn failure the error is reported on