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
// 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::{
        Aid, FrameControl, HtControl, MacAddr, OptionalField, Presence, ReasonCode,
        SequenceControl, StatusCode,
    },
    crate::TimeUnit,
    wlan_bitfield::bitfield,
    zerocopy::{AsBytes, FromBytes, FromZeros, NoCell, Unaligned},
};

// IEEE Std 802.11-2016, 9.4.1.4
#[bitfield(
    0       ess,
    1       ibss,
    2       cf_pollable,
    3       cf_poll_req,
    4       privacy,
    5       short_preamble,
    6..=7   _,  // reserved
    8       spectrum_mgmt,
    9       qos,
    10      short_slot_time,
    11      apsd,
    12      radio_measurement,
    13      _,  // reserved
    14      delayed_block_ack,
    15      immediate_block_ack,
)]
#[derive(AsBytes, FromZeros, FromBytes, NoCell, PartialEq, Eq, Clone, Copy, Hash)]
#[repr(C)]
pub struct CapabilityInfo(pub u16);

// IEEE Std 802.11-2016, 9.4.1.11, Table Table 9-47
#[derive(AsBytes, FromZeros, FromBytes, NoCell, Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct ActionCategory(u8);

impl ActionCategory {
    pub const SPECTRUM_MGMT: Self = Self(0);
    pub const QOS: Self = Self(1);
    pub const DLS: Self = Self(2);
    pub const BLOCK_ACK: Self = Self(3);
    pub const PUBLIC: Self = Self(4);
    pub const RADIO_MEASURE: Self = Self(5);
    pub const FT: Self = Self(6);
    pub const HT: Self = Self(7);
    pub const SA_QUERY: Self = Self(8);
    pub const PROTECTED_DUAL: Self = Self(9);
    pub const WNM: Self = Self(10);
    pub const UNPROTECTED_WNM: Self = Self(11);
    pub const TDLS: Self = Self(12);
    pub const MESH: Self = Self(13);
    pub const MULTIHOP: Self = Self(14);
    pub const SELF_PROTECTED: Self = Self(15);
    pub const DMG: Self = Self(16);
    // 17 reserved
    pub const FST: Self = Self(18);
    pub const ROBUST_AV_STREAM: Self = Self(19);
    pub const UNPROTECTED_DMG: Self = Self(20);
    pub const VHT: Self = Self(21);
    // 22 - 125 reserved
    pub const VENDOR_PROTECTED: Self = Self(126);
    pub const VENDOR: Self = Self(127);
    // 128 - 255: Error
}

// IEEE Std 802.11-2016, 9.3.3.2
#[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned, PartialEq, Eq, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct MgmtHdr {
    pub frame_ctrl: FrameControl,
    pub duration: u16,
    pub addr1: MacAddr,
    pub addr2: MacAddr,
    pub addr3: MacAddr,
    pub seq_ctrl: SequenceControl,
}

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

// IEEE Std 802.11-2016, 9.4.1.1
#[repr(C)]
#[derive(AsBytes, FromZeros, FromBytes, NoCell, PartialEq, Eq, Copy, Clone, Debug, Default)]
pub struct AuthAlgorithmNumber(pub u16);

impl AuthAlgorithmNumber {
    pub const OPEN: Self = Self(0);
    pub const SHARED_KEY: Self = Self(1);
    pub const FAST_BSS_TRANSITION: Self = Self(2);
    pub const SAE: Self = Self(3);
    // 4-65534 Reserved
    pub const VENDOR_SPECIFIC: Self = Self(65535);
}

// IEEE Std 802.11-2016, 9.3.3.3
#[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct BeaconHdr {
    // IEEE Std 802.11-2016, 9.4.1.10, 11.1.3.1, and 11.22
    // Intentionally private until TSF timer functionality implemented.
    timestamp: u64, // TSF timer value in increments of microseconds
    pub beacon_interval: TimeUnit,
    // IEEE Std 802.11-2016, 9.4.1.4
    pub capabilities: CapabilityInfo,
}

impl BeaconHdr {
    pub fn new(beacon_interval: TimeUnit, capabilities: CapabilityInfo) -> Self {
        Self {
            timestamp: 0, // always 0 since TSF Timer not implemented
            beacon_interval,
            capabilities,
        }
    }
}

// IEEE Std 802.11-2016, 9.3.3.12
#[derive(Default, FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct AuthHdr {
    pub auth_alg_num: AuthAlgorithmNumber,
    pub auth_txn_seq_num: u16,
    pub status_code: StatusCode,
}

// IEEE Std 802.11-2016, 9.3.3.13
#[derive(Default, FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct DeauthHdr {
    pub reason_code: ReasonCode,
}

// IEEE Std 802.11-2016, 9.3.3.6
#[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct AssocReqHdr {
    // IEEE Std 802.11-2016, 9.4.1.4
    pub capabilities: CapabilityInfo,
    pub listen_interval: u16,
}

// IEEE Std 802.11-2016, 9.3.3.7
#[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct AssocRespHdr {
    // IEEE Std 802.11-2016, 9.4.1.4
    pub capabilities: CapabilityInfo,
    pub status_code: StatusCode,
    pub aid: Aid,
}

// IEEE Std 802.11-2016, 9.3.3.5
#[derive(Default, FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct DisassocHdr {
    pub reason_code: ReasonCode,
}

