Skip to content

A WiFi security auditing software

scan: retune a running capture in place instead of restarting it

Martin Olivier5d449f719b97
diff --git a/crates/agent/src/backend/scan.rs b/crates/agent/src/backend/scan.rs
index 41624c8..8a709e4 100644
--- a/crates/agent/src/backend/scan.rs
+++ b/crates/agent/src/backend/scan.rs
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
+use std::sync::Mutex;
use std::sync::MutexGuard;
use std::sync::atomic::{AtomicBool, Ordering};
@@ -23,7 +24,12 @@ pub fn is_scan_process() -> bool {
SCAN_HANDLE.lock().unwrap().is_some()
}
-/// Start (or restart) the native capture thread.
+/// Start the native capture thread, or retune one already running on `iface`.
+///
+/// The capture thread re-reads its channel plan every loop, so a settings change on
+/// the interface it is already scanning (enable a band, clear the channel filter, …)
+/// just swaps the plan in place — no thread teardown, no lost capture state. A change
+/// of interface, or a first start, spins a fresh thread.
pub fn set_scan_process(
iface: &str,
ghz_2_4: bool,
@@ -34,21 +40,39 @@ pub fn set_scan_process(
return Err(ScanError::NoBandSelected);
}
- stop_scan_process()?;
-
let channels = sniffer::build_channel_list(ghz_2_4, ghz_5, channel_filter.as_deref());
+ // Adapt a scan already running on this interface by swapping its channel plan.
+ let running = match SCAN_HANDLE.lock().unwrap().as_ref() {
+ Some(handle) if handle.iface == iface => Some(handle.channels.clone()),
+ _ => None,
+ };
+ if let Some(running) = running {
+ *running.lock().unwrap() = channels;
+ log::info!(
+ "scan updated: 2.4ghz: {ghz_2_4}, 5ghz: {ghz_5}, channel filter: {channel_filter:?}"
+ );
+ return Ok(());
+ }
+
+ // No compatible scan running — tear down any scan on another interface and start.
+ stop_scan_process()?;
+
let stop = Arc::new(AtomicBool::new(false));
+ let channels = Arc::new(Mutex::new(channels));
let thread_stop = stop.clone();
+ let thread_channels = channels.clone();
let thread_iface = iface.to_string();
let handle = std::thread::spawn(move || {
- sniffer::run(thread_iface, channels, thread_stop);
+ sniffer::run(thread_iface, thread_channels, thread_stop);
});
- SCAN_HANDLE
- .lock()
- .unwrap()
- .replace(ScanHandle { stop, handle });
+ SCAN_HANDLE.lock().unwrap().replace(ScanHandle {
+ iface: iface.to_string(),
+ channels,
+ stop,
+ handle,
+ });
log::info!(
"scan started: 2.4ghz: {ghz_2_4}, 5ghz: {ghz_5}, channel filter: {channel_filter:?}"
diff --git a/crates/agent/src/backend/sniffer.rs b/crates/agent/src/backend/sniffer.rs
index 4a222e3..4ffe5fb 100644
--- a/crates/agent/src/backend/sniffer.rs
+++ b/crates/agent/src/backend/sniffer.rs
@@ -23,6 +23,7 @@ use libwifi::frame::components::{DataHeader, ManagementHeader, RsnAkmSuite, Stat
use std::collections::HashMap;
use std::sync::Arc;
+use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
@@ -61,7 +62,10 @@ pub fn build_channel_list(ghz_2_4: bool, ghz_5: bool, filter: Option<&str>) -> V
}
/// Capture-thread body. Runs until `stop` is raised.
-pub fn run(iface: String, channels: Vec<u32>, stop: Arc<AtomicBool>) {
+///
+/// `channels` is shared and re-read every loop, so the GUI can change the bands or
+/// channel filter (and the scanner retunes) without the thread being restarted.
+pub fn run(iface: String, channels: Arc<Mutex<Vec<u32>>>, stop: Arc<AtomicBool>) {
let socket = match raw_socket::open(&iface) {
Ok(socket) => socket,
Err(e) => {
@@ -83,21 +87,29 @@ pub fn run(iface: String, channels: Vec<u32>, stop: Arc<AtomicBool>) {
}
};
- let hopping = channels.len() > 1;
let mut chan_idx = 0;
- let mut current_channel = channels.first().copied().unwrap_or(0);
- if let Some(&channel) = channels.first() {
+ let mut current_channel = 0;
+ let mut last_hop = Instant::now();
+
+ // Tune to the first channel up front.
+ if let Some(&channel) = channels.lock().unwrap().first() {
+ current_channel = channel;
set_channel(&iface, channel);
}
- let mut last_hop = Instant::now();
let mut buf = [0u8; 8192];
while !stop.load(Ordering::Relaxed) {
- if hopping && last_hop.elapsed() >= HOP_INTERVAL {
- chan_idx = (chan_idx + 1) % channels.len();
- current_channel = channels[chan_idx];
- set_channel(&iface, current_channel);
+ // Re-read the (possibly just-updated) plan and retune if needed. The lock is
+ // held only to decide; the slow `set_channel` runs without it.
+ let hop_due = last_hop.elapsed() >= HOP_INTERVAL;
+ let retune = {
+ let channels = channels.lock().unwrap();
+ plan_channel(&channels, &mut chan_idx, current_channel, hop_due)
+ };
+ if let Some(channel) = retune {
+ current_channel = channel;
+ set_channel(&iface, channel);
last_hop = Instant::now();
}
@@ -138,6 +150,39 @@ fn set_channel(iface: &str, channel: u32) -> bool {
.unwrap_or(false)
}
+/// Decide the next channel to tune to from the live plan and where we sit in it.
+///
+/// Returns `Some(channel)` when the card should retune (advancing `chan_idx` for a
+/// multi-channel plan), or `None` to stay put. Keeping the hop/park decision pure
+/// lets it be unit-tested without a radio, and lets [`run`] re-read a plan the GUI
+/// swapped under it — enabling a band or clearing the channel filter reshapes the
+/// scan in place instead of restarting the thread:
+/// * empty plan — nothing to tune to;
+/// * single channel — park on it, retuning only if we are off it;
+/// * many channels — advance to the next once the dwell time is up.
+///
+/// A stale `chan_idx` left over from a longer plan is folded back in bounds by the
+/// modulo, so shrinking the plan never indexes out of range.
+fn plan_channel(
+ channels: &[u32],
+ chan_idx: &mut usize,
+ current: u32,
+ hop_due: bool,
+) -> Option<u32> {
+ match channels.len() {
+ 0 => None,
+ 1 => (channels[0] != current).then_some(channels[0]),
+ len => {
+ if hop_due {
+ *chan_idx = (*chan_idx + 1) % len;
+ Some(channels[*chan_idx])
+ } else {
+ None
+ }
+ }
+ }
+}
+
// --- frame processing -------------------------------------------------------
/// Strip radiotap, decode the 802.11 frame, and fold it into the shared state.
@@ -424,3 +469,82 @@ fn is_unicast(mac: &str) -> bool {
fn timestamp() -> String {
chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn build_channel_list_from_bands() {
+ assert_eq!(build_channel_list(true, false, None), CHANNELS_2_4.to_vec());
+ assert_eq!(build_channel_list(false, true, None), CHANNELS_5.to_vec());
+ assert!(build_channel_list(false, false, None).is_empty());
+
+ let both = build_channel_list(true, true, None);
+ assert_eq!(both.len(), CHANNELS_2_4.len() + CHANNELS_5.len());
+ assert_eq!(both.first(), Some(&1));
+ }
+
+ #[test]
+ fn build_channel_list_filter_overrides_bands() {
+ // An explicit filter wins over the band toggles, and junk entries are dropped.
+ assert_eq!(
+ build_channel_list(true, true, Some("1,6,11")),
+ vec![1, 6, 11]
+ );
+ assert_eq!(
+ build_channel_list(true, false, Some("36,x,40")),
+ vec![36, 40]
+ );
+ assert!(build_channel_list(true, false, Some("")).is_empty());
+ }
+
+ #[test]
+ fn plan_channel_empty_stays_put() {
+ let mut idx = 0;
+ assert_eq!(plan_channel(&[], &mut idx, 0, true), None);
+ }
+
+ #[test]
+ fn plan_channel_parked_holds_when_on_channel() {
+ let mut idx = 0;
+ assert_eq!(plan_channel(&[6], &mut idx, 6, true), None);
+ assert_eq!(idx, 0);
+ }
+
+ #[test]
+ fn plan_channel_parked_retunes_when_off_channel() {
+ // Filter narrowed to a single channel while the card sits elsewhere.
+ let mut idx = 0;
+ assert_eq!(plan_channel(&[1], &mut idx, 6, false), Some(1));
+ }
+
+ #[test]
+ fn plan_channel_hopping_waits_for_dwell() {
+ let mut idx = 0;
+ assert_eq!(plan_channel(&[1, 6, 11], &mut idx, 1, false), None);
+ assert_eq!(idx, 0);
+ }
+
+ #[test]
+ fn plan_channel_hopping_advances_when_due() {
+ let mut idx = 0;
+ assert_eq!(plan_channel(&[1, 6, 11], &mut idx, 1, true), Some(6));
+ assert_eq!(idx, 1);
+ }
+
+ #[test]
+ fn plan_channel_hopping_wraps_around() {
+ let mut idx = 2;
+ assert_eq!(plan_channel(&[1, 6, 11], &mut idx, 11, true), Some(1));
+ assert_eq!(idx, 0);
+ }
+
+ #[test]
+ fn plan_channel_stale_index_wraps_into_bounds() {
+ // The plan shrank under a stale index; the modulo keeps the access valid.
+ let mut idx = 5;
+ assert_eq!(plan_channel(&[1, 6, 11], &mut idx, 6, true), Some(1));
+ assert_eq!(idx, 0);
+ }
+}
diff --git a/crates/agent/src/globals.rs b/crates/agent/src/globals.rs
index 07372bb..25c5f0f 100644
--- a/crates/agent/src/globals.rs
+++ b/crates/agent/src/globals.rs
@@ -20,10 +20,16 @@ pub static CAPTURE_DIR: &str = "/var/lib/airgorah";
pub static LIVE_SCAN_PATH: &str = "/var/lib/airgorah/live_scan";
pub static OLD_SCAN_PATH: &str = "/var/lib/airgorah/old_scan";
-/// Handle to the running native capture thread. `stop` is raised to ask the
-/// thread to exit; `handle` is joined to wait for it to finish flushing the
-/// capture file. Replaces the old `airodump-ng` child process.
+/// Handle to the running native capture thread.
+///
+/// `channels` is the live channel plan the thread re-reads each loop, so swapping
+/// it retunes a running scan without restarting the thread; `iface` identifies the
+/// interface the scan runs on, so a request for the same one can adapt it in place.
+/// `stop` is raised to ask the thread to exit; `handle` is joined to wait for it to
+/// finish flushing the capture file.
pub struct ScanHandle {
+ pub iface: String,
+ pub channels: Arc<Mutex<Vec<u32>>>,
pub stop: Arc<AtomicBool>,
pub handle: JoinHandle<()>,
}