kawaiifi 0.2.0

Wi-Fi scanning library for Linux, macOS, and Windows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#[cfg(any(target_os = "linux", target_os = "windows"))]
use std::time::Duration;
use std::{fmt::Display, hash::Hash};

#[cfg(any(target_os = "linux", target_os = "windows"))]
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[cfg(any(target_os = "linux", target_os = "windows"))]
use super::CapabilityInfo;
#[cfg(target_os = "linux")]
use crate::nl80211::{BssScanWidth, BssStatus};
use crate::{
    Band, ChannelWidth, SecurityProtocols, WifiAmendments, WifiProtocols,
    ies::{Ie, IeData},
};

/// A basic service set (BSS).
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
pub struct Bss {
    // Cross-platform
    pub(super) bssid: [u8; 6],
    pub(super) frequency_mhz: u32,
    pub(super) signal_dbm: i32,
    pub(super) beacon_interval_tu: u16,
    #[serde(with = "crate::ies::serde_raw::ies_as_base64")]
    pub(super) ies: Vec<Ie>,

    // Linux and Windows
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    pub(super) tsf: u64,
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    #[serde(with = "crate::ies::serde_raw::capability_info_as_u16")]
    pub(super) capability_info: CapabilityInfo,
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    pub(super) last_seen_utc: Option<DateTime<Utc>>,

    // Linux-only
    #[cfg(target_os = "linux")]
    pub(super) status: Option<BssStatus>,
    #[cfg(target_os = "linux")]
    pub(super) is_from_probe_response: bool,
    #[cfg(target_os = "linux")]
    pub(super) parent_bssid: Option<[u8; 6]>,
    #[cfg(target_os = "linux")]
    pub(super) parent_tsf: Option<u64>,
    #[cfg(target_os = "linux")]
    pub(super) beacon_tsf: Option<u64>,
    #[cfg(target_os = "linux")]
    pub(super) frequency_offset_khz: Option<u32>,
    #[cfg(target_os = "linux")]
    pub(super) signal_percent: Option<u8>,
    #[cfg(target_os = "linux")]
    #[serde(with = "crate::ies::serde_raw::option_ies_as_base64")]
    pub(super) beacon_ies: Option<Vec<Ie>>,
    #[cfg(target_os = "linux")]
    pub(super) scan_width: Option<BssScanWidth>,
    #[cfg(target_os = "linux")]
    pub(super) last_seen_boottime: Option<u64>,
    #[cfg(target_os = "linux")]
    pub(super) seen_ms_ago: Option<u32>,
    #[cfg(target_os = "linux")]
    pub(super) mlo_link_id: Option<u8>,
    #[cfg(target_os = "linux")]
    pub(super) mld_address: Option<[u8; 6]>,

    // Windows-only
    #[cfg(target_os = "windows")]
    pub(super) link_quality: u8,

    // macOS-only
    #[cfg(target_os = "macos")]
    pub(super) noise_dbm: i32,
    #[cfg(target_os = "macos")]
    pub(super) is_wep: bool,
}

impl Bss {
    /// The 6-byte BSSID (MAC address) of the BSS.
    pub fn bssid(&self) -> &[u8; 6] {
        &self.bssid
    }

    /// The operating frequency in MHz.
    pub fn frequency_mhz(&self) -> u32 {
        self.frequency_mhz
    }

    /// The frequency band the BSS operates on.
    pub fn band(&self) -> Band {
        Band::from_freq_mhz(self.frequency_mhz)
    }

    /// The channel width used by the BSS.
    pub fn channel_width(&self) -> ChannelWidth {
        ChannelWidth::from(self.ies())
    }

