1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __ZCONFIG_IEEE80211_H__
6 #define __ZCONFIG_IEEE80211_H__
7 
8 #include "zconfig_utils.h"
9 #include "zconfig_protocol.h"
10 
11 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
12 extern "C" {
13 #endif
14 
15 #define WIFI_RX_SENSITIVITY (-85)
16 #ifndef ETH_ALEN
17 #define ETH_ALEN 6
18 #endif
19 
20 /*
21  * DS bit usage
22  *
23  * TA = transmitter address
24  * RA = receiver address
25  * DA = destination address
26  * SA = source address
27  *
28  * ToDS    FromDS  A1(RA)  A2(TA)  A3      A4      Use
29  * -----------------------------------------------------------------
30  *  0       0       DA      SA      BSSID   -       IBSS/DLS
31  *  0       1       DA      BSSID   SA      -       AP -> STA
32  *  1       0       BSSID   SA      DA      -       AP <- STA
33  *  1       1       RA      TA      DA      SA      unspecified (WDS)
34  */
35 #define FCS_LEN                       (4)
36 
37 #define IEEE80211_FCTL_VERS           (0x0003)
38 #define IEEE80211_FCTL_FTYPE          (0x000c)
39 #define IEEE80211_FCTL_STYPE          (0x00f0)
40 #define IEEE80211_FCTL_TODS           (0x0100)
41 #define IEEE80211_FCTL_FROMDS         (0x0200)
42 #define IEEE80211_FCTL_MOREFRAGS      (0x0400)
43 #define IEEE80211_FCTL_RETRY          (0x0800)
44 #define IEEE80211_FCTL_PM             (0x1000)
45 #define IEEE80211_FCTL_MOREDATA       (0x2000)
46 #define IEEE80211_FCTL_PROTECTED      (0x4000)
47 #define IEEE80211_FCTL_ORDER          (0x8000)
48 #define IEEE80211_FCTL_CTL_EXT        (0x0f00)
49 
50 #define IEEE80211_SCTL_FRAG           (0x000F)
51 #define IEEE80211_SCTL_SEQ            (0xFFF0)
52 
53 #define IEEE80211_FTYPE_MGMT          (0x0000)
54 #define IEEE80211_FTYPE_CTL           (0x0004)
55 #define IEEE80211_FTYPE_DATA          (0x0008)
56 #define IEEE80211_FTYPE_EXT           (0x000c)
57 
58 #define IEEE80211_STYPE_DATA          (0x0000)
59 #define IEEE80211_STYPE_QOS_DATA      (0x0080)
60 #define IEEE80211_STYPE_PROBE_REQ     (0x0040)
61 #define IEEE80211_STYPE_PROBE_RESP    (0x0050)
62 #define IEEE80211_STYPE_BEACON        (0x0080)
63 #define IEEE80211_STYPE_ACTION        (0x00D0)
64 
65 #define IEEE80211_QOS_CTL_LEN         (2)
66 #define IEEE80211_HT_CTL_LEN          (4)
67 
68 /* beacon capab_info */
69 #define WLAN_CAPABILITY_PRIVACY       (1 << 4)
70 
71 #define IEEE80211_SEQ_TO_SN(seq)      (((seq) & IEEE80211_SCTL_SEQ) >> 4)
72 #define IEEE80211_SN_TO_SEQ(ssn)      (((ssn) << 4) & IEEE80211_SCTL_SEQ)
73 
74 #define WLAN_CATEGORY_VENDOR_SPECIFIC (127)
75 
76 #define WLAN_EID_SSID                 (0)
77 #define WLAN_EID_DS_PARAMS            (3)
78 #define WLAN_EID_RSN                  (48)
79 #define WLAN_EID_HT_OPERATION         (61)
80 #define WLAN_EID_VENDOR_SPECIFIC      (221)
81 
82 #define WLAN_OUI_ALIBABA              (0xD896E0)
83 #define WLAN_OUI_TYPE_ALIBABA         (1)
84 #define WLAN_OUI_TYPE_ENROLLEE        (0xAA)
85 #define WLAN_OUI_TYPE_REGISTRAR       (0xAB)
86 
87 enum ALINK_TYPE {
88     ALINK_INVALID = 0,
89     ALINK_BROADCAST = 1,
90     ALINK_ROUTER = 2,
91     ALINK_ACTION = 3,
92     ALINK_WPS = 4,
93     ALINK_DEFAULT_SSID = 5,
94     ALINK_ZERO_CONFIG = 6,
95     ALINK_APLIST,
96     ALINK_HT_CTRL,
97 };
98 
99 /* 80211 frame parser result */
100 struct parser_res {
101     union _alink_type_ {
102         /* for broadcast data frame */
103         struct broadcast_info {
104             uint8_t encry_type; /* none/wep/tkip/aes */
105             uint16_t data_len;  /* framelen - 80211 hdr - fcs(4) */
106             uint16_t sn;
107         } br;
108         /* for alink ie frame */
109         struct ie_info {
110             uint8_t *alink_ie;
111             uint16_t alink_ie_len;
112         } ie;
113         /* for p2p action frame */
114         struct action_info {
115             uint8_t *data;
116             uint16_t data_len;
117         } action;
118         /* for p2p wps frame */
119         struct wps_info {
120             uint8_t *data;
121             uint16_t data_len;
122         } wps;
123         /* for ht40 ctrl frame */
124         struct ht_ctrl_info {
125             signed char rssi;
126             uint8_t filter;
127             uint16_t data_len;
128         } ht_ctrl;
129     } u;
130 
131     uint8_t *src;   /* src mac of sender */
132     uint8_t *dst;   /* ff:ff:ff:ff:ff:ff */
133     uint8_t *bssid; /* mac of AP */
134 
135     uint8_t tods;    /* fromDs or toDs */
136     uint8_t channel; /* 1 - 13 */
137 };
138 
139 struct ieee80211_hdr {
140     uint16_t frame_control;
141     uint16_t duration_id;
142     uint8_t addr1[ETH_ALEN];
143     uint8_t addr2[ETH_ALEN];
144     uint8_t addr3[ETH_ALEN];
145     uint16_t seq_ctrl;
146     uint8_t addr4[ETH_ALEN];
147 };
148 
149 /*
150  * The radio capture header precedes the 802.11 header.
151  *
152  * Note well: all radiotap fields are little-endian.
153  */
154 struct ieee80211_radiotap_header {
155     uint8_t it_version; /* Version 0. Only increases
156                          * for drastic changes,
157                          * introduction of compatible
158                          * new fields does not count.
159                          */
160     uint8_t it_pad;
161     uint16_t it_len;     /* length of the whole
162                           * header in bytes, including
163                           * it_version, it_pad,
164                           * it_len, and data fields.
165                           */
166     uint32_t it_present; /* A bitmap telling which
167                           * fields are present. Set bit 31
168                           * (0x80000000) to extend the
169                           * bitmap by another 32 bits.
170                           * Additional extensions are made
171                           * by setting bit 31.
172                           */
173 };
174 
175 /**
176  * struct ieee80211_ht_operation - HT operation IE
177  *
178  * This structure is the "HT operation element" as
179  * described in 802.11n-2009 7.3.2.57
180  */
181 struct ieee80211_ht_operation {
182     uint8_t primary_chan;
183     uint8_t ht_param;
184     uint16_t operation_mode;
185     uint16_t stbc_param;
186     uint8_t basic_set[16];
187 };
188 
189 struct ieee80211_vendor_ie {
190     uint8_t element_id;
191     uint8_t len;
192     uint8_t oui[3];
193     uint8_t oui_type;
194 };
195 /*
196  * i.e.    alibaba ie
197  *    @name        @len    @payload
198  *    element_id    1    221
199  *    len           1    22
200  *    oui           3    0xD896E0
201  *    oui_type      1    1 -- alink router service advertisement
202  *    version       1    1
203  *    challenge     16    non-zero-ascii code
204  *    reserve       1    0
205  */
206 
207 struct ieee80211_mgmt {
208     uint16_t frame_control;
209     uint16_t duration;
210     uint8_t da[ETH_ALEN];
211     uint8_t sa[ETH_ALEN];
212     uint8_t bssid[ETH_ALEN];
213     uint16_t seq_ctrl;
214     union {
215         struct {
216             /* __le64 timestamp; */
217             uint16_t timestamp[4];
218             uint16_t beacon_int;
219             uint16_t capab_info;
220             /* followed by some of SSID, Supported rates,
221              * FH Params, DS Params, CF Params, IBSS Params, TIM */
222             uint8_t variable;
223         } beacon;
224         struct {
225             /* only variable items: SSID, Supported rates */
226             uint8_t variable;
227         } probe_req;
228         struct {
229             /* __le64 timestamp; */
230             uint16_t timestamp[4];
231             uint16_t beacon_int;
232             uint16_t capab_info;
233             /* followed by some of SSID, Supported rates,
234              * FH Params, DS Params, CF Params, IBSS Params */
235             uint8_t variable;
236         } probe_resp;
237     } u;
238 };
239 
240 typedef int (*awss_protocol_process_func_type)(uint8_t *, int, int,
241                                                struct parser_res *,
242                                                signed char);
243 typedef int (*awss_protocol_finish_func_type)(struct parser_res *);
244 
245 struct awss_protocol_couple_type {
246     int type;
247     awss_protocol_process_func_type awss_protocol_process_func;
248     awss_protocol_finish_func_type awss_protocol_finish_func;
249 };
250 
251 int ieee80211_data_extract(uint8_t *in, int len, int link_type,
252                            struct parser_res *res, signed char rssi);
253 
254 struct ap_info *zconfig_get_apinfo(uint8_t *mac);
255 struct ap_info *zconfig_get_apinfo_by_3_byte_mac(uint8_t *last_3_byte_mac);
256 struct ap_info *zconfig_get_apinfo_by_ssid(uint8_t *ssid);
257 struct ap_info *zconfig_get_apinfo_by_ssid_prefix(uint8_t *ssid_prefix);
258 struct ap_info *zconfig_get_apinfo_by_ssid_suffix(uint8_t *ssid_suffix);
259 
260 /* add channel to scanning channel list */
261 int zconfig_add_active_channel(int channel);
262 uint8_t zconfig_get_press_status();
263 
264 int ieee80211_hdrlen_2(uint16_t fc);
265 int ieee80211_has_a4(uint16_t fc);
266 int ieee80211_is_ctl(uint16_t fc);
267 int ieee80211_is_mgmt(uint16_t fc);
268 int ieee80211_is_data(uint16_t fc);
269 int ieee80211_has_tods(uint16_t fc);
270 int ieee80211_has_frags(uint16_t fc);
271 int ieee80211_has_order(uint16_t fc);
272 int ieee80211_is_beacon(uint16_t fc);
273 int ieee80211_is_action(uint16_t fc);
274 int ieee80211_has_fromds(uint16_t fc);
275 int ieee80211_is_data_qos(uint16_t fc);
276 int ieee80211_is_probe_req(uint16_t fc);
277 int ieee80211_is_probe_resp(uint16_t fc);
278 int ieee80211_is_data_exact(uint16_t fc);
279 int ieee80211_has_protected(uint16_t fc);
280 int ieee80211_is_data_present(uint16_t fc);
281 int ieee80211_get_radiotap_len(uint8_t *data);
282 int ieee80211_get_bssid_2(uint8_t *in, uint8_t *mac);
283 int ieee80211_get_ssid(uint8_t *beacon_frame, uint16_t frame_len,
284                        uint8_t *ssid);
285 int ieee80211_data_extract(uint8_t *in, int len, int link_type,
286                            struct parser_res *res, signed char rssi);
287 int cfg80211_get_bss_channel(uint8_t *beacon_frame, uint16_t frame_len);
288 int cfg80211_get_cipher_info(uint8_t *beacon_frame, uint16_t frame_len,
289                              uint8_t *auth_type, uint8_t *pairwise_cipher_type,
290                              uint8_t *group_cipher_type);
291 uint8_t *ieee80211_get_SA(struct ieee80211_hdr *hdr);
292 uint8_t *ieee80211_get_DA(struct ieee80211_hdr *hdr);
293 uint8_t *ieee80211_get_BSSID(struct ieee80211_hdr *hdr);
294 const uint8_t *cfg80211_find_ie(uint8_t eid, const uint8_t *ies, int len);
295 const uint8_t *cfg80211_find_vendor_ie(uint32_t oui, uint8_t oui_type,
296                                        const uint8_t *ies, int len);
297 struct ap_info *zconfig_get_apinfo(uint8_t *mac);
298 struct ap_info *zconfig_get_apinfo_by_ssid(uint8_t *ssid);
299 struct ap_info *zconfig_get_apinfo_by_ssid_prefix(uint8_t *ssid_prefix);
300 struct ap_info *zconfig_get_apinfo_by_ssid_suffix(uint8_t *ssid_suffix);
301 
302 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
303 }
304 #endif
305 
306 #endif /* __IEEE80211_H */
307