// IEEE Std 802.11-2016, 9.3.3.11
#[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct ProbeRespHdr {
    // IEEE Std 802.11-2016, 9.4.1.10, 11.1.3.1, and 11.22
    // Intentionally private until TSF timer functionality implemented.
    timestamp: u64, // TSF timer value in increments of microseconds
    pub beacon_interval: TimeUnit,
    // IEEE Std 802.11-2016, 9.4.1.4
    pub capabilities: CapabilityInfo,
}

impl ProbeRespHdr {
    pub fn new(beacon_interval: TimeUnit, capabilities: CapabilityInfo) -> Self {
        Self {
            timestamp: 0, // always 0 since TSF Timer not implemented
            beacon_interval,
            capabilities,
        }
    }
}

// IEEE Std 802.11-2016, 9.3.3.14
#[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct ActionHdr {
    pub action: ActionCategory,
}

// IEEE Std 802.11-2016, 9.6.5.1
#[repr(C)]
#[derive(AsBytes, FromZeros, FromBytes, NoCell, PartialEq, Eq, Copy, Clone, Debug, Default)]
pub struct BlockAckAction(pub u8);

impl BlockAckAction {
    pub const ADDBA_REQUEST: Self = Self(0);
    pub const ADDBA_RESPONSE: Self = Self(1);
    pub const DELBA: Self = Self(2);
}

// IEEE Std 802.11-2016, 9.4.1.14
#[repr(C)]
#[derive(AsBytes, FromZeros, FromBytes, NoCell, PartialEq, Eq, Copy, Clone, Debug, Default)]
pub struct BlockAckPolicy(pub u8);

impl BlockAckPolicy {
    pub const DELAYED: Self = Self(0);
    pub const IMMEDIATE: Self = Self(1);
}

// IEEE Std 802.11-2016, 9.4.1.14
#[bitfield(
    0      amsdu,
    1..=1  policy as BlockAckPolicy(u8),
    2..=5  tid,
    6..=15 buffer_size,
)]
#[derive(AsBytes, FromZeros, FromBytes, NoCell, PartialEq, Eq, Clone, Copy, Default)]
#[repr(C)]
pub struct BlockAckParameters(pub u16);

// IEEE Std 802.11-2016, 9.4.1.16
#[bitfield(
    0..=10  reserved,
    11      initiator,
    12..=15 tid,
)]
#[derive(AsBytes, FromZeros, FromBytes, NoCell, PartialEq, Eq, Clone, Copy, Default)]
#[repr(C)]
pub struct DelbaParameters(pub u16);

// IEEE Std 802.11-2016, 9.6.5.2 & Figure 9-28
#[bitfield(
    0..=3  fragment_number, // Always set to 0 (IEEE Std 802.11-2016, 9.6.5.2)
    4..=15 starting_sequence_number,
)]
#[derive(AsBytes, FromZeros, FromBytes, NoCell, PartialEq, Eq, Clone, Copy, Default)]
#[repr(C)]
pub struct BlockAckStartingSequenceControl(pub u16);

// IEEE Std 802.11-2016, 9.6.5.2 - ADDBA stands for Add BlockAck.
#[derive(Default, FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct AddbaReqHdr {
    pub action: BlockAckAction,
    // IEEE Std 802.11-2016, 9.4.1.12 - This is a numeric value.
    pub dialog_token: u8,
    pub parameters: BlockAckParameters,
    // IEEE Std 802.11-2016, 9.4.1.15 - unit is TU, 0 disables the timeout.
    pub timeout: u16,
    pub starting_sequence_control: BlockAckStartingSequenceControl,
    // TODO(https://fxbug.dev/42104687): Evaluate the use cases and support optional fields.
    // GCR Group Address element
    // Multi-band
    // TCLAS
    // ADDBA Extension
}

// IEEE Std 802.11-2016, 9.6.5.3 - ADDBA stands for Add BlockAck.
#[derive(Default, FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct AddbaRespHdr {
    pub action: BlockAckAction,
    // IEEE Std 802.11-2016, 9.4.1.12 - This is a numeric value.
    pub dialog_token: u8,
    pub status: StatusCode,
    pub parameters: BlockAckParameters,
    // IEEE Std 802.11-2016, 9.4.1.15 - unit is TU, 0 disables the timeout.
    pub timeout: u16,
    // TODO(https://fxbug.dev/42104687): Evaluate the use cases and support optional fields.
    // GCR Group Address element
    // Multi-band
    // TCLAS
    // ADDBA Extension
}

// IEEE Std 802.11-2016, 9.6.5.4 - DELBA stands for Delete BlockAck.
#[derive(Default, FromZeros, FromBytes, AsBytes, NoCell, Unaligned, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct DelbaHdr {
    pub action: BlockAckAction,
    pub parameters: DelbaParameters,
    pub reason_code: ReasonCode,
    // TODO(https://fxbug.dev/42104687): Evaluate the use cases and support optional fields.
    // GCR Group Address element
    // Multi-band
    // TCLAS
}

// IEEE Std 802.11-2016, 9.6.2.1
#[repr(C)]
#[derive(AsBytes, FromZeros, FromBytes, NoCell, PartialEq, Eq, Copy, Clone, Debug, Default)]
pub struct SpectrumMgmtAction(pub u8);

impl SpectrumMgmtAction {
    pub const MEASUREMENT_REQUEST: Self = Self(0);
    pub const MEASUREMENT_REPORT: Self = Self(1);
    pub const TPC_REQUEST: Self = Self(2);
    pub const TPC_REPORT: Self = Self(3);
    pub const CHANNEL_SWITCH_ANNOUNCEMENT: Self = Self(4);
}