    /// The center frequency of the full channel in MHz.
    ///
    /// For 20 MHz channels this equals `frequency_mhz`. For wider channels,
    /// the primary channel frequency can be offset from the center of the full
    /// channel, so this reads the center channel frequency segments from the
    /// operation IEs (EHT > HE > VHT > HT priority).
    pub fn center_frequency_mhz(&self) -> u32 {
        use crate::ies::ht_operation::SecondaryChannelOffset;

        let primary = self.frequency_mhz;
        let band = self.band();

        let chan_to_freq = |chan: u8| -> u32 {
            match band {
                Band::FiveGhz => 5000 + (chan as u32 * 5),
                Band::SixGhz => 5950 + (chan as u32 * 5),
                Band::TwoPointFourGhz => primary,
            }
        };

        let (mut eht_op, mut he_op, mut vht_op, mut ht_op) = (None, None, None, None);
        for ie in &self.ies {
            match &ie.data {
                IeData::EhtOperation(op) => eht_op = Some(op),
                IeData::HeOperation(op) => he_op = Some(op),
                IeData::VhtOperation(op) => vht_op = Some(op),
                IeData::HtOperation(op) => ht_op = Some(op),
                _ => {}
            }
        }

        // EHT Operation:
        //   CCFS1 — center of the full 160 or 320 MHz channel (when present)
        //   CCFS0 — center for 20/40/80 MHz, or primary 80 MHz for 160 MHz,
        //           or primary 160 MHz for 320 MHz
        if let Some(eht_op) = eht_op
            && let Some(info) = eht_op.eht_operation_information
        {
            if info.ccfs1 != 0 {
                return chan_to_freq(info.ccfs1);
            }
            if info.ccfs0 != 0 {
                return chan_to_freq(info.ccfs0);
            }
        }

        // HE Operation: 6 GHz has its own info; 5 GHz embeds VHT operation info.
        // CCFS1 (when 8 channels/40 MHz from CCFS0) is the 160 MHz full center.
        if let Some(he_op) = he_op {
            if let Some(six_ghz) = &he_op.six_ghz_operation_information {
                let (ccfs0, ccfs1) = (
                    six_ghz.channel_center_frequency_segment_0,
                    six_ghz.channel_center_frequency_segment_1,
                );
                if ccfs1 != 0 && ccfs1.abs_diff(ccfs0) == 8 {
                    return chan_to_freq(ccfs1);
                }
                if ccfs0 != 0 {
                    return chan_to_freq(ccfs0);
                }
            }
            if let Some(vht_info) = &he_op.vht_operation_information {
                let (ccfs0, ccfs1) = (
                    vht_info.channel_center_frequency_segment_0,
                    vht_info.channel_center_frequency_segment_1,
                );
                if ccfs1 != 0 && ccfs1.abs_diff(ccfs0) == 8 {
                    return chan_to_freq(ccfs1);
                }
                if ccfs0 != 0 {
                    return chan_to_freq(ccfs0);
                }
            }
        }

        // VHT Operation (5 GHz): CCFS1 for 160 MHz full center, CCFS0 for 80 MHz
        if let Some(vht_op) = vht_op {
            let info = &vht_op.vht_operation_information;
            let (ccfs0, ccfs1) = (
                info.channel_center_frequency_segment_0,
                info.channel_center_frequency_segment_1,
            );
            if ccfs1 != 0 && ccfs1.abs_diff(ccfs0) == 8 {
                return chan_to_freq(ccfs1);
            }
            if ccfs0 != 0 {
                return chan_to_freq(ccfs0);
            }
        }

        // HT Operation: 40 MHz secondary channel is above or below the primary
        if let Some(ht_op) = ht_op {
            match ht_op.ht_operation_information.secondary_channel_offset {
                SecondaryChannelOffset::AbovePrimary => return primary + 10,
                SecondaryChannelOffset::BelowPrimary => return primary - 10,
                SecondaryChannelOffset::NoSecondary => {}
            }
        }

        primary
    }

    /// The 802.11 channel number.
    pub fn channel_number(&self) -> u8 {
        self.ies
            .iter()
            .find_map(|ie| match &ie.data {
                IeData::DsParameterSet(ds_parameter_set) => Some(ds_parameter_set.current_channel),
                IeData::HtOperation(ht_operation) => Some(ht_operation.primary_channel),
                _ => None,
            })
            .unwrap_or_else(|| {
                let freq = self.frequency_mhz();
                if Band::TwoPointFourGhz.range_mhz().contains(&freq) {
                    if freq == 2484 {
                        14
                    } else {
                        ((freq - 2407) / 5) as u8
                    }
                } else if Band::FiveGhz.range_mhz().contains(&freq) {
                    ((freq - 5000) / 5) as u8
                } else if Band::SixGhz.range_mhz().contains(&freq) {
                    ((freq - 5950) / 5) as u8
                } else {
                    0
                }
            })
    }

    /// The received signal strength in dBm.
    pub fn signal_dbm(&self) -> i32 {
        self.signal_dbm
    }

    /// The beacon interval in time units (1 TU = 1024 µs).
    pub fn beacon_interval_tu(&self) -> u16 {
        self.beacon_interval_tu
    }

    /// The beacon interval in milliseconds.
    pub fn beacon_interval_ms(&self) -> f64 {
        f64::from(self.beacon_interval_tu) * 1.024
    }

    /// The 802.11 capability information flags advertised by the BSS.
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    pub fn capability_info(&self) -> &CapabilityInfo {
        &self.capability_info
    }

    /// The information elements (IEs) included in the BSS's beacon or probe response.
    pub fn ies(&self) -> &[Ie] {
        &self.ies
    }

    /// The timing synchronization function (TSF) timer value.
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    pub fn tsf(&self) -> u64 {
        self.tsf
    }

    /// The estimated time the BSS has been running, derived from its TSF timer.
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    pub fn uptime(&self) -> Duration {
        Duration::from_micros(self.tsf)
    }

