Add decision step for channel widths and the process used to validate them

This commit is contained in:
iamdoubz committed 2026-06-10 20:38:24 -05:00
1 parent f51e3e8785
commit 92f0ab2c38
1 file changed
+130
+130
View File
@@ -72,6 +72,23 @@ Each OS uses the most accurate available API, with graceful fallback:
`scan_averaged(samples=10, delay=0.1)` takes the **median** (not mean) of 10
samples at 100 ms intervals. The median is more robust against RF burst spikes.
**Beacon IE parsing (Windows wlanapi).** The Windows scan list under-reports:
`dot11BssPhyType` caps at HE (10) even for Wi-Fi 7, channel width is never
exposed, and security is only available keyed by SSID (so hidden networks miss
it). To fix all three, the wlanapi backend snapshots each BSS entry's raw
802.11 Information Elements and parses them directly:
| Helper | Reads | Produces |
|---|---|---|
| `_mode_from_ies()` | EHT/HE/VHT/HT **Capabilities** elements | Wi-Fi 4/5/6/6E/7 mode |
| `_security_from_ies()` | RSN (48) + WPA1 vendor (221) elements | WPA2/WPA3/Enterprise/OWE |
| `_width_from_ies()` | EHT/HE/VHT/HT **Operation** elements | 20/40/80/160/320 MHz |
The IE bytes are read once per scan via `ctypes.string_at(base, dwTotalSize)`
(a bounded copy), and every field access is a length-guarded slice of that
`bytes` object — a malformed IE yields a blank result, never an out-of-bounds
read. See Decisions #16–#20.
### `data.py` — Session model
Key methods:
@@ -171,6 +188,10 @@ as the length and reading from offset 1. This is wrong — `uSSIDLength` is a
4-byte ULONG, so `ucSSID` starts at offset 4. The misread caused SSIDs to be
truncated (e.g. "NETGEAR22" → "NETGEA").
Mode/security/width are NOT taken from the scan list's struct fields (which
under-report — see Decisions #16–#20); they are parsed from each entry's raw
beacon IEs instead.
### Decision #7 — iw instead of nmcli on Linux
`nmcli` reads from NetworkManager's scan cache with its own smoothing.
@@ -248,6 +269,68 @@ because the device is never far from a strong signal. The solution is to select
individual BSSIDs (one per radio) on the Access Points tab and map each node
separately, or temporarily disable other nodes during a single-radio session.
### Decision #16 — Wi-Fi 6E / 7 and the 6 GHz band
Mode detection covers `n/ac/ax/ax(6E)/be` (Wi-Fi 4 → 7). The band is derived
from **frequency**, not channel number, because 6 GHz channel numbers overlap
with 2.4/5 GHz ones — `_band_from_freq()` maps 5925–7125 MHz → "6 GHz" and is
preferred over `_band_from_channel()` whenever a frequency is known. Channel
width adds **320 MHz** (Wi-Fi 7 only).
### Decision #17 — Mode comes from beacon IEs, not `dot11BssPhyType`
The Windows `WlanGetNetworkBssList` `dot11BssPhyType` field **caps at HE (10)
and never reports EHT (11)**, even for 802.11be APs — only the connected-
association path (`netsh wlan show interfaces`) surfaces `802.11be`. So Wi-Fi 7
is detectable from a scan *only* by parsing the **EHT Capabilities** element
(Element ID 255, Ext ID 108). `_mode_from_ies()` checks EHT(108) → Wi-Fi 7,
HE(35) [+ 6 GHz freq or HE-6GHz-band-cap (59)] → Wi-Fi 6E/6, VHT(191) → 5,
HT(45) → 4. The `dot11BssPhyType` map remains only as a fallback — and its
enum was corrected (7=HT/n, 8=VHT/ac, 9=DMG, 10=HE/ax, 11=EHT/be; the old map
was off by one for 7/8/9).
### Decision #18 — `WLAN_BSS_ENTRY` struct must be byte-exact for IE access
The original ctypes `WLAN_BSS_ENTRY` had `bInRegDomain` as `BOOL` (4 bytes; it
is `BOOLEAN` = 1 byte) and `wlanRateSet` as `c_ubyte * 16` (it is
`WLAN_RATE_SET` = **256 bytes**). Those fields precede `ulIeOffset`/`ulIeSize`,
so both were read 240+ bytes off — pointing at garbage. This is why an early
attempt to read IEs via `from_address()` **crashed the app**. Fixed layout:
`sizeof = 360`, `ulIeOffset` at offset 352. (RSSI/freq/phy sit *before* the
broken fields, which is why scanning itself always worked.) Verify any struct
change with `ctypes.sizeof` + field `.offset` against these numbers.
### Decision #19 — Security from RSN IE; hidden networks via Privacy bit
Security resolves in three tiers: (1) SSID-keyed `dot11DefaultAuthAlgorithm`
from `WlanGetAvailableNetworkList`; (2) for hidden networks (no SSID to key
on), `_security_from_ies()` parses the **RSN element (48)** AKM suite types —
PSK(2/4/6)→WPA2, SAE(8/9)→WPA3, 802.1X(1/3/5/11–13)→Enterprise, OWE(18) — plus
the WPA1 vendor IE (221, OUI 00:50:F2:01); (3) the **Privacy bit** (0x10) of
`usCapabilityInformation` → "Secured"/"Open". RSN field offsets are exact:
within the element body, pairwise-cipher count is at offset **6** (after
version[2] + group-cipher[4]), AKM list follows the pairwise list.
### Decision #20 — Channel width from Operation elements; "Local" vendor
`_width_from_ies()` parses width from the **Operation** elements (the scan
list never exposes it), newest generation winning: EHT Operation (Ext 106,
3-bit width 0–4 = 20/40/80/160/320) → HE Operation (Ext 36; 6 GHz Op Info or
embedded VHT Op Info) → VHT Operation (El 192, width+seg0/seg1) → HT Operation
(El 61, secondary-offset + width-any → 20/40). All reads are length-guarded.
The **HT, VHT, and HE** (incl. 6 GHz Op Info) offsets are validated byte-for-byte
against the Linux kernel headers — HE matches `ieee80211_he_6ghz_oper()` exactly
(`optional[]` at struct offset 6 → `body[7]`; `VHT_OPER_INFO=0x4000`,
`CO_HOSTED_BSS=0x8000`, `6GHZ_OP_INFO=0x20000`; 6 GHz `control & 0x3` →
20/40/80/160). The **EHT** path (Ext 106 → `params`[1] bit0 = info-present,
`basic_mcs_nss`[4], `control` at `body[6]`, 3-bit width 0–4 = 20/40/80/160/320)
is confirmed only against the spec encoding — no `ieee80211-eht.h` was available
to cross-check; re-verify against the kernel `ieee80211_eht_operation` /
`ieee80211_eht_operation_info` structs if touched. Separately, `lookup_vendor()`
returns **"Local"** for locally-administered BSSIDs (U/L bit 0x02 set in the
first octet) — these are the randomized virtual/guest/mesh BSSIDs a single radio
spawns, whose OUI is not a real IEEE assignment.
---
## Known Issues & Gotchas
@@ -277,6 +360,25 @@ defined as `c_ubyte * 33` which misread the 4-byte `uSSIDLength` field.
This truncated SSIDs to their first 6 bytes. Fixed by defining a proper
`DOT11_SSID` ctypes Structure.
**Wi-Fi 7 not seen in scans**: If an 802.11be AP shows as Wi-Fi 6/6E, the cause
is almost always the `dot11BssPhyType` HE cap (Decision #17) — confirm the EHT
Capabilities IE (Ext 108) is actually being parsed, not the phy-type field.
`netsh wlan show interfaces` showing `802.11be` does NOT mean the scan list
does; it uses a different (connected-association) code path.
**OUI vendor database**: `lookup_vendor()` is backed by the full IEEE OUI CSV
(~37k entries), downloaded once to `~/.pyfi/oui.csv` and refreshed every 30
days. The download runs in a daemon thread started at module import, so the
*first ever* scan (cold cache, before the download finishes) may briefly show
`-`/built-in-table vendors; subsequent scans have the full DB. A small
built-in table (`_OUI_BUILTIN`) is the offline fallback. Locally-administered
BSSIDs short-circuit to "Local" before any lookup.
**Show Hidden toggle**: The AP tab has a "Show Hidden" checkbox (default off)
that filters rows whose SSID is `<hidden>`/empty. `_populate_ap_table` skips
them using a separate `visible_idx` counter for row numbering/striping —
`_ap_row_widgets` stays keyed by BSSID, so Select All / toggles remain correct.
---
## AP_COLUMNS — Canonical Column Definition
@@ -406,6 +508,11 @@ testing without requiring OS WiFi hardware or a display:
| `data.py — Session save/load` | Round-trip preserves all fields, missing `ap_positions` key loads as empty dict |
| `scanner.py — scan_averaged median` | Given known readings, median is returned not mean; single-sample case returns that value |
| `scanner.py — _channel_from_freq` | 2437 → channel 6, 5180 → channel 36, 2484 → channel 14 |
| `scanner.py — _band_from_freq` | 2422 → 2.4 GHz, 5180 → 5 GHz, 6295 → 6 GHz |
| `scanner.py — _mode_from_ies` | EHT cap → Wi-Fi 7, HE cap + 6 GHz → 6E, VHT → 5, HT → 4, empty → "" |
| `scanner.py — _security_from_ies` | RSN AKM PSK → WPA2, SAE → WPA3, both → WPA2/WPA3, 802.1X → Enterprise, WPA1 vendor IE → WPA-Personal |
| `scanner.py — _width_from_ies` | EHT width 4 → 320, 3 → 160; VHT seg gap 8 → 160; HT sec+wide → 40; EHT wins over VHT/HT |
| `scanner.py — lookup_vendor` | U/L bit set (e.g. D6:…) → "Local"; globally-unique OUI → vendor name |
| `main.py — _resource_path` | When `sys._MEIPASS` is not set, returns path relative to `__file__`; when set, returns path relative to `_MEIPASS` |
| `renderer.py — _build_ekahau_rgba` | Output shape is `(H, W, 4)` uint8, NaN cells have alpha=0, strong signal cells have high alpha |
@@ -458,6 +565,29 @@ truncate or corrupt. The original bug (treating the struct as `c_ubyte * 33`)
produced SSIDs truncated to 6 characters. Test any struct change against a
network with a known SSID length > 6 characters.
**The `WLAN_BSS_ENTRY` ctypes struct layout is exact (and IE-critical).**
`bInRegDomain` is `BOOLEAN` (1 byte) and `wlanRateSet` is `WLAN_RATE_SET`
(256 bytes). Getting either wrong shifts `ulIeOffset`/`ulIeSize` and makes all
beacon-IE parsing (mode, security, width) read garbage — and a raw read at a
bad offset crashes the app. Invariants: `ctypes.sizeof(WLAN_BSS_ENTRY) == 360`,
`ulIeOffset` at offset 352, `ulIeSize` at 356, `lRssi` at 56,
`ulChCenterFrequency` at 92. Verify with `.offset` after any change (see
Decision #18). Note RSSI/freq/phy precede the IE fields, so a broken layout
still scans fine — only IE-derived columns break, which masks the bug.
**Beacon-IE parsers assume byte-exact element offsets.**
`_mode_from_ies`, `_security_from_ies`, `_width_from_ies` index into raw IE
bytes at fixed offsets derived from the 802.11 / Linux `ieee80211.h` layouts
(e.g. RSN pairwise count at body offset 6; EHT Operation control at body[6];
HE `he_oper_params` is a 4-byte LE field with VHT=0x4000 / 6GHz=0x20000 flags).
All accesses are length-guarded so bad data yields "" not a crash, but a wrong
offset silently mislabels. The HT/VHT/HE width offsets have been validated
byte-for-byte against the Linux kernel `ieee80211-ht.h` / `-vht.h` / `-he.h`
definitions (HE matches `ieee80211_he_6ghz_oper()` exactly); the **EHT** path
is the one still unverified against source. When touching these, diff against
the kernel `ieee80211_*_operation` structs and test on a live network of the
relevant generation.
---