Skip to main content

wlan_common/
bss.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::channel::Channel;
6use crate::ie::owe_transition::{OweTransition, parse_owe_transition};
7use crate::ie::rsn::suite_filter;
8use crate::ie::wsc::{ProbeRespWsc, parse_probe_resp_wsc};
9use crate::ie::{self, IeType};
10use crate::mac::CapabilityInfo;
11use anyhow::format_err;
12use fidl_fuchsia_wlan_ieee80211 as fidl_ieee80211;
13use fidl_fuchsia_wlan_sme as fidl_sme;
14use ieee80211::{Bssid, MacAddrBytes, Ssid};
15use static_assertions::assert_eq_size;
16use std::cmp::Ordering;
17use std::collections::HashMap;
18use std::fmt;
19use std::hash::Hash;
20use std::ops::Range;
21use zerocopy::{IntoBytes, Ref};
22
23#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
24pub enum Protection {
25    Unknown,
26    Open,
27    OpenOweTransition,
28    Owe,
29    Wep,
30    Wpa1,
31    Wpa1Wpa2PersonalTkipOnly,
32    Wpa2PersonalTkipOnly,
33    Wpa1Wpa2Personal,
34    Wpa2Personal,
35    Wpa2Wpa3Personal,
36    Wpa3Personal,
37    Wpa2Enterprise,
38    /// WPA3 Enterprise 192-bit mode. WPA3 spec specifies an optional 192-bit mode but says nothing
39    /// about a non 192-bit version. Thus, colloquially, it's likely that the term WPA3 Enterprise
40    /// will be used to refer to WPA3 Enterprise 192-bit mode.
41    Wpa3Enterprise,
42}
43
44impl From<Protection> for fidl_sme::Protection {
45    fn from(protection: Protection) -> fidl_sme::Protection {
46        match protection {
47            Protection::Unknown => fidl_sme::Protection::Unknown,
48            Protection::Open => fidl_sme::Protection::Open,
49            Protection::OpenOweTransition => fidl_sme::Protection::OpenOweTransition,
50            Protection::Owe => fidl_sme::Protection::Owe,
51            Protection::Wep => fidl_sme::Protection::Wep,
52            Protection::Wpa1 => fidl_sme::Protection::Wpa1,
53            Protection::Wpa1Wpa2PersonalTkipOnly => fidl_sme::Protection::Wpa1Wpa2PersonalTkipOnly,
54            Protection::Wpa2PersonalTkipOnly => fidl_sme::Protection::Wpa2PersonalTkipOnly,
55            Protection::Wpa1Wpa2Personal => fidl_sme::Protection::Wpa1Wpa2Personal,
56            Protection::Wpa2Personal => fidl_sme::Protection::Wpa2Personal,
57            Protection::Wpa2Wpa3Personal => fidl_sme::Protection::Wpa2Wpa3Personal,
58            Protection::Wpa3Personal => fidl_sme::Protection::Wpa3Personal,
59            Protection::Wpa2Enterprise => fidl_sme::Protection::Wpa2Enterprise,
60            Protection::Wpa3Enterprise => fidl_sme::Protection::Wpa3Enterprise,
61        }
62    }
63}
64
65impl From<fidl_sme::Protection> for Protection {
66    fn from(protection: fidl_sme::Protection) -> Self {
67        match protection {
68            fidl_sme::Protection::Unknown => Protection::Unknown,
69            fidl_sme::Protection::Open => Protection::Open,
70            fidl_sme::Protection::OpenOweTransition => Protection::OpenOweTransition,
71            fidl_sme::Protection::Owe => Protection::Owe,
72            fidl_sme::Protection::Wep => Protection::Wep,
73            fidl_sme::Protection::Wpa1 => Protection::Wpa1,
74            fidl_sme::Protection::Wpa1Wpa2PersonalTkipOnly => Protection::Wpa1Wpa2PersonalTkipOnly,
75            fidl_sme::Protection::Wpa2PersonalTkipOnly => Protection::Wpa2PersonalTkipOnly,
76            fidl_sme::Protection::Wpa1Wpa2Personal => Protection::Wpa1Wpa2Personal,
77            fidl_sme::Protection::Wpa2Personal => Protection::Wpa2Personal,
78            fidl_sme::Protection::Wpa2Wpa3Personal => Protection::Wpa2Wpa3Personal,
79            fidl_sme::Protection::Wpa3Personal => Protection::Wpa3Personal,
80            fidl_sme::Protection::Wpa2Enterprise => Protection::Wpa2Enterprise,
81            fidl_sme::Protection::Wpa3Enterprise => Protection::Wpa3Enterprise,
82        }
83    }
84}
85
86impl fmt::Display for Protection {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        match self {
89            Protection::Unknown => write!(f, "{}", "Unknown"),
90            Protection::Open => write!(f, "{}", "Open"),
91            Protection::OpenOweTransition => write!(f, "{}", "Open OWE Transition"),
92            Protection::Owe => write!(f, "{}", "OWE"),
93            Protection::Wep => write!(f, "{}", "WEP"),
94            Protection::Wpa1 => write!(f, "{}", "WPA1"),
95            Protection::Wpa1Wpa2PersonalTkipOnly => write!(f, "{}", "WPA1/2 PSK TKIP"),
96            Protection::Wpa2PersonalTkipOnly => write!(f, "{}", "WPA2 PSK TKIP"),
97            Protection::Wpa1Wpa2Personal => write!(f, "{}", "WPA1/2 PSK"),
98            Protection::Wpa2Personal => write!(f, "{}", "WPA2 PSK"),
99            Protection::Wpa2Wpa3Personal => write!(f, "{}", "WPA2/3 PSK"),
100            Protection::Wpa3Personal => write!(f, "{}", "WPA3 PSK"),
101            Protection::Wpa2Enterprise => write!(f, "{}", "WPA2 802.1X"),
102            Protection::Wpa3Enterprise => write!(f, "{}", "WPA3 802.1X"),
103        }
104    }
105}
106
107#[derive(Clone, Debug, Eq, Hash, PartialEq)]
108pub enum Standard {
109    Dot11A,
110    Dot11B,
111    Dot11G,
112    Dot11N,
113    Dot11Ac,
114}
115
116#[derive(Debug, Clone, PartialEq)]
117pub struct BssDescription {
118    // *** Fields originally in fidl_common::BssDescription
119    pub ssid: Ssid,
120    pub bssid: Bssid,
121    pub bss_type: fidl_ieee80211::BssType,
122    pub beacon_period: u16,
123    pub capability_info: u16,
124    pub channel: Channel,
125    pub rssi_dbm: i8,
126    pub snr_db: i8,
127    // Private because the parsed information reference the IEs
128    ies: Vec<u8>,
129
130    // *** Fields parsed out of fidl_common::BssDescription IEs
131    // IEEE Std 802.11-2016 9.4.2.3
132    // in 0.5 Mbps, with MSB indicating basic rate. See Table 9-78 for 126, 127.
133    // The rates here may include both the basic rates and extended rates, which are not
134    // continuous slices, hence we cannot use `Range`.
135    rates: Vec<ie::SupportedRate>,
136    tim_range: Option<Range<usize>>,
137    country_range: Option<Range<usize>>,
138    rsne_range: Option<Range<usize>>,
139    ht_cap_range: Option<Range<usize>>,
140    ht_op_range: Option<Range<usize>>,
141    rm_enabled_cap_range: Option<Range<usize>>,
142    ext_cap_range: Option<Range<usize>>,
143    vht_cap_range: Option<Range<usize>>,
144    vht_op_range: Option<Range<usize>>,
145    rsnxe_range: Option<Range<usize>>,
146    owe_transition_range: Option<Range<usize>>,
147}
148
149impl BssDescription {
150    pub fn rates(&self) -> &[ie::SupportedRate] {
151        &self.rates[..]
152    }
153
154    pub fn dtim_period(&self) -> u8 {
155        self.tim_range
156            .as_ref()
157            .map(|range|
158            // Safe to unwrap because we made sure TIM is parseable in `from_fidl`
159            ie::parse_tim(&self.ies[range.clone()]).unwrap().header.dtim_period)
160            .unwrap_or(0)
161    }
162
163    pub fn country(&self) -> Option<&[u8]> {
164        self.country_range.as_ref().map(|range| &self.ies[range.clone()])
165    }
166
167    pub fn rsne(&self) -> Option<&[u8]> {
168        self.rsne_range.as_ref().map(|range| &self.ies[range.clone()])
169    }
170
171    pub fn ht_cap(&self) -> Option<Ref<&[u8], ie::HtCapabilities>> {
172        self.ht_cap_range.clone().map(|range| {
173            // Safe to unwrap because we already verified HT caps is parseable in `from_fidl`
174            ie::parse_ht_capabilities(&self.ies[range]).unwrap()
175        })
176    }
177
178    pub fn raw_ht_cap(&self) -> Option<fidl_ieee80211::HtCapabilities> {
179        type HtCapArray = [u8; fidl_ieee80211::HT_CAP_LEN as usize];
180        self.ht_cap().map(|ht_cap| {
181            assert_eq_size!(ie::HtCapabilities, HtCapArray);
182            let bytes: HtCapArray = ht_cap.as_bytes().try_into().unwrap();
183            fidl_ieee80211::HtCapabilities { bytes }
184        })
185    }
186
187    pub fn ht_op(&self) -> Option<Ref<&[u8], ie::HtOperation>> {
188        self.ht_op_range.clone().map(|range| {
189            // Safe to unwrap because we already verified HT op is parseable in `from_fidl`
190            ie::parse_ht_operation(&self.ies[range]).unwrap()
191        })
192    }
193
194    pub fn rm_enabled_cap(&self) -> Option<Ref<&[u8], ie::RmEnabledCapabilities>> {
195        self.rm_enabled_cap_range.clone().map(|range| {
196            // Safe to unwrap because we already verified RM enabled cap is parseable in `from_fidl`
197            ie::parse_rm_enabled_capabilities(&self.ies[range]).unwrap()
198        })
199    }
200
201    pub fn ext_cap(&self) -> Option<ie::ExtCapabilitiesView<&[u8]>> {
202        self.ext_cap_range.clone().map(|range| ie::parse_ext_capabilities(&self.ies[range]))
203    }
204
205    pub fn raw_ht_op(&self) -> Option<fidl_ieee80211::HtOperation> {
206        type HtOpArray = [u8; fidl_ieee80211::HT_OP_LEN as usize];
207        self.ht_op().map(|ht_op| {
208            assert_eq_size!(ie::HtOperation, HtOpArray);
209            let bytes: HtOpArray = ht_op.as_bytes().try_into().unwrap();
210            fidl_ieee80211::HtOperation { bytes }
211        })
212    }
213
214    pub fn vht_cap(&self) -> Option<Ref<&[u8], ie::VhtCapabilities>> {
215        self.vht_cap_range.clone().map(|range| {
216            // Safe to unwrap because we already verified VHT caps is parseable in `from_fidl`
217            ie::parse_vht_capabilities(&self.ies[range]).unwrap()
218        })
219    }
220
221    pub fn raw_vht_cap(&self) -> Option<fidl_ieee80211::VhtCapabilities> {
222        type VhtCapArray = [u8; fidl_ieee80211::VHT_CAP_LEN as usize];
223        self.vht_cap().map(|vht_cap| {
224            assert_eq_size!(ie::VhtCapabilities, VhtCapArray);
225            let bytes: VhtCapArray = vht_cap.as_bytes().try_into().unwrap();
226            fidl_ieee80211::VhtCapabilities { bytes }
227        })
228    }
229
230    pub fn vht_op(&self) -> Option<Ref<&[u8], ie::VhtOperation>> {
231        self.vht_op_range.clone().map(|range| {
232            // Safe to unwrap because we already verified VHT op is parseable in `from_fidl`
233            ie::parse_vht_operation(&self.ies[range]).unwrap()
234        })
235    }
236
237    pub fn raw_vht_op(&self) -> Option<fidl_ieee80211::VhtOperation> {
238        type VhtOpArray = [u8; fidl_ieee80211::VHT_OP_LEN as usize];
239        self.vht_op().map(|vht_op| {
240            assert_eq_size!(ie::VhtOperation, VhtOpArray);
241            let bytes: VhtOpArray = vht_op.as_bytes().try_into().unwrap();
242            fidl_ieee80211::VhtOperation { bytes }
243        })
244    }
245
246    pub fn rsnxe(&self) -> Option<ie::RsnxeView<&[u8]>> {
247        self.rsnxe_range.clone().map(|range| ie::parse_rsnxe(&self.ies[range]))
248    }
249
250    pub fn ies(&self) -> &[u8] {
251        &self.ies[..]
252    }
253
254    /// Return bool on whether BSS is protected.
255    pub fn is_protected(&self) -> bool {
256        self.protection() != Protection::Open && self.protection() != Protection::OpenOweTransition
257    }
258
259    /// Return bool on whether BSS has security type that would require exchanging EAPOL frames.
260    pub fn needs_eapol_exchange(&self) -> bool {
261        match self.protection() {
262            Protection::Unknown
263            | Protection::Open
264            | Protection::OpenOweTransition
265            | Protection::Wep => false,
266            _ => true,
267        }
268    }
269
270    /// Categorize BSS on what protection it supports.
271    pub fn protection(&self) -> Protection {
272        if !CapabilityInfo(self.capability_info).privacy() {
273            if self.owe_transition_range.is_some() {
274                return Protection::OpenOweTransition;
275            } else {
276                return Protection::Open;
277            }
278        }
279
280        let supports_wpa_1 = self
281            .wpa_ie()
282            .map(|wpa_ie| {
283                let rsne = ie::rsn::rsne::Rsne {
284                    group_data_cipher_suite: Some(wpa_ie.multicast_cipher),
285                    pairwise_cipher_suites: wpa_ie.unicast_cipher_list,
286                    akm_suites: wpa_ie.akm_list,
287                    ..Default::default()
288                };
289                suite_filter::WPA1_PERSONAL.is_satisfied(&rsne)
290            })
291            .unwrap_or(false);
292
293        let rsne = match self.rsne() {
294            Some(rsne) => match ie::rsn::rsne::from_bytes(rsne) {
295                Ok((_, rsne)) => rsne,
296                Err(_e) => {
297                    return Protection::Unknown;
298                }
299            },
300            None if self.find_wpa_ie().is_some() => {
301                if supports_wpa_1 {
302                    return Protection::Wpa1;
303                } else {
304                    return Protection::Unknown;
305                }
306            }
307            None => return Protection::Wep,
308        };
309
310        let rsn_caps = rsne.rsn_capabilities.as_ref().unwrap_or(&ie::rsn::rsne::RsnCapabilities(0));
311        let mfp_req = rsn_caps.mgmt_frame_protection_req();
312        let mfp_cap = rsn_caps.mgmt_frame_protection_cap();
313
314        if suite_filter::WPA3_PERSONAL.is_satisfied(&rsne) {
315            if suite_filter::WPA2_PERSONAL.is_satisfied(&rsne) {
316                if mfp_cap {
317                    return Protection::Wpa2Wpa3Personal;
318                }
319            } else if mfp_cap && mfp_req {
320                return Protection::Wpa3Personal;
321            }
322        }
323        if suite_filter::WPA2_PERSONAL.is_satisfied(&rsne) {
324            if supports_wpa_1 {
325                return Protection::Wpa1Wpa2Personal;
326            } else {
327                return Protection::Wpa2Personal;
328            }
329        }
330        if suite_filter::WPA2_PERSONAL_TKIP_ONLY.is_satisfied(&rsne) {
331            if supports_wpa_1 {
332                return Protection::Wpa1Wpa2PersonalTkipOnly;
333            } else {
334                return Protection::Wpa2PersonalTkipOnly;
335            }
336        }
337        if supports_wpa_1 {
338            return Protection::Wpa1;
339        }
340        if suite_filter::WPA3_ENTERPRISE_192_BIT.is_satisfied(&rsne) {
341            if mfp_cap && mfp_req {
342                return Protection::Wpa3Enterprise;
343            }
344        }
345        if suite_filter::WPA2_ENTERPRISE.is_satisfied(&rsne) {
346            return Protection::Wpa2Enterprise;
347        }
348        if suite_filter::OWE.is_satisfied(&rsne) {
349            return Protection::Owe;
350        }
351        Protection::Unknown
352    }
353
354    /// Get the latest WLAN standard that the BSS supports.
355    pub fn latest_standard(&self) -> Standard {
356        if self.vht_cap().is_some() && self.vht_op().is_some() {
357            Standard::Dot11Ac
358        } else if self.ht_cap().is_some() && self.ht_op().is_some() {
359            Standard::Dot11N
360        } else if self.channel.band == fidl_ieee80211::WlanBand::TwoGhz {
361            if self.rates.iter().any(|r| match r.rate() {
362                12 | 18 | 24 | 36 | 48 | 72 | 96 | 108 => true,
363                _ => false,
364            }) {
365                Standard::Dot11G
366            } else {
367                Standard::Dot11B
368            }
369        } else {
370            Standard::Dot11A
371        }
372    }
373
374    /// Search for vendor-specific Info Element for WPA. If found, return the body.
375    pub fn find_wpa_ie(&self) -> Option<&[u8]> {
376        ie::Reader::new(&self.ies[..])
377            .filter_map(|(id, ie)| match id {
378                ie::Id::VENDOR_SPECIFIC => match ie::parse_vendor_ie(ie) {
379                    Ok(ie::VendorIe::MsftLegacyWpa(body)) => Some(&body[..]),
380                    _ => None,
381                },
382                _ => None,
383            })
384            .next()
385    }
386
387    /// Search for WPA Info Element and parse it. If no WPA Info Element is found, or a WPA Info
388    /// Element is found but is not valid, return an error.
389    pub fn wpa_ie(&self) -> Result<ie::wpa::WpaIe, anyhow::Error> {
390        ie::parse_wpa_ie(self.find_wpa_ie().ok_or_else(|| format_err!("no wpa ie found"))?)
391            .map_err(|e| e.into())
392    }
393
394    /// Search for vendor-specific Info Element for WMM Parameter. If found, return the body.
395    pub fn find_wmm_param(&self) -> Option<&[u8]> {
396        ie::Reader::new(&self.ies[..])
397            .filter_map(|(id, ie)| match id {
398                ie::Id::VENDOR_SPECIFIC => match ie::parse_vendor_ie(ie) {
399                    Ok(ie::VendorIe::WmmParam(body)) => Some(&body[..]),
400                    _ => None,
401                },
402                _ => None,
403            })
404            .next()
405    }
406
407    /// Search for WMM Parameter Element and parse it. If no WMM Parameter Element is found,
408    /// return an error.
409    pub fn wmm_param(&self) -> Result<Ref<&[u8], ie::WmmParam>, anyhow::Error> {
410        ie::parse_wmm_param(
411            self.find_wmm_param().ok_or_else(|| format_err!("no wmm parameter found"))?,
412        )
413        .map_err(|e| e.into())
414    }
415
416    /// Search for the WiFi Simple Configuration Info Element. If found, return the body.
417    pub fn find_wsc_ie(&self) -> Option<&[u8]> {
418        ie::Reader::new(&self.ies[..])
419            .filter_map(|(id, ie)| match id {
420                ie::Id::VENDOR_SPECIFIC => match ie::parse_vendor_ie(ie) {
421                    Ok(ie::VendorIe::Wsc(body)) => Some(&body[..]),
422                    _ => None,
423                },
424                _ => None,
425            })
426            .next()
427    }
428
429    pub fn probe_resp_wsc(&self) -> Option<ProbeRespWsc> {
430        match self.find_wsc_ie() {
431            Some(ie) => match parse_probe_resp_wsc(ie) {
432                Ok(wsc) => Some(wsc),
433                // Parsing could fail because the WSC IE comes from a beacon, which does
434                // not contain all the information that a probe response WSC is expected
435                // to have. We don't have the information to distinguish between a beacon
436                // and a probe response, so we let this case fail silently.
437                Err(_) => None,
438            },
439            None => None,
440        }
441    }
442
443    pub fn owe_transition(&self) -> Option<OweTransition> {
444        self.owe_transition_range.clone().map(|range| {
445            // Safe to unwrap because we already verified OWE Transition is parseable in TryFrom
446            parse_owe_transition(&self.ies[range]).unwrap()
447        })
448    }
449
450    pub fn supports_uapsd(&self) -> bool {
451        let wmm_info = ie::Reader::new(&self.ies[..])
452            .filter_map(|(id, ie)| match id {
453                ie::Id::VENDOR_SPECIFIC => match ie::parse_vendor_ie(ie) {
454                    Ok(ie::VendorIe::WmmInfo(body)) => {
455                        ie::parse_wmm_info(body).map(|wmm_info| *wmm_info).ok()
456                    }
457                    Ok(ie::VendorIe::WmmParam(body)) => {
458                        ie::parse_wmm_param(body).map(|wmm_param| wmm_param.wmm_info).ok()
459                    }
460                    _ => None,
461                },
462                _ => None,
463            })
464            .next();
465        wmm_info.map(|wmm_info| wmm_info.ap_wmm_info().uapsd()).unwrap_or(false)
466    }
467
468    /// IEEE 802.11-2016 4.5.4.8
469    pub fn supports_ft(&self) -> bool {
470        ie::Reader::new(&self.ies[..]).any(|(id, _ie)| id == ie::Id::MOBILITY_DOMAIN)
471    }
472
473    /// Returns a simplified BssCandidacy which implements PartialOrd.
474    pub fn candidacy(&self) -> BssCandidacy {
475        let rssi_dbm = self.rssi_dbm;
476        match rssi_dbm {
477            // The value 0 is considered a marker for an invalid RSSI and is therefore
478            // transformed to the minimum RSSI value.
479            0 => BssCandidacy { protection: self.protection(), rssi_dbm: i8::MIN },
480            _ => BssCandidacy { protection: self.protection(), rssi_dbm },
481        }
482    }
483
484    /// Returns a string representation of the BssDescriptionExt. This representation
485    /// is not suitable for protecting the privacy of an SSID and BSSID.
486    pub fn to_non_obfuscated_string(&self) -> String {
487        format!(
488            "SSID: {}, BSSID: {}, Protection: {}, Pri Chan: {}, Rx dBm: {}",
489            self.ssid.to_string_not_redactable(),
490            self.bssid,
491            self.protection(),
492            self.channel.primary,
493            self.rssi_dbm,
494        )
495    }
496
497    pub fn is_open(&self) -> bool {
498        matches!(self.protection(), Protection::Open | Protection::OpenOweTransition)
499    }
500
501    pub fn has_owe_configured(&self) -> bool {
502        matches!(self.protection(), Protection::Owe)
503    }
504
505    pub fn has_wep_configured(&self) -> bool {
506        matches!(self.protection(), Protection::Wep)
507    }
508
509    pub fn has_wpa1_configured(&self) -> bool {
510        matches!(
511            self.protection(),
512            Protection::Wpa1 | Protection::Wpa1Wpa2PersonalTkipOnly | Protection::Wpa1Wpa2Personal
513        )
514    }
515
516    pub fn has_wpa2_personal_configured(&self) -> bool {
517        matches!(
518            self.protection(),
519            Protection::Wpa1Wpa2PersonalTkipOnly
520                | Protection::Wpa1Wpa2Personal
521                | Protection::Wpa2PersonalTkipOnly
522                | Protection::Wpa2Personal
523                | Protection::Wpa2Wpa3Personal
524        )
525    }
526
527    pub fn has_wpa3_personal_configured(&self) -> bool {
528        matches!(self.protection(), Protection::Wpa2Wpa3Personal | Protection::Wpa3Personal)
529    }
530}
531
532impl From<BssDescription> for fidl_ieee80211::BssDescription {
533    fn from(bss: BssDescription) -> fidl_ieee80211::BssDescription {
534        let (bandwidth, secondary80_num) = bss.channel.cbw.to_fidl();
535        let secondary80 =
536            fidl_ieee80211::ChannelNumber { band: bss.channel.band, number: secondary80_num };
537        fidl_ieee80211::BssDescription {
538            bssid: bss.bssid.to_array(),
539            bss_type: bss.bss_type,
540            beacon_period: bss.beacon_period,
541            capability_info: bss.capability_info,
542            primary: bss.channel.into(),
543            bandwidth,
544            vht_secondary_80_channel: secondary80,
545            rssi_dbm: bss.rssi_dbm,
546            snr_db: bss.snr_db,
547            ies: bss.ies,
548        }
549    }
550}
551
552impl fmt::Display for BssDescription {
553    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
554        write!(
555            f,
556            "SSID: {}, BSSID: {}, Protection: {}, Pri Chan: {}, Rx dBm: {}",
557            self.ssid,
558            self.bssid,
559            self.protection(),
560            self.channel.primary,
561            self.rssi_dbm,
562        )
563    }
564}
565// TODO(https://fxbug.dev/42164415): The error printed should include a minimal amount of information
566// about the BSS Description that could not be converted to aid debugging.
567impl TryFrom<fidl_ieee80211::BssDescription> for BssDescription {
568    type Error = anyhow::Error;
569
570    fn try_from(bss: fidl_ieee80211::BssDescription) -> Result<BssDescription, Self::Error> {
571        let mut ssid_range = None;
572        let mut rates = None;
573        let mut tim_range = None;
574        let mut country_range = None;
575        let mut rsne_range = None;
576        let mut ht_cap_range = None;
577        let mut ht_op_range = None;
578        let mut rm_enabled_cap_range = None;
579        let mut ext_cap_range = None;
580        let mut vht_cap_range = None;
581        let mut vht_op_range = None;
582        let mut rsnxe_range = None;
583        let mut owe_transition_range = None;
584
585        for (ie_type, range) in ie::IeSummaryIter::new(&bss.ies[..]) {
586            let body = &bss.ies[range.clone()];
587            match ie_type {
588                IeType::SSID => {
589                    ie::parse_ssid(body)?;
590                    ssid_range = Some(range);
591                }
592                IeType::SUPPORTED_RATES => {
593                    rates.get_or_insert(vec![]).extend(&*ie::parse_supported_rates(body)?);
594                }
595                IeType::EXTENDED_SUPPORTED_RATES => {
596                    rates.get_or_insert(vec![]).extend(&*ie::parse_extended_supported_rates(body)?);
597                }
598                IeType::TIM => {
599                    ie::parse_tim(body)?;
600                    tim_range = Some(range);
601                }
602                IeType::COUNTRY => country_range = Some(range),
603                // Decrement start of range by two to include the IE header.
604                IeType::RSNE => rsne_range = Some(range.start - 2..range.end),
605                IeType::HT_CAPABILITIES => {
606                    ie::parse_ht_capabilities(body)?;
607                    ht_cap_range = Some(range);
608                }
609                IeType::HT_OPERATION => {
610                    ie::parse_ht_operation(body)?;
611                    ht_op_range = Some(range);
612                }
613                IeType::RM_ENABLED_CAPABILITIES => {
614                    if let Ok(_) = ie::parse_rm_enabled_capabilities(body) {
615                        rm_enabled_cap_range = Some(range);
616                    }
617                }
618                IeType::EXT_CAPABILITIES => {
619                    // Parsing ExtCapabilities always succeeds, so no need to test parsing it here
620                    ext_cap_range = Some(range);
621                }
622                IeType::VHT_CAPABILITIES => {
623                    ie::parse_vht_capabilities(body)?;
624                    vht_cap_range = Some(range);
625                }
626                IeType::VHT_OPERATION => {
627                    ie::parse_vht_operation(body)?;
628                    vht_op_range = Some(range);
629                }
630                IeType::RSNXE => {
631                    rsnxe_range = Some(range);
632                }
633                IeType::OWE_TRANSITION => {
634                    if let Ok(_) = parse_owe_transition(body) {
635                        owe_transition_range = Some(range);
636                    }
637                }
638                _ => (),
639            }
640        }
641
642        let ssid_range = ssid_range.ok_or_else(|| format_err!("Missing SSID IE"))?;
643        let rates = rates.ok_or_else(|| format_err!("Missing rates IE"))?;
644
645        Ok(Self {
646            ssid: Ssid::from_bytes_unchecked(bss.ies[ssid_range].to_vec()),
647            bssid: Bssid::from(bss.bssid),
648            bss_type: bss.bss_type,
649            beacon_period: bss.beacon_period,
650            capability_info: bss.capability_info,
651            channel: crate::channel::Channel::from_fidl(
652                bss.primary,
653                bss.bandwidth,
654                bss.vht_secondary_80_channel,
655            )?,
656            rssi_dbm: bss.rssi_dbm,
657            snr_db: bss.snr_db,
658            ies: bss.ies,
659
660            rates,
661            tim_range,
662            country_range,
663            rsne_range,
664            ht_cap_range,
665            ht_op_range,
666            rm_enabled_cap_range,
667            ext_cap_range,
668            vht_cap_range,
669            vht_op_range,
670            rsnxe_range,
671            owe_transition_range,
672        })
673    }
674}
675
676/// The BssCandidacy type is used to rank fidl_common::BssDescription values. It is ordered
677/// first by Protection and then by Dbm.
678#[derive(Debug, Eq, PartialEq)]
679pub struct BssCandidacy {
680    protection: Protection,
681    rssi_dbm: i8,
682}
683
684impl PartialOrd for BssCandidacy {
685    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
686        Some(self.cmp(other))
687    }
688}
689
690impl Ord for BssCandidacy {
691    fn cmp(&self, other: &Self) -> Ordering {
692        self.protection.cmp(&other.protection).then(self.rssi_dbm.cmp(&other.rssi_dbm))
693    }
694}
695
696/// Given a list of BssDescription, categorize each one based on the latest PHY standard it
697/// supports and return a mapping from Standard to number of BSS.
698pub fn phy_standard_map(bss_list: &Vec<BssDescription>) -> HashMap<Standard, usize> {
699    info_map(bss_list, |bss| bss.latest_standard())
700}
701
702/// Given a list of BssDescription, return a mapping from channel to the number of BSS using
703/// that channel.
704pub fn channel_map(bss_list: &Vec<BssDescription>) -> HashMap<u8, usize> {
705    info_map(bss_list, |bss| bss.channel.primary)
706}
707
708fn info_map<F, T>(bss_list: &Vec<BssDescription>, f: F) -> HashMap<T, usize>
709where
710    T: Eq + Hash,
711    F: Fn(&BssDescription) -> T,
712{
713    let mut info_map: HashMap<T, usize> = HashMap::new();
714    for bss in bss_list {
715        *info_map.entry(f(&bss)).or_insert(0) += 1
716    }
717    info_map
718}
719
720#[cfg(test)]
721mod tests {
722    use super::*;
723    use crate::channel::Cbw;
724    use crate::fake_bss_description;
725    use crate::ie::IeType;
726    use crate::ie::fake_ies::{fake_owe_transition, fake_wmm_param};
727    use crate::test_utils::fake_frames::{
728        fake_unknown_rsne, fake_wmm_param_body, fake_wpa1_ie_body, fake_wpa2_mfpc_rsne,
729        fake_wpa2_mfpr_rsne, fake_wpa2_rsne, fake_wpa2_wpa3_mfpr_rsne, fake_wpa2_wpa3_no_mfp_rsne,
730        invalid_wpa3_enterprise_192_bit_rsne, invalid_wpa3_rsne,
731    };
732    use crate::test_utils::fake_stas::IesOverrides;
733    use assert_matches::assert_matches;
734    use test_case::test_case;
735
736    #[test_case(fake_bss_description!(
737        Wpa1Wpa2,
738        channel: Channel::new(36, Cbw::Cbw80P80{ secondary80: 106 }, fidl_ieee80211::WlanBand::FiveGhz),
739        rssi_dbm: -20,
740        short_preamble: true,
741        ies_overrides: IesOverrides::new()
742            .set(IeType::DSSS_PARAM_SET, [136].to_vec())
743    ))]
744    #[test_case(fake_bss_description!(
745        Open,
746        channel: Channel::new(1, Cbw::Cbw20, fidl_ieee80211::WlanBand::TwoGhz),
747        beacon_period: 110,
748        short_preamble: true,
749        radio_measurement: true,
750        rates: vec![0x02, 0x04, 0x0c],
751    ))]
752    fn test_bss_lossless_conversion(bss: BssDescription) {
753        let fidl_bss = fidl_ieee80211::BssDescription::from(bss.clone());
754        assert_eq!(bss, BssDescription::try_from(fidl_bss.clone()).unwrap());
755        assert_eq!(
756            fidl_bss,
757            fidl_ieee80211::BssDescription::from(
758                BssDescription::try_from(fidl_bss.clone()).unwrap()
759            )
760        );
761    }
762
763    #[test]
764    fn test_known_protection() {
765        assert_eq!(Protection::Open, fake_bss_description!(Open).protection());
766        assert_eq!(
767            Protection::OpenOweTransition,
768            fake_bss_description!(OpenOweTransition).protection()
769        );
770        assert_eq!(Protection::Owe, fake_bss_description!(Owe).protection());
771        assert_eq!(Protection::Wep, fake_bss_description!(Wep).protection());
772        assert_eq!(Protection::Wpa1, fake_bss_description!(Wpa1).protection());
773        assert_eq!(Protection::Wpa1, fake_bss_description!(Wpa1Enhanced).protection());
774        assert_eq!(
775            Protection::Wpa1Wpa2PersonalTkipOnly,
776            fake_bss_description!(Wpa1Wpa2TkipOnly).protection()
777        );
778        assert_eq!(
779            Protection::Wpa2PersonalTkipOnly,
780            fake_bss_description!(Wpa2TkipOnly).protection()
781        );
782        assert_eq!(Protection::Wpa1Wpa2Personal, fake_bss_description!(Wpa1Wpa2).protection());
783        assert_eq!(Protection::Wpa2Personal, fake_bss_description!(Wpa2TkipCcmp).protection());
784        assert_eq!(Protection::Wpa2Personal, fake_bss_description!(Wpa2).protection());
785        assert_eq!(Protection::Wpa2Wpa3Personal, fake_bss_description!(Wpa2Wpa3).protection());
786        assert_eq!(Protection::Wpa3Personal, fake_bss_description!(Wpa3).protection());
787        assert_eq!(Protection::Wpa2Enterprise, fake_bss_description!(Wpa2Enterprise).protection());
788        assert_eq!(Protection::Wpa3Enterprise, fake_bss_description!(Wpa3Enterprise).protection());
789    }
790
791    #[test]
792    fn test_pmf_configs_supported() {
793        let bss = fake_bss_description!(Wpa2,
794            ies_overrides: IesOverrides::new()
795                .set(IeType::RSNE, fake_wpa2_mfpc_rsne()[2..].to_vec())
796        );
797        assert_eq!(Protection::Wpa2Personal, bss.protection());
798
799        let bss = fake_bss_description!(Wpa2,
800            ies_overrides: IesOverrides::new()
801                .set(IeType::RSNE, fake_wpa2_mfpr_rsne()[2..].to_vec())
802        );
803        assert_eq!(Protection::Wpa2Personal, bss.protection());
804
805        let bss = fake_bss_description!(Wpa2,
806            ies_overrides: IesOverrides::new()
807                .set(IeType::RSNE, fake_wpa2_wpa3_mfpr_rsne()[2..].to_vec())
808        );
809        assert_eq!(Protection::Wpa2Wpa3Personal, bss.protection());
810    }
811
812    #[test]
813    fn test_downgrade() {
814        // If Wpa3 doesn't use MFP, ignore it and use Wpa2 instead.
815        let bss = fake_bss_description!(Wpa2,
816            ies_overrides: IesOverrides::new()
817                .set(IeType::RSNE, fake_wpa2_wpa3_no_mfp_rsne()[2..].to_vec())
818        );
819        assert_eq!(Protection::Wpa2Personal, bss.protection());
820
821        // Downgrade to Wpa1 as well.
822        let bss = fake_bss_description!(Wpa1,
823            ies_overrides: IesOverrides::new()
824                .set(IeType::RSNE, invalid_wpa3_rsne()[2..].to_vec())
825        );
826        assert_eq!(Protection::Wpa1, bss.protection());
827    }
828
829    #[test]
830    fn test_unknown_protection() {
831        let bss = fake_bss_description!(Wpa2,
832            ies_overrides: IesOverrides::new()
833                .set(IeType::RSNE, fake_unknown_rsne()[2..].to_vec())
834        );
835        assert_eq!(Protection::Unknown, bss.protection());
836
837        let bss = fake_bss_description!(Wpa2,
838            ies_overrides: IesOverrides::new()
839                .set(IeType::RSNE, invalid_wpa3_rsne()[2..].to_vec())
840        );
841        assert_eq!(Protection::Unknown, bss.protection());
842
843        let bss = fake_bss_description!(Wpa2,
844            ies_overrides: IesOverrides::new()
845                .set(IeType::RSNE, invalid_wpa3_enterprise_192_bit_rsne()[2..].to_vec())
846        );
847        assert_eq!(Protection::Unknown, bss.protection());
848    }
849
850    #[test]
851    fn test_needs_eapol_exchange() {
852        assert!(fake_bss_description!(Owe).needs_eapol_exchange());
853        assert!(fake_bss_description!(Wpa1).needs_eapol_exchange());
854        assert!(fake_bss_description!(Wpa2).needs_eapol_exchange());
855
856        assert!(!fake_bss_description!(Open).needs_eapol_exchange());
857        assert!(!fake_bss_description!(OpenOweTransition).needs_eapol_exchange());
858        assert!(!fake_bss_description!(Wep).needs_eapol_exchange());
859    }
860
861    #[test]
862    fn test_rm_enabled_cap_ie() {
863        let bss = fake_bss_description!(Wpa2,
864            ies_overrides: IesOverrides::new()
865                .remove(IeType::RM_ENABLED_CAPABILITIES)
866        );
867        assert!(bss.rm_enabled_cap().is_none());
868
869        #[rustfmt::skip]
870        let rm_enabled_capabilities = vec![
871            0x03, // link measurement and neighbor report enabled
872            0x00, 0x00, 0x00, 0x00,
873        ];
874        let bss = fake_bss_description!(Wpa2,
875            ies_overrides: IesOverrides::new()
876                .remove(IeType::RM_ENABLED_CAPABILITIES)
877                .set(IeType::RM_ENABLED_CAPABILITIES, rm_enabled_capabilities.clone())
878        );
879        assert_matches!(bss.rm_enabled_cap(), Some(cap) => {
880            assert_eq!(cap.as_bytes(), &rm_enabled_capabilities[..]);
881        });
882    }
883
884    #[test]
885    fn test_ext_cap_ie() {
886        let bss = fake_bss_description!(Wpa2,
887            ies_overrides: IesOverrides::new()
888                .remove(IeType::EXT_CAPABILITIES)
889        );
890        assert!(bss.ext_cap().is_none());
891
892        #[rustfmt::skip]
893        let ext_capabilities = vec![
894            0x04, 0x00,
895            0x08, // BSS transition supported
896            0x00, 0x00, 0x00, 0x00, 0x40
897        ];
898        let bss = fake_bss_description!(Wpa2,
899            ies_overrides: IesOverrides::new()
900                .remove(IeType::EXT_CAPABILITIES)
901                .set(IeType::EXT_CAPABILITIES, ext_capabilities.clone())
902        );
903        let ext_cap = bss.ext_cap().expect("expect bss.ext_cap() to be Some");
904        assert_eq!(ext_cap.ext_caps_octet_1.map(|o| o.0), Some(0x04));
905        assert_eq!(ext_cap.ext_caps_octet_2.map(|o| o.0), Some(0x00));
906        assert_eq!(ext_cap.ext_caps_octet_3.map(|o| o.0), Some(0x08));
907        assert_eq!(ext_cap.remaining, &[0x00, 0x00, 0x00, 0x00, 0x40]);
908    }
909
910    #[test]
911    fn test_wpa_ie() {
912        let buf =
913            fake_bss_description!(Wpa1).wpa_ie().expect("failed to find WPA1 IE").into_bytes();
914        assert_eq!(&fake_wpa1_ie_body(false)[..], &buf[..]);
915        fake_bss_description!(Wpa2).wpa_ie().expect_err("found unexpected WPA1 IE");
916    }
917
918    #[test]
919    fn test_wmm_param() {
920        let bss = fake_bss_description!(Wpa2, qos: true, wmm_param: Some(fake_wmm_param()));
921        let wmm_param = bss.wmm_param().expect("failed to find wmm param");
922        assert_eq!(fake_wmm_param_body(), wmm_param.as_bytes());
923    }
924
925    #[test]
926    fn test_owe_transition() {
927        let bss = fake_bss_description!(OpenOweTransition);
928        let owe_transition = bss.owe_transition().expect("failed to find owe transition ie");
929        assert_eq!(owe_transition, fake_owe_transition());
930    }
931
932    #[test]
933    fn test_latest_standard_ac() {
934        let bss = fake_bss_description!(Open,
935            ies_overrides: IesOverrides::new()
936                .set(IeType::VHT_CAPABILITIES, vec![0; fidl_ieee80211::VHT_CAP_LEN as usize])
937                .set(IeType::VHT_OPERATION, vec![0; fidl_ieee80211::VHT_OP_LEN as usize]),
938        );
939        assert_eq!(Standard::Dot11Ac, bss.latest_standard());
940    }
941
942    #[test]
943    fn test_latest_standard_n() {
944        let bss = fake_bss_description!(Open,
945            ies_overrides: IesOverrides::new()
946                .set(IeType::HT_CAPABILITIES, vec![0; fidl_ieee80211::HT_CAP_LEN as usize])
947                .set(IeType::HT_OPERATION, vec![0; fidl_ieee80211::HT_OP_LEN as usize])
948                .remove(IeType::VHT_CAPABILITIES)
949                .remove(IeType::VHT_OPERATION),
950        );
951        assert_eq!(Standard::Dot11N, bss.latest_standard());
952    }
953
954    #[test]
955    fn test_latest_standard_g() {
956        let bss = fake_bss_description!(Open,
957            channel: Channel::new(1, Cbw::Cbw20, fidl_ieee80211::WlanBand::TwoGhz),
958            rates: vec![12],
959            ies_overrides: IesOverrides::new()
960                .remove(IeType::HT_CAPABILITIES)
961                .remove(IeType::HT_OPERATION)
962                .remove(IeType::VHT_CAPABILITIES)
963                .remove(IeType::VHT_OPERATION),
964        );
965        assert_eq!(Standard::Dot11G, bss.latest_standard());
966    }
967
968    #[test]
969    fn test_latest_standard_b() {
970        let bss = fake_bss_description!(Open,
971            channel: Channel::new(1, Cbw::Cbw20, fidl_ieee80211::WlanBand::TwoGhz),
972            rates: vec![2],
973            ies_overrides: IesOverrides::new()
974                .remove(IeType::HT_CAPABILITIES)
975                .remove(IeType::HT_OPERATION)
976                .remove(IeType::VHT_CAPABILITIES)
977                .remove(IeType::VHT_OPERATION),
978        );
979        assert_eq!(Standard::Dot11B, bss.latest_standard());
980    }
981
982    #[test]
983    fn test_latest_standard_b_with_basic() {
984        let bss = fake_bss_description!(Open,
985            channel: Channel::new(1, Cbw::Cbw20, fidl_ieee80211::WlanBand::TwoGhz),
986            rates: vec![ie::SupportedRate(2).with_basic(true).0],
987            ies_overrides: IesOverrides::new()
988                .remove(IeType::HT_CAPABILITIES)
989                .remove(IeType::HT_OPERATION)
990                .remove(IeType::VHT_CAPABILITIES)
991                .remove(IeType::VHT_OPERATION),
992        );
993        assert_eq!(Standard::Dot11B, bss.latest_standard());
994    }
995
996    #[test]
997    fn test_latest_standard_a() {
998        let bss = fake_bss_description!(Open,
999            channel: Channel::new(36, Cbw::Cbw20, fidl_ieee80211::WlanBand::FiveGhz),
1000            rates: vec![48],
1001            ies_overrides: IesOverrides::new()
1002                .remove(IeType::HT_CAPABILITIES)
1003                .remove(IeType::HT_OPERATION)
1004                .remove(IeType::VHT_CAPABILITIES)
1005                .remove(IeType::VHT_OPERATION),
1006        );
1007        assert_eq!(Standard::Dot11A, bss.latest_standard());
1008    }
1009
1010    #[test]
1011    fn test_supports_uapsd() {
1012        let bss = fake_bss_description!(Wpa2,
1013            ies_overrides: IesOverrides::new()
1014                .remove(IeType::WMM_INFO)
1015                .remove(IeType::WMM_PARAM)
1016        );
1017        assert!(!bss.supports_uapsd());
1018
1019        let mut wmm_info = vec![0x80]; // U-APSD enabled
1020        let bss = fake_bss_description!(Wpa2,
1021            ies_overrides: IesOverrides::new()
1022                .remove(IeType::WMM_INFO)
1023                .remove(IeType::WMM_PARAM)
1024                .set(IeType::WMM_INFO, wmm_info.clone())
1025        );
1026        assert!(bss.supports_uapsd());
1027
1028        wmm_info = vec![0x00]; // U-APSD not enabled
1029        let bss = fake_bss_description!(Wpa2,
1030            ies_overrides: IesOverrides::new()
1031                .remove(IeType::WMM_INFO)
1032                .remove(IeType::WMM_PARAM)
1033                .set(IeType::WMM_INFO, wmm_info)
1034        );
1035        assert!(!bss.supports_uapsd());
1036
1037        #[rustfmt::skip]
1038        let mut wmm_param = vec![
1039            0x80, // U-APSD enabled
1040            0x00, // reserved
1041            0x03, 0xa4, 0x00, 0x00, // AC_BE parameters
1042            0x27, 0xa4, 0x00, 0x00, // AC_BK parameters
1043            0x42, 0x43, 0x5e, 0x00, // AC_VI parameters
1044            0x62, 0x32, 0x2f, 0x00, // AC_VO parameters
1045        ];
1046        let bss = fake_bss_description!(Wpa2,
1047            ies_overrides: IesOverrides::new()
1048                .remove(IeType::WMM_INFO)
1049                .remove(IeType::WMM_PARAM)
1050                .set(IeType::WMM_PARAM, wmm_param.clone())
1051        );
1052        assert!(bss.supports_uapsd());
1053
1054        wmm_param[0] = 0x00; // U-APSD not enabled
1055        let bss = fake_bss_description!(Wpa2,
1056            ies_overrides: IesOverrides::new()
1057                .remove(IeType::WMM_INFO)
1058                .remove(IeType::WMM_PARAM)
1059                .set(IeType::WMM_PARAM, wmm_param)
1060        );
1061        assert!(!bss.supports_uapsd());
1062    }
1063
1064    #[test]
1065    fn test_supports_ft() {
1066        let bss = fake_bss_description!(Wpa2,
1067            ies_overrides: IesOverrides::new()
1068                .remove(IeType::MOBILITY_DOMAIN)
1069        );
1070        assert!(!bss.supports_ft());
1071
1072        let bss = fake_bss_description!(Wpa2,
1073            ies_overrides: IesOverrides::new()
1074                .remove(IeType::MOBILITY_DOMAIN)
1075                // We only check that the IE exists, so just set the content to bytes 0's.
1076                .set(IeType::MOBILITY_DOMAIN, vec![0x00; 3])
1077        );
1078        assert!(bss.supports_ft());
1079    }
1080
1081    #[test]
1082    fn test_candidacy() {
1083        let bss_candidacy = fake_bss_description!(Wpa2, rssi_dbm: -10).candidacy();
1084        assert_eq!(
1085            bss_candidacy,
1086            BssCandidacy { protection: Protection::Wpa2Personal, rssi_dbm: -10 }
1087        );
1088
1089        let bss_candidacy = fake_bss_description!(Open, rssi_dbm: -10).candidacy();
1090        assert_eq!(bss_candidacy, BssCandidacy { protection: Protection::Open, rssi_dbm: -10 });
1091
1092        let bss_candidacy = fake_bss_description!(Wpa2, rssi_dbm: -20).candidacy();
1093        assert_eq!(
1094            bss_candidacy,
1095            BssCandidacy { protection: Protection::Wpa2Personal, rssi_dbm: -20 }
1096        );
1097
1098        let bss_candidacy = fake_bss_description!(Wpa2, rssi_dbm: 0).candidacy();
1099        assert_eq!(
1100            bss_candidacy,
1101            BssCandidacy { protection: Protection::Wpa2Personal, rssi_dbm: i8::MIN }
1102        );
1103    }
1104
1105    fn assert_bss_comparison(worse: &BssDescription, better: &BssDescription) {
1106        assert_eq!(Ordering::Less, worse.candidacy().cmp(&better.candidacy()));
1107        assert_eq!(Ordering::Greater, better.candidacy().cmp(&worse.candidacy()));
1108    }
1109
1110    #[test]
1111    fn test_bss_comparison() {
1112        //  Two BSSDescription values with the same protection and RSSI are equivalent.
1113        assert_eq!(
1114            Ordering::Equal,
1115            fake_bss_description!(Wpa2, rssi_dbm: -10)
1116                .candidacy()
1117                .cmp(&fake_bss_description!(Wpa2, rssi_dbm: -10).candidacy())
1118        );
1119
1120        // Higher security is better.
1121        assert_bss_comparison(
1122            &fake_bss_description!(Wpa1, rssi_dbm: -10),
1123            &fake_bss_description!(Wpa2, rssi_dbm: -50),
1124        );
1125        assert_bss_comparison(
1126            &fake_bss_description!(Open, rssi_dbm: -10),
1127            &fake_bss_description!(Wpa2, rssi_dbm: -50),
1128        );
1129        // Higher RSSI is better if security is equivalent.
1130        assert_bss_comparison(
1131            &fake_bss_description!(Wpa2, rssi_dbm: -50),
1132            &fake_bss_description!(Wpa2, rssi_dbm: -10),
1133        );
1134        // Having an RSSI measurement is always better than not having any measurement
1135        assert_bss_comparison(
1136            &fake_bss_description!(Wpa2, rssi_dbm: 0),
1137            &fake_bss_description!(Wpa2, rssi_dbm: -100),
1138        );
1139    }
1140
1141    #[test]
1142    fn test_bss_ie_fields() {
1143        #[rustfmt::skip]
1144        let ht_cap = vec![
1145            0xef, 0x09, // HT Capabilities Info
1146            0x1b, // A-MPDU Parameters: 0x1b
1147            0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // MCS Set
1148            0x00, 0x00, // HT Extended Capabilities
1149            0x00, 0x00, 0x00, 0x00, // Transmit Beamforming Capabilities
1150            0x00
1151        ];
1152        #[rustfmt::skip]
1153        let ht_op = vec![
1154            0x9d, // Primary Channel: 157
1155            0x0d, // HT Info Subset - secondary channel above, any channel width, RIFS permitted
1156            0x00, 0x00, 0x00, 0x00, // HT Info Subsets
1157            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Basic MCS Set
1158        ];
1159        #[rustfmt::skip]
1160        let vht_cap = vec![
1161            0xb2, 0x01, 0x80, 0x33, // VHT Capabilities Info
1162            0xea, 0xff, 0x00, 0x00, 0xea, 0xff, 0x00, 0x00, // VHT Supported MCS Set
1163        ];
1164        let vht_op = vec![0x01, 0x9b, 0x00, 0xfc, 0xff];
1165        let rsnxe = vec![0b00100001];
1166
1167        let bss = fake_bss_description!(Wpa2,
1168            ies_overrides: IesOverrides::new()
1169                .set(IeType::SSID, b"ssidie".to_vec())
1170                .set(IeType::SUPPORTED_RATES, vec![0x81, 0x82, 0x83])
1171                .set(IeType::EXTENDED_SUPPORTED_RATES, vec![4, 5, 6])
1172                .set(IeType::COUNTRY, vec![1, 2, 3])
1173                .set(IeType::HT_CAPABILITIES, ht_cap.clone())
1174                .set(IeType::HT_OPERATION, ht_op.clone())
1175                .set(IeType::VHT_CAPABILITIES, vht_cap.clone())
1176                .set(IeType::VHT_OPERATION, vht_op.clone())
1177                .set(IeType::RSNXE, rsnxe.clone())
1178        );
1179        assert_eq!(bss.ssid, Ssid::try_from("ssidie").unwrap());
1180        assert_eq!(
1181            bss.rates(),
1182            &[
1183                ie::SupportedRate(0x81),
1184                ie::SupportedRate(0x82),
1185                ie::SupportedRate(0x83),
1186                ie::SupportedRate(4),
1187                ie::SupportedRate(5),
1188                ie::SupportedRate(6)
1189            ]
1190        );
1191        assert_eq!(bss.country(), Some(&[1, 2, 3][..]));
1192        assert_eq!(bss.rsne(), Some(&fake_wpa2_rsne()[..]));
1193        assert_matches!(bss.ht_cap(), Some(capability_info) => {
1194            assert_eq!(Ref::bytes(&capability_info), &ht_cap[..]);
1195        });
1196        assert_eq!(
1197            bss.raw_ht_cap().map(|capability_info| capability_info.bytes.to_vec()),
1198            Some(ht_cap)
1199        );
1200        assert_matches!(bss.ht_op(), Some(op) => {
1201            assert_eq!(Ref::bytes(&op), &ht_op[..]);
1202        });
1203        assert_eq!(bss.raw_ht_op().map(|op| op.bytes.to_vec()), Some(ht_op));
1204        assert_matches!(bss.vht_cap(), Some(capability_info) => {
1205            assert_eq!(Ref::bytes(&capability_info), &vht_cap[..]);
1206        });
1207        assert_eq!(
1208            bss.raw_vht_cap().map(|capability_info| capability_info.bytes.to_vec()),
1209            Some(vht_cap)
1210        );
1211        assert_matches!(bss.vht_op(), Some(op) => {
1212            assert_eq!(Ref::bytes(&op), &vht_op[..]);
1213        });
1214        assert_eq!(bss.raw_vht_op().map(|op| op.bytes.to_vec()), Some(vht_op));
1215        assert_matches!(bss.rsnxe(), Some(r) => {
1216            assert_eq!(Ref::bytes(&r.rsnxe_octet_1.expect("no octet 1")), &rsnxe[..]);
1217        });
1218    }
1219
1220    #[test]
1221    fn test_protection_conversions() {
1222        assert_eq!(
1223            Protection::Unknown,
1224            Protection::from(fidl_sme::Protection::from(Protection::Unknown))
1225        );
1226        assert_eq!(
1227            Protection::Open,
1228            Protection::from(fidl_sme::Protection::from(Protection::Open))
1229        );
1230        assert_eq!(
1231            Protection::OpenOweTransition,
1232            Protection::from(fidl_sme::Protection::from(Protection::OpenOweTransition))
1233        );
1234        assert_eq!(Protection::Owe, Protection::from(fidl_sme::Protection::from(Protection::Owe)));
1235        assert_eq!(Protection::Wep, Protection::from(fidl_sme::Protection::from(Protection::Wep)));
1236        assert_eq!(
1237            Protection::Wpa1,
1238            Protection::from(fidl_sme::Protection::from(Protection::Wpa1))
1239        );
1240        assert_eq!(
1241            Protection::Wpa1Wpa2PersonalTkipOnly,
1242            Protection::from(fidl_sme::Protection::from(Protection::Wpa1Wpa2PersonalTkipOnly))
1243        );
1244        assert_eq!(
1245            Protection::Wpa2PersonalTkipOnly,
1246            Protection::from(fidl_sme::Protection::from(Protection::Wpa2PersonalTkipOnly))
1247        );
1248        assert_eq!(
1249            Protection::Wpa1Wpa2Personal,
1250            Protection::from(fidl_sme::Protection::from(Protection::Wpa1Wpa2Personal))
1251        );
1252        assert_eq!(
1253            Protection::Wpa2Personal,
1254            Protection::from(fidl_sme::Protection::from(Protection::Wpa2Personal))
1255        );
1256        assert_eq!(
1257            Protection::Wpa2Wpa3Personal,
1258            Protection::from(fidl_sme::Protection::from(Protection::Wpa2Wpa3Personal))
1259        );
1260        assert_eq!(
1261            Protection::Wpa3Personal,
1262            Protection::from(fidl_sme::Protection::from(Protection::Wpa3Personal))
1263        );
1264        assert_eq!(
1265            Protection::Wpa2Enterprise,
1266            Protection::from(fidl_sme::Protection::from(Protection::Wpa2Enterprise))
1267        );
1268        assert_eq!(
1269            Protection::Wpa3Enterprise,
1270            Protection::from(fidl_sme::Protection::from(Protection::Wpa3Enterprise))
1271        );
1272    }
1273}