aboutsummaryrefslogtreecommitdiff
path: root/rust/daemon/src/wifi/frame.rs
blob: 1ec890443cfd7b1d485a2a5f5fa2b737d976ea38 (plain)
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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::ieee80211::{Ieee80211, MacAddress};
use super::packets::mac80211_hwsim::{
    HwsimAttr, HwsimCmd, HwsimMsg, HwsimMsgHdr, TxRate, TxRateFlag,
};
use crate::wifi::hwsim_attr_set::HwsimAttrSet;
use anyhow::{anyhow, Context};
use log::{info, warn};

/// Parser for the hwsim Frame command (HWSIM_CMD_FRAME).
///
/// The Frame command is sent by the kernel's mac80211_hwsim subsystem
/// and contains the IEEE 802.11 frame along with hwsim attributes.
///
/// This module parses the required and optional hwsim attributes and
/// returns errors if any required attributes are missing.

// The Frame struct contains parsed attributes along with the raw and
// parsed 802.11 frame in `data` and `ieee80211.`
#[derive(Debug)]
pub struct Frame {
    pub transmitter: MacAddress,
    pub flags: u32,
    pub tx_info: Vec<TxRate>,
    pub cookie: u64,
    pub signal: Option<u32>,
    pub freq: Option<u32>,
    pub data: Vec<u8>,
    pub ieee80211: Ieee80211,
}

impl Frame {
    // Builds and validates the Frame from the attributes in the
    // packet. Called when a hwsim packet with HwsimCmd::Frame is
    // found.
    pub fn parse(msg: &HwsimMsg) -> anyhow::Result<Frame> {
        // Only expected to be called with HwsimCmd::Frame
        if (msg.hwsim_hdr.hwsim_cmd != HwsimCmd::Frame) {
            panic!("Invalid hwsim_cmd");
        }
        let attrs = HwsimAttrSet::parse(&msg.attributes).context("HwsimAttrSet")?;
        let frame = attrs.frame.clone().context("Frame")?;
        let ieee80211 = Ieee80211::parse(&frame).context("Ieee80211")?;
        // Required attributes are unwrapped and return an error if
        // they are not present.
        Ok(Frame {
            transmitter: attrs.transmitter.context("transmitter")?,
            flags: attrs.flags.context("flags")?,
            tx_info: attrs.tx_info.clone().context("tx_info")?,
            cookie: attrs.cookie.context("cookie")?,
            signal: attrs.signal,
            freq: attrs.freq,
            data: frame,
            ieee80211,
        })
    }
}