Verify channel widths using linux kernel source
This commit is contained in:
1 parent
bc2c3168bc
commit
f51e3e8785
1 file changed
+86
-1
+86
-1
@@ -372,6 +372,87 @@ def _mode_from_ies(ie_bytes: bytes, freq_mhz: int = 0) -> str:
|
||||
return ""
|
||||
|
||||
|
||||
def _vht_op_width(cw: int, seg0: int, seg1: int) -> str:
|
||||
"""
|
||||
Decode a VHT Operation 'Channel Width / CCFS0 / CCFS1' triple into a label.
|
||||
|
||||
cw: 0 = use HT (20/40), 1 = 80 MHz, 2 = 160 (deprecated), 3 = 80+80 (dep).
|
||||
Modern APs signal 160/80+80 with cw=1 plus a non-zero CCFS1 segment, so the
|
||||
gap between the two centre-frequency segment indices disambiguates them.
|
||||
"""
|
||||
if cw == 2:
|
||||
return "160 MHz"
|
||||
if cw == 3:
|
||||
return "80+80 MHz"
|
||||
if cw == 1:
|
||||
if seg1:
|
||||
d = abs(seg1 - seg0)
|
||||
if d == 8: return "160 MHz"
|
||||
if d > 16: return "80+80 MHz"
|
||||
return "80 MHz"
|
||||
return "" # cw == 0 → width is carried by the HT Operation element
|
||||
|
||||
|
||||
def _width_from_ies(ie_bytes: bytes) -> str:
|
||||
"""
|
||||
Determine the operating channel width from the beacon's *Operation* elements.
|
||||
|
||||
The Windows scan list never exposes channel width, so it is parsed here. A
|
||||
Wi-Fi 7 AP also carries HE/VHT/HT operation elements for backward compat,
|
||||
so the newest-generation element present wins:
|
||||
EHT Operation (Ext 106) → 20/40/80/160/320 (320 MHz is EHT-only)
|
||||
HE Operation (Ext 36) → 6 GHz info: 20/40/80/160 (Wi-Fi 6E)
|
||||
→ embedded VHT info (Wi-Fi 6 on 5 GHz)
|
||||
VHT Operation (El 192) → 80/160/80+80 (Wi-Fi 5)
|
||||
HT Operation (El 61) → 20/40 (Wi-Fi 4)
|
||||
|
||||
Field offsets follow the Linux kernel ieee80211.h layouts. All reads are
|
||||
length-guarded slices, so a malformed IE yields "" rather than an error.
|
||||
"""
|
||||
eht = he = vht = ht = ""
|
||||
i, n = 0, len(ie_bytes)
|
||||
while i + 2 <= n:
|
||||
eid = ie_bytes[i]
|
||||
ln = ie_bytes[i + 1]
|
||||
if i + 2 + ln > n:
|
||||
break
|
||||
body = ie_bytes[i + 2:i + 2 + ln]
|
||||
|
||||
if eid == 61 and ln >= 2: # HT Operation
|
||||
sec = body[1] & 0x03 # secondary channel offset
|
||||
wide = body[1] & 0x04 # STA channel width = "any"
|
||||
ht = "40 MHz" if (wide and sec in (1, 3)) else "20 MHz"
|
||||
|
||||
elif eid == 192 and ln >= 3: # VHT Operation
|
||||
vht = _vht_op_width(body[0], body[1], body[2])
|
||||
|
||||
elif eid == 255 and ln >= 1:
|
||||
ext = body[0]
|
||||
if ext == 36 and ln >= 7: # HE Operation
|
||||
# he_oper_params[4] he_mcs_nss[2] then optional fields at off 7
|
||||
params = int.from_bytes(body[1:5], "little")
|
||||
off = 7
|
||||
if params & 0x4000 and off + 3 <= ln: # VHT Op Info present
|
||||
he = _vht_op_width(body[off], body[off + 1], body[off + 2])
|
||||
off += 3
|
||||
if params & 0x8000: # Max Co-Hosted BSSID
|
||||
off += 1
|
||||
if params & 0x20000 and off + 2 <= ln: # 6 GHz Op Info present
|
||||
w = body[off + 1] & 0x03 # control byte, width bits
|
||||
he = {0: "20 MHz", 1: "40 MHz",
|
||||
2: "80 MHz", 3: "160 MHz"}.get(w, he)
|
||||
elif ext == 106 and ln >= 7: # EHT Operation
|
||||
# params[1] basic_mcs_nss[4] then EHT Op Info (control at body[6])
|
||||
if body[1] & 0x01: # EHT Operation Info present
|
||||
w = body[6] & 0x07 # 3-bit channel width
|
||||
eht = {0: "20 MHz", 1: "40 MHz", 2: "80 MHz",
|
||||
3: "160 MHz", 4: "320 MHz"}.get(w, "")
|
||||
|
||||
i += 2 + ln
|
||||
|
||||
return eht or he or vht or ht or ""
|
||||
|
||||
|
||||
def _mode_from_flags(s: str) -> str:
|
||||
u = s.upper()
|
||||
if any(x in u for x in ("EHT", "802.11BE", "WIFI 7", "WI-FI 7")): return "be (Wi-Fi 7)"
|
||||
@@ -677,10 +758,14 @@ def _wlanapi_enumerate_and_scan(wlan, handle) -> list[AccessPoint]:
|
||||
privacy = bool(entry.usCapabilityInformation & 0x10)
|
||||
security = "Secured" if privacy else "Open"
|
||||
|
||||
# Channel width — parsed from the HT/VHT/HE/EHT Operation elements,
|
||||
# since the Windows scan list does not report it.
|
||||
width = _width_from_ies(ie_bytes) if ie_bytes else ""
|
||||
|
||||
aps.append(AccessPoint(
|
||||
ssid=ssid, bssid=bssid, signal_dbm=signal_dbm,
|
||||
channel=channel, freq_mhz=freq_mhz,
|
||||
mode=mode, security=security,
|
||||
channel_width=width, mode=mode, security=security,
|
||||
))
|
||||
|
||||
wlan.WlanFreeMemory(bss_list_ptr)
|
||||
|
||||
Reference in new issue
Block a user