    /// The UTC date and time when the BSS was last seen, or `None` if unavailable.
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    pub fn last_seen_utc(&self) -> Option<DateTime<Utc>> {
        self.last_seen_utc
    }

    /// The SSID (network name), or `None` if hidden or unavailable.
    pub fn ssid(&self) -> Option<&str> {
        fn find_ssid(ies: &[Ie]) -> Option<&str> {
            ies.iter().find_map(|ie| {
                if let IeData::Ssid(ssid) = &ie.data
                    && !ssid.is_empty()
                {
                    ssid.as_str().ok()
                } else {
                    None
                }
            })
        }

        #[cfg(target_os = "linux")]
        {
            find_ssid(&self.ies).or_else(|| self.beacon_ies.as_deref().and_then(find_ssid))
        }

        #[cfg(not(target_os = "linux"))]
        {
            find_ssid(&self.ies)
        }
    }

    /// The security protocols supported by the BSS.
    pub fn security_protocols(&self) -> SecurityProtocols {
        let mut protocols = SecurityProtocols::from(self.ies.as_slice());
        #[cfg(any(target_os = "linux", target_os = "windows"))]
        if self.capability_info.privacy && protocols.is_empty() {
            protocols |=
                SecurityProtocols::from(enumflags2::BitFlags::from(crate::SecurityProtocol::WEP));
        }

        #[cfg(target_os = "macos")]
        if self.is_wep {
            protocols |=
                SecurityProtocols::from(enumflags2::BitFlags::from(crate::SecurityProtocol::WEP));
        }

        protocols
    }

    /// The Wi-Fi protocols supported by the BSS.
    pub fn wifi_protocols(&self) -> WifiProtocols {
        WifiProtocols::from_ies_for_band(self.ies.as_slice(), self.band())
    }

    /// The Wi-Fi amendments supported by the BSS.
    pub fn wifi_amendments(&self) -> WifiAmendments {
        let amendments = WifiAmendments::from(self.ies.as_slice());
        #[cfg(any(target_os = "linux", target_os = "windows"))]
        let amendments = amendments | WifiAmendments::from(&self.capability_info);

        amendments
    }

    /// The maximum supported data rate in Mbps.
    pub fn max_rate_mbps(&self) -> f64 {
        // Iterate once through the IEs and find all of the following IEs that can be used
        // to figure out the max rate
        let (mut eht_caps, mut he_caps, mut vht_caps, mut ht_caps) = (None, None, None, None);
        let (mut supported_rates, mut extended_supported_rates) = (None, None);

        for ie in self.ies.iter() {
            match &ie.data {
                IeData::EhtCapabilities(ie_data) => eht_caps = Some(ie_data),
                IeData::HeCapabilities(ie_data) => he_caps = Some(ie_data),
                IeData::VhtCapabilities(ie_data) => vht_caps = Some(ie_data),
                IeData::HtCapabilities(ie_data) => ht_caps = Some(ie_data),
                IeData::SupportedRates(ie_data) => supported_rates = Some(ie_data),
                IeData::ExtendedSupportedRates(ie_data) => extended_supported_rates = Some(ie_data),
                _ => continue,
            }
        }

        // Find the max rate reported by an EHT/HE/VHT/HT Capability element
        let channel_width = self.channel_width();
        let rates = [
            eht_caps.map(|c| c.max_rate(channel_width)),
            he_caps.map(|c| c.max_rate(channel_width)),
            vht_caps.map(|c| c.max_rate(channel_width, ht_caps.map(|h| h.as_ref()))),
            ht_caps.map(|c| c.max_rate(channel_width)),
        ];

        if let Some(max_rate) = rates.into_iter().flatten().max_by(|a, b| a.total_cmp(b)) {
            return max_rate;
        }

        // If we don't have an EHT/HE/VHT/HT Capability element then use the extended supported
        // rates or supported rates element

        let mut max_rate = 0.0f64;
        if let Some(supported_rates) = supported_rates {
            max_rate = max_rate.max(
                supported_rates
                    .rates()
                    .iter()
                    .map(|rate| rate.value())
                    .max_by(|r1, r2| r1.total_cmp(r2))
                    .unwrap_or_default(),
            );
        }

        if let Some(extended_supported_rates) = extended_supported_rates {
            max_rate = max_rate.max(
                extended_supported_rates
                    .rates()
                    .iter()
                    .map(|rate| rate.value())
                    .max_by(|r1, r2| r1.total_cmp(r2))
                    .unwrap_or_default(),
            );
        }

        max_rate
    }

    /// The fraction of time the BSS's channel is busy, as a value from 0 to 255, where 255 represents 100%, or `None` if unavailable.
    pub fn channel_utilization(&self) -> Option<u8> {
        self.ies.iter().find_map(|ie| {
            if let IeData::BssLoad(bss_load) = &ie.data {
                Some(bss_load.channel_utilization)
            } else {
                None
            }
        })
    }

