1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
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 #ifndef _ESP_WIFI_DRIVER_H_
16 #define _ESP_WIFI_DRIVER_H_
17 
18 #include "esp_err.h"
19 #include "esp_wifi.h"
20 
21 #if CONFIG_NEWLIB_NANO_FORMAT
22 #define TASK_STACK_SIZE_ADD 0
23 #else
24 #define TASK_STACK_SIZE_ADD 512
25 #endif
26 
27 #define WPA2_TASK_STACK_SIZE  (6144 + TASK_STACK_SIZE_ADD)
28 #define WPS_TASK_STACK_SIZE  (12288 + TASK_STACK_SIZE_ADD)
29 
30 enum wpa_alg{
31     WIFI_WPA_ALG_NONE   = 0,
32     WIFI_WPA_ALG_WEP40  = 1,
33     WIFI_WPA_ALG_TKIP   = 2,
34     WIFI_WPA_ALG_CCMP   = 3,
35     WIFI_WAPI_ALG_SMS4  = 4,
36     WIFI_WPA_ALG_WEP104 = 5,
37     WIFI_WPA_ALG_WEP    = 6,
38     WIFI_WPA_ALG_IGTK   = 7,
39     WIFI_WPA_ALG_PMK    = 8,
40     WIFI_WPA_ALG_GCMP   = 9,
41 };
42 
43 typedef enum {
44     WIFI_APPIE_PROBEREQ = 0,
45     WIFI_APPIE_ASSOC_REQ,
46     WIFI_APPIE_ASSOC_RESP,
47     WIFI_APPIE_WPA,
48     WIFI_APPIE_RSN,
49     WIFI_APPIE_WPS_PR,
50     WIFI_APPIE_WPS_AR,
51     WIFI_APPIE_MESH_QUICK,
52     WIFI_APPIE_FREQ_ERROR,
53     WIFI_APPIE_ESP_MANUFACTOR,
54     WIFI_APPIE_COUNTRY,
55     WIFI_APPIE_MAX,
56 } wifi_appie_t;
57 
58 /* wifi_appie_t is in rom code and can't be changed anymore, use wifi_appie_ram_t for new app IEs */
59 typedef enum {
60     WIFI_APPIE_RM_ENABLED_CAPS = WIFI_APPIE_MAX,
61     WIFI_APPIE_RAM_MAX,
62 } wifi_appie_ram_t;
63 
64 enum {
65     NONE_AUTH           = 0x01,
66     WPA_AUTH_UNSPEC     = 0x02,
67     WPA_AUTH_PSK        = 0x03,
68     WPA2_AUTH_ENT       = 0x04,
69     WPA2_AUTH_PSK       = 0x05,
70     WPA_AUTH_CCKM       = 0x06,
71     WPA2_AUTH_CCKM      = 0x07,
72     WPA2_AUTH_PSK_SHA256= 0x08,
73     WPA3_AUTH_PSK       = 0x09,
74     WPA2_AUTH_ENT_SHA256= 0x0a,
75     WAPI_AUTH_PSK       = 0x0b,
76     WAPI_AUTH_CERT      = 0x0c,
77     WPA2_AUTH_INVALID   = 0x0d,
78 };
79 
80 typedef enum {
81     WPA2_ENT_EAP_STATE_NOT_START,
82     WPA2_ENT_EAP_STATE_IN_PROGRESS,
83     WPA2_ENT_EAP_STATE_SUCCESS,
84     WPA2_ENT_EAP_STATE_FAIL,
85 } wpa2_ent_eap_state_t;
86 
87 struct wifi_appie {
88     uint16_t   ie_len;
89     uint8_t    ie_data[];
90 };
91 
92 struct wifi_ssid {
93     int len;
94     uint8_t ssid[32];
95 };
96 
97 struct wps_scan_ie {
98     uint8_t    *bssid;
99     uint8_t    chan;
100     uint16_t   capinfo;
101     uint8_t    *ssid;
102     uint8_t    *wpa;
103     uint8_t    *rsn;
104     uint8_t    *wps;
105 };
106 
107 typedef struct {
108     int proto;
109     int pairwise_cipher;
110     int group_cipher;
111     int key_mgmt;
112     int capabilities;
113     size_t num_pmkid;
114     const u8 *pmkid;
115     int mgmt_group_cipher;
116 } wifi_wpa_ie_t;
117 
118 struct wpa_funcs {
119     bool (*wpa_sta_init)(void);
120     bool (*wpa_sta_deinit)(void);
121     void (*wpa_sta_connect)(uint8_t *bssid);
122     void (*wpa_sta_disconnected_cb)(uint8_t reason_code);
123     int (*wpa_sta_rx_eapol)(u8 *src_addr, u8 *buf, u32 len);
124     bool (*wpa_sta_in_4way_handshake)(void);
125     void *(*wpa_ap_init)(void);
126     bool (*wpa_ap_deinit)(void *data);
127     bool (*wpa_ap_join)(void **sm, u8 *bssid, u8 *wpa_ie, u8 wpa_ie_len);
128     bool (*wpa_ap_remove)(void *sm);
129     uint8_t *(*wpa_ap_get_wpa_ie)(uint8_t *len);
130     bool (*wpa_ap_rx_eapol)(void *hapd_data, void *sm, u8 *data, size_t data_len);
131     void (*wpa_ap_get_peer_spp_msg)(void *sm, bool *spp_cap, bool *spp_req);
132     char *(*wpa_config_parse_string)(const char *value, size_t *len);
133     int (*wpa_parse_wpa_ie)(const u8 *wpa_ie, size_t wpa_ie_len, wifi_wpa_ie_t *data);
134     int (*wpa_config_bss)(u8 *bssid);
135     int (*wpa_michael_mic_failure)(u16 is_unicast);
136     uint8_t *(*wpa3_build_sae_msg)(uint8_t *bssid, uint32_t type, size_t *len);
137     int (*wpa3_parse_sae_msg)(uint8_t *buf, size_t len, uint32_t type, uint16_t status);
138     int (*wpa_sta_rx_mgmt)(u8 type, u8 *frame, size_t len, u8 *sender, u32 rssi, u8 channel, u64 current_tsf);
139 };
140 
141 struct wpa2_funcs {
142     int  (*wpa2_sm_rx_eapol)(u8 *src_addr, u8 *buf, u32 len, u8 *bssid);
143     int  (*wpa2_start)(void);
144     u8   (*wpa2_get_state)(void);
145     int  (*wpa2_init)(void);
146     void (*wpa2_deinit)(void);
147 };
148 
149 struct wps_funcs {
150     bool (*wps_parse_scan_result)(struct wps_scan_ie *scan);
151     int  (*wifi_station_wps_start)(void);
152     int  (*wps_sm_rx_eapol)(u8 *src_addr, u8 *buf, u32 len);
153     int  (*wps_start_pending)(void);
154 };
155 
156 typedef esp_err_t (*wifi_wpa2_fn_t)(void *);
157 typedef struct {
158     wifi_wpa2_fn_t   fn;
159     void    *param;
160 } wifi_wpa2_param_t;
161 
162 #define IS_WPS_REGISTRAR(type) (((type)>WPS_TYPE_MAX)?(((type)<WPS_TYPE_MAX)?true:false):false)
163 #define IS_WPS_ENROLLEE(type)  (((type)>WPS_TYPE_DISABLE)?(((type)<WPS_TYPE_MAX)?true:false):false)
164 
165 typedef enum wps_status {
166     WPS_STATUS_DISABLE = 0,
167     WPS_STATUS_SCANNING,
168     WPS_STATUS_PENDING,
169     WPS_STATUS_SUCCESS,
170     WPS_STATUS_MAX,
171 } WPS_STATUS_t;
172 
173 #define WIFI_TXCB_EAPOL_ID  3
174 typedef void(*wifi_tx_cb_t)(void *);
175 typedef int (*wifi_ipc_fn_t)(void *);
176 typedef struct {
177     wifi_ipc_fn_t fn;
178     void *arg;
179     uint32_t arg_size;
180 } wifi_ipc_config_t;
181 
182 #define WPA_IGTK_LEN 16
183 typedef struct {
184     uint8_t keyid[2];
185     uint8_t pn[6];
186     uint8_t igtk[WPA_IGTK_LEN];
187 } wifi_wpa_igtk_t;
188 
189 typedef struct {
190     wifi_interface_t ifx;
191     uint8_t subtype;
192     uint32_t data_len;
193     uint8_t data[0];
194 } wifi_mgmt_frm_req_t;
195 
196 uint8_t *esp_wifi_ap_get_prof_pmk_internal(void);
197 struct wifi_ssid *esp_wifi_ap_get_prof_ap_ssid_internal(void);
198 uint8_t esp_wifi_ap_get_prof_authmode_internal(void);
199 uint8_t esp_wifi_sta_get_prof_authmode_internal(void);
200 uint8_t *esp_wifi_ap_get_prof_password_internal(void);
201 struct wifi_ssid *esp_wifi_sta_get_prof_ssid_internal(void);
202 uint8_t esp_wifi_sta_get_reset_param_internal(void);
203 uint8_t esp_wifi_sta_get_pairwise_cipher_internal(void);
204 uint8_t esp_wifi_sta_get_group_cipher_internal(void);
205 bool esp_wifi_sta_prof_is_wpa_internal(void);
206 int esp_wifi_get_macaddr_internal(uint8_t if_index, uint8_t *macaddr);
207 int esp_wifi_set_appie_internal(uint8_t type, uint8_t *ie, uint16_t len, uint8_t flag);
208 int esp_wifi_unset_appie_internal(uint8_t type);
209 struct wifi_appie *esp_wifi_get_appie_internal(uint8_t type);
210 void *esp_wifi_get_hostap_private_internal(void); //1
211 uint8_t *esp_wifi_sta_get_prof_password_internal(void);
212 void esp_wifi_deauthenticate_internal(u8 reason_code);
213 uint16_t esp_wifi_get_spp_attrubute_internal(uint8_t ifx);
214 bool esp_wifi_sta_is_running_internal(void);
215 bool esp_wifi_auth_done_internal(void);
216 int esp_wifi_set_ap_key_internal(int alg, const u8 *addr, int idx, u8 *key, size_t key_len);
217 int esp_wifi_get_sta_hw_key_idx_internal(int key_idx);
218 int esp_wifi_set_sta_key_internal(int alg, u8 *addr, int key_idx, int set_tx,
219                                   u8 *seq, size_t seq_len, u8 *key, size_t key_len, int key_entry_valid);
220 int  esp_wifi_get_sta_key_internal(uint8_t *ifx, int *alg, u8 *addr, int *key_idx,
221                                    u8 *key, size_t key_len, int key_entry_valid);
222 bool esp_wifi_wpa_ptk_init_done_internal(uint8_t *mac);
223 uint8_t esp_wifi_sta_set_reset_param_internal(uint8_t reset_flag);
224 uint8_t esp_wifi_get_sta_gtk_index_internal(void);
225 void esp_wifi_set_sta_gtk_index_internal(u8 valid, u8 index);
226 int esp_wifi_register_tx_cb_internal(wifi_tx_cb_t fn, u8 id);
227 int esp_wifi_register_wpa_cb_internal(struct wpa_funcs *cb);
228 int esp_wifi_unregister_wpa_cb_internal(void);
229 int esp_wifi_get_assoc_bssid_internal(uint8_t *bssid);
230 bool esp_wifi_sta_is_ap_notify_completed_rsne_internal(void);
231 int esp_wifi_ap_deauth_internal(uint8_t *mac, uint32_t reason);
232 int esp_wifi_ipc_internal(wifi_ipc_config_t *cfg, bool sync);
233 int esp_wifi_register_wpa2_cb_internal(struct wpa2_funcs *cb);
234 int esp_wifi_unregister_wpa2_cb_internal(void);
235 bool esp_wifi_sta_prof_is_wpa2_internal(void);
236 bool esp_wifi_sta_prof_is_wpa3_internal(void);
237 bool esp_wifi_sta_prof_is_wapi_internal(void);
238 esp_err_t esp_wifi_sta_wpa2_ent_disable_internal(wifi_wpa2_param_t *param);
239 esp_err_t esp_wifi_sta_wpa2_ent_enable_internal(wifi_wpa2_param_t *param);
240 esp_err_t esp_wifi_set_wpa2_ent_state_internal(wpa2_ent_eap_state_t state);
241 int esp_wifi_get_wps_type_internal(void);
242 int esp_wifi_set_wps_type_internal(uint32_t type);
243 int esp_wifi_get_wps_status_internal(void);
244 int esp_wifi_set_wps_status_internal(uint32_t status);
245 int esp_wifi_disarm_sta_connection_timer_internal(void);
246 bool esp_wifi_get_sniffer_internal(void);
247 int esp_wifi_set_wps_cb_internal(struct wps_funcs *wps_cb);
248 bool esp_wifi_enable_sta_privacy_internal(void);
249 uint8_t esp_wifi_get_user_init_flag_internal(void);
250 esp_err_t esp_wifi_internal_supplicant_header_md5_check(const char *md5);
251 int esp_wifi_sta_update_ap_info_internal(void);
252 uint8_t *esp_wifi_sta_get_ap_info_prof_pmk_internal(void);
253 esp_err_t esp_wifi_set_wps_start_flag_internal(bool start);
254 uint16_t esp_wifi_sta_pmf_enabled(void);
255 wifi_cipher_type_t esp_wifi_sta_get_mgmt_group_cipher(void);
256 int esp_wifi_set_igtk_internal(uint8_t if_index, const wifi_wpa_igtk_t *igtk);
257 esp_err_t esp_wifi_internal_issue_disconnect(uint8_t reason_code);
258 bool esp_wifi_skip_supp_pmkcaching(void);
259 bool esp_wifi_is_rm_enabled_internal(uint8_t if_index);
260 bool esp_wifi_is_btm_enabled_internal(uint8_t if_index);
261 esp_err_t esp_wifi_register_mgmt_frame_internal(uint32_t type, uint32_t subtype);
262 esp_err_t esp_wifi_send_mgmt_frm_internal(const wifi_mgmt_frm_req_t *req);
263 uint8_t esp_wifi_ap_get_prof_pairwise_cipher_internal(void);
264 esp_err_t esp_wifi_action_tx_req(uint8_t type, uint8_t channel,
265                                  uint32_t wait_time_ms, const wifi_action_tx_req_t *req);
266 esp_err_t esp_wifi_remain_on_channel(uint8_t ifx, uint8_t type, uint8_t channel,
267                                      uint32_t wait_time_ms, wifi_action_rx_cb_t rx_cb);
268 
269 #endif /* _ESP_WIFI_DRIVER_H_ */
270