• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <modules/wifi_types.h>
18 
19 /// MAC address structure.
20 struct wifi_mac_addr
21 {
22     uint8_t array[WIFI_MAC_LEN];
23 } __attribute__((packed));
24 
25 /// Structure of a long control frame MAC header
26 struct wifi_mac_hdr
27 {
28     /// Frame control
29     uint16_t fctl;
30     /// Duration/ID
31     uint16_t durid;
32     /// Address 1
33     struct wifi_mac_addr addr1;
34     /// Address 2
35     struct wifi_mac_addr addr2;
36     /// Address 3
37     struct wifi_mac_addr addr3;
38     /// Sequence control
39     uint16_t seq;
40 } __attribute__((packed));
41 
42 /// Structure of a Beacon or ProbeRsp frame
43 struct wifi_bcn_frame
44 {
45     /// MAC Header
46     struct wifi_mac_hdr h;
47     /// Timestamp
48     uint64_t tsf;
49     /// Beacon interval
50     uint16_t bcnint;
51     /// Capability information
52     uint16_t capa;
53     /// Rest of the payload
54     uint8_t variable[];
55 } __attribute__((packed));
56 
57 /// MIC Header Length -> DA (6 bytes) + SA (6 bytes) + Priority (4 bytes)
58 #define MIC_HDR_LEN     (16)
59 /// IV Length
60 #define IV_LEN          (4)
61 /// EIV Length
62 #define EIV_LEN         (4)
63 /// MIC Length
64 #define MIC_LEN         (8)
65 /// ICV Length
66 #define ICV_LEN         (4)
67 // WPI IV length
68 #define WPI_IV_LEN      (18)
69 // WPI MIC length
70 #define WPI_MIC_LEN     (16)
71 
72 /// Word 1 of CCMP IV
73 #define EIV_PRESENT     (0x2000)
74 
75 /**
76  * MAC HEADER LENGTH DEFINITIONS
77  ****************************************************************************************
78  */
79 /// Long MAC Header length (with QoS control field and HT control field)
80 #define MAC_LONG_QOS_HTC_MAC_HDR_LEN   36
81 /// Long MAC Header length (with QoS control field)
82 #define MAC_LONG_QOS_MAC_HDR_LEN       32
83 /// Long MAC Header length (without QoS control field)
84 #define MAC_LONG_MAC_HDR_LEN           30
85 /// Short MAC Header length (with QoS control field and HT control field)
86 #define MAC_SHORT_QOS_HTC_MAC_HDR_LEN  30
87 /// Short MAC Header length (with QoS control field)
88 #define MAC_SHORT_QOS_MAC_HDR_LEN      26
89 /// Short MAC Header length (without QoS control field)
90 #define MAC_SHORT_MAC_HDR_LEN          24
91 
92 /// QoS Control Field Length
93 #define MAC_HDR_QOS_CTRL_LEN           2
94 
95 /// Longest possible MAC Header
96 #define MAC_HDR_LEN_MAX                MAC_LONG_QOS_HTC_MAC_HDR_LEN
97 
98 /// Long control frame header length
99 #define MAC_LONG_CTRLFRAME_LEN         16
100 /// Short control frame header length (ACK/CTS)
101 #define MAC_SHORT_CTRLFRAME_LEN        10
102 
103 
104 /**
105  * MAC Header offset definitions
106  */
107 /// 802.11 MAC header definition
108 #define MAC_HEAD_FCTRL_OFT              0
109 #define MAC_HEAD_DURATION_OFT           2
110 #define MAC_HEAD_DURATION_CFP      0x8000
111 #define MAC_HEAD_ADDR1_OFT              4
112 #define MAC_HEAD_ADDR2_OFT             10
113 #define MAC_HEAD_ADDR3_OFT             16
114 #define MAC_HEAD_CTRL_OFT              22
115 #define MAC_HEAD_ADDR4_OFT             24
116 #define MAC_HEAD_SHORT_QOS_OFT         24
117 #define MAC_HEAD_LONG_QOS_OFT          30
118 #define MAC_ORIGINAL_ETHTYPE_OFT       36
119 
120 /**
121  * Frame Control bit field definitions
122  */
123 /* The type information in the header of a frame consists of the Type and Subtype fields
124  * When using the term "frame type" in the code, we refer to the type field and not to
125  * the combined type+subtype information.
126  */
127 /// 802.11 frame control definition
128 #define MAC_FCTRL_LEN                   2
129 
130 #define MAC_FCTRL_PROTOCOLVERSION_MASK  0x0003
131 #define MAC_FCTRL_TYPE_MASK             0x000C
132 #define MAC_FCTRL_SUBT_MASK             0x00F0
133 #define MAC_FCTRL_TODS                  0x0100
134 #define MAC_FCTRL_FROMDS                0x0200
135 #define MAC_FCTRL_MOREFRAG              0x0400
136 #define MAC_FCTRL_RETRY                 0x0800
137 #define MAC_FCTRL_PWRMGT                0x1000
138 #define MAC_FCTRL_MOREDATA              0x2000
139 #define MAC_FCTRL_PROTECTEDFRAME        0x4000
140 #define MAC_FCTRL_ORDER                 0x8000
141 
142 #define MAC_FCTRL_TODS_FROMDS          (MAC_FCTRL_TODS | MAC_FCTRL_FROMDS)
143 /** @} */
144 
145 /**
146  * Frame Control Type definitions
147  */
148 /// 802.11 type definition
149 #define MAC_FCTRL_MGT_T                 0x0000
150 #define MAC_FCTRL_CTRL_T                0x0004
151 #define MAC_FCTRL_DATA_T                0x0008
152 /** @} */
153 
154 /**
155  * Frame Control Subtype definitions
156  */
157 /// 802.11 subtype definition
158 // Management SubType
159 #define MAC_FCTRL_ASSOCREQ_ST           0x0000
160 #define MAC_FCTRL_ASSOCRSP_ST           0x0010
161 #define MAC_FCTRL_REASSOCREQ_ST         0x0020
162 #define MAC_FCTRL_REASSOCRSP_ST         0x0030
163 #define MAC_FCTRL_PROBEREQ_ST           0x0040
164 #define MAC_FCTRL_PROBERSP_ST           0x0050
165 #define MAC_FCTRL_BEACON_ST             0x0080
166 #define MAC_FCTRL_ATIM_ST               0x0090
167 #define MAC_FCTRL_DISASSOC_ST           0x00A0
168 #define MAC_FCTRL_AUTHENT_ST            0x00B0
169 #define MAC_FCTRL_DEAUTHENT_ST          0x00C0
170 #define MAC_FCTRL_ACTION_ST             0x00D0
171 #define MAC_FCTRL_ACTION_NO_ACK_ST      0x00E0
172 // Control SubTypes
173 #define MAC_FCTRL_HE_TRIGGER_ST         0x0020
174 #define MAC_FCTRL_BFM_REPORT_POLL_ST    0x0040
175 #define MAC_FCTRL_VHT_NDPA_ST           0x0050
176 #define MAC_FCTRL_CTRL_WRAPPER_ST       0x0070
177 #define MAC_FCTRL_BAR_ST                0x0080
178 #define MAC_FCTRL_BA_ST                 0x0090
179 #define MAC_FCTRL_PSPOLL_ST             0x00A0
180 #define MAC_FCTRL_RTS_ST                0x00B0
181 #define MAC_FCTRL_CTS_ST                0x00C0
182 #define MAC_FCTRL_ACK_ST                0x00D0
183 #define MAC_FCTRL_CFEND_ST              0x00E0
184 #define MAC_FCTRL_CFEND_CFACK_ST        0x00F0
185 
186 // Data SubTypes
187 /* Decoding the subtypes of data type frames can take advantage of the fact that
188  * each subtype field bit position is used to indicate a specific modification of
189  * the basic data frame (subtype 0). Frame control bit 4 is set to 1 in data
190  * subtypes which include +CF-Ack, bit 5 is set to 1 in data subtypes which include
191  * +CF-Poll, bit 6 is set to 1 in data subtypes that contain no Frame Body,
192  * and bit 7 is set to 1 in the QoS data subtypes, which have QoS Control fields in
193  * their MAC headers.
194  *
195  * Usage: check FrameT and FrameSubT individually. If the FrameT is MAC_FCTRL_DATA_T,
196  * check the following bits of the FrameSubT
197  */
198 #define MAC_CFACK_ST_BIT                CO_BIT(4)
199 #define MAC_CFPOLL_ST_BIT               CO_BIT(5)
200 #define MAC_NODATA_ST_BIT               CO_BIT(6)
201 #define MAC_QOS_ST_BIT                  CO_BIT(7)
202 #define MAC_FCTRL_DATACFACKPOLL_ST      (MAC_CFACK_ST_BIT | MAC_CFPOLL_ST_BIT)
203 
204 /**
205  * Frame Control various frame type/subtype definitions
206  */
207 /// 802.11 type/subtype definition
208 #define MAC_FCTRL_TYPESUBTYPE_MASK      (MAC_FCTRL_TYPE_MASK | MAC_FCTRL_SUBT_MASK)
209 #define MAC_FCTRL_ASSOCREQ              (MAC_FCTRL_MGT_T     | MAC_FCTRL_ASSOCREQ_ST)
210 #define MAC_FCTRL_ASSOCRSP              (MAC_FCTRL_MGT_T     | MAC_FCTRL_ASSOCRSP_ST)
211 #define MAC_FCTRL_REASSOCREQ            (MAC_FCTRL_MGT_T     | MAC_FCTRL_REASSOCREQ_ST)
212 #define MAC_FCTRL_REASSOCRSP            (MAC_FCTRL_MGT_T     | MAC_FCTRL_REASSOCRSP_ST)
213 #define MAC_FCTRL_PROBEREQ              (MAC_FCTRL_MGT_T     | MAC_FCTRL_PROBEREQ_ST)
214 #define MAC_FCTRL_PROBERSP              (MAC_FCTRL_MGT_T     | MAC_FCTRL_PROBERSP_ST)
215 #define MAC_FCTRL_BEACON                (MAC_FCTRL_MGT_T     | MAC_FCTRL_BEACON_ST)
216 #define MAC_FCTRL_ATIM                  (MAC_FCTRL_MGT_T     | MAC_FCTRL_ATIM_ST)
217 #define MAC_FCTRL_DISASSOC              (MAC_FCTRL_MGT_T     | MAC_FCTRL_DISASSOC_ST)
218 #define MAC_FCTRL_AUTHENT               (MAC_FCTRL_MGT_T     | MAC_FCTRL_AUTHENT_ST)
219 #define MAC_FCTRL_DEAUTHENT             (MAC_FCTRL_MGT_T     | MAC_FCTRL_DEAUTHENT_ST)
220 #define MAC_FCTRL_ACTION                (MAC_FCTRL_MGT_T     | MAC_FCTRL_ACTION_ST)
221 #define MAC_FCTRL_ACTION_NO_ACK         (MAC_FCTRL_MGT_T     | MAC_FCTRL_ACTION_NO_ACK_ST)
222 #define MAC_FCTRL_BFM_REPORT_POLL       (MAC_FCTRL_CTRL_T    | MAC_FCTRL_BFM_REPORT_POLL_ST)
223 #define MAC_FCTRL_HE_TRIGGER            (MAC_FCTRL_CTRL_T    | MAC_FCTRL_HE_TRIGGER_ST)
224 #define MAC_FCTRL_VHT_NDPA              (MAC_FCTRL_CTRL_T    | MAC_FCTRL_VHT_NDPA_ST)
225 #define MAC_FCTRL_BA                    (MAC_FCTRL_CTRL_T    | MAC_FCTRL_BA_ST)
226 #define MAC_FCTRL_BAR                   (MAC_FCTRL_CTRL_T    | MAC_FCTRL_BAR_ST)
227 #define MAC_FCTRL_PSPOLL                (MAC_FCTRL_CTRL_T    | MAC_FCTRL_PSPOLL_ST)
228 #define MAC_FCTRL_RTS                   (MAC_FCTRL_CTRL_T    | MAC_FCTRL_RTS_ST)
229 #define MAC_FCTRL_CTS                   (MAC_FCTRL_CTRL_T    | MAC_FCTRL_CTS_ST)
230 #define MAC_FCTRL_ACK                   (MAC_FCTRL_CTRL_T    | MAC_FCTRL_ACK_ST)
231 #define MAC_FCTRL_CFEND                 (MAC_FCTRL_CTRL_T    | MAC_FCTRL_CFEND_ST)
232 #define MAC_FCTRL_CFEND_CFACK           (MAC_FCTRL_CFEND     | MAC_CFACK_ST_BIT)
233 #define MAC_FCTRL_DATA_CFACK            (MAC_FCTRL_DATA_T    | MAC_CFACK_ST_BIT)
234 #define MAC_FCTRL_DATA_CFPOLL           (MAC_FCTRL_DATA_T    | MAC_CFPOLL_ST_BIT)
235 #define MAC_FCTRL_DATA_CFACKPOLL        (MAC_FCTRL_DATA_T    | MAC_FCTRL_DATACFACKPOLL_ST)
236 #define MAC_FCTRL_NULL_FUNCTION         (MAC_FCTRL_DATA_T    | MAC_NODATA_ST_BIT)
237 #define MAC_FCTRL_NULL_CFACK            (MAC_FCTRL_NULL_FUNCTION  | MAC_CFACK_ST_BIT)
238 #define MAC_FCTRL_NULL_CFPOLL           (MAC_FCTRL_NULL_FUNCTION  | MAC_CFPOLL_ST_BIT)
239 #define MAC_FCTRL_NULL_CFACKPOLL        (MAC_FCTRL_NULL_FUNCTION  | MAC_FCTRL_DATACFACKPOLL_ST)
240 #define MAC_FCTRL_QOS_DATA              (MAC_FCTRL_DATA_T    | MAC_QOS_ST_BIT)
241 #define MAC_FCTRL_QOS_DATA_CFACK        (MAC_FCTRL_QOS_DATA  | MAC_CFACK_ST_BIT)
242 #define MAC_FCTRL_QOS_DATA_CFPOLL       (MAC_FCTRL_QOS_DATA  | MAC_CFPOLL_ST_BIT)
243 #define MAC_FCTRL_QOS_DATA_CFACKPOLL    (MAC_FCTRL_QOS_DATA  | MAC_FCTRL_DATACFACKPOLL_ST)
244 #define MAC_FCTRL_QOS_NULL              (MAC_FCTRL_QOS_DATA  | MAC_NODATA_ST_BIT)
245 #define MAC_FCTRL_QOS_NULL_CFACK        (MAC_FCTRL_QOS_DATA  | MAC_FCTRL_NULL_CFACK)
246 #define MAC_FCTRL_QOS_NULL_CFPOLL       (MAC_FCTRL_QOS_DATA  | MAC_FCTRL_NULL_CFPOLL)
247 #define MAC_FCTRL_QOS_NULL_CFACKPOLL    (MAC_FCTRL_QOS_DATA  | MAC_FCTRL_NULL_CFACKPOLL)
248 
249 #define MAC_FCTRL_IS(fc, type) (((fc) & MAC_FCTRL_TYPESUBTYPE_MASK) == MAC_FCTRL_##type)
250 #define MAC_FCTRL_SUBTYPE(fc) (((fc) & MAC_FCTRL_SUBT_MASK) >> 4)
251 
252 /*
253  *  * Beacon Frame offset (Table 5 p46)
254  *   */
255 #define MAC_BEACON_TIMESTAMP_OFT          MAC_SHORT_MAC_HDR_LEN   // Order 1
256 #define MAC_BEACON_INTERVAL_OFT          (MAC_SHORT_MAC_HDR_LEN + 8)   // Order 2
257 #define MAC_BEACON_CAPA_OFT              (MAC_SHORT_MAC_HDR_LEN + 10)   // Order 3
258 #define MAC_BEACON_VARIABLE_PART_OFT     (MAC_SHORT_MAC_HDR_LEN + 12)   // Order 4
259 
260 #define MAC_ELTID_DS                      3
261 
262 // DS PARAM SET
263 #define MAC_DS_ID_OFT                           0
264 #define MAC_DS_LEN_OFT                          1
265 #define MAC_DS_CHANNEL_OFT                      2
266 #define MAC_DS_PARAM_LEN                        3
267 
268 uint32_t bk_wifi_find_ie(uint32_t addr, uint16_t buflen, uint8_t ie_id);
269