    /// The number of devices associated with the BSS, or `None` if unavailable.
    pub fn station_count(&self) -> Option<u16> {
        self.ies.iter().find_map(|ie| {
            if let IeData::BssLoad(bss_load) = &ie.data {
                Some(bss_load.station_count)
            } else {
                None
            }
        })
    }

    /// The maximum number of spatial streams advertised by the BSS for its current
    /// channel width.
    pub fn max_spatial_streams(&self) -> u8 {
        let (mut eht_caps, mut he_caps, mut vht_caps, mut ht_caps) = (None, None, None, None);

        for ie in self.ies.iter() {
            match &ie.data {
                IeData::EhtCapabilities(ie_data) => eht_caps = Some(ie_data),
                IeData::HeCapabilities(ie_data) => he_caps = Some(ie_data),
                IeData::VhtCapabilities(ie_data) => vht_caps = Some(ie_data),
                IeData::HtCapabilities(ie_data) => ht_caps = Some(ie_data),
                _ => continue,
            }
        }

        if let Some(caps) = eht_caps
            && let Some(mcs_nss_set) = &caps.supported_eht_mcs_and_nss_set
        {
            let default_mcs_map_spatial_streams = mcs_nss_set
                .eht_mcs_map_bw_lte_80_mhz_except_20_mhz_only_non_ap_sta
                .max_spatial_streams();
            return match self.channel_width() {
                ChannelWidth::TwentyMhz | ChannelWidth::FortyMhz | ChannelWidth::EightyMhz => {
                    default_mcs_map_spatial_streams
                }
                ChannelWidth::EightyPlusEightyMhz | ChannelWidth::OneSixtyMhz => mcs_nss_set
                    .eht_mcs_map_bw_eq_160_mhz
                    .map_or(default_mcs_map_spatial_streams, |mcs_map| {
                        mcs_map.max_spatial_streams()
                    }),
                ChannelWidth::ThreeHundredTwentyMhz => mcs_nss_set
                    .eht_mcs_map_bw_eq_320_mhz
                    .map_or(default_mcs_map_spatial_streams, |mcs_map| {
                        mcs_map.max_spatial_streams()
                    }),
            };
        }

        if let Some(caps) = he_caps {
            let default_mcs_map_spatial_streams = caps
                .supported_he_mcs_and_nss_set
                .rx_he_mcs_map_less_than_or_equal_to_eighty_mhz
                .max_spatial_streams();
            return match self.channel_width() {
                ChannelWidth::TwentyMhz | ChannelWidth::FortyMhz | ChannelWidth::EightyMhz => {
                    default_mcs_map_spatial_streams
                }

                ChannelWidth::EightyPlusEightyMhz => caps
                    .supported_he_mcs_and_nss_set
                    .rx_he_mcs_map_eighty_plus_eighty_mhz
                    .map_or(default_mcs_map_spatial_streams, |mcs_map| {
                        mcs_map.max_spatial_streams()
                    }),
                ChannelWidth::OneSixtyMhz | ChannelWidth::ThreeHundredTwentyMhz => caps
                    .supported_he_mcs_and_nss_set
                    .rx_he_mcs_map_one_hundred_sixty_mhz
                    .map_or(default_mcs_map_spatial_streams, |mcs_map| {
                        mcs_map.max_spatial_streams()
                    }),
            };
        }

        if let Some(caps) = vht_caps {
            return caps
                .supported_vht_mcs_and_nss_set
                .rx_vht_mcs_map
                .max_spatial_streams();
        }

        if let Some(caps) = ht_caps {
            return caps.supported_mcs_set.max_spatial_streams();
        }

        1
    }

    pub(crate) fn resolve_ie_dependencies(&mut self) {
        crate::ies::resolve_ie_dependencies(&mut self.ies);
    }
}

impl Hash for Bss {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.bssid.hash(state);
    }
}

impl PartialEq for Bss {
    fn eq(&self, other: &Self) -> bool {
        self.bssid == other.bssid
    }
}

impl Display for Bss {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "BSSID: {:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}\nSSID: {}\nRSSI: {} dBm\nChannel Number: {}\nChannel Width: {}\nWi-Fi Protocols: {}",
            self.bssid[0],
            self.bssid[1],
            self.bssid[2],
            self.bssid[3],
            self.bssid[4],
            self.bssid[5],
            self.ssid().unwrap_or_default(),
            self.signal_dbm,
            self.channel_number(),
            self.channel_width(),
            self.wifi_protocols()
        )?;

        #[cfg(any(target_os = "linux", target_os = "windows"))]
        writeln!(f, "{}", self.capability_info)?;

        Ok(())
    }
}