wlan_common/mac/data/
fields.rs

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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::mac::{FrameControl, HtControl, MacAddr, OptionalField, Presence, SequenceControl};
use wlan_bitfield::bitfield;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};

// IEEE Std 802.11-2016, 9.2.4.5.1, Table 9-6
#[bitfield(
    0..=3   tid,
    4       eosp,
    5..=6   ack_policy,
    7       amsdu_present,
    8..=15  high_byte, // interpretation varies
)]
#[repr(C)]
#[derive(IntoBytes, KnownLayout, FromBytes, Immutable, Copy, Clone, PartialEq, Eq)]
pub struct QosControl(pub u16);

pub type Addr4 = MacAddr;

// IEEE Std 802.11-2016, 9.3.2.1
#[derive(
    KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned, PartialEq, Eq, Clone, Copy, Debug,
)]
#[repr(C, packed)]
pub struct FixedDataHdrFields {
    pub frame_ctrl: FrameControl,
    pub duration: u16,
    pub addr1: MacAddr,
    pub addr2: MacAddr,
    pub addr3: MacAddr,
    pub seq_ctrl: SequenceControl,
}

impl FixedDataHdrFields {
    /// Returns the length in bytes of a data header including all its fixed and optional
    /// fields (if they are present).
    pub fn len(
        has_addr4: Presence<Addr4>,
        has_qos_ctrl: Presence<QosControl>,
        has_ht_ctrl: Presence<HtControl>,
    ) -> usize {
        let mut bytes = std::mem::size_of::<Self>();
        bytes += match has_addr4 {
            Addr4::PRESENT => std::mem::size_of::<MacAddr>(),
            Addr4::ABSENT => 0,
        };
        bytes += match has_qos_ctrl {
            QosControl::PRESENT => std::mem::size_of::<QosControl>(),
            QosControl::ABSENT => 0,
        };
        bytes += match has_ht_ctrl {
            HtControl::PRESENT => std::mem::size_of::<HtControl>(),
            HtControl::ABSENT => 0,
        };
        bytes
    }
}

pub struct OptionalDataHdrFields {
    pub addr4: Option<Addr4>,
    pub qos_ctrl: Option<QosControl>,
    pub ht_ctrl: Option<HtControl>,
}
impl OptionalDataHdrFields {
    pub fn none() -> Self {
        Self { addr4: None, qos_ctrl: None, ht_ctrl: None }
    }
}

// IEEE Std 802.11-2016, Table 9-26 defines DA, SA, RA, TA, BSSID
pub fn data_dst_addr(hdr: &FixedDataHdrFields) -> MacAddr {
    let fc = hdr.frame_ctrl;
    if fc.to_ds() {
        hdr.addr3
    } else {
        hdr.addr1
    }
}

pub fn data_src_addr(hdr: &FixedDataHdrFields, addr4: Option<MacAddr>) -> Option<MacAddr> {
    let fc = hdr.frame_ctrl;
    match (fc.to_ds(), fc.from_ds()) {
        (_, false) => Some(hdr.addr2),
        (false, true) => Some(hdr.addr3),
        (true, true) => addr4,
    }
}

pub fn data_transmitter_addr(hdr: &FixedDataHdrFields) -> MacAddr {
    hdr.addr2
}

pub fn data_receiver_addr(hdr: &FixedDataHdrFields) -> MacAddr {
    hdr.addr1
}

/// BSSID: basic service set ID
pub fn data_bssid(hdr: &FixedDataHdrFields) -> Option<MacAddr> {
    let fc = hdr.frame_ctrl;
    match (fc.to_ds(), fc.from_ds()) {
        (false, false) => Some(hdr.addr3),
        (false, true) => Some(hdr.addr2),
        (true, false) => Some(hdr.addr1),
        (true, true) => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mac::*;
    use crate::test_utils::fake_frames::*;

    #[test]
    fn fixed_fields_len() {
        assert_eq!(
            FixedDataHdrFields::len(Addr4::ABSENT, QosControl::ABSENT, HtControl::ABSENT),
            24
        );
        assert_eq!(
            FixedDataHdrFields::len(Addr4::PRESENT, QosControl::ABSENT, HtControl::ABSENT),
            30
        );
        assert_eq!(
            FixedDataHdrFields::len(Addr4::ABSENT, QosControl::PRESENT, HtControl::ABSENT),
            26
        );
        assert_eq!(
            FixedDataHdrFields::len(Addr4::ABSENT, QosControl::ABSENT, HtControl::PRESENT),
            28
        );
        assert_eq!(
            FixedDataHdrFields::len(Addr4::PRESENT, QosControl::PRESENT, HtControl::ABSENT),
            32
        );
        assert_eq!(
            FixedDataHdrFields::len(Addr4::ABSENT, QosControl::PRESENT, HtControl::PRESENT),
            30
        );
        assert_eq!(
            FixedDataHdrFields::len(Addr4::PRESENT, QosControl::ABSENT, HtControl::PRESENT),
            34
        );
        assert_eq!(
            FixedDataHdrFields::len(Addr4::PRESENT, QosControl::PRESENT, HtControl::PRESENT),
            36
        );
    }

    #[test]
    fn fixed_fields_dst_addr() {
        let mut fixed_fields = make_data_hdr(None, [0, 0], None);
        let (mut fixed_fields, _) =
            Ref::<_, FixedDataHdrFields>::from_prefix(&mut fixed_fields[..])
                .expect("invalid data header");
        let mut fc = FrameControl(0);
        fc.set_to_ds(true);
        fixed_fields.frame_ctrl = fc;
        assert_eq!(data_dst_addr(&fixed_fields), MacAddr::from([5; 6])); // Addr3
        fc.set_to_ds(false);
        fixed_fields.frame_ctrl = fc;
        assert_eq!(data_dst_addr(&fixed_fields), MacAddr::from([3; 6])); // Addr1
    }

    #[test]
    fn fixed_fields_src_addr() {
        let mut fixed_fields = make_data_hdr(None, [0, 0], None);
        let (mut fixed_fields, _) =
            Ref::<_, FixedDataHdrFields>::from_prefix(&mut fixed_fields[..])
                .expect("invalid data header");
        let mut fc = FrameControl(0);
        // to_ds == false && from_ds == false
        fixed_fields.frame_ctrl = fc;
        assert_eq!(data_src_addr(&fixed_fields, None), Some(MacAddr::from([4; 6]))); // Addr2

        fc.set_to_ds(true);
        // to_ds == true && from_ds == false
        fixed_fields.frame_ctrl = fc;
        assert_eq!(data_src_addr(&fixed_fields, None), Some(MacAddr::from([4; 6]))); // Addr2

        fc.set_from_ds(true);
        // to_ds == true && from_ds == true;
        fixed_fields.frame_ctrl = fc;
        assert_eq!(
            data_src_addr(&fixed_fields, Some(MacAddr::from([11; 6]))),
            Some(MacAddr::from([11; 6])) // Addr4
        );

        fc.set_to_ds(false);
        // to_ds == false && from_ds == true;
        fixed_fields.frame_ctrl = fc;
        assert_eq!(
            data_src_addr(&fixed_fields, None),
            Some(MacAddr::from([5; 6])) // Addr3
        );
    }

    #[test]
    fn fixed_fields_ta() {
        let mut fixed_fields = make_data_hdr(None, [0, 0], None);
        let (fixed_fields, _) = Ref::<_, FixedDataHdrFields>::from_prefix(&mut fixed_fields[..])
            .expect("invalid data header");
        assert_eq!(
            data_transmitter_addr(&fixed_fields),
            MacAddr::from([4; 6]) // Addr2
        );
    }

    #[test]
    fn fixed_fields_ra() {
        let mut fixed_fields = make_data_hdr(None, [0, 0], None);
        let (fixed_fields, _) = Ref::<_, FixedDataHdrFields>::from_prefix(&mut fixed_fields[..])
            .expect("invalid data header");
        assert_eq!(data_receiver_addr(&fixed_fields), MacAddr::from([3; 6])); // Addr2
    }

    #[test]
    fn fixed_fields_bssid() {
        let mut fixed_fields = make_data_hdr(None, [0, 0], None);
        let (mut fixed_fields, _) =
            Ref::<_, FixedDataHdrFields>::from_prefix(&mut fixed_fields[..])
                .expect("invalid data header");
        let mut fc = FrameControl(0);
        // to_ds == false && from_ds == false
        fixed_fields.frame_ctrl = fc;
        assert_eq!(data_bssid(&fixed_fields), Some(MacAddr::from([5; 6]))); // Addr3

        fc.set_to_ds(true);
        // to_ds == true && from_ds == false
        fixed_fields.frame_ctrl = fc;
        assert_eq!(data_bssid(&fixed_fields), Some(MacAddr::from([3; 6]))); // Addr1

        fc.set_from_ds(true);
        // to_ds == true && from_ds == true;
        fixed_fields.frame_ctrl = fc;
        assert_eq!(data_bssid(&fixed_fields), None);

        fc.set_to_ds(false);
        // to_ds == false && from_ds == true;
        fixed_fields.frame_ctrl = fc;
        assert_eq!(data_bssid(&fixed_fields), Some(MacAddr::from([4; 6]))); // Addr2
    }
}