• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: wifi APIs
15  */
16 
17 #include "utils/common.h"
18 #include "wifi_api.h"
19 #include "utils/eloop.h"
20 #include "wpa_supplicant_i.h"
21 #include "wpa_cli_rtos.h"
22 #include "wpa_supplicant_if.h"
23 #include "common/ieee802_11_common.h"
24 #include "config_ssid.h"
25 #include "src/crypto/sha1.h"
26 #include "driver_soc.h"
27 #include "driver_soc_ioctl.h"
28 #include "soc_osal.h"
29 #ifdef CONFIG_WPS
30 #include "wps/wps.h"
31 #endif
32 #ifdef LOS_CONFIG_MESH
33 #include "soc_mesh.h"
34 #endif /* LOS_CONFIG_MESH */
35 #include "securec.h"
36 #ifdef LOS_CONFIG_WPA_ENTERPRISE
37 #include "tls.h"
38 #endif /* LOS_CONFIG_WPA_ENTERPRISE */
39 
40 #define WPA_TASK_STACK_SIZE          0x1800UL
41 #define WPA_CB_STACK_SIZE            0x800UL
42 #define WPA_TASK_PRIO_NUM            4
43 #define WPS_PIN_LENGTH 8
44 #define WPA_USE_IE_LEN_SUBSCRIPT     1
45 #define WPA_USE_IE_INIT_BYTE_NUM     2
46 #define WPA_DEFAULT_IDENTITY         "\"xxx\""
47 #define WPA_DEFAULT_PRIV_KEY         "\"wifi\""
48 #define WPA_IDENTITY_EXTRA_LEN       3
49 
50 #define WPA_KEY_MGMT_WPA_PSK         "WPA-PSK"
51 #define WPA_KEY_MGMT_WPA_PSK_PMF     "WPA-PSK WPA-PSK-SHA256"
52 #define WPA_KEY_MGMT_WPA3_PSK        "SAE"
53 #define WPA_KEY_MGMT_WPA3_PSK_MIX    "SAE WPA-PSK WPA-PSK-SHA256"
54 #define WPA_KEY_MGMT_WPA2            "WPA-EAP"
55 #define WPA_KEY_MGMT_WPA3            "WPA-EAP-SHA256"
56 #define WPA_KEY_MGMT_WPA3_WPA2_MIX   "WPA-EAP WPA-EAP-SHA256"
57 #define WPA_GROUP_MGMT_AES_128_CMAC  "AES-128-CMAC"
58 #define WPA_KEY_MGMT_WPA_FT_PSK         "WPA-PSK FT-PSK"
59 #define WPA_KEY_MGMT_WPA_FT_PSK_PMF     "WPA-PSK WPA-PSK-SHA256 FT-PSK"
60 #define WPA_KEY_MGMT_WPA3_FT_PSK        "SAE FT-SAE"
61 #define WPA_KEY_MGMT_WPA3_FT_PSK_MIX    "SAE WPA-PSK WPA-PSK-SHA256 FT-SAE FT-PSK"
62 #define WPA2_PROTO  "WPA2"
63 
64 static bool g_wpa_event_inited_flag;
65 unsigned int g_wpataskid;
66 struct ext_wifi_dev *g_wifi_dev[WPA_MAX_WIFI_DEV_NUM] = { NULL };
67 struct wifi_ap_opt_set g_ap_opt_set                    = { 0 };
68 struct wifi_sta_opt_set g_sta_opt_set                  = { 0 };
69 struct wifi_reconnect_set g_reconnect_set              = { 0 };
70 struct ext_scan_record g_scan_record                  = { 0 };
71 char *g_scan_result_buf                                = NULL;
72 size_t g_result_len                                    = 0;
73 int g_mesh_sta_flag                                    = 0;
74 int g_fast_connect_flag                                = 0;
75 int g_fast_connect_scan_flag                           = 0;
76 int g_connecting_flag                                  = 0;
77 int g_usr_scanning_flag                                = 0;
78 int g_mesh_flag                                        = 0;
79 int g_scan_flag                                        = WPA_FLAG_OFF;
80 static unsigned int g_lock_flag                        = WPA_FLAG_OFF;
81 static int g_ft_flag                                   = WPA_FLAG_ON;
82 static int g_assoc_auth                                = EXT_WIFI_SEC_TYPE_INVALID;
83 unsigned char g_quick_conn_psk[EXT_WIFI_STA_PSK_LEN]    = { 0 };
84 unsigned int g_quick_conn_psk_flag                     = WPA_FLAG_OFF;
85 ext_wifi_status *g_sta_status                           = NULL;
86 ext_wifi_ap_sta_info *g_ap_sta_info                     = NULL;
87 unsigned int g_sta_num                                 = 0;
88 wpa_rm_network g_wpa_rm_network                        = 0;
89 char g_csi_ifname[WIFI_IFNAME_MAX_SIZE]               = { 0 };
90 
91 /* call back configuration */
92 uapi_wifi_event_cb g_wpa_event_cb                        = 0;
93 static unsigned int g_wpa_event_running                  = 0;
94 static unsigned int g_wpa_event_taskid                   = 0;
95 static unsigned char g_direct_cb                         = 0; /* 0:create new task call cb, 1:direct call cb */
96 static unsigned char g_cb_task_prio                      = 20; /* callback task priority */
97 static unsigned short g_cb_stack_size                    = 0x800; /* callback task stack size 2k */
98 
99 ext_wifi_scan_no_save_cb g_raw_scan_cb = NULL;
100 
101 #ifdef LOS_CONFIG_WPA_ENTERPRISE
102 #define EXT_WIFI_IDENTITY_LEN 64
103 #define ENTERPRISE_CERT_MAX_LEN 2048
104 #define ENTERPRISE_WFA_CERT_MAX_LEN  3072
105 static char g_eap_identity[EXT_WIFI_IDENTITY_LEN + WPA_IDENTITY_EXTRA_LEN] = WPA_DEFAULT_IDENTITY;
106 static ext_wifi_eap_method g_eap_method = EXT_WIFI_EAP_METHOD_TLS;
107 static const char* g_eap_method_name[(int)EXT_WIFI_EAP_METHOD_BUTT] = {
108     [(int)EXT_WIFI_EAP_METHOD_TLS] = "TLS"
109 };
110 static unsigned char *g_wfa_ca_cert = NULL;
111 static unsigned char *g_wfa_cli_cert = NULL;
112 static unsigned char *g_wfa_cli_key = NULL;
113 static char *g_wfa_ent_identity = NULL;
114 extern unsigned char __attribute__((weak)) g_ent_ca_cert[ENTERPRISE_CERT_MAX_LEN];
115 extern unsigned char __attribute__((weak)) g_ent_cli_cert[ENTERPRISE_CERT_MAX_LEN];
116 extern unsigned char __attribute__((weak)) g_ent_cli_key[ENTERPRISE_CERT_MAX_LEN];
117 extern char __attribute__((weak)) g_ent_identity[EXT_WIFI_IDENTITY_LEN];
118 #endif /* LOS_CONFIG_WPA_ENTERPRISE */
119 
wpa_event_task_free(void)120 void wpa_event_task_free(void)
121 {
122 #ifdef __LITEOS__
123     // 防止LiteOS任务无法回收,触发一次任务回收
124     LOS_TaskResRecycle();
125 #endif
126     if (g_wpa_event_taskid != 0) {
127         osal_kfree((void *)g_wpa_event_taskid);
128         g_wpa_event_taskid = 0; /* 防止异常return后,重复释放 */
129     }
130 }
131 
lock_flag_opr(int (* cb)(void))132 static int lock_flag_opr(int (*cb)(void))
133 {
134     int ret;
135     unsigned int int_save;
136 
137     if (cb == NULL) {
138         return EXT_WIFI_FAIL;
139     }
140 
141     os_intlock(&int_save);
142     ret = cb();
143     os_intrestore(int_save);
144 
145     return ret;
146 }
147 
try_set_lock_flag_internal(void)148 static int try_set_lock_flag_internal(void)
149 {
150     int ret = EXT_WIFI_OK;
151 
152     if (g_lock_flag == WPA_FLAG_ON)
153         ret = EXT_WIFI_FAIL;
154     else
155         g_lock_flag = WPA_FLAG_ON;
156 
157     return ret;
158 }
159 
160 /* forward declaration */
try_set_lock_flag(void)161 int try_set_lock_flag(void)
162 {
163     return lock_flag_opr(try_set_lock_flag_internal);
164 }
165 
is_lock_flag_off_internal(void)166 static int is_lock_flag_off_internal(void)
167 {
168     int ret = EXT_WIFI_OK;
169 
170     if (g_lock_flag == WPA_FLAG_ON)
171         ret = EXT_WIFI_FAIL;
172 
173     return ret;
174 }
175 
is_lock_flag_off(void)176 int is_lock_flag_off(void)
177 {
178     return lock_flag_opr(is_lock_flag_off_internal);
179 }
180 
clr_lock_flag_internal(void)181 static int clr_lock_flag_internal(void)
182 {
183     g_lock_flag = WPA_FLAG_OFF;
184     return EXT_WIFI_OK;
185 }
186 
clr_lock_flag(void)187 void clr_lock_flag(void)
188 {
189     (void)lock_flag_opr(clr_lock_flag_internal);
190 }
191 
chan_to_freq(unsigned char chan)192 int chan_to_freq(unsigned char chan)
193 {
194     if (chan == 0)
195         return chan;
196     if ((chan < 1) || (chan > 14)) { /* 1: channel 1; 14: channel 14 */
197         wpa_error_log1(MSG_ERROR, "warning : bad channel number: %u \n", (unsigned int)chan);
198         return EXT_WIFI_FAIL;
199     }
200     if (chan == 14) /* 14: channel 14 */
201         return 2414 + 5 * chan; /* 2414: frequency, 5: the number of channel  */
202     return 2407 + 5 * chan; /* 2407: frequency, 5: the number of channel */
203 }
204 
addr_precheck(const unsigned char * addr)205 int addr_precheck(const unsigned char *addr)
206 {
207     if ((addr == NULL) || is_zero_ether_addr(addr) || is_broadcast_ether_addr(addr)) {
208         wpa_error_log0(MSG_ERROR, "bad addr");
209         return EXT_WIFI_FAIL;
210     }
211     return EXT_WIFI_OK;
212 }
213 
sta_precheck(void)214 static int sta_precheck(void)
215 {
216     unsigned int ret = (unsigned int)(los_count_wifi_dev_in_use() >= WPA_DOUBLE_IFACE_WIFI_DEV_NUM);
217     unsigned int int_save;
218     os_intlock(&int_save);
219 
220     for (int i = 0; i < WPA_MAX_WIFI_DEV_NUM; i++) {
221         if (g_wifi_dev[i] != NULL)
222             ret |= (unsigned int)((g_wifi_dev[i]->iftype == EXT_WIFI_IFTYPE_STATION));
223     }
224     os_intrestore(int_save);
225 
226     if (ret)
227         return EXT_WIFI_FAIL;
228     return EXT_WIFI_OK;
229 }
230 
wifi_sta_enterprise_check(ext_wifi_auth_mode mode)231 static int wifi_sta_enterprise_check(ext_wifi_auth_mode mode)
232 {
233     if ((mode == EXT_WIFI_SECURITY_WPA) || (mode == EXT_WIFI_SECURITY_WPA2) || (mode == EXT_WIFI_SECURITY_WPA3) ||
234         (mode == EXT_WIFI_SECURITY_WPA3_WPA2_MIX))
235         return EXT_WIFI_OK;
236     return EXT_WIFI_FAIL;
237 }
238 
is_sta_on(void)239 static int is_sta_on(void)
240 {
241     if (los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_STATION) == NULL)
242         return EXT_WIFI_FAIL;
243 
244     return EXT_WIFI_OK;
245 }
246 
is_ap_mesh_or_p2p_on(void)247 int is_ap_mesh_or_p2p_on(void)
248 {
249     if ((los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_AP) != NULL) ||
250         (los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_MESH_POINT) != NULL) ||
251         (los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_P2P_CLIENT) != NULL) ||
252         (los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_P2P_GO) != NULL) ||
253         (los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_P2P_DEVICE) != NULL))
254         return EXT_WIFI_OK;
255 
256     return EXT_WIFI_FAIL;
257 }
258 
259 
los_count_wifi_dev_in_use(void)260 int los_count_wifi_dev_in_use(void)
261 {
262     int i;
263     int count = 0;
264     unsigned int int_save;
265 
266     os_intlock(&int_save);
267     for (i = 0; i < WPA_MAX_WIFI_DEV_NUM; i++) {
268         if (g_wifi_dev[i] != NULL)
269             count++;
270     }
271     os_intrestore(int_save);
272 
273     return count;
274 }
275 
los_get_wifi_dev_by_name(const char * ifname)276 struct ext_wifi_dev * los_get_wifi_dev_by_name(const char *ifname)
277 {
278     int i;
279     if (ifname == NULL)
280         return NULL;
281 
282     unsigned int int_save;
283     os_task_lock(&int_save);
284     for (i = 0; i < WPA_MAX_WIFI_DEV_NUM; i++) {
285         if ((g_wifi_dev[i] != NULL) && (strcmp(g_wifi_dev[i]->ifname, ifname) == 0)) {
286             g_wifi_dev[i]->network_id = i;
287             os_task_unlock(int_save);
288             return g_wifi_dev[i];
289         }
290     }
291     os_task_unlock(int_save);
292 
293     return NULL;
294 }
295 
los_get_wifi_dev_by_iftype(ext_wifi_iftype iftype)296 struct ext_wifi_dev * los_get_wifi_dev_by_iftype(ext_wifi_iftype iftype)
297 {
298     int i;
299     struct ext_wifi_dev *dev = NULL;
300 
301     if (iftype >= EXT_WIFI_IFTYPES_BUTT)
302         return NULL;
303 
304     unsigned int int_save;
305     os_task_lock(&int_save);
306     for (i = 0; i < WPA_MAX_WIFI_DEV_NUM; i++) {
307         if ((g_wifi_dev[i] != NULL) && (g_wifi_dev[i]->iftype == iftype)) {
308             dev = g_wifi_dev[i];
309             break;
310         }
311     }
312     os_task_unlock(int_save);
313 
314     return dev;
315 }
316 
los_get_wifi_dev_by_priv(const void * ctx)317 struct ext_wifi_dev * los_get_wifi_dev_by_priv(const void *ctx)
318 {
319     int i;
320     struct ext_wifi_dev *dev = NULL;
321 
322     if (ctx == NULL)
323         return NULL;
324 
325     unsigned int int_save;
326     os_task_lock(&int_save);
327     for (i = 0; i < WPA_MAX_WIFI_DEV_NUM; i++) {
328         if ((g_wifi_dev[i] != NULL) && (g_wifi_dev[i]->priv == ctx)) {
329             dev = g_wifi_dev[i];
330             break;
331         }
332     }
333     os_task_unlock(int_save);
334 
335     return dev;
336 }
337 
wpa_get_other_existed_wpa_wifi_dev(const void * priv)338 struct ext_wifi_dev * wpa_get_other_existed_wpa_wifi_dev(const void *priv)
339 {
340     int i;
341     struct ext_wifi_dev *dev = NULL;
342 
343     unsigned int int_save;
344     os_task_lock(&int_save);
345     for (i = 0; i < WPA_MAX_WIFI_DEV_NUM; i++) {
346         if ((g_wifi_dev[i] != NULL) && (g_wifi_dev[i]->priv != priv) &&
347             ((g_wifi_dev[i]->iftype == EXT_WIFI_IFTYPE_STATION) ||
348              (g_wifi_dev[i]->iftype == EXT_WIFI_IFTYPE_MESH_POINT) ||
349              ((g_wifi_dev[i]->iftype >= EXT_WIFI_IFTYPE_P2P_CLIENT) &&
350              (g_wifi_dev[i]->iftype <= EXT_WIFI_IFTYPE_P2P_DEVICE)))) {
351             dev = g_wifi_dev[i];
352             break;
353         }
354     }
355     os_task_unlock(int_save);
356 
357     return dev;
358 }
359 
wifi_dev_get(ext_wifi_iftype iftype)360 struct ext_wifi_dev * wifi_dev_get(ext_wifi_iftype iftype)
361 {
362     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
363         wpa_error_log0(MSG_ERROR, "wifi_dev_get: wifi dev start or stop is running.");
364         return NULL;
365     }
366 
367     struct ext_wifi_dev *wifi_dev = los_get_wifi_dev_by_iftype(iftype);
368     if (wifi_dev == NULL) {
369         wpa_error_log1(MSG_ERROR, "wifi_dev_get: get iftype = %u dev failed.", (unsigned int)iftype);
370         return NULL;
371     }
372 
373     return wifi_dev;
374 }
375 
los_free_wifi_dev(struct ext_wifi_dev * wifi_dev)376 void los_free_wifi_dev(struct ext_wifi_dev *wifi_dev)
377 {
378     if (wifi_dev == NULL)
379         return;
380 
381     wpa_error_log1(MSG_ERROR, "los_free_wifi_dev enter, iftype = %u", (unsigned int)wifi_dev->iftype);
382     unsigned int int_save;
383     os_task_lock(&int_save);
384 
385     for (int i = 0; i < WPA_MAX_WIFI_DEV_NUM; i++) {
386         if (g_wifi_dev[i] == wifi_dev) {
387             g_wifi_dev[i] = NULL;
388             break;
389         }
390     }
391     (void)memset_s(wifi_dev, sizeof(struct ext_wifi_dev), 0, sizeof(struct ext_wifi_dev));
392     os_free(wifi_dev);
393     os_task_unlock(int_save);
394 }
395 
wifi_dev_creat(ext_wifi_iftype iftype,protocol_mode_enum mode)396 struct ext_wifi_dev * wifi_dev_creat(ext_wifi_iftype iftype, protocol_mode_enum mode)
397 {
398     int ret;
399     struct ext_wifi_dev *wifi_dev = NULL;
400     wifi_dev = (struct ext_wifi_dev *)os_zalloc(sizeof(struct ext_wifi_dev));
401     if (wifi_dev == NULL) {
402         wpa_error_log0(MSG_ERROR, "wifi_dev malloc err.");
403         return NULL;
404     }
405     wifi_dev->ifname_len = WIFI_IFNAME_MAX_SIZE;
406 
407     ret = wal_init_drv_wlan_netdev(iftype, mode, wifi_dev->ifname, &(wifi_dev->ifname_len));
408     if (ret != EXT_WIFI_OK) {
409         wpa_error_log0(MSG_ERROR, "wifi_dev_creat: wal_init_drv_wlan_netdev failed. \n\n\r!");
410         os_free(wifi_dev);
411         return NULL;
412     }
413     if ((wifi_dev->ifname_len <= 0) ||
414         (wifi_dev->ifname_len > WIFI_IFNAME_MAX_SIZE) ||
415         (wifi_dev->ifname_len != (int)strnlen(wifi_dev->ifname, WIFI_IFNAME_MAX_SIZE + 1))) {
416         wpa_error_log0(MSG_ERROR, " wifi_dev_creat : Invalid iface name. \n\n\r");
417         goto WIFI_DEV_CREAT_ERROR;
418     }
419     wifi_dev->iftype = iftype;
420     wpa_warning_buf(MSG_INFO, "wifi_dev_creat: ifname:%s", wifi_dev->ifname, strlen(wifi_dev->ifname));
421     wpa_warning_log2(MSG_INFO, "wifi_dev_creat: len:%d, iftype: %u\n", wifi_dev->ifname_len,
422                      (unsigned int)wifi_dev->iftype);
423     return wifi_dev;
424 
425 WIFI_DEV_CREAT_ERROR:
426     (void)wal_deinit_drv_wlan_netdev(wifi_dev->ifname);
427     os_free(wifi_dev);
428     return NULL;
429 }
430 
los_set_wifi_dev(struct ext_wifi_dev * wifi_dev)431 int los_set_wifi_dev(struct ext_wifi_dev *wifi_dev)
432 {
433     unsigned int int_save;
434 
435     if (wifi_dev == NULL)
436         return EXT_WIFI_FAIL;
437 
438     os_intlock(&int_save);
439     for (int i = 0; i < WPA_MAX_WIFI_DEV_NUM; i++) {
440         if (g_wifi_dev[i] == NULL) {
441             g_wifi_dev[i] = wifi_dev;
442             g_wifi_dev[i]->network_id = i;
443             os_intrestore(int_save);
444 
445             return EXT_WIFI_OK;
446         }
447     }
448     os_intrestore(int_save);
449 
450     return EXT_WIFI_FAIL;
451 }
452 
los_wpa_ssid_config_set(struct wpa_ssid * ssid,const char * name,const char * value)453 int los_wpa_ssid_config_set(struct wpa_ssid *ssid, const char *name, const char *value)
454 {
455     errno_t rc;
456     u8 *ssid_txt = NULL;
457     if ((ssid == NULL) || (name == NULL) || (value == NULL) || (strlen(value) > EXT_WIFI_MAX_SSID_LEN))
458         return EXT_WIFI_FAIL;
459     if (((ssid->ssid) != NULL) && (strlen((char *)(ssid->ssid)) == strlen(value)) &&
460         (os_memcmp(ssid->ssid, value, strlen(value)) == 0)) {
461         return EXT_WIFI_FAIL;
462     }
463     str_clear_free((char *)(ssid->ssid));
464     ssid_txt = os_zalloc(EXT_WIFI_MAX_SSID_LEN + 1);
465     if (ssid_txt == NULL)
466         return EXT_WIFI_FAIL;
467     rc = memcpy_s(ssid_txt, EXT_WIFI_MAX_SSID_LEN + 1, value, strlen(value));
468     if (rc != EOK) {
469         os_free(ssid_txt);
470         wpa_error_log1(MSG_ERROR, "los_wpa_ssid_config_set memcpy_s failed(%d).", rc);
471         return EXT_WIFI_FAIL;
472     }
473     ssid->ssid = ssid_txt;
474     ssid->ssid_len = strlen((char *)ssid_txt);
475     return EXT_WIFI_OK;
476 }
477 
wifi_sta_psk_init(char * param,unsigned int param_len,unsigned int key_len,const struct wpa_assoc_request * assoc)478 static int wifi_sta_psk_init(char *param, unsigned int param_len,
479                              unsigned int key_len, const struct wpa_assoc_request *assoc)
480 {
481     int ret;
482     unsigned int max_key_len = WPA_MAX_KEY_LEN;
483     if ((param == NULL) || (param_len == 0) || (key_len == 0) || (assoc == NULL))
484         return EXT_WIFI_FAIL;
485 #ifdef CONFIG_WAPI
486     /* WAPI-PSK supports a maximum of 64-byte keys. */
487     if (assoc->auth == EXT_WIFI_SECURITY_WAPI_PSK) {
488         max_key_len = WAPI_MAX_KEY_LEN;
489     }
490 #endif
491 #ifdef CONFIG_DRIVER_SOC
492 #ifdef CONFIG_WAPI
493     if (assoc->auth == EXT_WIFI_SECURITY_WAPI_PSK) {
494         if ((key_len < WPA_MIN_KEY_LEN) || (key_len > max_key_len))
495             return EXT_WIFI_FAIL;
496         if (assoc->hex_flag) {
497             if (key_len & 0x01)
498                 return EXT_WIFI_FAIL;
499             if (memcpy_s(param, param_len, assoc->key, key_len) != EOK)
500                 return EXT_WIFI_FAIL;
501         } else {
502             ret = snprintf_s(param, param_len, param_len - 1, "\"%s\"", assoc->key);
503             if (ret < 0)
504                 return EXT_WIFI_FAIL;
505         }
506     } else {
507 #endif /* CONFIG_WAPI */
508         if (key_len < max_key_len || assoc->auth == EXT_WIFI_SECURITY_SAE ||
509             assoc->auth == EXT_WIFI_SECURITY_WPA3_WPA2_PSK_MIX) {
510             ret = snprintf_s(param, param_len, param_len - 1, "\"%s\"", assoc->key);
511             if (ret < 0)
512                 return EXT_WIFI_FAIL;
513         } else {
514             if (memcpy_s(param, param_len, assoc->key, key_len) != EOK)
515                 return EXT_WIFI_FAIL;
516         }
517 #ifdef CONFIG_WAPI
518     }
519 #endif /* CONFIG_WAPI */
520 #else
521     ret  = snprintf_s(param, param_len, param_len - 1, "\"%s\"", assoc->key);
522     if (ret < 0)
523         return EXT_WIFI_FAIL;
524 #endif
525     return EXT_WIFI_OK;
526 }
527 
528 #ifdef CONFIG_WPA3
wifi_sta_wpa3_network_set(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc)529 static int wifi_sta_wpa3_network_set(struct wpa_supplicant *wpa_s, const char *id,
530                                      const struct wpa_assoc_request *assoc)
531 {
532     const char *key_mgmt = NULL;
533 
534     if (assoc->auth == EXT_WIFI_SECURITY_WPA3_WPA2_PSK_MIX) {
535         key_mgmt = assoc->ft_flag == 0 ? WPA_KEY_MGMT_WPA3_PSK_MIX : WPA_KEY_MGMT_WPA3_FT_PSK_MIX;
536         if ((wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "key_mgmt", key_mgmt,
537             strlen(key_mgmt)) != EXT_WIFI_OK) ||
538             (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "ieee80211w", "1", strlen("1")) != EXT_WIFI_OK))
539             return EXT_WIFI_FAIL;
540     } else if (assoc->auth == EXT_WIFI_SECURITY_SAE) {
541         key_mgmt = assoc->ft_flag == 0 ? WPA_KEY_MGMT_WPA3_PSK : WPA_KEY_MGMT_WPA3_FT_PSK;
542         if ((wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "key_mgmt", key_mgmt,
543             strlen(key_mgmt)) != EXT_WIFI_OK) ||
544             (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "ieee80211w", "2", strlen("2")) != EXT_WIFI_OK))
545             return EXT_WIFI_FAIL;
546     }
547     return EXT_WIFI_OK;
548 }
549 #endif /* CONFIG_WPA3 */
550 
wifi_sta_set_pairwise(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc)551 static int wifi_sta_set_pairwise(struct wpa_supplicant *wpa_s, const char *id,
552                                  const struct wpa_assoc_request *assoc)
553 {
554     if (assoc->wpa_pairwise == EXT_WIFI_PAIRWISE_TKIP) {
555         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "pairwise", "CCMP TKIP", strlen("CCMP TKIP")) != EXT_WIFI_OK)
556             return EXT_WIFI_FAIL;
557     } else if (assoc->wpa_pairwise == EXT_WIFI_PAIRWISE_AES) {
558         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "pairwise", "CCMP TKIP", strlen("CCMP TKIP")) != EXT_WIFI_OK)
559             return EXT_WIFI_FAIL;
560     } else if (assoc->wpa_pairwise == EXT_WIFI_PAIRWISE_TKIP_AES_MIX) {
561         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "pairwise", "CCMP TKIP", strlen("CCMP TKIP")) != EXT_WIFI_OK)
562             return EXT_WIFI_FAIL;
563     } else if (assoc->wpa_pairwise == EXT_WIFI_PAIRWISE_CCMP256) {
564         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "pairwise", "CCMP-256", strlen("CCMP-256")) != EXT_WIFI_OK)
565             return EXT_WIFI_FAIL;
566         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "group", "CCMP-256", strlen("CCMP-256")) != EXT_WIFI_OK)
567             return EXT_WIFI_FAIL;
568     } else if (assoc->wpa_pairwise == EXT_WIFI_PAIRWISE_CCMP256_CCMP) {
569         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "pairwise", "CCMP-256 CCMP", strlen("CCMP-256 CCMP")) != EXT_WIFI_OK)
570             return EXT_WIFI_FAIL;
571         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "group", "CCMP-256 CCMP", strlen("CCMP-256 CCMP")) != EXT_WIFI_OK)
572             return EXT_WIFI_FAIL;
573     } else if (assoc->wpa_pairwise == EXT_WIFI_PAIRWISE_CCMP256_TKIP) {
574         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "pairwise", "CCMP-256 TKIP", strlen("CCMP-256 TKIP")) != EXT_WIFI_OK)
575             return EXT_WIFI_FAIL;
576         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "group", "CCMP-256 TKIP", strlen("CCMP-256 TKIP")) != EXT_WIFI_OK)
577             return EXT_WIFI_FAIL;
578     }
579     return EXT_WIFI_OK;
580 }
581 
582 #ifdef LOS_CONFIG_WPA_ENTERPRISE
wifi_sta_set_key_mgmt_ent(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc)583 static int wifi_sta_set_key_mgmt_ent(struct wpa_supplicant *wpa_s, const char *id,
584                                      const struct wpa_assoc_request *assoc)
585 {
586     int ret = EXT_WIFI_OK;
587     if ((wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "eap", g_eap_method_name[g_eap_method],
588         strlen(g_eap_method_name[g_eap_method])) != EXT_WIFI_OK) ||
589         (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "private_key", WPA_DEFAULT_PRIV_KEY,
590             strlen(WPA_DEFAULT_PRIV_KEY)) != EXT_WIFI_OK))
591         return EXT_WIFI_FAIL;
592 
593     if (assoc->auth == EXT_WIFI_SECURITY_WPA3_WPA2_MIX) {
594         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "key_mgmt", WPA_KEY_MGMT_WPA3_WPA2_MIX,
595             strlen(WPA_KEY_MGMT_WPA3_WPA2_MIX) != EXT_WIFI_OK))
596             return EXT_WIFI_FAIL;
597         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "group_mgmt", WPA_GROUP_MGMT_AES_128_CMAC,
598             strlen(WPA_GROUP_MGMT_AES_128_CMAC) != EXT_WIFI_OK))
599             return EXT_WIFI_FAIL;
600         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "proto", WPA2_PROTO,
601             strlen(WPA_GROUP_MGMT_AES_128_CMAC) != EXT_WIFI_OK))
602             return EXT_WIFI_FAIL;
603         ret = wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "ieee80211w", "1", strlen("1"));
604     } else if (assoc->auth == EXT_WIFI_SECURITY_WPA3) {
605         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "key_mgmt", WPA_KEY_MGMT_WPA3,
606             strlen(WPA_KEY_MGMT_WPA3) != EXT_WIFI_OK))
607             return EXT_WIFI_FAIL;
608         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "group_mgmt", WPA_GROUP_MGMT_AES_128_CMAC,
609             strlen(WPA_GROUP_MGMT_AES_128_CMAC) != EXT_WIFI_OK))
610             return EXT_WIFI_FAIL;
611         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "proto", WPA2_PROTO,
612             strlen(WPA_GROUP_MGMT_AES_128_CMAC) != EXT_WIFI_OK))
613             return EXT_WIFI_FAIL;
614         ret = wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "ieee80211w", "2", strlen("2"));
615     } else {
616         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "key_mgmt", WPA_KEY_MGMT_WPA2,
617             strlen(WPA_KEY_MGMT_WPA2) != EXT_WIFI_OK))
618             return EXT_WIFI_FAIL;
619         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "proto", WPA2_PROTO,
620             strlen(WPA_GROUP_MGMT_AES_128_CMAC) != EXT_WIFI_OK))
621             return EXT_WIFI_FAIL;
622     }
623     return ret;
624 }
625 
wifi_sta_set_pairwise_ent(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc)626 static int wifi_sta_set_pairwise_ent(struct wpa_supplicant *wpa_s, const char *id,
627                                      const struct wpa_assoc_request *assoc)
628 {
629     (void)assoc;
630     if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "group", "CCMP TKIP", strlen("CCMP TKIP")) !=
631         EXT_WIFI_OK)
632             return EXT_WIFI_FAIL;
633     return wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "pairwise", "CCMP TKIP", strlen("CCMP TKIP"));
634 }
635 #endif /* LOS_CONFIG_WPA_ENTERPRISE */
636 
wifi_sta_set_key_mgmt_wpa(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc)637 static int wifi_sta_set_key_mgmt_wpa(struct wpa_supplicant *wpa_s, const char *id,
638     const struct wpa_assoc_request *assoc)
639 {
640     const char *key_mgmt = NULL;
641 
642     if (g_sta_opt_set.pmf >= WIFI_MGMT_FRAME_PROTECTION_OPTIONAL) {
643         key_mgmt = assoc->ft_flag == 0 ? WPA_KEY_MGMT_WPA_PSK_PMF :
644             (g_ft_flag == WPA_FLAG_OFF ? WPA_KEY_MGMT_WPA_PSK_PMF : WPA_KEY_MGMT_WPA_FT_PSK_PMF);
645     } else {
646         if (assoc->wpa_pairwise >= EXT_WIFI_PAIRWISE_CCMP256) {
647             key_mgmt = assoc->ft_flag == 0 ? WPA_KEY_MGMT_WPA_PSK_PMF :
648                 (g_ft_flag == WPA_FLAG_OFF ? WPA_KEY_MGMT_WPA_PSK_PMF : WPA_KEY_MGMT_WPA_FT_PSK_PMF);
649         } else {
650             key_mgmt = assoc->ft_flag == 0 ? WPA_KEY_MGMT_WPA_PSK :
651                 (g_ft_flag == WPA_FLAG_OFF ? WPA_KEY_MGMT_WPA_PSK : WPA_KEY_MGMT_WPA_FT_PSK);
652         }
653     }
654 
655     if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "key_mgmt", key_mgmt,
656         strlen(key_mgmt)) != EXT_WIFI_OK) {
657         return EXT_WIFI_FAIL;
658     }
659     return EXT_WIFI_OK;
660 }
661 
wifi_sta_set_key_mgmt(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc)662 static int wifi_sta_set_key_mgmt(struct wpa_supplicant *wpa_s, const char *id,
663                                  const struct wpa_assoc_request *assoc)
664 {
665     switch (assoc->auth) {
666         case EXT_WIFI_SECURITY_WEP:
667             if (wpa_cli_configure_wep(wpa_s, id, assoc) != EXT_WIFI_OK)
668                 return EXT_WIFI_FAIL;
669             return EXT_WIFI_OK;
670         case EXT_WIFI_SECURITY_WPAPSK:
671         case EXT_WIFI_SECURITY_WPA2PSK:
672             /* fall-through */
673         case EXT_WIFI_SECURITY_WPAPSK_WPA2PSK_MIX:
674             if (wifi_sta_set_key_mgmt_wpa(wpa_s, id, assoc) != EXT_WIFI_OK) {
675                 return EXT_WIFI_FAIL;
676             }
677             break;
678 #ifdef CONFIG_WPA3
679         case EXT_WIFI_SECURITY_SAE:
680         case EXT_WIFI_SECURITY_WPA3_WPA2_PSK_MIX:
681             if (wifi_sta_wpa3_network_set(wpa_s, id, assoc) != EXT_WIFI_OK)
682                 return EXT_WIFI_FAIL;
683             break;
684 #endif /* CONFIG_WPA3 */
685 #ifdef LOS_CONFIG_WPA_ENTERPRISE
686         case EXT_WIFI_SECURITY_WPA:
687         case EXT_WIFI_SECURITY_WPA2:
688         case EXT_WIFI_SECURITY_WPA3:
689         case EXT_WIFI_SECURITY_WPA3_WPA2_MIX:
690             if (wifi_sta_set_key_mgmt_ent(wpa_s, id, assoc) != EXT_WIFI_OK)
691                 return EXT_WIFI_FAIL;
692             break;
693 #endif /* LOS_CONFIG_WPA_ENTERPRISE */
694 #ifdef CONFIG_WAPI
695         case EXT_WIFI_SECURITY_WAPI_PSK:
696             if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "key_mgmt", "WAPI-PSK",
697                 strlen("WAPI-PSK")) != EXT_WIFI_OK)
698                 return EXT_WIFI_FAIL;
699             break;
700 #endif
701         default:
702             return EXT_WIFI_FAIL;
703     }
704     return EXT_WIFI_OK;
705 }
706 
wifi_sta_psk_separate_set(struct wpa_supplicant * wpa_s,const struct wpa_assoc_request * assoc,const char * id,bool is_fast_connnect)707 static int wifi_sta_psk_separate_set(struct wpa_supplicant *wpa_s, const struct wpa_assoc_request *assoc,
708                                      const char *id, bool is_fast_connnect)
709 {
710     /* add \"\" (length 4) for param */
711     char param[WPA_MAX_SSID_KEY_INPUT_LEN + 4 + 1] = { 0 };
712     /* if fast connect and use psk,not check key */
713     if ((is_fast_connnect != false) && (assoc->psk_flag != EXT_WIFI_WPA_PSK_NOT_USE)) {
714         if ((snprintf_s(param, sizeof(param), sizeof(param) - 1, "%u", (unsigned int)assoc->psk_flag) < 0) ||
715             (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "set_psk", param, sizeof(param)) != EXT_WIFI_OK)) {
716             wpa_error_log0(MSG_ERROR, " only psk set fail .\n");
717             return EXT_WIFI_FAIL;
718         }
719     } else {
720         if (wifi_sta_psk_init(param, sizeof(param), os_strlen(assoc->key), assoc) != EXT_WIFI_OK)
721             return EXT_WIFI_FAIL;
722 #ifdef CONFIG_WPA3
723         if (((assoc->auth == EXT_WIFI_SECURITY_WPA3_WPA2_PSK_MIX) || (assoc->auth == EXT_WIFI_SECURITY_SAE)) &&
724             (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "sae_password", param, sizeof(param))
725                 != EXT_WIFI_OK)) {
726             (void)memset_s(param, sizeof(param), 0, sizeof(param));
727             return EXT_WIFI_FAIL;
728         }
729 #endif /* CONFIG_WPA3 */
730 
731         if (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "psk", param, sizeof(param)) != EXT_WIFI_OK) {
732             (void)memset_s(param, sizeof(param), 0, sizeof(param));
733             return EXT_WIFI_FAIL;
734         }
735     }
736     (void)memset_s(param, sizeof(param), 0, sizeof(param));
737     return EXT_WIFI_OK;
738 }
739 
740 #ifdef CONFIG_OWE
wifi_sta_owe_network_set(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc)741 static int wifi_sta_owe_network_set(struct wpa_supplicant *wpa_s, const char *id,
742     const struct wpa_assoc_request *assoc)
743 {
744     if ((wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN,
745         "key_mgmt", "OWE", strlen("OWE")) != EXT_WIFI_OK) ||
746         (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "ieee80211w", "2", strlen("2")) != EXT_WIFI_OK) ||
747         (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "scan_ssid", "1", strlen("1")) != EXT_WIFI_OK)) {
748         wpa_error_log0(MSG_ERROR, " key_mgmt set owe fail .\n");
749         return EXT_WIFI_FAIL;
750     }
751     return wifi_sta_set_pairwise(wpa_s, id, assoc);
752 }
753 #endif /* CONFIG_OWE */
754 
wifi_sta_set_network_psk(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc,bool is_fast_connnect)755 static int wifi_sta_set_network_psk(struct wpa_supplicant *wpa_s, const char *id,
756                                     const struct wpa_assoc_request *assoc, bool is_fast_connnect)
757 {
758     if (assoc->auth == EXT_WIFI_SECURITY_OPEN) {
759         if ((wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN,
760                                  "auth_alg", "OPEN", strlen("OPEN")) != EXT_WIFI_OK) ||
761             (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN,
762                                  "key_mgmt", "NONE", strlen("NONE")) != EXT_WIFI_OK)) {
763             wpa_error_log0(MSG_ERROR, " key_mgmt set fail .\n");
764             return EXT_WIFI_FAIL;
765         }
766         return EXT_WIFI_OK;
767     }
768 #ifdef CONFIG_OWE
769     if (assoc->auth == EXT_WIFI_SECURITY_OWE) {
770         return wifi_sta_owe_network_set(wpa_s, id, assoc);
771     }
772 #endif /* CONFIG_OWE */
773 
774     /* set security type */
775     if (wifi_sta_set_key_mgmt(wpa_s, id, assoc) != EXT_WIFI_OK)
776         return EXT_WIFI_FAIL;
777 
778     if (wifi_sta_psk_separate_set(wpa_s, assoc, id, is_fast_connnect) != EXT_WIFI_OK) {
779         wpa_error_log0(MSG_ERROR, " psk set fail .\n");
780         return EXT_WIFI_FAIL;
781     }
782 
783     return wifi_sta_set_pairwise(wpa_s, id, assoc);
784 }
785 
786 #ifdef LOS_CONFIG_WPA_ENTERPRISE
uapi_wifi_put_ca_cert(const unsigned char * ca_cert,unsigned int len)787 int uapi_wifi_put_ca_cert(const unsigned char *ca_cert, unsigned int len)
788 {
789     if (g_wfa_ca_cert == NULL) {
790         g_wfa_ca_cert = (char *)os_zalloc(sizeof(char) * ENTERPRISE_WFA_CERT_MAX_LEN);
791 
792         if (g_wfa_ca_cert == NULL) {
793             return EXT_WIFI_FAIL;
794         }
795     }
796 
797     if (ca_cert != NULL) {
798         (osal_void)memset_s(g_wfa_ca_cert, ENTERPRISE_WFA_CERT_MAX_LEN, 0, ENTERPRISE_WFA_CERT_MAX_LEN);
799 
800         if (memcpy_s(g_wfa_ca_cert, ENTERPRISE_WFA_CERT_MAX_LEN, ca_cert, len + 1) != EOK) {
801             wpa_error_log1(MSG_ERROR, " uapi_wifi_put_ca_cert memcpy failed, len:%d .\n", len);
802             return EXT_WIFI_FAIL;
803         }
804     }
805 
806     return EXT_WIFI_OK;
807 }
808 
uapi_wifi_put_cli_cert(const unsigned char * cli_cert,unsigned int len)809 int uapi_wifi_put_cli_cert(const unsigned char *cli_cert, unsigned int len)
810 {
811     if (g_wfa_cli_cert == NULL) {
812         g_wfa_cli_cert = (char *)os_zalloc(sizeof(char) * ENTERPRISE_WFA_CERT_MAX_LEN);
813 
814         if (g_wfa_cli_cert == NULL) {
815             return EXT_WIFI_FAIL;
816         }
817     }
818 
819     if (cli_cert != NULL) {
820         (osal_void)memset_s(g_wfa_cli_cert, ENTERPRISE_WFA_CERT_MAX_LEN, 0, ENTERPRISE_WFA_CERT_MAX_LEN);
821 
822         if (memcpy_s(g_wfa_cli_cert, ENTERPRISE_WFA_CERT_MAX_LEN, cli_cert, len + 1) != EOK) {
823             wpa_error_log1(MSG_ERROR, " uapi_wifi_put_cli_cert memcpy failed, len:%d .\n", len);
824             return EXT_WIFI_FAIL;
825         }
826     }
827 
828     return EXT_WIFI_OK;
829 }
830 
uapi_wifi_put_cli_key(const unsigned char * cli_key,unsigned int len)831 int uapi_wifi_put_cli_key(const unsigned char *cli_key, unsigned int len)
832 {
833     if (g_wfa_cli_key == NULL) {
834         g_wfa_cli_key = (char *)os_zalloc(sizeof(char) * ENTERPRISE_WFA_CERT_MAX_LEN);
835 
836         if (g_wfa_cli_key == NULL) {
837             return EXT_WIFI_FAIL;
838         }
839     }
840 
841     if (cli_key != NULL) {
842         (osal_void)memset_s(g_wfa_cli_key, ENTERPRISE_WFA_CERT_MAX_LEN, 0, ENTERPRISE_WFA_CERT_MAX_LEN);
843 
844         if (memcpy_s(g_wfa_cli_key, ENTERPRISE_WFA_CERT_MAX_LEN, cli_key, len) != EOK) {
845             wpa_error_log1(MSG_ERROR, " uapi_wifi_put_cli_key memcpy failed, len:%d .\n", len);
846             return EXT_WIFI_FAIL;
847         }
848     }
849 
850     return EXT_WIFI_OK;
851 }
852 
uapi_wifi_put_identity(const unsigned char * identity,unsigned int len)853 int uapi_wifi_put_identity(const unsigned char *identity, unsigned int len)
854 {
855     if (g_wfa_ent_identity == NULL) {
856         g_wfa_ent_identity = (char *)os_zalloc(sizeof(char) * EXT_WIFI_IDENTITY_LEN);
857 
858         if (g_wfa_ent_identity == NULL) {
859             return EXT_WIFI_FAIL;
860         }
861     }
862 
863     if (identity != NULL) {
864         (osal_void)memset_s(g_wfa_ent_identity, EXT_WIFI_IDENTITY_LEN, 0, EXT_WIFI_IDENTITY_LEN);
865 
866         if (memcpy_s(g_wfa_ent_identity, EXT_WIFI_IDENTITY_LEN, identity, len) != EOK) {
867             wpa_error_log1(MSG_ERROR, " uapi_wifi_put_identity memcpy failed, len:%d .\n", len);
868             return EXT_WIFI_FAIL;
869         }
870     }
871 
872     return EXT_WIFI_OK;
873 }
uapi_wifi_ca_cert_import_cb(const unsigned char ** buf,unsigned int * len)874 int uapi_wifi_ca_cert_import_cb(const unsigned char **buf, unsigned int *len)
875 {
876     if (g_wfa_ca_cert != NULL) {
877         *len = strlen(g_wfa_ca_cert) + 1;
878         *buf = g_wfa_ca_cert;
879     } else if (g_ent_ca_cert != NULL) {
880         *len = strlen(g_ent_ca_cert) + 1;
881         *buf = g_ent_ca_cert;
882     }
883 
884     return EXT_WIFI_OK;
885 }
886 
uapi_wifi_cli_cert_import_cb(const unsigned char ** buf,unsigned int * len)887 int uapi_wifi_cli_cert_import_cb(const unsigned char **buf, unsigned int *len)
888 {
889     if (g_wfa_cli_cert != NULL) {
890         *len = strlen(g_wfa_cli_cert) + 1;
891         *buf = g_wfa_cli_cert;
892     } else if (g_ent_cli_cert != NULL) {
893         *len = strlen(g_ent_cli_cert) + 1;
894         *buf = g_ent_cli_cert;
895     }
896 
897     return EXT_WIFI_OK;
898 }
899 
uapi_wifi_cli_key_import_cb(const unsigned char ** buf,unsigned int * len)900 int uapi_wifi_cli_key_import_cb(const unsigned char **buf, unsigned int *len)
901 {
902     if (g_wfa_cli_key != NULL) {
903         *len = strlen(g_wfa_cli_key) + 1;
904         *buf = g_wfa_cli_key;
905     } else if (g_ent_cli_key != NULL) {
906         *len = strlen(g_ent_cli_key) + 1;
907         *buf = g_ent_cli_key;
908     }
909 
910     return EXT_WIFI_OK;
911 }
912 
wifi_sta_set_network_ent(struct wpa_supplicant * wpa_s,const char * id,const struct wpa_assoc_request * assoc)913 static int wifi_sta_set_network_ent(struct wpa_supplicant *wpa_s, const char *id,
914                                     const struct wpa_assoc_request *assoc)
915 {
916     ext_wifi_ent_import_callback ent_cb;
917 
918     uapi_wifi_sta_ent_set_eap_method(EXT_WIFI_EAP_METHOD_TLS);
919     if (g_wfa_ent_identity != NULL) {
920         if (uapi_wifi_sta_ent_set_identity(g_wfa_ent_identity, strlen(g_wfa_ent_identity))) {
921             wpa_error_log0(MSG_ERROR, "wifi_sta_set_network_ent:set identity error.");
922             return EXT_WIFI_FAIL;
923         }
924     } else if (uapi_wifi_sta_ent_set_identity(g_ent_identity, strlen(g_ent_identity))) {
925         wpa_error_log0(MSG_ERROR, "wifi_sta_set_network_ent:set identity error.");
926         return EXT_WIFI_FAIL;
927     }
928 
929     ent_cb.ca_cert_import = uapi_wifi_ca_cert_import_cb;
930     ent_cb.cli_cert_import = uapi_wifi_cli_cert_import_cb;
931     ent_cb.cli_key_import = uapi_wifi_cli_key_import_cb;
932     uapi_wifi_sta_ent_set_import_callback(&ent_cb);
933 
934     if ((wifi_sta_set_key_mgmt(wpa_s, id, assoc) != EXT_WIFI_OK) ||
935         (wpa_cli_set_network(wpa_s, id, WPA_NETWORK_ID_TXT_LEN, "identity",
936                              g_eap_identity, strlen(g_eap_identity)) != EXT_WIFI_OK))
937         return EXT_WIFI_FAIL;
938 
939     return wifi_sta_set_pairwise_ent(wpa_s, id, assoc);
940 }
941 #endif /* LOS_CONFIG_WPA_ENTERPRISE */
942 
los_freq_to_channel(int freq,unsigned int * channel)943 int los_freq_to_channel(int freq, unsigned int *channel)
944 {
945     unsigned char tmp_channel = 0;
946     if (channel == NULL)
947         return EXT_WIFI_FAIL;
948 
949     if (ieee80211_freq_to_chan(freq, &tmp_channel) == NUM_HOSTAPD_MODES)
950         return EXT_WIFI_FAIL;
951 
952     *channel = tmp_channel;
953     return EXT_WIFI_OK;
954 }
955 
wifi_scan_ssid_set(const struct wpa_scan_params * params,char * buf,int bufflen)956 static int wifi_scan_ssid_set(const struct wpa_scan_params *params, char *buf, int bufflen)
957 {
958     int ret;
959     if (params->ssid_len < 0) {
960         wpa_error_log0(MSG_ERROR, " ssid scan len error .\n");
961         return EXT_WIFI_FAIL;
962     }
963     if (params->ssid_len == 0)
964         return EXT_WIFI_OK;
965 
966     ret = snprintf_s(buf, (size_t)bufflen, (size_t)(bufflen - 1), "ssid %s", params->ssid);
967     if (ret < 0)
968         return EXT_WIFI_FAIL;
969 
970     ret = snprintf_s(g_scan_record.ssid, sizeof(g_scan_record.ssid), sizeof(params->ssid) - 1, "%s", params->ssid);
971     if (ret < 0)
972         return EXT_WIFI_FAIL;
973 
974     return EXT_WIFI_OK;
975 }
976 
wifi_scan_param_handle(const struct wpa_scan_params * params,char * addr_txt,int addr_len,char * freq_buff,int freq_len)977 static int wifi_scan_param_handle(const struct wpa_scan_params *params, char *addr_txt, int addr_len,
978                                   char *freq_buff, int freq_len)
979 {
980     errno_t rc;
981     int freq;
982     int ret;
983     int i;
984     int sum = 0;
985     char freq_str[WPA_MAX_FREQ_LEN] = { 0 };
986     if ((params == NULL) || (addr_txt == NULL) || (freq_buff == NULL) || (addr_len <= 0) || (freq_len <= 0)) {
987         wpa_error_log0(MSG_ERROR, "wpa_pre_prarms_handle: input params is NULL\n");
988         return EXT_WIFI_FAIL;
989     }
990     rc = memset_s(&g_scan_record, sizeof(g_scan_record), 0, sizeof(g_scan_record));
991     if (rc != EOK) {
992         wpa_error_log0(MSG_ERROR, "wpa_pre_prarms_handle memset_s failed");
993         return EXT_WIFI_FAIL;
994     }
995     if (params->flag == EXT_BSSID_SCAN) {
996         ret = snprintf_s(addr_txt, (size_t)addr_len, (size_t)(addr_len - 1), MACSTR, MAC2STR(params->bssid));
997         if (ret < 0) {
998             wpa_error_log0(MSG_ERROR, "wpa_pre_prarms_handle snprintf_s faild");
999             return EXT_WIFI_FAIL;
1000         }
1001     }
1002     g_scan_record.flag = params->flag;
1003     if (params->flag == EXT_PREFIX_SSID_SCAN)
1004         g_ssid_prefix_flag = WPA_FLAG_ON;
1005     // freq scan
1006     for (i = 0; (i < params->chan_num) && (i < WIFI_24G_CHANNEL_NUMS); i++) {
1007         freq = chan_to_freq((unsigned char)params->chan_list[i]);
1008         if (freq <= 0) {
1009             wpa_error_log0(MSG_ERROR, "wpa_pre_prarms_handle invalid .\n");
1010             return EXT_WIFI_FAIL;
1011         }
1012 
1013         if (i == 0) {
1014             ret = snprintf_s(freq_str, WPA_MAX_FREQ_LEN, WPA_MAX_FREQ_LEN, "freq=%d", freq);
1015         } else {
1016             ret = snprintf_s(freq_str, WPA_MAX_FREQ_LEN, WPA_MIN_FREQ_LEN, ",%d", freq);
1017         }
1018         if (ret < 0) {
1019             wpa_error_log0(MSG_ERROR, "wifi_scan_param_handle snprintf_s faild");
1020             return EXT_WIFI_FAIL;
1021         }
1022 
1023         ret = snprintf_s(freq_buff + sum, (size_t)(freq_len - sum), (size_t)(strlen(freq_str) + 1), "%s", freq_str);
1024         if (ret < 0) {
1025             wpa_error_log0(MSG_ERROR, "wpa_pre_prarms_handle snprintf_s faild");
1026             return EXT_WIFI_FAIL;
1027         }
1028         sum += ret;
1029         if (g_scan_record.chan_num < WIFI_24G_CHANNEL_NUMS)
1030             g_scan_record.chan_list[g_scan_record.chan_num++] = params->chan_list[i];
1031     }
1032     return EXT_WIFI_OK;
1033 }
1034 
wifi_scan_buffer_process(const char * freq_buff,const char * ssid_buff,const char * bssid_buff,ext_wifi_iftype iftype)1035 static int wifi_scan_buffer_process(const char *freq_buff, const char *ssid_buff,
1036                                     const char *bssid_buff, ext_wifi_iftype iftype)
1037 {
1038     int sum;
1039     int ret                            = EXT_WIFI_OK;
1040     unsigned int ret_val               = EXT_WIFI_OK;
1041     char buf[WPA_EXTERNED_SSID_LEN]    = { 0 };
1042     struct wpa_supplicant *wpa_s       = NULL;
1043     struct ext_wifi_dev *wifi_dev = los_get_wifi_dev_by_iftype(iftype);
1044     if ((wifi_dev == NULL) || ((wifi_dev)->priv == NULL)) {
1045         wpa_error_log0(MSG_ERROR, "wpa_buffer_handle: get wifi dev failed");
1046         return EXT_WIFI_FAIL;
1047     }
1048     wpa_s = (struct wpa_supplicant *)(wifi_dev->priv);
1049     if (freq_buff[0] != '\0') {
1050         ret = snprintf_s(buf, sizeof(buf), strlen(freq_buff) + 1, "%s ", freq_buff);
1051         if (ret < 0) {
1052             wpa_error_log0(MSG_ERROR, "wpa_buffer_handle: freq_buff snprintf_s faild");
1053             return EXT_WIFI_FAIL;
1054         }
1055     }
1056     sum = ret;
1057     if (bssid_buff[0] != '\0') {
1058         ret = snprintf_s((char *)(buf + sum), (size_t)(sizeof(buf) - (size_t)sum),
1059                          (size_t)(strlen(bssid_buff) + 1), "%s ", bssid_buff);
1060         if (ret < 0) {
1061             wpa_error_log0(MSG_ERROR, "wpa_buffer_handle: bssid_buff snprintf_s faild");
1062             return EXT_WIFI_FAIL;
1063         }
1064     }
1065     sum += ret;
1066     if (ssid_buff[0] != '\0' && g_ssid_prefix_flag != WPA_FLAG_ON) {
1067         ret = snprintf_s((char *)(buf + sum), (size_t)(sizeof(buf) - (size_t)sum),
1068                          (size_t)(strlen(ssid_buff) + 1), "%s", ssid_buff);
1069         if (ret < 0) {
1070             wpa_error_log0(MSG_ERROR, "wpa_buffer_handle: ssid_buff snprintf_s faild");
1071             return EXT_WIFI_FAIL;
1072         }
1073     }
1074     sum += ret;
1075     g_usr_scanning_flag = WPA_FLAG_ON;
1076     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_SCAN_OK);
1077     if (wpa_cli_scan(wpa_s, buf, sum) == EXT_WIFI_OK) {
1078         wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_SCAN_OK");
1079         (void)os_event_read(g_wpa_event, WPA_EVENT_SCAN_OK, &ret_val,
1080                             WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_EVENT_DELAY_5S);
1081     }
1082     g_usr_scanning_flag = WPA_FLAG_OFF;
1083     g_ssid_prefix_flag = WPA_FLAG_OFF;
1084     if (ret_val != WPA_EVENT_SCAN_OK) {
1085         wpa_error_log1(MSG_ERROR, "os_event_read WPA_EVENT_SCAN_OK failed ret_val = %x", ret_val);
1086         return EXT_WIFI_FAIL;
1087     }
1088     return EXT_WIFI_OK;
1089 }
1090 
wifi_wpa_scan(const struct wpa_scan_params * params,ext_wifi_iftype iftype)1091 static int wifi_wpa_scan(const struct wpa_scan_params *params, ext_wifi_iftype iftype)
1092 {
1093     int ret;
1094     errno_t rc;
1095     char freq_buff[WPA_MIN_FREQ_LEN * WIFI_24G_CHANNEL_NUMS] = { 0 };
1096     char ssid_buff[WPA_MAX_SSID_LEN * 2]    = { 0 };
1097     char bssid_buff[WPA_CMD_BSSID_LEN]      = { 0 };
1098     char addr_txt[EXT_WIFI_TXT_ADDR_LEN + 1] = { 0 };
1099 
1100     wpa_error_log0(MSG_ERROR, " ---> ### los_wpa_scan enter. \n\n\r");
1101     if (iftype == EXT_WIFI_IFTYPE_AP)
1102         return EXT_WIFI_FAIL;
1103 
1104     if (wifi_scan_param_handle(params, addr_txt, EXT_WIFI_TXT_ADDR_LEN + 1, freq_buff,
1105         (int)sizeof(freq_buff)) != EXT_WIFI_OK)
1106         return EXT_WIFI_FAIL;
1107 
1108     ret = wifi_scan_ssid_set(params, ssid_buff, (int)sizeof(ssid_buff));
1109     if (ret != EXT_WIFI_OK) {
1110         wpa_error_log0(MSG_ERROR, " los_wpa_scan: los_wifi_wpa_ssid_scan_set failed");
1111         return EXT_WIFI_FAIL;
1112     }
1113     if (params->flag == EXT_BSSID_SCAN) {
1114         ret = snprintf_s(bssid_buff, WPA_CMD_BSSID_LEN, WPA_CMD_BSSID_LEN - 1, "bssid=%s", addr_txt);
1115         if (ret < 0) {
1116             wpa_error_log0(MSG_ERROR, "los_wpa_scan snprintf_s faild");
1117             return EXT_WIFI_FAIL;
1118         }
1119         rc = memcpy_s(g_scan_record.bssid, ETH_ALEN, params->bssid, ETH_ALEN);
1120         if (rc != EOK)
1121             return EXT_WIFI_FAIL;
1122     }
1123     if (((strlen(freq_buff) + 1) + (strlen(bssid_buff) + 1) + (strlen(ssid_buff) + 1)) > WPA_EXTERNED_SSID_LEN) {
1124         wpa_error_log0(MSG_ERROR, " ssid_buff is too long.\n");
1125         return EXT_WIFI_FAIL;
1126     }
1127     return wifi_scan_buffer_process(freq_buff, ssid_buff, bssid_buff, iftype);
1128 }
1129 
wifi_scan_params_parse_channel_scan(const ext_wifi_scan_params * sp,struct wpa_scan_params * scan_params)1130 static int wifi_scan_params_parse_channel_scan(const ext_wifi_scan_params *sp, struct wpa_scan_params *scan_params)
1131 {
1132     unsigned int i;
1133     unsigned int j;
1134     unsigned int is_found;
1135 
1136     if ((sp->chan_num > WIFI_24G_CHANNEL_NUMS)) {
1137         wpa_error_log0(MSG_ERROR, "Invalid channel_num!");
1138         return EXT_WIFI_FAIL;
1139     }
1140     for (i = 0; i < sp->chan_num; i++) {
1141         if (wifi_channel_check(sp->chan_list[i]) == EXT_WIFI_FAIL) {
1142             wpa_error_log0(MSG_ERROR, "Invalid channel!");
1143             return EXT_WIFI_FAIL;
1144         }
1145         is_found = 0;
1146         for (j = 0; j < scan_params->chan_num; j++) {
1147             if (scan_params->chan_list[j] == sp->chan_list[i]) {
1148                 is_found = 1;
1149                 break;
1150             }
1151         }
1152         if (is_found)
1153             continue;
1154         scan_params->chan_list[scan_params->chan_num++] = sp->chan_list[i];
1155     }
1156     scan_params->flag = EXT_CHANNEL_SCAN;
1157     return EXT_WIFI_OK;
1158 }
1159 
wifi_scan_params_parse_ssid_scan(const ext_wifi_scan_params * sp,struct wpa_scan_params * scan_params)1160 static int wifi_scan_params_parse_ssid_scan(const ext_wifi_scan_params *sp, struct wpa_scan_params *scan_params)
1161 {
1162     unsigned int len = (unsigned int)strnlen(sp->ssid, EXT_WIFI_MAX_SSID_LEN + 1);
1163     if ((sp->ssid_len == 0) || (len != sp->ssid_len) || (len > EXT_WIFI_MAX_SSID_LEN) ||
1164         (sp->ssid_len > EXT_WIFI_MAX_SSID_LEN)) {
1165         wpa_error_log0(MSG_ERROR, "invalid scan ssid parameter");
1166         return EXT_WIFI_FAIL;
1167     }
1168     scan_params->ssid_len = sp->ssid_len;
1169     if ((memcpy_s(scan_params->ssid, sizeof(scan_params->ssid), sp->ssid, sp->ssid_len + 1)) != EOK) {
1170         wpa_error_log0(MSG_ERROR, "EXT_WIFI_SSID_PREFIX_SCAN wpa_scan_params_parse memcpy_s");
1171         return EXT_WIFI_FAIL;
1172     }
1173     scan_params->flag = (sp->scan_type == EXT_WIFI_SSID_SCAN) ? EXT_SSID_SCAN : EXT_PREFIX_SSID_SCAN;
1174     return EXT_WIFI_OK;
1175 }
1176 
wifi_scan_params_parse_bssid_scan(const ext_wifi_scan_params * sp,struct wpa_scan_params * scan_params)1177 static int wifi_scan_params_parse_bssid_scan(const ext_wifi_scan_params *sp, struct wpa_scan_params *scan_params)
1178 {
1179     if (addr_precheck(sp->bssid) != EXT_WIFI_OK) {
1180         wpa_error_log0(MSG_ERROR, "invalid scan bssid parameter");
1181         return EXT_WIFI_FAIL;
1182     }
1183     scan_params->flag = EXT_BSSID_SCAN;
1184     if ((memcpy_s(scan_params->bssid, ETH_ALEN, sp->bssid, ETH_ALEN)) != EOK) {
1185         wpa_error_log0(MSG_ERROR, "EXT_WIFI_BSSID_SCAN wpa_scan_params_parse memcpy_s");
1186         return EXT_WIFI_FAIL;
1187     }
1188     return EXT_WIFI_OK;
1189 }
1190 
wifi_scan_params_parse(const ext_wifi_scan_params * sp,struct wpa_scan_params * scan_params)1191 static int wifi_scan_params_parse(const ext_wifi_scan_params *sp, struct wpa_scan_params *scan_params)
1192 {
1193     int ret = EXT_WIFI_FAIL;
1194 
1195     if ((sp == NULL) || (sp->scan_type == EXT_WIFI_BASIC_SCAN)) {
1196         scan_params->flag = EXT_SCAN;
1197         wpa_warning_log0(MSG_ERROR, "uapi_wifi_sta_scan: basic scan!");
1198         return EXT_WIFI_OK;
1199     }
1200     switch (sp->scan_type) {
1201         case EXT_WIFI_CHANNEL_SCAN:
1202             ret = wifi_scan_params_parse_channel_scan(sp, scan_params);
1203             break;
1204         case EXT_WIFI_SSID_SCAN:
1205             /* fall-through */
1206         case EXT_WIFI_SSID_PREFIX_SCAN:
1207             ret = wifi_scan_params_parse_ssid_scan(sp, scan_params);
1208             break;
1209         case EXT_WIFI_BSSID_SCAN:
1210             ret = wifi_scan_params_parse_bssid_scan(sp, scan_params);
1211             break;
1212         default:
1213             wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_scan: Invalid scan_type!");
1214             return EXT_WIFI_FAIL;
1215     }
1216     return ret;
1217 }
1218 
wifi_scan(ext_wifi_iftype iftype,bool is_mesh,const ext_wifi_scan_params * sp)1219 int wifi_scan(ext_wifi_iftype iftype, bool is_mesh, const ext_wifi_scan_params *sp)
1220 {
1221     int ret;
1222     struct wpa_scan_params scan_params = { 0 };
1223     wpa_error_log0(MSG_ERROR, " ---> ### wpa_scan enter. \n\n\r");
1224 
1225     if (is_mesh == true) {
1226         if ((iftype == EXT_WIFI_IFTYPE_STATION) && (g_mesh_sta_flag == WPA_FLAG_OFF)) {
1227             wpa_error_log0(MSG_ERROR, "los_wpa_scan: mesh sta scan while g_mesh_sta_flag is off");
1228             return EXT_WIFI_FAIL;
1229         }
1230 
1231         if ((iftype == EXT_WIFI_IFTYPE_MESH_POINT) && (g_mesh_sta_flag != WPA_FLAG_OFF)) {
1232             wpa_error_log0(MSG_ERROR, "los_wpa_scan: mesh scan while g_mesh_sta_flag is on");
1233             return EXT_WIFI_FAIL;
1234         }
1235     } else if (iftype == EXT_WIFI_IFTYPE_STATION) {
1236             if (g_mesh_sta_flag != WPA_FLAG_OFF) {
1237                 wpa_error_log0(MSG_ERROR, "los_wpa_scan: sta scan while g_mesh_sta_flag is on.");
1238                 return EXT_WIFI_FAIL;
1239             }
1240     } else {
1241         wpa_error_log0(MSG_ERROR, "los_wpa_scan: Invalid scan cmd.");
1242         return EXT_WIFI_FAIL;
1243     }
1244 
1245     if ((wifi_dev_get(iftype) == NULL)) {
1246         wpa_error_log0(MSG_ERROR, "los_wpa_scan: wifi dev get fail.");
1247         return EXT_WIFI_FAIL;
1248     }
1249     if (wifi_scan_params_parse(sp, &scan_params) != EXT_WIFI_OK) {
1250         wpa_error_log0(MSG_ERROR, "los_wpa_scan: scan_params_parse fail.");
1251         return EXT_WIFI_FAIL;
1252     }
1253 
1254     g_scan_flag = WPA_FLAG_ON;
1255     ret = wifi_wpa_scan(&scan_params, iftype);
1256     if (ret != EXT_WIFI_OK) {
1257         g_scan_flag = WPA_FLAG_OFF;
1258         wpa_error_log0(MSG_ERROR, "los_wpa_scan fail.");
1259         return EXT_WIFI_FAIL;
1260     }
1261 
1262     wpa_error_log1(MSG_ERROR, "los_wpa_scan: scan_params.flag: %u", (unsigned int)scan_params.flag);
1263     return EXT_WIFI_OK;
1264 }
1265 
wifi_free_scan_param(ext_scan_stru * scan_params)1266 static void wifi_free_scan_param(ext_scan_stru *scan_params)
1267 {
1268     if (scan_params == NULL)
1269         return;
1270 
1271     if (scan_params->ssids != NULL) {
1272         os_free(scan_params->ssids);
1273         scan_params->ssids = NULL;
1274     }
1275 
1276     if (scan_params->bssid != NULL) {
1277         os_free(scan_params->bssid);
1278         scan_params->bssid = NULL;
1279     }
1280 
1281     if (scan_params->freqs != NULL) {
1282         os_free(scan_params->freqs);
1283         scan_params->freqs = NULL;
1284     }
1285 
1286     os_free(scan_params);
1287     scan_params = NULL;
1288 }
1289 
uapi_wifi_ap_scan(void)1290 int uapi_wifi_ap_scan(void)
1291 {
1292     errno_t rc;
1293     unsigned int ret = EXT_WIFI_OK;
1294     ext_scan_stru *scan_params = NULL;
1295     struct wpa_supplicant *wpa_s       = NULL;
1296     struct ext_wifi_dev *wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_AP);
1297 
1298     if ((wifi_dev == NULL) || (wifi_dev->ifname == NULL)) {
1299         wpa_error_log0(MSG_ERROR, "uapi_wifi_ap_scan: get wifi dev failed");
1300         return EXT_WIFI_FAIL;
1301     }
1302 
1303     scan_params = (ext_scan_stru *)os_zalloc(sizeof(ext_scan_stru));
1304     if (scan_params == NULL) {
1305         return EXT_WIFI_FAIL;
1306     }
1307     scan_params->num_ssids = 1;
1308     scan_params->ssids = (ext_driver_scan_ssid_stru *)os_zalloc(sizeof(ext_driver_scan_ssid_stru));
1309     if (scan_params->ssids == NULL) {
1310         ret = EXT_WIFI_FAIL;
1311         goto EXIT;
1312     }
1313 
1314     g_scan_flag = WPA_FLAG_ON;
1315     rc = memset_s(&g_scan_record, sizeof(g_scan_record), 0, sizeof(g_scan_record));
1316     if (rc != EOK) {
1317         wpa_error_log0(MSG_ERROR, "uapi_wifi_ap_scan memset_s failed");
1318         ret = EXT_WIFI_FAIL;
1319         goto EXIT;
1320     }
1321     g_scan_record.flag = EXT_SCAN;
1322     g_usr_scanning_flag = WPA_FLAG_ON;
1323 
1324     ret = drv_soc_ioctl_scan(wifi_dev->ifname, scan_params);
1325     ret = (ret != EXT_SUCC) ? EXT_WIFI_FAIL : EXT_WIFI_OK;
1326     g_usr_scanning_flag = WPA_FLAG_OFF;
1327     g_ssid_prefix_flag = WPA_FLAG_OFF;
1328 EXIT:
1329     wifi_free_scan_param(scan_params);
1330     return ret;
1331 }
1332 
wifi_encap_raw_scan_param(ext_wifi_scan_params * sp,ext_scan_stru * scan_params)1333 static int wifi_encap_raw_scan_param(ext_wifi_scan_params *sp, ext_scan_stru *scan_params)
1334 {
1335     errno_t rc;
1336 
1337     if (sp->scan_type == EXT_WIFI_BASIC_SCAN) {
1338         g_scan_record.flag = EXT_SCAN;
1339         return EXT_WIFI_OK;
1340     } else if (sp->scan_type == EXT_WIFI_CHANNEL_SCAN) {
1341         g_scan_record.flag = EXT_CHANNEL_SCAN;
1342         scan_params->freqs = (int32 *)os_zalloc((sizeof(int32)));
1343         if (scan_params->freqs == NULL) {
1344             return EXT_WIFI_FAIL;
1345         }
1346         if (g_scan_record.chan_num < WIFI_24G_CHANNEL_NUMS) {
1347             g_scan_record.chan_list[g_scan_record.chan_num++] = sp->chan_list[0];
1348         }
1349         scan_params->freqs[0] = chan_to_freq((unsigned char)sp->chan_list[0]);
1350         scan_params->num_freqs = sp->chan_num;
1351     } else if (sp->scan_type == EXT_WIFI_SSID_SCAN || sp->scan_type == EXT_WIFI_SSID_PREFIX_SCAN) {
1352         g_scan_record.flag = (sp->scan_type == EXT_WIFI_SSID_SCAN) ? EXT_SSID_SCAN : EXT_PREFIX_SSID_SCAN;
1353         g_ssid_prefix_flag = WPA_FLAG_ON;
1354         rc = memcpy_s(scan_params->ssids[0].ssid, MAX_SSID_LEN, sp->ssid, sp->ssid_len + 1);
1355         if (rc != EOK) {
1356             return EXT_WIFI_FAIL;
1357         }
1358         rc = memcpy_s(g_scan_record.ssid, sizeof(g_scan_record.ssid), sp->ssid, sp->ssid_len +1);
1359         if (rc != EOK)
1360             return EXT_WIFI_FAIL;
1361     } else if (sp->scan_type == EXT_WIFI_BSSID_SCAN) {
1362         g_scan_record.flag = EXT_BSSID_SCAN;
1363         scan_params->bssid = (uint8 *)os_zalloc(ETH_ALEN);
1364         if (scan_params->bssid == NULL)
1365             return EXT_WIFI_FAIL;
1366         rc = memcpy_s(scan_params->bssid, ETH_ALEN, sp->bssid, ETH_ALEN);
1367         if (rc != EOK) {
1368             return EXT_WIFI_FAIL;
1369         }
1370         rc = memcpy_s(g_scan_record.bssid, ETH_ALEN, scan_params->bssid, ETH_ALEN);
1371         if (rc != EOK)
1372             return EXT_WIFI_FAIL;
1373     }
1374     return EXT_WIFI_OK;
1375 }
1376 
uapi_wifi_sta_raw_scan(ext_wifi_scan_params * sp,ext_wifi_scan_no_save_cb cb)1377 int uapi_wifi_sta_raw_scan(ext_wifi_scan_params *sp, ext_wifi_scan_no_save_cb cb)
1378 {
1379     errno_t rc;
1380     unsigned int ret = EXT_WIFI_OK;
1381     ext_scan_stru *scan_params = NULL;
1382     struct wpa_supplicant *wpa_s       = NULL;
1383     struct ext_wifi_dev *wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_STATION);
1384 
1385     if ((wifi_dev == NULL) || (wifi_dev->ifname == NULL)) {
1386         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_raw_scan: get wifi dev failed");
1387         return EXT_WIFI_FAIL;
1388     }
1389 
1390     scan_params = (ext_scan_stru *)os_zalloc(sizeof(ext_scan_stru));
1391     if (scan_params == NULL) {
1392         return EXT_WIFI_FAIL;
1393     }
1394     scan_params->num_ssids = 1;
1395     scan_params->ssids = (ext_driver_scan_ssid_stru *)os_zalloc(sizeof(ext_driver_scan_ssid_stru));
1396     if (scan_params->ssids == NULL) {
1397         ret = EXT_WIFI_FAIL;
1398         goto EXIT;
1399     }
1400 
1401     rc = memset_s(&g_scan_record, sizeof(g_scan_record), 0, sizeof(g_scan_record));
1402     if (rc != EOK) {
1403         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_raw_scan memset_s failed");
1404         ret = EXT_WIFI_FAIL;
1405         goto EXIT;
1406     }
1407 
1408     if (wifi_encap_raw_scan_param(sp, scan_params) != EXT_WIFI_OK) {
1409         ret = EXT_WIFI_FAIL;
1410         goto EXIT;
1411     }
1412 
1413     g_raw_scan_cb = cb;
1414     g_scan_flag = WPA_FLAG_ON;
1415     g_usr_scanning_flag = WPA_FLAG_ON;
1416     ret = drv_soc_ioctl_scan(wifi_dev->ifname, scan_params);
1417     ret = (ret != EXT_SUCC) ? EXT_WIFI_FAIL : EXT_WIFI_OK;
1418     g_usr_scanning_flag = WPA_FLAG_OFF;
1419     g_ssid_prefix_flag = WPA_FLAG_OFF;
1420 EXIT:
1421     wifi_free_scan_param(scan_params);
1422     return ret;
1423 }
1424 
uapi_wifi_get_scan_flag(void)1425 int uapi_wifi_get_scan_flag(void)
1426 {
1427     return g_scan_flag;
1428 }
1429 
uapi_wifi_sta_scan(void)1430 int uapi_wifi_sta_scan(void)
1431 {
1432     return wifi_scan(EXT_WIFI_IFTYPE_STATION, false, NULL);
1433 }
1434 
uapi_wifi_sta_advance_scan(ext_wifi_scan_params * sp)1435 int uapi_wifi_sta_advance_scan(ext_wifi_scan_params *sp)
1436 {
1437     return wifi_scan(EXT_WIFI_IFTYPE_STATION, false, sp);
1438 }
1439 
wifi_scan_result(ext_wifi_iftype iftype)1440 int wifi_scan_result(ext_wifi_iftype iftype)
1441 {
1442     struct ext_wifi_dev *wifi_dev = wifi_dev_get(iftype);
1443     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL) ||
1444         (wifi_dev->iftype == EXT_WIFI_IFTYPE_AP) ||
1445         (wifi_dev->iftype >= EXT_WIFI_IFTYPE_P2P_CLIENT)) {
1446         wpa_error_log0(MSG_ERROR, "wifi_scan_result: get wifi dev failed\n");
1447         return EXT_WIFI_FAIL;
1448     }
1449 
1450     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_GET_SCAN_RESULT_FLAG);
1451     if (wpa_cli_scan_results((struct wpa_supplicant *)(wifi_dev->priv)) != EXT_WIFI_OK) {
1452         wpa_error_log0(MSG_ERROR, "wifi_scan_result: wpa_cli_scan_results failed.");
1453         return EXT_WIFI_FAIL;
1454     }
1455 
1456     wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_GET_SCAN_RESULT_FLAG");
1457     unsigned int ret_val;
1458     (void)os_event_read(g_wpa_event, WPA_EVENT_GET_SCAN_RESULT_FLAG, &ret_val,
1459                         WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_EVENT_DELAY_5S);
1460     if (ret_val == WPA_EVENT_GET_SCAN_RESULT_OK) {
1461         if (g_scan_result_buf == NULL)
1462             return EXT_WIFI_FAIL;
1463     } else {
1464         wpa_error_log1(MSG_ERROR, "os_event_read WPA_EVENT_GET_SCAN_RESULT_FLAG error ret_val =%x", ret_val);
1465         return EXT_WIFI_FAIL;
1466     }
1467     return EXT_WIFI_OK;
1468 }
1469 
wifi_scan_result_bssid_parse(char ** starttmp,void * buf,size_t * reply_len)1470 int wifi_scan_result_bssid_parse(char **starttmp, void *buf, size_t *reply_len)
1471 {
1472     char *endtmp = NULL;
1473     char addr_txt[EXT_WIFI_TXT_ADDR_LEN + 1] = { 0 };
1474     ext_wifi_ap_info *ap_list = (ext_wifi_ap_info *)buf;
1475     if ((starttmp == NULL) || (*starttmp == NULL) || (buf == NULL) || (reply_len == NULL))
1476         return EXT_WIFI_FAIL;
1477 
1478     endtmp = strchr(*starttmp, '\t');
1479     if (endtmp == NULL)
1480         return EXT_WIFI_FAIL;
1481 
1482     *endtmp = '\0';
1483     if (strncpy_s(addr_txt, sizeof(addr_txt), *starttmp, EXT_WIFI_TXT_ADDR_LEN) != EOK)
1484         return EXT_WIFI_FAIL;
1485 
1486     if (hwaddr_aton(addr_txt, ap_list->bssid))
1487         return EXT_WIFI_FAIL;
1488 
1489     *reply_len -= (size_t)(endtmp - (*starttmp) + 1);
1490     *starttmp = ++endtmp;
1491     return EXT_WIFI_OK;
1492 }
1493 
wifi_scan_result_freq_parse(char ** starttmp,void * buf,size_t * reply_len)1494 int wifi_scan_result_freq_parse(char **starttmp, void *buf, size_t *reply_len)
1495 {
1496     char *endtmp = NULL;
1497     ext_wifi_ap_info *ap_list = (ext_wifi_ap_info *)buf;
1498     if ((starttmp == NULL) || (*starttmp == NULL) || (buf == NULL) || (reply_len == NULL))
1499         return EXT_WIFI_FAIL;
1500 
1501     endtmp = strchr(*starttmp, '\t');
1502     if (endtmp == NULL)
1503         return EXT_WIFI_FAIL;
1504 
1505     *endtmp = '\0';
1506     if (los_freq_to_channel(atoi(*starttmp), &(ap_list->channel)) != EXT_WIFI_OK)
1507         ap_list->channel = 0;
1508 
1509     *reply_len -= (size_t)(endtmp - (*starttmp) + 1);
1510     *starttmp = ++endtmp;
1511     return EXT_WIFI_OK;
1512 }
1513 
wifi_scan_result_rssi_parse(char ** starttmp,void * buf,size_t * reply_len)1514 int wifi_scan_result_rssi_parse(char **starttmp, void *buf, size_t *reply_len)
1515 {
1516     char *endtmp = NULL;
1517     ext_wifi_ap_info *ap_list = (ext_wifi_ap_info *)buf;
1518     if ((starttmp == NULL) || (*starttmp == NULL) || (buf == NULL) || (reply_len == NULL))
1519         return EXT_WIFI_FAIL;
1520 
1521     endtmp = strchr(*starttmp, '\t');
1522     if (endtmp == NULL)
1523         return EXT_WIFI_FAIL;
1524 
1525     *endtmp = '\0';
1526     if (**starttmp == '-') {
1527         ap_list->rssi = -atoi(++(*starttmp));
1528         *reply_len -= (size_t)(endtmp - (*starttmp) + 1 + 1);
1529         *starttmp = ++endtmp;
1530     } else {
1531         ap_list->rssi = atoi(*starttmp);
1532         *reply_len -= (size_t)(endtmp - (*starttmp) + 1);
1533         *starttmp = ++endtmp;
1534     }
1535     return EXT_WIFI_OK;
1536 }
1537 
wifi_scan_result_parse_pairwise(const char * starttmp,ext_wifi_ap_info * ap_list)1538 static void wifi_scan_result_parse_pairwise(const char *starttmp, ext_wifi_ap_info *ap_list)
1539 {
1540     /* 与枚举值ext_wifi_pairwise定义是逆序的,优先匹配混合型 */
1541     char *cipher_str[] = {"\"CCMP-256\"\"+TKIP\"", "\"CCMP-256\"\"+CCMP\"", "\"CCMP-256\"", "\"CCMP\"\"+TKIP\"",
1542         "\"TKIP\"", "\"CCMP\""};
1543     int idx, num;
1544 
1545     num = sizeof(cipher_str) / sizeof(char *);
1546     for (idx= 0; idx < num; idx++) {
1547         if (strstr(starttmp, cipher_str[idx]) != NULL) {
1548             break;
1549         }
1550     }
1551     if ((idx >= num) || (idx >= EXT_WIFI_PAIRWISE_BUTT)) {
1552         ap_list->pairwise = EXT_WIFI_PARIWISE_UNKNOWN;
1553     } else {
1554         ap_list->pairwise = EXT_WIFI_PAIRWISE_BUTT - idx - 1;
1555     }
1556 }
1557 
wifi_scan_result_base_flag_parse(const char * starttmp,void * buf)1558 void wifi_scan_result_base_flag_parse(const char *starttmp, void *buf)
1559 {
1560     ext_wifi_ap_info *ap_list = (ext_wifi_ap_info *)buf;
1561 
1562     if ((starttmp == NULL) || (buf == NULL))
1563         return;
1564 
1565     char *str_open      = strstr(starttmp, "OPEN");
1566     char *str_sae       = strstr(starttmp, "SAE");
1567     char *str_wpa_psk   = strstr(starttmp, "WPA-PSK");
1568     char *str_wpa2_psk  = strstr(starttmp, "WPA2-PSK");
1569     char *str_wpa       = strstr(starttmp, "WPA-EAP");
1570     char *str_wpa2      = strstr(starttmp, "WPA2-EAP");
1571 #ifdef CONFIG_IEEE80211R
1572     char *str_ft        = strstr(starttmp, "FT");
1573 #endif
1574     char *str_wep       = strstr(starttmp, "WEP");
1575     char *str_wps       = strstr(starttmp, "WPS");
1576     char *str_wmm       = strstr(starttmp, "WMM");
1577     char *str_ext_mesh  = strstr(starttmp, "EXT_MESH");
1578     char *str_rsn_psk   = strstr(starttmp, "RSN-PSK");
1579     char *str_wapi_psk  = strstr(starttmp, "WAPI-PSK");
1580     char *str_eap_sha256 = strstr(starttmp, "EAP-SHA256");
1581     char *str_eap_suiteb_192 = strstr(starttmp, "EAP-SUITE-B-192");
1582     char *str_owe = strstr(starttmp, "WPA2-OWE");
1583 
1584     if (str_sae != NULL) {
1585         if (str_wpa2_psk != NULL)
1586             ap_list->auth = EXT_WIFI_SECURITY_WPA3_WPA2_PSK_MIX;
1587         else
1588             ap_list->auth = EXT_WIFI_SECURITY_SAE;
1589     } else if ((str_wpa_psk != NULL) && ((str_wpa2_psk != NULL) || (str_rsn_psk != NULL))) {
1590         ap_list->auth = EXT_WIFI_SECURITY_WPAPSK_WPA2PSK_MIX;
1591     } else if (str_wpa_psk != NULL) {
1592         ap_list->auth = EXT_WIFI_SECURITY_WPAPSK;
1593     } else if ((str_wpa2_psk != NULL) || (str_rsn_psk != NULL)) {
1594         ap_list->auth = EXT_WIFI_SECURITY_WPA2PSK;
1595     } else if (str_wpa != NULL) {
1596         ap_list->auth = EXT_WIFI_SECURITY_WPA;
1597     } else if (str_wpa2 != NULL) {
1598         if ((str_eap_sha256 != NULL) && (str_eap_suiteb_192 != NULL))
1599             ap_list->auth = EXT_WIFI_SECURITY_WPA3_WPA2_MIX;
1600         else if (str_eap_sha256 != NULL)
1601             ap_list->auth = EXT_WIFI_SECURITY_WPA3;
1602         else if (str_eap_suiteb_192 != NULL)
1603             ap_list->auth = EXT_WIFI_SECURITY_WPA3_192BIT;
1604         else
1605             ap_list->auth = EXT_WIFI_SECURITY_WPA2;
1606     } else if (str_wep != NULL) {
1607         ap_list->auth = EXT_WIFI_SECURITY_WEP;
1608     } else if (str_open != NULL) {
1609         ap_list->auth = EXT_WIFI_SECURITY_OPEN;
1610     } else if (str_owe != NULL) {
1611         ap_list->auth = EXT_WIFI_SECURITY_OWE;
1612     } else if (str_wapi_psk != NULL) {
1613         ap_list->auth = EXT_WIFI_SECURITY_WAPI_PSK;
1614     } else {
1615         ap_list->auth = EXT_WIFI_SECURITY_UNKNOWN;
1616     }
1617     if (str_wps != NULL)
1618         ap_list->wps_flag = WPA_FLAG_ON;
1619 
1620     if (str_wmm != NULL)
1621         ap_list->wmm = WPA_FLAG_ON;
1622 
1623     if (str_ext_mesh != NULL)
1624         ap_list->ext_mesh_flag = WPA_FLAG_ON;
1625 
1626 #ifdef CONFIG_IEEE80211R
1627     if (str_ft != NULL)
1628         ap_list->ft_flag = WPA_FLAG_ON;
1629 #endif
1630     wifi_scan_result_parse_pairwise(starttmp, ap_list);
1631 }
1632 
wifi_scan_result_filter_parse(const void * buf)1633 int wifi_scan_result_filter_parse(const void *buf)
1634 {
1635     const ext_wifi_ap_info *ap_list = (const ext_wifi_ap_info *)buf;
1636     unsigned char ssid_flag;
1637     unsigned char chl_flag = 0;
1638     unsigned char prefix_ssid_flag;
1639     unsigned char bssid_flag;
1640     unsigned char i;
1641     prefix_ssid_flag = (unsigned char)(os_strncmp(g_scan_record.ssid, ap_list->ssid, strlen(g_scan_record.ssid)) == 0);
1642 
1643     for (i = 0; i < g_scan_record.chan_num; i++) {
1644         if (g_scan_record.chan_list[i] == ap_list->channel) {
1645             chl_flag = 1;
1646             break;
1647         }
1648     }
1649 
1650     ssid_flag = (unsigned char)(os_strcmp(g_scan_record.ssid, ap_list->ssid) == 0);
1651     bssid_flag = (unsigned char)(os_memcmp(g_scan_record.bssid, ap_list->bssid, sizeof(ap_list->bssid)) == 0);
1652     return (g_scan_record.flag == EXT_SCAN_UNSPECIFIED) ||
1653            (g_scan_record.flag == EXT_SCAN) ||
1654            ((g_scan_record.flag == EXT_PREFIX_SSID_SCAN) && prefix_ssid_flag) ||
1655            ((g_scan_record.flag == EXT_CHANNEL_SCAN) && chl_flag) ||
1656            ((g_scan_record.flag == EXT_BSSID_SCAN) && bssid_flag) ||
1657            ((g_scan_record.flag == EXT_SSID_SCAN) && ssid_flag);
1658 }
1659 
wifi_scan_result_ssid_parse(char ** starttmp,void * buf,size_t * reply_len)1660 int wifi_scan_result_ssid_parse(char **starttmp, void *buf, size_t *reply_len)
1661 {
1662     char *endtmp = NULL;
1663     ext_wifi_ap_info *ap_list = (ext_wifi_ap_info *)buf;
1664 
1665     if ((starttmp == NULL) || (*starttmp == NULL) || (buf == NULL) || (reply_len == NULL))
1666         return EXT_WIFI_FAIL;
1667 
1668     endtmp = strchr(*starttmp, '\n');
1669     if (endtmp == NULL)
1670         return EXT_WIFI_FAIL;
1671 
1672     *endtmp = '\0';
1673     if (strncpy_s(ap_list->ssid, (size_t)sizeof(ap_list->ssid), *starttmp, (size_t)(endtmp - *starttmp)) != (int)EOK) {
1674         ap_list->ssid[(sizeof(ap_list->ssid)) - 1] = '\0';
1675         *reply_len -= (size_t)(endtmp - (*starttmp) + 1);
1676         *starttmp = ++endtmp;
1677         return EXT_WIFI_FAIL;
1678     }
1679     ap_list->ssid[(sizeof(ap_list->ssid)) - 1] = '\0';
1680     *reply_len -= (size_t)(endtmp - (*starttmp) + 1);
1681     *starttmp = ++endtmp;
1682     return wifi_scan_result_filter_parse(ap_list) ? EXT_WIFI_OK : EXT_WIFI_FAIL;
1683 }
1684 
1685 /*************************************************************************************
1686  buf content format:
1687  bssid / frequency / signal level / flags / ssid
1688  xx:xx:xx:xx:xx:xx 2462 -21 [WPA2-PSK-CCMP][ESS] ssid-test
1689  xx:xx:xx:xx:xx:xx 2437 -35 [WPA2-PSK-CCMP][ESS] ssid
1690  xx:xx:xx:xx:xx:xx 2412 -37 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP-preauth][ESS] abc
1691  ***************************************************************************************/
wifi_scan_results_parse(ext_wifi_ap_info * ap_list,unsigned int * ap_num)1692 static int wifi_scan_results_parse(ext_wifi_ap_info *ap_list, unsigned int *ap_num)
1693 {
1694     char *starttmp = NULL;
1695     char *endtmp   = NULL;
1696     int count = 0;
1697     size_t reply_len = g_result_len;
1698     char *buf = g_scan_result_buf;
1699     int ret = EXT_WIFI_FAIL;
1700     int max_element = (int)WPA_MIN(*ap_num, WIFI_SCAN_AP_LIMIT);
1701 
1702     if (buf == NULL)
1703         goto EXIT;
1704     starttmp = strchr(buf, '\n');
1705     if (starttmp == NULL)
1706         goto EXIT;
1707 
1708     *starttmp++ = '\0';
1709     reply_len -= (size_t)(starttmp - buf);
1710     while (count < max_element && reply_len > 0) {
1711         if (wifi_scan_result_bssid_parse(&starttmp, &ap_list[count], &reply_len) == EXT_WIFI_FAIL) {
1712             ret = EXT_WIFI_OK; // set ok if it is the end of the string
1713             goto EXIT;
1714         }
1715         if ((wifi_scan_result_freq_parse(&starttmp, &ap_list[count], &reply_len) == EXT_WIFI_FAIL) ||
1716             (wifi_scan_result_rssi_parse(&starttmp, &ap_list[count], &reply_len) == EXT_WIFI_FAIL))
1717             goto EXIT;
1718         endtmp = strchr(starttmp, '\t');
1719         if (endtmp == NULL)
1720             goto EXIT;
1721 
1722         *endtmp = '\0';
1723         wifi_scan_result_base_flag_parse(starttmp, &ap_list[count]);
1724 
1725         reply_len -= (size_t)((endtmp - starttmp) + 1);
1726         starttmp = ++endtmp;
1727         if (wifi_scan_result_ssid_parse(&starttmp, &ap_list[count], &reply_len) == EXT_WIFI_OK)
1728             count++;
1729         else
1730             (void)memset_s(&ap_list[count], sizeof(*ap_list), 0, sizeof(*ap_list));
1731     }
1732     ret = EXT_WIFI_OK;
1733 EXIT:
1734     (void)memset_s(&g_scan_record, sizeof(g_scan_record), 0, sizeof(g_scan_record));
1735     *ap_num = (unsigned int)count;
1736     g_scan_result_buf = NULL;
1737     (void)os_event_write(g_wpa_event, WPA_EVENT_SCAN_RESULT_FREE_OK);
1738     return ret;
1739 }
1740 
uapi_wifi_sta_scan_results(ext_wifi_ap_info * ap_list,unsigned int * ap_num)1741 __attribute__((weak)) int uapi_wifi_sta_scan_results(ext_wifi_ap_info *ap_list, unsigned int *ap_num)
1742 {
1743     if ((ap_list == NULL) || (ap_num == NULL) || (g_mesh_sta_flag == WPA_FLAG_ON))
1744         return EXT_WIFI_FAIL;
1745 
1746     if (wifi_dev_get(EXT_WIFI_IFTYPE_STATION) == NULL)
1747         return EXT_WIFI_FAIL;
1748 
1749     if (memset_s(ap_list, sizeof(ext_wifi_ap_info) * (*ap_num), 0, sizeof(ext_wifi_ap_info) * (*ap_num)) != EOK)
1750         return EXT_WIFI_FAIL;
1751     if (wifi_scan_result(EXT_WIFI_IFTYPE_STATION) == EXT_WIFI_OK)
1752         return wifi_scan_results_parse(ap_list, ap_num);
1753     else {
1754         (void)memset_s(&g_scan_record, sizeof(g_scan_record), 0, sizeof(g_scan_record));
1755         *ap_num = 0;
1756         (void)os_event_write(g_wpa_event, WPA_EVENT_SCAN_RESULT_FREE_OK);
1757         return EXT_WIFI_FAIL;
1758     }
1759 }
1760 
wpa_cli_scan_results_clear(struct wpa_supplicant * wpa_s)1761 int wpa_cli_scan_results_clear(struct wpa_supplicant *wpa_s)
1762 {
1763     char *cmd[] = {"BSS_FLUSH "};
1764 
1765     return wpa_cli_cmd(wpa_s, 1, cmd);
1766 }
1767 
wifi_scan_result_clear(ext_wifi_iftype iftype)1768 int wifi_scan_result_clear(ext_wifi_iftype iftype)
1769 {
1770     struct ext_wifi_dev *wifi_dev = wifi_dev_get(iftype);
1771     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL) ||
1772         (wifi_dev->iftype == EXT_WIFI_IFTYPE_AP) ||
1773         (wifi_dev->iftype >= EXT_WIFI_IFTYPE_P2P_CLIENT)) {
1774         wpa_error_log0(MSG_ERROR, "wifi_scan_result_clear: get wifi dev failed\n");
1775         return EXT_WIFI_FAIL;
1776     }
1777 
1778     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_SCAN_RESULT_CLEAR_OK);
1779     if (wpa_cli_scan_results_clear((struct wpa_supplicant *)(wifi_dev->priv)) != EXT_WIFI_OK) {
1780         wpa_error_log0(MSG_ERROR, "wifi_scan_result_clear: wpa_cli_scan_results failed.");
1781         return EXT_WIFI_FAIL;
1782     }
1783 
1784     wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_SCAN_RESULT_CLEAR_OK");
1785     unsigned int ret_val;
1786     (void)os_event_read(g_wpa_event, WPA_EVENT_SCAN_RESULT_CLEAR_OK, &ret_val,
1787                         WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_EVENT_DELAY_5S);
1788 
1789     return EXT_WIFI_OK;
1790 }
1791 
uapi_wifi_sta_scan_results_clear(void)1792 __attribute__((weak)) int uapi_wifi_sta_scan_results_clear(void)
1793 {
1794     if (g_mesh_sta_flag == WPA_FLAG_ON)
1795         return EXT_WIFI_FAIL;
1796 
1797     if (wifi_dev_get(EXT_WIFI_IFTYPE_STATION) == NULL)
1798         return EXT_WIFI_FAIL;
1799 
1800     return wifi_scan_result_clear(EXT_WIFI_IFTYPE_STATION);
1801 }
1802 
1803 #ifdef CONFIG_WNM
uapi_wifi_wnm_bss_query(int reason_code,int candidate_list)1804 int uapi_wifi_wnm_bss_query(int reason_code, int candidate_list)
1805 {
1806     struct ext_wifi_dev *wifi_dev = wifi_dev_get(EXT_WIFI_IFTYPE_STATION);
1807     char buf[WPA_CMD_BUF_SIZE] = { 0 };
1808     int ret = 0;
1809     int len = 0;
1810 
1811     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL) ||
1812         (wifi_dev->iftype == EXT_WIFI_IFTYPE_AP) ||
1813         (wifi_dev->iftype >= EXT_WIFI_IFTYPE_P2P_CLIENT)) {
1814         wpa_error_log0(MSG_ERROR, "uapi_wifi_wnm_bss_query: get wifi dev failed\n");
1815         return EXT_WIFI_FAIL;
1816     }
1817 
1818     if (candidate_list != 0) {
1819         ret = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "%d list", reason_code);
1820         if (ret < 0) {
1821             wpa_error_log0(MSG_ERROR, "uapi_wifi_wnm_bss_query:: snprintf_s faild");
1822             return EXT_WIFI_FAIL;
1823         }
1824     } else {
1825         ret = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "%d", reason_code);
1826         if (ret < 0) {
1827             wpa_error_log0(MSG_ERROR, "uapi_wifi_wnm_bss_query:: snprintf_s faild");
1828             return EXT_WIFI_FAIL;
1829         }
1830     }
1831     len = ret;
1832 
1833     printf("uapi_wifi_wnm_bss_query::send_bss_query[%s], len[%d], buf_len[%d]\r\n", buf, len, strlen(buf));
1834     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_STA_WNM_BSS_QUERY);
1835     if (wpa_cli_wnm_bss_query((struct wpa_supplicant *)(wifi_dev->priv), buf, len) != EXT_WIFI_OK) {
1836         wpa_error_log0(MSG_ERROR, "uapi_wifi_wnm_bss_query: wpa_cli_cmd_wnm_bss_query failed.");
1837         return EXT_WIFI_FAIL;
1838     }
1839 
1840     wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_STA_WNM_BSS_QUERY");
1841     unsigned int ret_val;
1842     (void)os_event_read(g_wpa_event, WPA_EVENT_STA_WNM_BSS_QUERY, &ret_val,
1843                         WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_EVENT_DELAY_5S);
1844 
1845     return EXT_WIFI_OK;
1846 }
1847 #endif
1848 
uapi_wifi_csi_check_param(const ext_csi_config * config)1849 int uapi_wifi_csi_check_param(const ext_csi_config *config)
1850 {
1851     /* 0 RA 1 TA */
1852     if (config->cfg_match_ta_ra_sel != 0 && config->cfg_match_ta_ra_sel != 1) {
1853         wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_check_param::get addr_filter_type err!}");
1854         return EXT_WIFI_FAIL;
1855     }
1856 
1857     if (addr_precheck(config->mac_addr) != EXT_WIFI_OK) {
1858         wpa_error_log1(MSG_ERROR, "{uapi_wifi_csi_check_param::get mac err, maclen:%d!}",
1859             strlen(config->mac_addr));
1860         return EXT_WIFI_FAIL;
1861     }
1862 
1863     /* user frame type support, range:0~7, bit0 control, bit1 manament, bit2 data */
1864     if (config->frame_type_filter_bitmap > 7) {
1865         wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_check_param::get frame_type err!}");
1866         return EXT_WIFI_FAIL;
1867     }
1868 
1869     /* sub_type_filter switch, 0 close, 1 open */
1870     if (config->sub_type_filter_enable != 0 && config->sub_type_filter_enable != 1) {
1871         wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_check_param::get frame sub type enable err!}");
1872         return EXT_WIFI_FAIL;
1873     }
1874 
1875     /* range 0 ~ 15 */
1876     if (config->sub_type_filter > 15) {
1877         wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_check_param::get frame sub type filter err!}");
1878         return EXT_WIFI_FAIL;
1879     }
1880 
1881     /* range 0 ~ 63 */
1882     if (config->ppdu_filter_bitmap > 63) {
1883         wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_check_param::get frame ppdu_filter_bitmap err!}");
1884         return EXT_WIFI_FAIL;
1885     }
1886 
1887     /* range 0 ~ 4095 */
1888     if (config->period > 4095) {
1889         wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_check_param::get period err!}");
1890         return EXT_WIFI_FAIL;
1891     }
1892 
1893     return EXT_WIFI_OK;
1894 }
1895 
uapi_wifi_csi_set_config(const char * ifname,const ext_csi_config * config)1896 int uapi_wifi_csi_set_config(const char *ifname, const ext_csi_config *config)
1897 {
1898     int ret;
1899     /* range 0 ~ 3 */
1900     if (config->user_idx > 3) {
1901         wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_set_config::get user_idx err!}");
1902         return EXT_WIFI_FAIL;
1903     }
1904 
1905     if (config->enable != 0 && config->enable != 1) {
1906         wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_set_config::get enable err!}");
1907         return EXT_WIFI_FAIL;
1908     }
1909 
1910     /* only when enable is 1, check other param */
1911     if (config->enable == 1) {
1912         ret = uapi_wifi_csi_check_param(config);
1913         if (ret != EXT_WIFI_OK) {
1914             wpa_error_log0(MSG_ERROR, "{uapi_wifi_csi_set_config::csi params check failed!}");
1915             return EXT_WIFI_FAIL;
1916         }
1917     }
1918 
1919     if (memcpy_s(g_csi_ifname, WIFI_IFNAME_MAX_SIZE, ifname, WIFI_IFNAME_MAX_SIZE) != EOK) {
1920         (void)memset_s(g_csi_ifname, WIFI_IFNAME_MAX_SIZE, 0, WIFI_IFNAME_MAX_SIZE);
1921         return EXT_WIFI_FAIL;
1922     }
1923 
1924     if (drv_soc_set_csi_config(ifname, config) != EXT_WIFI_OK) {
1925         wpa_error_log0(MSG_ERROR, "wifi_set_usr_app_ie_internal: set csi config failed.");
1926         return EXT_WIFI_FAIL;
1927     }
1928     return EXT_WIFI_OK;
1929 }
1930 
uapi_csi_register_data_report_cb(wifi_csi_data_cb data_cb)1931 void uapi_csi_register_data_report_cb(wifi_csi_data_cb data_cb)
1932 {
1933     wal_csi_register_data_report_cb(data_cb);
1934 }
uapi_wifi_csi_start(void)1935 int uapi_wifi_csi_start(void)
1936 {
1937     int ret;
1938     char *ifname = NULL;
1939     int switch_flag = WIFI_CSI_SWITCH_ON;
1940 
1941     ifname = g_csi_ifname;
1942     ret = drv_soc_csi_switch(ifname, &switch_flag);
1943     if (ret != EXT_WIFI_OK) {
1944         wpa_error_log0(MSG_ERROR, "uapi_wifi_csi_start: start csi failed.");
1945     }
1946     return ret;
1947 }
1948 
uapi_wifi_csi_stop(void)1949 int uapi_wifi_csi_stop(void)
1950 {
1951     int ret;
1952     char *ifname = NULL;
1953     int switch_flag = WIFI_CSI_SWITCH_OFF;
1954 
1955     ifname = g_csi_ifname;
1956     ret = drv_soc_csi_switch(ifname, &switch_flag);
1957     if (ret != EXT_WIFI_OK) {
1958         wpa_error_log0(MSG_ERROR, "uapi_wifi_csi_start: stop csi failed.");
1959     }
1960     (void)memset_s(g_csi_ifname, WIFI_IFNAME_MAX_SIZE, 0, WIFI_IFNAME_MAX_SIZE);
1961     return ret;
1962 }
1963 
uapi_wifi_psk_calc(ext_wifi_sta_psk_config psk_config,unsigned char * get_psk,unsigned int psk_len)1964 int uapi_wifi_psk_calc(ext_wifi_sta_psk_config psk_config, unsigned char *get_psk, unsigned int psk_len)
1965 {
1966     unsigned char psk_calc[EXT_WIFI_STA_PSK_LEN] = { 0 };
1967     struct ext_wifi_dev *wifi_dev = NULL;
1968 
1969     if ((get_psk == NULL) || (psk_len != EXT_WIFI_STA_PSK_LEN))
1970         return EXT_WIFI_FAIL;
1971 
1972     if ((strnlen((char *)psk_config.ssid, EXT_WIFI_MAX_SSID_LEN + 1) > EXT_WIFI_MAX_SSID_LEN) ||
1973         (strlen((char *)psk_config.ssid) < 1)) {
1974         wpa_error_log0(MSG_ERROR, "invalid ssid length.");
1975         return EXT_WIFI_FAIL;
1976     }
1977     if ((strnlen(psk_config.key, EXT_WIFI_AP_KEY_LEN + 1) > EXT_WIFI_AP_KEY_LEN) ||
1978         (strlen(psk_config.key) < 1)) {
1979         wpa_error_log0(MSG_ERROR, "invalid key length.");
1980         return EXT_WIFI_FAIL;
1981     }
1982 
1983     wifi_dev = wifi_dev_get(EXT_WIFI_IFTYPE_STATION);
1984     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL)) {
1985         wpa_error_log0(MSG_ERROR, "uapi_wifi_psk_calc: get wifi dev failed\n");
1986         return EXT_WIFI_FAIL;
1987     }
1988     if (memset_s(get_psk, psk_len, 0, psk_len) != EOK) {
1989         wpa_error_log0(MSG_ERROR, "uapi_wifi_psk_calc: memset_s failed\n");
1990         return EXT_WIFI_FAIL;
1991     }
1992     if (pbkdf2_sha1(psk_config.key, psk_config.ssid, strlen((char *)psk_config.ssid),
1993         WPA_STA_ITERA, psk_calc, WPA_STA_PMK_LEN) != EXT_WIFI_OK) {
1994         return EXT_WIFI_FAIL;
1995     }
1996     if (memcpy_s(get_psk, psk_len, psk_calc, EXT_WIFI_STA_PSK_LEN) != EOK) {
1997         (void)memset_s(psk_calc, sizeof(psk_calc), 0, sizeof(psk_calc));
1998         (void)memset_s(get_psk, psk_len, 0, psk_len);
1999         return EXT_WIFI_FAIL;
2000     }
2001     (void)memset_s(psk_calc, sizeof(psk_calc), 0, sizeof(psk_calc));
2002     return EXT_WIFI_OK;
2003 }
2004 
uapi_wifi_psk_calc_and_store(ext_wifi_sta_psk_config psk_config)2005 int uapi_wifi_psk_calc_and_store(ext_wifi_sta_psk_config psk_config)
2006 {
2007     if (uapi_wifi_psk_calc(psk_config, g_quick_conn_psk, EXT_WIFI_STA_PSK_LEN) != EXT_WIFI_OK)
2008         return EXT_WIFI_FAIL;
2009 
2010     return EXT_WIFI_OK;
2011 }
2012 
los_wifi_connect_prepare(const struct wpa_assoc_request * assoc,struct wpa_supplicant * wpa_s,const char * network_id_txt,bool is_fast_connnect)2013 static int los_wifi_connect_prepare(const struct wpa_assoc_request *assoc,
2014                                     struct wpa_supplicant *wpa_s,
2015                                     const char *network_id_txt,
2016                                     bool is_fast_connnect)
2017 {
2018     char addr_txt[EXT_WIFI_TXT_ADDR_LEN + 1] = { 0 };
2019     char fre_buff[WPA_CMD_MIN_SIZE] = { 0 };
2020     int ret;
2021 
2022     if ((wpa_cli_disconnect(wpa_s) != EXT_WIFI_OK) ||
2023         (wpa_cli_remove_network(wpa_s, network_id_txt, WPA_NETWORK_ID_TXT_LEN) != EXT_WIFI_OK) ||
2024         (wpa_cli_add_network(wpa_s) != EXT_WIFI_OK)) {
2025         wpa_error_log0(MSG_ERROR, "los_wifi_connect_prepare re-add network faild.\n");
2026         return EXT_WIFI_FAIL;
2027     }
2028 
2029     if (assoc->scan_type & (unsigned char)SOC_WPA_BIT_SCAN_BSSID) { // set bssid
2030         if (snprintf_s(addr_txt, sizeof(addr_txt), sizeof(addr_txt) - 1, MACSTR, MAC2STR(assoc->bssid)) < 0) {
2031             wpa_error_log0(MSG_ERROR, "los_wifi_connect_prepare addr_txt snprintf_s faild.\n");
2032             return EXT_WIFI_FAIL;
2033         }
2034 
2035         if (wpa_cli_set_network(wpa_s, network_id_txt, WPA_NETWORK_ID_TXT_LEN, "bssid", addr_txt,
2036             sizeof(addr_txt)) != EXT_WIFI_OK) {
2037             wpa_error_log0(MSG_ERROR, "wpa_cli_set_network bssid faild.\n");
2038             return EXT_WIFI_FAIL;
2039         }
2040     }
2041 
2042     if (assoc->scan_type & (unsigned char)SOC_WPA_BIT_SCAN_SSID) { // set ssid
2043         if (strlen(assoc->ssid) > WPA_MAX_SSID_LEN) {
2044             wpa_error_log0(MSG_ERROR, "los_wifi_connect_prepare: ssid len error.\n");
2045             return EXT_WIFI_FAIL;
2046         }
2047         if (wpa_cli_set_network(wpa_s, network_id_txt, WPA_NETWORK_ID_TXT_LEN, "ssid", assoc->ssid,
2048             sizeof(assoc->ssid)) != EXT_WIFI_OK) {
2049             wpa_error_log0(MSG_ERROR, "wpa_cli_set_network ssid faild.\n");
2050             return EXT_WIFI_FAIL;
2051         }
2052     }
2053 
2054     if (is_fast_connnect) { // set frequence
2055         if (snprintf_s(fre_buff, sizeof(fre_buff), sizeof(fre_buff) - 1, "%d",
2056             chan_to_freq(assoc->channel)) < 0) {
2057             wpa_error_log0(MSG_ERROR, "los_wifi_connect_prepare: fre_buff snprintf_s faild");
2058             return EXT_WIFI_FAIL;
2059         }
2060         if (wpa_cli_set_network(wpa_s, network_id_txt, WPA_NETWORK_ID_TXT_LEN, "frequency", fre_buff,
2061             sizeof(fre_buff)) != EXT_WIFI_OK) {
2062             wpa_error_log0(MSG_ERROR, "wpa_cli_set_network frequency faild.\n");
2063             return EXT_WIFI_FAIL;
2064         }
2065     }
2066     // set sta mode
2067     if (wpa_cli_set_network(wpa_s, network_id_txt, WPA_NETWORK_ID_TXT_LEN, "mode", "0", strlen("0")) != EXT_WIFI_OK) {
2068         wpa_error_log0(MSG_ERROR, "wpa_cli_set_network mode faild.\n");
2069         return EXT_WIFI_FAIL;
2070     }
2071 
2072     // set security type
2073 #ifdef LOS_CONFIG_WPA_ENTERPRISE
2074     if (wifi_sta_enterprise_check(assoc->auth) == EXT_WIFI_OK)
2075         ret = wifi_sta_set_network_ent(wpa_s, network_id_txt, assoc);
2076     else
2077 #endif /* LOS_CONFIG_WPA_ENTERPRISE */
2078         ret = wifi_sta_set_network_psk(wpa_s, network_id_txt, assoc, is_fast_connnect);
2079 
2080     if (ret != EXT_WIFI_OK) {
2081         (void)wpa_cli_remove_network(wpa_s, network_id_txt, WPA_NETWORK_ID_TXT_LEN);
2082         return EXT_WIFI_FAIL;
2083     }
2084 
2085     return EXT_WIFI_OK;
2086 }
2087 
los_wifi_connect(struct wpa_assoc_request * assoc,bool is_fast_connnect)2088 static int los_wifi_connect(struct wpa_assoc_request *assoc, bool is_fast_connnect)
2089 {
2090     struct ext_wifi_dev *wifi_dev = NULL;
2091     char network_id_txt[WPA_NETWORK_ID_TXT_LEN + 1] = { 0 };
2092     int ret;
2093     unsigned int ret_val;
2094 
2095     wpa_error_log0(MSG_ERROR, " ---> ### los_wifi_connect enter. \n\n\r");
2096 
2097     wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_STATION);
2098     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL)) {
2099         wpa_error_log0(MSG_ERROR, "los_wifi_connect: get wifi dev failed\n");
2100         return EXT_WIFI_FAIL;
2101     }
2102     if (assoc->scan_type == (unsigned char)SOC_WPA_BIT_SCAN_UNKNOW) {
2103         wpa_error_log0(MSG_ERROR, "los_wifi_connect: please set ssid or bssid\n");
2104         return EXT_WIFI_FAIL;
2105     }
2106     ret = snprintf_s(network_id_txt, sizeof(network_id_txt), sizeof(network_id_txt) - 1, "%d", wifi_dev->network_id);
2107     if (ret < 0) {
2108         wpa_error_log0(MSG_ERROR, "los_wifi_connect: network_id_txt snprintf_s failed");
2109         return EXT_WIFI_FAIL;
2110     }
2111     if (g_assoc_auth != EXT_WIFI_SEC_TYPE_INVALID) {
2112         assoc->auth = g_assoc_auth;
2113     }
2114     if (los_wifi_connect_prepare(assoc, (struct wpa_supplicant *)(wifi_dev->priv), network_id_txt, is_fast_connnect)
2115         < 0)
2116         return EXT_WIFI_FAIL;
2117 
2118     g_fast_connect_flag = is_fast_connnect ? WPA_FLAG_ON : WPA_FLAG_OFF;
2119     g_connecting_flag = WPA_FLAG_ON;
2120     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_QUICK_CONNECT_FLAG);
2121 
2122     if (wpa_cli_select_network((struct wpa_supplicant *)(wifi_dev->priv),
2123                                network_id_txt, WPA_NETWORK_ID_TXT_LEN) != EXT_WIFI_OK) {
2124         wpa_error_log0(MSG_ERROR, "los_wifi_connect: wpa_cli_select_network failed");
2125         return EXT_WIFI_FAIL;
2126     }
2127     if (is_fast_connnect) {
2128         wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_QUICK_CONNECT_FLAG");
2129         (void)os_event_read(g_wpa_event, WPA_EVENT_QUICK_CONNECT_FLAG, &ret_val,
2130                             WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_EVENT_DELAY_5S);
2131         g_fast_connect_flag = WPA_FLAG_OFF;
2132         g_connecting_flag = WPA_FLAG_OFF;
2133         if (ret_val != WPA_EVENT_QUICK_CONNECT_OK) {
2134             wpa_error_log1(MSG_ERROR, "os_event_read WPA_EVENT_QUICK_CONNECT_FLAG failed ret_val = %x", ret_val);
2135             return EXT_WIFI_FAIL;
2136         }
2137     }
2138 
2139     return EXT_WIFI_OK;
2140 }
2141 
wifi_sta_connect_param_check(const ext_wifi_assoc_request * req)2142 static int wifi_sta_connect_param_check(const ext_wifi_assoc_request *req)
2143 {
2144     if (req == NULL) {
2145         wpa_error_log0(MSG_ERROR, "input param is NULL.");
2146         return EXT_WIFI_FAIL;
2147     }
2148     if ((strnlen(req->ssid, EXT_WIFI_MAX_SSID_LEN + 1) > EXT_WIFI_MAX_SSID_LEN) ||
2149         (strnlen(req->key, EXT_WIFI_MAX_KEY_LEN + 1) > EXT_WIFI_MAX_KEY_LEN)) {
2150         wpa_error_log0(MSG_ERROR, "invalid ssid/key.");
2151         return EXT_WIFI_FAIL;
2152     }
2153     if ((req->auth >= EXT_WIFI_SECURITY_UNKNOWN)) {
2154         wpa_error_log0(MSG_ERROR, "invalid auth.");
2155         return EXT_WIFI_FAIL;
2156     }
2157     if ((wifi_is_need_psk(req->auth) == 1) && (os_strlen(req->key) > WPA_MAX_KEY_LEN)) {
2158         wpa_error_log0(MSG_ERROR, "invalid key length.");
2159         return EXT_WIFI_FAIL;
2160     }
2161     if (is_broadcast_ether_addr(req->bssid)) {
2162         wpa_error_log0(MSG_ERROR, "invalid bssid.");
2163         return EXT_WIFI_FAIL;
2164     }
2165     if (req->pairwise >= EXT_WIFI_PAIRWISE_BUTT) {
2166         wpa_error_log0(MSG_ERROR, "invalid pairwise.");
2167         return EXT_WIFI_FAIL;
2168     }
2169     if (((req->auth == EXT_WIFI_SECURITY_SAE) ||
2170         (req->auth == EXT_WIFI_SECURITY_OWE) ||
2171         (req->auth == EXT_WIFI_SECURITY_WPA3_WPA2_PSK_MIX)) &&
2172         (req->pairwise == EXT_WIFI_PAIRWISE_TKIP)) {
2173         wpa_error_log0(MSG_ERROR, "pairwise and auth do not match.");
2174         return EXT_WIFI_FAIL;
2175     }
2176     return EXT_WIFI_OK;
2177 }
2178 
wifi_key_has_ctrl_char(const char * data,size_t len)2179 static int wifi_key_has_ctrl_char(const char *data, size_t len)
2180 {
2181     for (size_t i = 0; i < len; i++) {
2182         if (data[i] < 32 || data[i] == 127) { /* less than 32 or equal 127 is ctrl char */
2183             return EXT_WIFI_FAIL;
2184         }
2185     }
2186     return EXT_WIFI_OK;
2187 }
2188 
wifi_key_check(const ext_wifi_assoc_request * req,const ext_wifi_fast_assoc_request * fast_request,struct wpa_assoc_request * assoc,bool is_fast_connnect)2189 static int wifi_key_check(const ext_wifi_assoc_request *req,
2190                           const ext_wifi_fast_assoc_request *fast_request,
2191                           struct wpa_assoc_request *assoc,
2192                           bool is_fast_connnect)
2193 {
2194     size_t keysize = strnlen(req->key, EXT_WIFI_MAX_KEY_LEN + 1);
2195 
2196     if (req->auth == EXT_WIFI_SECURITY_WAPI_PSK) {
2197         if ((keysize > WPA_MAX_KEY_LEN) || (keysize < WPA_MIN_KEY_LEN)) {
2198             wpa_error_log0(MSG_ERROR, "Invalid wapi size");
2199             return EXT_WIFI_FAIL;
2200         }
2201         if (req->hex_flag) {
2202             if ((keysize & 0x01) || (is_hex_string(req->key, keysize) == EXT_WIFI_FAIL)) {
2203                 wpa_error_log0(MSG_ERROR, "Invalid wapi hex character");
2204                 return EXT_WIFI_FAIL;
2205             }
2206         }
2207     } else {
2208         /* When key size is 64, we think this is psk set in WPA. Psk is hexadecimal. */
2209         if ((keysize == WPA_MAX_KEY_LEN) && (req->auth != EXT_WIFI_SECURITY_WPA3_WPA2_PSK_MIX) &&
2210             (req->auth != EXT_WIFI_SECURITY_SAE) && (is_hex_string(req->key, WPA_MAX_KEY_LEN) == EXT_WIFI_FAIL)) {
2211             wpa_error_log0(MSG_ERROR, "Invalid hex character");
2212             return EXT_WIFI_FAIL;
2213         }
2214     }
2215 
2216     /*
2217      * when the connection is sta_connect,the len of key should not be zero.
2218      * when the connection is sta_fast_connect and the psk is not used, the len of key should not be zero
2219      */
2220     if ((is_fast_connnect == false) || (fast_request->psk_flag == EXT_WIFI_WPA_PSK_NOT_USE)) {
2221         if (keysize == 0) {
2222             wpa_error_log0(MSG_ERROR, "key length is zero.");
2223             return EXT_WIFI_FAIL;
2224         }
2225         if ((req->auth == EXT_WIFI_SECURITY_WPAPSK) || (req->auth == EXT_WIFI_SECURITY_WPA2PSK) ||
2226             (req->auth == EXT_WIFI_SECURITY_WPAPSK_WPA2PSK_MIX)) {
2227             if (wifi_key_has_ctrl_char(req->key, keysize) != EXT_WIFI_OK) {
2228                 wpa_error_log0(MSG_ERROR, "Invalid character");
2229                 return EXT_WIFI_FAIL;
2230             }
2231         }
2232         if (memcpy_s(assoc->key, sizeof(assoc->key), req->key, keysize + 1) != EOK)
2233             return EXT_WIFI_FAIL;
2234     }
2235     return EXT_WIFI_OK;
2236 }
2237 
wifi_sta_connect(const ext_wifi_assoc_request * req,const ext_wifi_fast_assoc_request * fast_request,bool is_fast_connnect)2238 static int wifi_sta_connect(const ext_wifi_assoc_request *req,
2239                             const ext_wifi_fast_assoc_request *fast_request,
2240                             bool is_fast_connnect)
2241 {
2242     int ret;
2243     struct wpa_assoc_request assoc;
2244 
2245     ret = wifi_sta_connect_param_check(req);
2246     if (ret != EXT_WIFI_OK) {
2247         wpa_error_log0(MSG_ERROR, "los_los_wifi_connect_precheck failed.");
2248         return EXT_WIFI_FAIL;
2249     }
2250     assoc.scan_type = SOC_WPA_BIT_SCAN_UNKNOW;
2251     if ((strlen(req->ssid) > 0) && (strlen(req->ssid) <= EXT_WIFI_MAX_SSID_LEN)) {
2252         assoc.scan_type |= (unsigned char)SOC_WPA_BIT_SCAN_SSID;
2253         if (memcpy_s(assoc.ssid, sizeof(assoc.ssid), req->ssid, strlen(req->ssid) + 1) != EOK)
2254             return EXT_WIFI_FAIL;
2255     }
2256     if (!is_zero_ether_addr(req->bssid))  {
2257         assoc.scan_type |= (unsigned char)SOC_WPA_BIT_SCAN_BSSID;
2258         if (memcpy_s(assoc.bssid, sizeof(assoc.bssid), req->bssid, sizeof(req->bssid)) != EOK)
2259             return EXT_WIFI_FAIL;
2260     } else {
2261         if (memset_s(assoc.bssid, sizeof(assoc.bssid), 0, sizeof(req->bssid)) != EOK)
2262             return EXT_WIFI_FAIL;
2263     }
2264     if ((wifi_is_need_psk(req->auth) == 1) && (wifi_sta_enterprise_check(req->auth) != EXT_WIFI_OK)) {
2265         if (wifi_key_check(req, fast_request, &assoc, is_fast_connnect) != EXT_WIFI_OK) {
2266             wpa_error_log0(MSG_ERROR, "key check failed");
2267             return EXT_WIFI_FAIL;
2268         }
2269     } else {
2270         if (strlen(req->key) != 0) {
2271             wpa_error_log0(MSG_ERROR, "invalid key.length.");
2272             return EXT_WIFI_FAIL;
2273         }
2274     }
2275     assoc.auth = req->auth;
2276     assoc.wpa_pairwise = req->pairwise;
2277     assoc.hex_flag = req->hex_flag;
2278     assoc.ft_flag = req->ft_flag;
2279     if (is_fast_connnect) {
2280         assoc.channel = fast_request->channel;
2281         assoc.psk_flag = fast_request->psk_flag;
2282     }
2283 
2284     ret = los_wifi_connect(&assoc, is_fast_connnect);
2285     (void)memset_s(&assoc, sizeof(assoc), 0, sizeof(assoc));
2286     if (ret != EXT_WIFI_OK) {
2287         wpa_error_log1(MSG_ERROR, "%d:los_wifi_connect failed.", (int)is_fast_connnect);
2288         return EXT_WIFI_FAIL;
2289     }
2290     return EXT_WIFI_OK;
2291 }
2292 
uapi_wifi_sta_connect(const ext_wifi_assoc_request * req)2293 int uapi_wifi_sta_connect(const ext_wifi_assoc_request *req)
2294 {
2295     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
2296         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
2297         return EXT_WIFI_FAIL;
2298     }
2299     return wifi_sta_connect(req, NULL, false);
2300 }
2301 
wifi_channel_check(unsigned char chan)2302 int wifi_channel_check(unsigned char chan)
2303 {
2304     char cc[WPA_COUNTRY_CODE_LEN] = "";
2305     unsigned char len = WPA_COUNTRY_CODE_LEN;
2306     unsigned char max;
2307     int ret = EXT_WIFI_OK;
2308 
2309 #ifdef CONFIG_ACS
2310     if (chan > 14) /* 14: chan is allowed to be 0 to 14. */
2311         return EXT_WIFI_FAIL;
2312 #else
2313     if (chan < 1)
2314         return EXT_WIFI_FAIL;
2315 #endif
2316 
2317     if (uapi_wifi_get_country(cc, &len) == EXT_WIFI_FAIL) {
2318         wpa_error_log0(MSG_ERROR, "fail to get country.");
2319         return EXT_WIFI_FAIL;
2320     }
2321     if (!strncmp(cc, WPA_COUNTRY_CODE_USA, WPA_COUNTRY_CODE_LEN - 1))
2322         max = WPA_CHANNEL_MAX_USA;
2323     else if (!strncmp(cc, WPA_COUNTRY_CODE_ZZ, WPA_COUNTRY_CODE_LEN - 1) ||
2324         !strncmp(cc, WPA_COUNTRY_CODE_JAPAN, WPA_COUNTRY_CODE_LEN - 1))
2325         max = WPA_CHANNEL_MAX_JAPAN;
2326     else
2327         max = WPA_CHANNEL_MAX_OTHERS;
2328 
2329     if (chan > max)
2330         ret = EXT_WIFI_FAIL;
2331     return ret;
2332 }
2333 
uapi_wifi_sta_fast_connect(const ext_wifi_fast_assoc_request * fast_request)2334 int uapi_wifi_sta_fast_connect(const ext_wifi_fast_assoc_request *fast_request)
2335 {
2336     int ret;
2337     if (fast_request == NULL) {
2338         wpa_error_log0(MSG_ERROR, "input param is NULL.");
2339         return EXT_WIFI_FAIL;
2340     }
2341     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
2342         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
2343         return EXT_WIFI_FAIL;
2344     }
2345     if (wifi_channel_check(fast_request->channel) == EXT_WIFI_FAIL) {
2346         wpa_error_log0(MSG_ERROR, "invalid channel.");
2347         return EXT_WIFI_FAIL;
2348     }
2349 
2350     if (fast_request->psk_flag == EXT_WIFI_WPA_PSK_USE_OUTER) {
2351         if (fast_request->psk[0] == '\0') {
2352             wpa_error_log0(MSG_ERROR, "invalid psk.");
2353             return EXT_WIFI_FAIL;
2354         }
2355         if (memcpy_s(g_quick_conn_psk, sizeof(g_quick_conn_psk), fast_request->psk, sizeof(fast_request->psk)) != EOK) {
2356             wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_fast_connect memcpy_s failed.");
2357             return EXT_WIFI_FAIL;
2358         }
2359     }
2360     if ((fast_request->psk_flag != EXT_WIFI_WPA_PSK_NOT_USE) &&
2361         (fast_request->req.auth != EXT_WIFI_SECURITY_WPAPSK) &&
2362         (fast_request->req.auth != EXT_WIFI_SECURITY_WPA2PSK) &&
2363         (fast_request->req.auth != EXT_WIFI_SECURITY_WPAPSK_WPA2PSK_MIX)) {
2364         wpa_error_log0(MSG_ERROR, "this auth not support set psk");
2365         return EXT_WIFI_FAIL;
2366     }
2367     g_quick_conn_psk_flag = (fast_request->psk_flag == EXT_WIFI_WPA_PSK_NOT_USE) ? WPA_FLAG_OFF : WPA_FLAG_ON;
2368     ret = wifi_sta_connect(&fast_request->req, fast_request, true);
2369     if ((fast_request->psk_flag != EXT_WIFI_WPA_PSK_NOT_USE) &&
2370         (memset_s(g_quick_conn_psk, sizeof(g_quick_conn_psk), 0, sizeof(g_quick_conn_psk)) != EOK)) {
2371         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_fast_connect memset_s failed.");
2372     }
2373     g_quick_conn_psk_flag = WPA_FLAG_OFF;
2374     return ret;
2375 }
2376 
uapi_wifi_sta_disconnect(void)2377 int uapi_wifi_sta_disconnect(void)
2378 {
2379     int ret;
2380     char network_id_txt[WPA_NETWORK_ID_TXT_LEN + 1] = { 0 };
2381 
2382     wpa_error_log0(MSG_ERROR, " ---> ### uapi_wifi_sta_disconnect enter. \n\n\r");
2383 
2384     struct ext_wifi_dev *wifi_dev = wifi_dev_get(EXT_WIFI_IFTYPE_STATION);
2385     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL)) {
2386         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_disconnect: get wifi dev failed\n");
2387         return EXT_WIFI_FAIL;
2388     }
2389 
2390     ret = wpa_cli_disconnect((struct wpa_supplicant *)(wifi_dev->priv));
2391     if (ret != EXT_WIFI_OK) {
2392         wpa_error_log0(MSG_ERROR, "wpa_cli_disconnect fail.");
2393         return EXT_WIFI_FAIL;
2394     }
2395 
2396     ret = snprintf_s(network_id_txt, sizeof(network_id_txt), sizeof(network_id_txt) - 1, "%d", wifi_dev->network_id);
2397     if (ret < 0) {
2398         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_disconnect snprintf_s faild");
2399         return EXT_WIFI_FAIL;
2400     }
2401     ret = wpa_cli_remove_network((struct wpa_supplicant *)(wifi_dev->priv), network_id_txt, WPA_NETWORK_ID_TXT_LEN);
2402     if (ret != EXT_WIFI_OK) {
2403         wpa_error_log0(MSG_ERROR, "wpa_cli_remove_network fail.");
2404         return EXT_WIFI_FAIL;
2405     }
2406     g_connecting_flag = WPA_FLAG_OFF;
2407     g_assoc_auth = EXT_WIFI_SEC_TYPE_INVALID;
2408     return EXT_WIFI_OK;
2409 }
2410 
uapi_wifi_sta_get_connect_info(ext_wifi_status * connect_status)2411 int uapi_wifi_sta_get_connect_info(ext_wifi_status *connect_status)
2412 {
2413     struct ext_wifi_dev *wifi_dev = NULL;
2414     struct wpa_supplicant *wpa_s   = NULL;
2415     ext_wifi_status_sem wifi_status_sem = { 0 };
2416     errno_t rc = EXT_WIFI_OK;
2417     unsigned int ret_val;
2418 
2419     if (connect_status == NULL) {
2420         wpa_error_log0(MSG_ERROR, "input param is NULL.");
2421         return EXT_WIFI_FAIL;
2422     }
2423     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
2424         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
2425         return EXT_WIFI_FAIL;
2426     }
2427     rc = memset_s(connect_status, sizeof(ext_wifi_status), 0, sizeof(ext_wifi_status));
2428     if (rc != EOK)
2429         return EXT_WIFI_FAIL;
2430 
2431     if (memset_s(&wifi_status_sem, sizeof(ext_wifi_status_sem), 0, sizeof(ext_wifi_status_sem)) != EOK) {
2432         return EXT_WIFI_FAIL;
2433     }
2434 
2435     wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_STATION);
2436     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL))
2437         return EXT_WIFI_FAIL;
2438 
2439     wpa_s = (struct wpa_supplicant *)(wifi_dev->priv);
2440     wifi_status_sem.conn_status = connect_status;
2441 
2442     /* 初始化一个二值信号量 */
2443     if (osal_sem_binary_sem_init(&wifi_status_sem.sta_status_sem, 0) != OSAL_SUCCESS) {
2444         wpa_error_log0(MSG_ERROR, "binary sem init fail.");
2445         osal_sem_destroy(&wifi_status_sem.sta_status_sem);
2446         return EXT_WIFI_FAIL;
2447     }
2448 
2449     (void)wpa_cli_get_sta_status(wpa_s, &wifi_status_sem);
2450 
2451     if (wifi_status_sem.status != WIFI_STASTUS_OK) {
2452         wpa_error_log1(MSG_ERROR, "wifi status obtain fail, status = %d.", wifi_status_sem.status);
2453         rc = EXT_WIFI_FAIL;
2454     }
2455     (void)osal_sem_down(&wifi_status_sem.sta_status_sem);
2456     osal_sem_destroy(&wifi_status_sem.sta_status_sem);
2457 
2458     return rc;
2459 }
2460 
wifi_wpa_task_create(const struct ext_wifi_dev * wifi_dev)2461 static int wifi_wpa_task_create(const struct ext_wifi_dev *wifi_dev)
2462 {
2463     wifi_task_attr wpa_task = {0};
2464     wpa_error_log0(MSG_ERROR, "---> uapi_wpa_task_create enter.");
2465     wpa_task.task_entry = (wifi_tsk_entry_func)wpa_supplicant_main_task;
2466     wpa_task.stack_size  = WPA_TASK_STACK_SIZE;
2467     wpa_task.task_name = "wpa_supplicant";
2468     wpa_task.task_prio = WPA_TASK_PRIO_NUM;
2469     wpa_task.arg = (void *)wifi_dev;
2470     wpa_task.task_policy = WIFI_TASK_STATUS_DETACHED;
2471     if (os_task_create(&g_wpataskid, &wpa_task) != WIFI_OS_OK) {
2472         return EXT_WIFI_FAIL;
2473     }
2474     return EXT_WIFI_OK;
2475 }
2476 
wifi_wpa_start(const struct ext_wifi_dev * wifi_dev)2477 int wifi_wpa_start(const struct ext_wifi_dev *wifi_dev)
2478 {
2479     int ret_val;
2480     unsigned int event = WPA_EVENT_WPA_START_ERROR;
2481     unsigned int int_save;
2482 
2483     if (wifi_dev == NULL)
2484         return EXT_WIFI_FAIL;
2485 
2486     wpa_error_log0(MSG_ERROR, "---> wifi_wpa_start enter.");
2487     os_intlock(&int_save);
2488     if (g_wpa_event_inited_flag == WPA_FLAG_OFF) {
2489         (void)os_event_init(&g_wpa_event, WIFI_EVENT_WPA);
2490         (void)os_event_init(&g_softap_event, WIFI_EVENT_SOFTAP);
2491 #ifdef LOS_CONFIG_P2P
2492         (void)os_event_init(&g_p2p_event, WIFI_EVENT_P2P);
2493 #endif /* LOS_CONFIG_P2P */
2494         g_wpa_event_inited_flag = WPA_FLAG_ON;
2495     }
2496     os_intrestore(int_save);
2497     int wifi_dev_count = los_count_wifi_dev_in_use();
2498     if (wifi_dev_count < WPA_BASE_WIFI_DEV_NUM) {
2499         wpa_error_log1(MSG_ERROR, "wifi_wpa_start: wifi_dev_count = %d.\n", wifi_dev_count);
2500         return EXT_WIFI_FAIL;
2501     }
2502     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_WPA_START_FLAG);
2503     wpa_error_log0(MSG_ERROR, "os_event_clear WPA_EVENT_WPA_START_FLAG");
2504     if (wifi_dev_count == WPA_BASE_WIFI_DEV_NUM) {
2505         ret_val = wifi_wpa_task_create(wifi_dev);
2506         if (ret_val != EXT_WIFI_OK) {
2507             wpa_error_log1(MSG_ERROR, "wifi_wpa_start: wpa_supplicant task create failed, ret_val = %x.", ret_val);
2508             return EXT_WIFI_FAIL;
2509         }
2510     } else {
2511         if (wpa_cli_if_start(NULL, wifi_dev->iftype, wifi_dev->ifname, WIFI_IFNAME_MAX_SIZE) != EXT_WIFI_OK) {
2512             wpa_error_log0(MSG_ERROR, "wifi_wpa_start: if start failed.");
2513             return EXT_WIFI_FAIL;
2514         }
2515     }
2516     wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_WPA_START_FLAG");
2517     (void)os_event_read(g_wpa_event, WPA_EVENT_WPA_START_FLAG, &event,
2518                         WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_WAIT_FOREVER);
2519     if (event == WPA_EVENT_WPA_START_OK) {
2520         wpa_error_log0(MSG_ERROR, "wifi_wpa_start: successful.\n");
2521     } else {
2522         wpa_error_log1(MSG_ERROR, "wifi_wpa_start: failed, event: %x\n", event);
2523         return EXT_WIFI_FAIL;
2524     }
2525     return EXT_WIFI_OK;
2526 }
2527 
wifi_wpa_stop(ext_wifi_iftype iftype)2528 int wifi_wpa_stop(ext_wifi_iftype iftype)
2529 {
2530     struct ext_wifi_dev *wifi_dev  = NULL;
2531     char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
2532     int ret;
2533     wpa_error_log0(MSG_ERROR, "---> wifi_wpa_stop enter.");
2534 
2535     wifi_dev = los_get_wifi_dev_by_iftype(iftype);
2536     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL) || (wifi_dev->iftype == EXT_WIFI_IFTYPE_AP)) {
2537         wpa_error_log0(MSG_ERROR, "wifi_wpa_stop: get wifi dev failed");
2538         return EXT_WIFI_FAIL;
2539     }
2540     if (memcpy_s(ifname, WIFI_IFNAME_MAX_SIZE + 1, wifi_dev->ifname, WIFI_IFNAME_MAX_SIZE) != EOK)
2541         return EXT_WIFI_FAIL;
2542 
2543     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_STA_STOP_FLAG);
2544     wpa_error_log0(MSG_ERROR, "os_event_clear WPA_EVENT_STA_STOP_FLAG");
2545     if (wpa_cli_terminate(NULL, ELOOP_TASK_WPA) != EXT_WIFI_OK) {
2546         wpa_error_log0(MSG_ERROR, "wifi_wpa_stop: terminate failed");
2547         return EXT_WIFI_FAIL;
2548     }
2549     unsigned int ret_val = 0;
2550     (void)os_event_read(g_wpa_event, WPA_EVENT_STA_STOP_FLAG, &ret_val,
2551                         WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_WAIT_FOREVER);
2552     if (ret_val == WPA_EVENT_STA_STOP_OK) {
2553         wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_STA_STOP_FLAG success\n");
2554     } else {
2555         wpa_error_log1(MSG_ERROR, "os_event_read WPA_EVENT_STA_STOP_FLAG failed: %x\n", ret_val);
2556         return EXT_WIFI_FAIL;
2557     }
2558     ret = wal_deinit_drv_wlan_netdev(ifname);
2559     if (ret != EXT_WIFI_OK)
2560         wpa_error_log0(MSG_ERROR, "wifi_wpa_stop: wal_deinit_drv_wlan_netdev failed!");
2561 
2562     return ret;
2563 }
2564 
wifi_add_iface(const struct ext_wifi_dev * wifi_dev)2565 int wifi_add_iface(const struct ext_wifi_dev *wifi_dev)
2566 {
2567     struct ext_wifi_dev *old_wifi_dev = NULL;
2568     int ret;
2569     unsigned int ret_val;
2570 
2571     wpa_error_log0(MSG_ERROR, "---> wifi_add_iface enter.");
2572 
2573     if (wifi_dev == NULL) {
2574         wpa_error_log0(MSG_ERROR, "wifi_add_iface: wifi_dev is NULL.");
2575         return EXT_WIFI_FAIL;
2576     }
2577 
2578     old_wifi_dev = wpa_get_other_existed_wpa_wifi_dev(NULL);
2579     if ((old_wifi_dev == NULL) || (old_wifi_dev->priv == NULL)) {
2580         wpa_error_log0(MSG_ERROR, "no wpa wifi dev is running.");
2581         return EXT_WIFI_FAIL;
2582     }
2583     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_ADD_IFACE_FLAG);
2584 
2585     wpa_error_log0(MSG_ERROR, "os_event_clear WPA_EVENT_ADD_IFACE_FLAG");
2586     ret = wpa_cli_add_iface((struct wpa_supplicant *)old_wifi_dev->priv, wifi_dev->ifname);
2587     if (ret != EXT_WIFI_OK) {
2588         wpa_error_log0(MSG_ERROR, "wpa_add_iface: failed\n");
2589         return EXT_WIFI_FAIL;
2590     }
2591 
2592     (void)os_event_read(g_wpa_event, WPA_EVENT_ADD_IFACE_FLAG, &ret_val,
2593                         WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_WAIT_FOREVER);
2594     if (ret_val == WPA_EVENT_ADD_IFACE_OK) {
2595         wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_ADD_IFACE_FLAG successful.");
2596     } else {
2597         wpa_error_log1(MSG_ERROR, "os_event_read WPA_EVENT_ADD_IFACE_FLAG failed ret_val: %x\n", ret_val);
2598         return EXT_WIFI_FAIL;
2599     }
2600     return EXT_WIFI_OK;
2601 }
2602 
wifi_remove_iface(struct ext_wifi_dev * wifi_dev)2603 int wifi_remove_iface(struct ext_wifi_dev *wifi_dev)
2604 {
2605     struct wpa_supplicant *wpa_s = NULL;
2606     char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
2607     int ret;
2608     unsigned int ret_val;
2609 
2610     if (wifi_dev == NULL)
2611         return EXT_WIFI_FAIL;
2612 
2613     wpa_s = wifi_dev->priv;
2614     if (memcpy_s(ifname, sizeof(ifname), wifi_dev->ifname, (size_t)wifi_dev->ifname_len) != (int)EOK)
2615         return EXT_WIFI_FAIL;
2616 
2617     (void)os_event_clear(g_wpa_event, ~(unsigned int)WPA_EVENT_REMOVE_IFACE_FLAG);
2618     wpa_error_log0(MSG_ERROR, "os_event_clear WPA_EVENT_REMOVE_IFACE_FLAG");
2619     ret = wpa_cli_remove_iface(wpa_s);
2620     if (ret != EXT_WIFI_OK) {
2621         wpa_error_log0(MSG_ERROR, "wifi_remove_iface: failed\n");
2622         return EXT_WIFI_FAIL;
2623     }
2624     (void)os_event_read(g_wpa_event, WPA_EVENT_REMOVE_IFACE_FLAG, &ret_val,
2625                         WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_WAIT_FOREVER);
2626     if (ret_val == WPA_EVENT_REMOVE_IFACE_FLAG) {
2627         wpa_error_log0(MSG_ERROR, "os_event_read WPA_EVENT_REMOVE_IFACE_FLAG success");
2628     } else {
2629         wpa_error_log1(MSG_ERROR, "os_event_read WPA_EVENT_REMOVE_IFACE_FLAG failed ret_val = %x", ret_val);
2630     }
2631     (void)wal_deinit_drv_wlan_netdev(ifname);
2632     return EXT_WIFI_OK;
2633 }
2634 
uapi_wifi_sta_start(char * ifname,int * len)2635 int uapi_wifi_sta_start(char *ifname, int *len)
2636 {
2637     struct ext_wifi_dev *wifi_dev = NULL;
2638     int rc;
2639     int ret;
2640 
2641     if ((ifname == NULL) || (len == NULL)) {
2642         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_start: invalid param");
2643         return EXT_WIFI_FAIL;
2644     }
2645 
2646     if (try_set_lock_flag() == EXT_WIFI_FAIL) {
2647         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_start: wifi dev start or stop is running.");
2648         return EXT_WIFI_FAIL;
2649     }
2650 
2651     if (sta_precheck() != EXT_WIFI_OK) {
2652         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_start: sta_precheck fail!");
2653         goto WIFI_STA_EXIT;
2654     }
2655     /* Sta协议模式默认使用11ax */
2656     if (g_sta_opt_set.hw_mode == WIFI_MODE_UNDEFINE) {
2657         g_sta_opt_set.hw_mode = WIFI_MODE_11B_G_N_AX;
2658     }
2659     wifi_dev = wifi_dev_creat(EXT_WIFI_IFTYPE_STATION, g_sta_opt_set.hw_mode);
2660     if (wifi_dev == NULL) {
2661         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_start: wifi_dev_creat failed.");
2662         goto WIFI_STA_EXIT;
2663     }
2664 
2665     if ((los_set_wifi_dev(wifi_dev) != EXT_WIFI_OK) || (*len < wifi_dev->ifname_len + 1))
2666         goto WIFI_STA_ERROR;
2667 
2668     rc = memcpy_s(ifname, (size_t)(*len), wifi_dev->ifname, (size_t)wifi_dev->ifname_len);
2669     if (rc != EOK) {
2670         wpa_error_log0(MSG_ERROR, "Could not copy wifi dev ifname.");
2671         goto WIFI_STA_ERROR;
2672     }
2673 
2674     ifname[wifi_dev->ifname_len] = '\0';
2675     *len = wifi_dev->ifname_len;
2676     if ((los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_P2P_CLIENT) != NULL) ||
2677         (los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_P2P_GO) != NULL) ||
2678         (los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_P2P_DEVICE) != NULL) ||
2679         (los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_MESH_POINT) != NULL)) {
2680         ret = wifi_add_iface(wifi_dev);
2681     } else
2682         ret = wifi_wpa_start(wifi_dev);
2683 
2684     if (ret != EXT_WIFI_OK)
2685         goto WIFI_STA_ERROR;
2686 
2687     clr_lock_flag();
2688     return EXT_WIFI_OK;
2689 WIFI_STA_ERROR:
2690     (void)wal_deinit_drv_wlan_netdev(wifi_dev->ifname);
2691     los_free_wifi_dev(wifi_dev);
2692 WIFI_STA_EXIT:
2693     clr_lock_flag();
2694     return EXT_WIFI_FAIL;
2695 }
2696 
uapi_wifi_sta_stop(void)2697 int uapi_wifi_sta_stop(void)
2698 {
2699     int ret = EXT_WIFI_FAIL;
2700     unsigned int int_save;
2701 
2702     if (try_set_lock_flag() == EXT_WIFI_FAIL) {
2703         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
2704         return EXT_WIFI_FAIL;
2705     }
2706 
2707     struct ext_wifi_dev *wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_STATION);
2708     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL)) {
2709         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_stop: get wifi dev failed");
2710         goto WIFI_STA_STOP_FAIL;
2711     }
2712     if (wpa_get_other_existed_wpa_wifi_dev(wifi_dev->priv) != NULL)
2713         ret = wifi_remove_iface(wifi_dev);
2714     else
2715         ret = wifi_wpa_stop(EXT_WIFI_IFTYPE_STATION);
2716     if (ret != EXT_WIFI_OK) {
2717         wpa_error_log0(MSG_ERROR, "wpa_stop failed");
2718         ret = EXT_WIFI_FAIL;
2719         goto WIFI_STA_STOP_FAIL;
2720     }
2721     os_intlock(&int_save);
2722 
2723     (void)memset_s(&g_sta_opt_set, sizeof(struct wifi_sta_opt_set), 0, sizeof(struct wifi_sta_opt_set));
2724     (void)memset_s(&g_reconnect_set, sizeof(struct wifi_reconnect_set), 0, sizeof(struct wifi_reconnect_set));
2725     (void)memset_s(&g_scan_record, sizeof(struct ext_scan_record), 0, sizeof(struct ext_scan_record));
2726     (void)memset_s(g_quick_conn_psk, sizeof(g_quick_conn_psk), 0, sizeof(g_quick_conn_psk));
2727 
2728     g_assoc_auth = EXT_WIFI_SEC_TYPE_INVALID;
2729     g_mesh_sta_flag = WPA_FLAG_OFF;
2730     g_connecting_flag = WPA_FLAG_OFF;
2731     g_wpa_rm_network = SOC_WPA_RM_NETWORK_END;
2732     os_intrestore(int_save);
2733 
2734 #ifdef LOS_CONFIG_WPA_ENTERPRISE
2735     if (g_wfa_ca_cert != NULL) {
2736         free(g_wfa_ca_cert);
2737         g_wfa_ca_cert = NULL;
2738     }
2739 
2740     if (g_wfa_cli_cert != NULL) {
2741         free(g_wfa_cli_cert);
2742         g_wfa_cli_cert = NULL;
2743     }
2744 
2745     if (g_wfa_cli_key != NULL) {
2746         free(g_wfa_cli_key);
2747         g_wfa_cli_key = NULL;
2748     }
2749 
2750     if (g_wfa_ent_identity != NULL) {
2751         free(g_wfa_ent_identity);
2752         g_wfa_ent_identity = NULL;
2753     }
2754 #endif
2755 
2756     ret = EXT_WIFI_OK;
2757 WIFI_STA_STOP_FAIL:
2758     clr_lock_flag();
2759     return ret;
2760 }
2761 
2762 #ifdef CONFIG_WPS
uapi_wifi_sta_wps_pbc(const unsigned char * bssid,unsigned int bssid_len)2763 int uapi_wifi_sta_wps_pbc(const unsigned char *bssid, unsigned int bssid_len)
2764 {
2765     int ret;
2766     struct ext_wifi_dev *wifi_dev = NULL;
2767     char network_id_txt[WPA_NETWORK_ID_TXT_LEN + 1] = { 0 };
2768     char addr_txt[EXT_WIFI_TXT_ADDR_LEN + 1] = { 0 };
2769 
2770     wifi_dev = wifi_dev_get(EXT_WIFI_IFTYPE_STATION);
2771     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL)) {
2772         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_wps_pbc: get wifi dev failed.");
2773         return EXT_WIFI_FAIL;
2774     }
2775     if (bssid != NULL) {
2776         if ((bssid_len != ETH_ALEN) || (addr_precheck(bssid) != EXT_WIFI_OK)) {
2777             wpa_error_log0(MSG_ERROR, "Invalid bssid.");
2778             return EXT_WIFI_FAIL;
2779         }
2780     }
2781     ret = snprintf_s(network_id_txt, sizeof(network_id_txt), sizeof(network_id_txt) - 1, "%d", wifi_dev->network_id);
2782     if (ret < 0) {
2783         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_wps_pbc snprintf_s faild");
2784         return EXT_WIFI_FAIL;
2785     }
2786     ret = wpa_cli_remove_network((struct wpa_supplicant *)(wifi_dev->priv), network_id_txt, WPA_NETWORK_ID_TXT_LEN);
2787     if (ret != EXT_WIFI_OK) {
2788         wpa_error_log0(MSG_ERROR, "wpa_cli_remove_network fail.");
2789         return EXT_WIFI_FAIL;
2790     }
2791     if (bssid == NULL) {
2792         ret = wpa_cli_wps_pbc((struct wpa_supplicant *)(wifi_dev->priv), NULL, 0);
2793     } else {
2794         ret = snprintf_s(addr_txt, sizeof(addr_txt), sizeof(addr_txt) - 1, MACSTR, MAC2STR(bssid));
2795         if (ret < 0) {
2796             wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_wps_pbc snprintf_s faild");
2797             return EXT_WIFI_FAIL;
2798         }
2799         ret = wpa_cli_wps_pbc((struct wpa_supplicant *)(wifi_dev->priv), addr_txt, sizeof(addr_txt));
2800     }
2801     if (ret != EXT_WIFI_OK) {
2802         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_wps_pbc failed");
2803         return EXT_WIFI_FAIL;
2804     }
2805     return EXT_WIFI_OK;
2806 }
2807 
wps_pin_check(const char * pin,unsigned int pin_len)2808 static int wps_pin_check(const char *pin, unsigned int pin_len)
2809 {
2810     unsigned int i;
2811     if ((pin_len != WPS_PIN_LENGTH) || (strnlen(pin, WPS_PIN_LENGTH + 1) != pin_len))
2812         return EXT_WIFI_FAIL;
2813     for (i = 0; i < pin_len; i++)
2814         if ((pin[i] < '0') || (pin[i] > '9')) {
2815             wpa_error_log0(MSG_ERROR, "wps_pin: pin Format should be 0 ~ 9.");
2816             return EXT_WIFI_FAIL;
2817         }
2818     return EXT_WIFI_OK;
2819 }
2820 
uapi_wifi_sta_wps_pin(const char * pin,unsigned int pin_len,const unsigned char * bssid,unsigned int bssid_len)2821 int uapi_wifi_sta_wps_pin(const char *pin, unsigned int pin_len, const unsigned char *bssid, unsigned int bssid_len)
2822 {
2823     int ret;
2824     char network_id_txt[WPA_NETWORK_ID_TXT_LEN + 1] = { 0 };
2825     struct ext_wifi_dev *wifi_dev                  = NULL;
2826     char addr_txt[EXT_WIFI_TXT_ADDR_LEN + 1]         = { 0 };
2827 
2828     wifi_dev = wifi_dev_get(EXT_WIFI_IFTYPE_STATION);
2829     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL)) {
2830         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_wps_pin: get wifi dev failed\n");
2831         return EXT_WIFI_FAIL;
2832     }
2833     if (pin == NULL) {
2834         wpa_warning_log0(MSG_DEBUG, "wpa_cli_wps_pin : pin is NULL");
2835         return EXT_WIFI_FAIL;
2836     }
2837     if (bssid != NULL) {
2838         if ((bssid_len != ETH_ALEN) || (addr_precheck(bssid) != EXT_WIFI_OK)) {
2839             wpa_error_log0(MSG_ERROR, "Invalid bssid.");
2840             return EXT_WIFI_FAIL;
2841         }
2842     }
2843     if (wps_pin_check(pin, pin_len) == EXT_WIFI_FAIL)
2844         return EXT_WIFI_FAIL;
2845     ret = snprintf_s(network_id_txt, sizeof(network_id_txt), sizeof(network_id_txt) - 1, "%d", wifi_dev->network_id);
2846     if (ret < 0) {
2847         wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_wps_pin: network_id_txt snprintf_s faild");
2848         return EXT_WIFI_FAIL;
2849     }
2850     ret = wpa_cli_remove_network((struct wpa_supplicant *)(wifi_dev->priv), network_id_txt, WPA_NETWORK_ID_TXT_LEN);
2851     if (ret != EXT_WIFI_OK) {
2852         wpa_error_log0(MSG_ERROR, "wpa_cli_remove_network fail.");
2853         return EXT_WIFI_FAIL;
2854     }
2855     if (bssid == NULL) {
2856         ret = wpa_cli_wps_pin((struct wpa_supplicant *)(wifi_dev->priv), pin, pin_len, NULL, 0);
2857     } else {
2858         ret = snprintf_s(addr_txt, sizeof(addr_txt), sizeof(addr_txt) - 1, MACSTR, MAC2STR(bssid));
2859         if (ret < 0) {
2860             wpa_error_log0(MSG_ERROR, "uapi_wifi_sta_wps_pin: addr_txt snprintf_s faild");
2861             return EXT_WIFI_FAIL;
2862         }
2863         ret = wpa_cli_wps_pin((struct wpa_supplicant *)(wifi_dev->priv), pin, pin_len, addr_txt, sizeof(addr_txt));
2864     }
2865     return ret;
2866 }
2867 
uapi_wifi_sta_wps_pin_get(char * pin,unsigned int len)2868 int uapi_wifi_sta_wps_pin_get(char *pin, unsigned int len)
2869 {
2870     unsigned int val = 0;
2871     int ret;
2872 
2873     if (pin == NULL) {
2874         wpa_error_log0(MSG_ERROR, " ---> pin ptr is NULL.");
2875         return EXT_WIFI_FAIL;
2876     }
2877     if (len != (WIFI_WPS_PIN_LEN + 1)) {
2878         wpa_error_log1(MSG_ERROR, " ---> wps pin buffer length should be %d.", (WIFI_WPS_PIN_LEN + 1));
2879         return EXT_WIFI_FAIL;
2880     }
2881     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
2882         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
2883         return EXT_WIFI_FAIL;
2884     }
2885     ret = wps_generate_pin(&val);
2886     if (ret != EXT_WIFI_OK) {
2887         wpa_error_log0(MSG_ERROR, " ---> wps generate pin failed.");
2888         return EXT_WIFI_FAIL;
2889     }
2890     ret = snprintf_s(pin, WIFI_WPS_PIN_LEN + 1, WIFI_WPS_PIN_LEN, "%08u", val);
2891     return (ret < 0) ? EXT_WIFI_FAIL : EXT_WIFI_OK;
2892 }
2893 #endif /* CONFIG_WPS */
2894 
2895 /*****************************change STA settingts start ***********************************/
wifi_sta_set_cond_check(void)2896 static int wifi_sta_set_cond_check(void)
2897 {
2898     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
2899         wpa_error_log0(MSG_ERROR, "wifi_sta_set_cond_check: wifi dev start or stop is running.");
2900         return EXT_WIFI_FAIL;
2901     }
2902     if (is_sta_on() == EXT_WIFI_OK) {
2903         wpa_error_log0(MSG_ERROR, "wifi_sta_set_cond_check: sta is already in progress, set failed.");
2904         return EXT_WIFI_FAIL;
2905     }
2906     return EXT_WIFI_OK;
2907 }
2908 
uapi_wifi_set_assoc_auth(int auth_type)2909 int uapi_wifi_set_assoc_auth(int auth_type)
2910 {
2911     if (auth_type > EXT_WIFI_SECURITY_UNKNOWN || auth_type < 0) {
2912         wpa_error_log0(MSG_ERROR, "uapi_wifi_set_assoc_auth: invalid auth type.");
2913         return EXT_WIFI_FAIL;
2914     }
2915 
2916     g_assoc_auth = auth_type;
2917     return EXT_WIFI_OK;
2918 }
2919 
uapi_wifi_set_ft_flag(int flag)2920 int uapi_wifi_set_ft_flag(int flag)
2921 {
2922     if (flag != WPA_FLAG_OFF && flag != WPA_FLAG_ON) {
2923         wpa_error_log0(MSG_ERROR, "uapi_wifi_set_ft_flag: invalid flag.");
2924         return EXT_WIFI_FAIL;
2925     }
2926 
2927     g_ft_flag = flag;
2928     return EXT_WIFI_OK;
2929 }
2930 
uapi_wifi_set_pmf(wifi_pmf_option_enum pmf)2931 int uapi_wifi_set_pmf(wifi_pmf_option_enum pmf)
2932 {
2933     if (wifi_sta_set_cond_check() == EXT_WIFI_FAIL)
2934         return EXT_WIFI_FAIL;
2935 
2936     if (pmf > WIFI_MGMT_FRAME_PROTECTION_REQUIRED) {
2937         wpa_error_log0(MSG_ERROR, "uapi_wifi_set_pmf: invalid pmf options.");
2938         return EXT_WIFI_FAIL;
2939     }
2940 
2941     g_sta_opt_set.pmf = pmf;
2942     g_sta_opt_set.usr_pmf_set_flag = WPA_FLAG_ON;
2943     return EXT_WIFI_OK;
2944 }
2945 
wifi_softap_set_pmf(int pmf)2946 int wifi_softap_set_pmf(int pmf)
2947 {
2948     if (pmf > MGMT_FRAME_PROTECTION_REQUIRED) {
2949         wpa_error_log0(MSG_ERROR, "wifi_softap_set_pmf: invalid pmf options.");
2950         return EXT_WIFI_FAIL;
2951     }
2952 
2953     g_ap_opt_set.ieee80211w = pmf;
2954     return EXT_WIFI_OK;
2955 }
2956 
2957 
2958 #ifdef CONFIG_WPA3
wifi_sta_set_sae_pwe(wifi_sae_pwe_option_enum pwe)2959 int wifi_sta_set_sae_pwe(wifi_sae_pwe_option_enum pwe)
2960 {
2961     if (pwe > WIFI_SAE_PWE_BOTH) {
2962         wpa_error_log0(MSG_ERROR, "wifi_sta_set_sae_pwe: invalid pwe options.");
2963         return EXT_WIFI_FAIL;
2964     }
2965 
2966     g_sta_opt_set.sae_pwe = pwe;
2967     return EXT_WIFI_OK;
2968 }
2969 
wifi_softap_set_sae_pwe(wifi_sae_pwe_option_enum pwe)2970 int wifi_softap_set_sae_pwe(wifi_sae_pwe_option_enum pwe)
2971 {
2972     if (pwe > WIFI_SAE_PWE_BOTH) {
2973         wpa_error_log0(MSG_ERROR, "wifi_softap_set_sae_pwe: invalid pwe options.");
2974         return EXT_WIFI_FAIL;
2975     }
2976 
2977     g_ap_opt_set.sae_pwe = pwe;
2978     return EXT_WIFI_OK;
2979 }
2980 
wifi_softap_set_transition(int transition)2981 int wifi_softap_set_transition(int transition)
2982 {
2983     g_ap_opt_set.transition = transition;
2984     return EXT_WIFI_OK;
2985 }
2986 
wifi_softap_set_clog_threshold(int clog_threshold)2987 int wifi_softap_set_clog_threshold(int clog_threshold)
2988 {
2989     g_ap_opt_set.clog_threshold = clog_threshold;
2990     return EXT_WIFI_OK;
2991 }
2992 
wifi_sta_set_sae_groups(int * groups,int len)2993 int wifi_sta_set_sae_groups(int *groups, int len)
2994 {
2995     if (memcpy_s(g_sta_opt_set.sae_groups, sizeof(g_ap_opt_set.sae_groups), groups, len) != EXT_WIFI_OK) {
2996         wpa_error_log0(MSG_ERROR, "wifi_sta_set_sae_groups: invalid groups param.");
2997         return EXT_WIFI_FAIL;
2998     }
2999 
3000     return EXT_WIFI_OK;
3001 }
3002 
wifi_softap_set_sae_groups(int * groups,int len)3003 int wifi_softap_set_sae_groups(int *groups, int len)
3004 {
3005     if (memcpy_s(g_ap_opt_set.sae_groups, sizeof(g_ap_opt_set.sae_groups), groups, len) != EXT_WIFI_OK) {
3006         wpa_error_log0(MSG_ERROR, "wifi_softap_set_sae_groups: invalid groups param.");
3007         return EXT_WIFI_FAIL;
3008     }
3009 
3010     return EXT_WIFI_OK;
3011 }
3012 
3013 #endif
3014 
uapi_wifi_sta_set_reconnect_policy(int enable,unsigned int seconds,unsigned int period,unsigned int max_try_count)3015 int uapi_wifi_sta_set_reconnect_policy(int enable, unsigned int seconds,
3016                                        unsigned int period, unsigned int max_try_count)
3017 {
3018     struct ext_wifi_dev *wifi_dev = NULL;
3019     struct wpa_supplicant *wpa_s = NULL;
3020     struct wpa_ssid *current_ssid = g_reconnect_set.current_ssid;
3021     int is_connected = WPA_FLAG_OFF;
3022 
3023     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
3024         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
3025         return EXT_WIFI_FAIL;
3026     }
3027     if (is_sta_on() != EXT_WIFI_OK) {
3028         wpa_error_log0(MSG_ERROR, "sta is not started, set reconnect policy failed.");
3029         return EXT_WIFI_FAIL;
3030     }
3031 
3032     if ((enable < WPA_FLAG_OFF) || (enable > WPA_FLAG_ON) ||
3033         (seconds < WIFI_MIN_RECONNECT_TIMEOUT) || (seconds > WIFI_MAX_RECONNECT_TIMEOUT) ||
3034         (period < WIFI_MIN_RECONNECT_PERIOD) || (period > WIFI_MAX_RECONNECT_PERIOD) ||
3035         (max_try_count < WIFI_MIN_RECONNECT_TIMES) || (max_try_count > WIFI_MAX_RECONNECT_TIMES)) {
3036         wpa_error_log0(MSG_ERROR, "input value error.");
3037         return EXT_WIFI_FAIL;
3038     }
3039 
3040     unsigned int int_save;
3041     os_task_lock(&int_save);
3042     (void)memset_s(&g_reconnect_set, sizeof(struct wifi_reconnect_set), 0, sizeof(struct wifi_reconnect_set));
3043     g_reconnect_set.enable = enable;
3044     g_reconnect_set.timeout = seconds;
3045     g_reconnect_set.period = period;
3046     g_reconnect_set.max_try_count = max_try_count;
3047     os_task_unlock(int_save);
3048 
3049     wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_STATION);
3050     os_task_lock(&int_save);
3051     if ((wifi_dev != NULL) && (wifi_dev->priv != NULL)) {
3052         wpa_s = (struct wpa_supplicant *)wifi_dev->priv;
3053         is_connected = (wpa_s->wpa_state == WPA_COMPLETED) ? WPA_FLAG_ON : WPA_FLAG_OFF;
3054         g_reconnect_set.current_ssid = (is_connected == WPA_FLAG_ON) ? wpa_s->current_ssid : current_ssid;
3055     }
3056     os_task_unlock(int_save);
3057 
3058     return EXT_WIFI_OK;
3059 }
3060 
uapi_wifi_sta_set_protocol_mode(protocol_mode_enum hw_mode)3061 int uapi_wifi_sta_set_protocol_mode(protocol_mode_enum hw_mode)
3062 {
3063     if (wifi_sta_set_cond_check() == EXT_WIFI_FAIL)
3064         return EXT_WIFI_FAIL;
3065 
3066     if (hw_mode == WIFI_MODE_UNDEFINE || hw_mode > WIFI_MODE_11B_G_N_AX) {
3067         wpa_error_log0(MSG_ERROR, "physical mode value is error.");
3068         return EXT_WIFI_FAIL;
3069     }
3070     g_sta_opt_set.hw_mode = hw_mode;
3071     return EXT_WIFI_OK;
3072 }
3073 
uapi_wifi_get_pmf(void)3074 wifi_pmf_option_enum uapi_wifi_get_pmf(void)
3075 {
3076     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
3077         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
3078         return WIFI_MGMT_FRAME_PROTECTION_BUTT;
3079     }
3080     return g_sta_opt_set.pmf;
3081 }
3082 
uapi_wifi_sta_get_protocol_mode(void)3083 protocol_mode_enum uapi_wifi_sta_get_protocol_mode(void)
3084 {
3085     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
3086         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
3087         return WIFI_MODE_UNDEFINE;
3088     }
3089     return g_sta_opt_set.hw_mode;
3090 }
3091 
wifi_event_task_handler(unsigned int event)3092 void wifi_event_task_handler(unsigned int event)
3093 {
3094     if (g_wpa_event_cb != NULL) {
3095         g_wpa_event_cb((ext_wifi_event *)(uintptr_t)event);
3096     }
3097     g_wpa_event_running = 0;
3098     os_free((void *)(uintptr_t)event);
3099 }
3100 
3101 /* create a task and call user's cb */
wifi_new_task_event_cb(const ext_wifi_event * event_cb)3102 void wifi_new_task_event_cb(const ext_wifi_event *event_cb)
3103 {
3104     wifi_task_attr event_cb_task = {0};
3105     ext_wifi_event *event_new = NULL;
3106     unsigned int timeout = 0;
3107 
3108     if ((g_wpa_event_cb == NULL) || (event_cb == NULL)) {
3109         return;
3110     }
3111     if (g_direct_cb == 1) { /* call directly */
3112         g_wpa_event_cb(event_cb);
3113         return;
3114     }
3115     while (g_wpa_event_running != 0) {
3116         os_task_delay(10);   /* delay 1 tick (10ms) and try again */
3117         timeout++;
3118         if (timeout > 100) { /* 100 tick = 1s */
3119             printf("wifi_new_task_event_cb::current event[%d] wait timeout, last event[%d]\r\n",
3120                 event_cb->event, g_wpa_event_running - 1);
3121             return;
3122         }
3123     }
3124     wpa_event_task_free();
3125     event_new = (ext_wifi_event *)os_zalloc(sizeof(ext_wifi_event));
3126     if (event_new == NULL) {
3127         wpa_error_log0(MSG_ERROR, "ext_wifi_event malloc err.");
3128         return;
3129     }
3130     if (memcpy_s(event_new, sizeof(ext_wifi_event), event_cb, sizeof(ext_wifi_event)) != EOK) {
3131         wpa_error_log0(MSG_ERROR, "memcpy event info err.");
3132         os_free(event_new);
3133         return;
3134     }
3135     event_cb_task.task_entry = (wifi_tsk_entry_func)wifi_event_task_handler;
3136     event_cb_task.stack_size  = g_cb_stack_size;
3137     event_cb_task.task_name = "wpa_event_cb";
3138     event_cb_task.task_prio = g_cb_task_prio; /* task prio, must lower than wifi/lwip/wpa */
3139     event_cb_task.arg = (void *)event_new;
3140     event_cb_task.task_policy = WIFI_TASK_STATUS_DETACHED;
3141     g_wpa_event_running = event_cb->event + 1; /* event runing */
3142     if (os_task_create(&g_wpa_event_taskid, &event_cb_task) != WIFI_OS_OK) {
3143         osal_printk("Event cb create task failed, %d.\r\n", event_cb->event);
3144         g_wpa_event_running = 0;
3145         os_free(event_new);
3146     }
3147 }
3148 
uapi_wifi_config_callback(unsigned char mode,unsigned char task_prio,unsigned short stack_size)3149 int uapi_wifi_config_callback(unsigned char mode, unsigned char task_prio, unsigned short stack_size)
3150 {
3151     /* only support 0 and 1 */
3152     if ((mode & 1) != mode) {
3153         wpa_error_log1(MSG_ERROR, "uapi_wifi_config_callback: invalid mode:%u.", (unsigned int)mode);
3154         return EXT_WIFI_FAIL;
3155     }
3156     g_direct_cb = mode;
3157     /* directly call don't need follow configuraion */
3158     if (g_direct_cb == 1)
3159         return EXT_WIFI_OK;
3160 
3161     if ((task_prio < EXT_WIFI_CB_MIN_PRIO) || (task_prio > EXT_WIFI_CB_MAX_PRIO)) {
3162         wpa_error_log1(MSG_ERROR, "uapi_wifi_config_callback: invalid prio:%u.", (unsigned int)task_prio);
3163         return EXT_WIFI_FAIL;
3164     }
3165     if (stack_size < WPA_CB_STACK_SIZE) {
3166         wpa_error_log1(MSG_ERROR, "uapi_wifi_config_callback: invalid stack size:%u.", (unsigned int)stack_size);
3167         return EXT_WIFI_FAIL;
3168     }
3169     g_cb_task_prio = task_prio;
3170     g_cb_stack_size = stack_size & 0xFFF0;  /* 16bytes allign */
3171     return EXT_WIFI_OK;
3172 }
3173 
uapi_wifi_register_event_callback(uapi_wifi_event_cb event_cb)3174 int uapi_wifi_register_event_callback(uapi_wifi_event_cb event_cb)
3175 {
3176     g_wpa_event_cb = event_cb;
3177     return EXT_WIFI_OK;
3178 }
3179 
3180 
uapi_wifi_sta_reconnect_for_wfa_test(void)3181 int uapi_wifi_sta_reconnect_for_wfa_test(void)
3182 {
3183     struct ext_wifi_dev *wifi_dev = NULL;
3184     char network_id_txt[WPA_NETWORK_ID_TXT_LEN + 1] = { 0 };
3185     int ret;
3186 
3187     wpa_error_log0(MSG_ERROR, " ---> ### uapi_wifi_sta_reconnect_for_wfa_test enter. \n\n\r");
3188 
3189     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
3190         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
3191         return EXT_WIFI_FAIL;
3192     }
3193 
3194     wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_STATION);
3195     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL)) {
3196         wpa_error_log0(MSG_ERROR, "sta_reconnect: get wifi dev failed\n");
3197         return EXT_WIFI_FAIL;
3198     }
3199 
3200     ret = snprintf_s(network_id_txt, sizeof(network_id_txt), sizeof(network_id_txt) - 1, "%d", wifi_dev->network_id);
3201     if (ret < 0) {
3202         wpa_error_log0(MSG_ERROR, "sta_reconnect: network_id_txt snprintf_s failed");
3203         return EXT_WIFI_FAIL;
3204     }
3205     if (wpa_cli_disconnect((struct wpa_supplicant *)(wifi_dev->priv)) < 0)
3206         return EXT_WIFI_FAIL;
3207 
3208     g_connecting_flag = WPA_FLAG_ON;
3209 
3210     if (wpa_cli_select_network((struct wpa_supplicant *)(wifi_dev->priv), network_id_txt, WPA_NETWORK_ID_TXT_LEN) < 0)
3211         return EXT_WIFI_FAIL;
3212     return EXT_WIFI_OK;
3213 }
3214 
wifi_ie_index_convert(ext_wifi_extend_ie_index index)3215 static wifi_extend_ie_index wifi_ie_index_convert(ext_wifi_extend_ie_index index)
3216 {
3217     wifi_extend_ie_index ie_index;
3218 
3219     switch (index) {
3220         case EXT_WIFI_EXTEND_IE1:
3221             ie_index = WIFI_EXTEND_IE1;
3222             break;
3223         case EXT_WIFI_EXTEND_IE2:
3224             ie_index = WIFI_EXTEND_IE2;
3225             break;
3226         case EXT_WIFI_EXTEND_IE3:
3227             ie_index = WIFI_EXTEND_IE3;
3228             break;
3229         case EXT_WIFI_EXTEND_IE4:
3230             ie_index = WIFI_EXTEND_IE4;
3231             break;
3232         default:
3233             ie_index = WIFI_EXTEND_IE_BUTT;
3234             break;
3235     }
3236 
3237     return ie_index;
3238 }
3239 
wifi_set_usr_app_ie(ext_wifi_iftype iftype,ext_wifi_extend_ie_index ie_index,const unsigned char frame_type_bitmap,const unsigned char * ie,unsigned short ie_len)3240 static int wifi_set_usr_app_ie(ext_wifi_iftype iftype, ext_wifi_extend_ie_index ie_index,
3241                                const unsigned char frame_type_bitmap,
3242                                const unsigned char *ie, unsigned short ie_len)
3243 {
3244     struct ext_wifi_dev *wifi_dev = NULL;
3245     wifi_extend_ie_index index;
3246     uint8 set = (ie != NULL) ? 1 : 0;
3247 
3248     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
3249         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
3250         return EXT_WIFI_FAIL;
3251     }
3252 
3253     if ((iftype != EXT_WIFI_IFTYPE_STATION) && (iftype != EXT_WIFI_IFTYPE_AP) &&
3254         (iftype != EXT_WIFI_IFTYPE_P2P_DEVICE) && (iftype != EXT_WIFI_IFTYPE_P2P_GO)) {
3255         wpa_error_log0(MSG_ERROR, "wifi_set_usr_app_ie_internal: iftype invalid.");
3256         return EXT_WIFI_FAIL;
3257     }
3258 
3259     if (ie_index >= EXT_WIFI_EXTEND_IE_BUTT) {
3260         wpa_error_log0(MSG_ERROR, "wifi_set_usr_app_ie_internal: usr_ie_type or ie_index invalid.");
3261         return EXT_WIFI_FAIL;
3262     }
3263 
3264     if (!(((unsigned char)EXT_WIFI_BEACON_IE | (unsigned char)EXT_WIFI_PROBE_RSP_IE |
3265         (unsigned char)EXT_WIFI_PROBE_REQ_IE) & frame_type_bitmap))
3266         return EXT_WIFI_FAIL;
3267 
3268     index = wifi_ie_index_convert(ie_index);
3269 
3270     wifi_dev = wifi_dev_get(iftype);
3271     if ((wifi_dev == NULL) || (wifi_dev->priv == NULL)) {
3272         wpa_error_log0(MSG_ERROR, "wifi_set_usr_app_ie_internal: get wifi dev failed.");
3273         return EXT_WIFI_FAIL;
3274     }
3275 
3276     if (drv_soc_set_usr_app_ie(wifi_dev->ifname, set, index, frame_type_bitmap, ie, ie_len) != EXT_WIFI_OK) {
3277         wpa_error_log0(MSG_ERROR, "wifi_set_usr_app_ie_internal: set usr ie failed.");
3278         return EXT_WIFI_FAIL;
3279     }
3280     return EXT_WIFI_OK;
3281 }
3282 
uapi_wifi_add_usr_app_ie(ext_wifi_iftype iftype,ext_wifi_extend_ie_index ie_index,const unsigned char frame_type_bitmap,const unsigned char * ie,unsigned short ie_len)3283 int uapi_wifi_add_usr_app_ie(ext_wifi_iftype iftype, ext_wifi_extend_ie_index ie_index,
3284                              const unsigned char frame_type_bitmap,
3285                              const unsigned char *ie, unsigned short ie_len)
3286 {
3287     if ((ie == NULL) || (ie_len > EXT_WIFI_USR_IE_MAX_SIZE) || (ie_len == 0) ||
3288         (ie[WPA_USE_IE_LEN_SUBSCRIPT] != ie_len - WPA_USE_IE_INIT_BYTE_NUM)) {
3289         wpa_error_log0(MSG_ERROR, "uapi_wifi_add_usr_app_ie: invalid input.");
3290         return EXT_WIFI_FAIL;
3291     }
3292 
3293     return wifi_set_usr_app_ie(iftype, ie_index, frame_type_bitmap, ie, ie_len); /* 1: add */
3294 }
3295 
uapi_wifi_delete_usr_app_ie(ext_wifi_iftype iftype,ext_wifi_extend_ie_index ie_index,const unsigned char frame_type_bitmap)3296 int uapi_wifi_delete_usr_app_ie(ext_wifi_iftype iftype, ext_wifi_extend_ie_index ie_index,
3297                                 const unsigned char frame_type_bitmap)
3298 {
3299     return wifi_set_usr_app_ie(iftype, ie_index, frame_type_bitmap, NULL, 0); /* 0: delete */
3300 }
3301 
3302 #ifdef LOS_CONFIG_WPA_ENTERPRISE
uapi_wifi_sta_ent_set_eap_method(ext_wifi_eap_method method)3303 int uapi_wifi_sta_ent_set_eap_method(ext_wifi_eap_method method)
3304 {
3305     if (method >= EXT_WIFI_EAP_METHOD_BUTT)
3306         return EXT_WIFI_FAIL;
3307     g_eap_method = method;
3308     return EXT_WIFI_OK;
3309 }
3310 
uapi_wifi_sta_ent_set_identity(const char * identity,unsigned int len)3311 int uapi_wifi_sta_ent_set_identity(const char *identity, unsigned int len)
3312 {
3313     struct ext_wifi_dev *wifi_dev = NULL;
3314     int                  ret;
3315     unsigned int         int_save;
3316 
3317     if ((identity == NULL) || (len > EXT_WIFI_IDENTITY_LEN) ||
3318         (strnlen(identity, len + 1) != len))
3319         return EXT_WIFI_FAIL;
3320 
3321     if (is_lock_flag_off() == EXT_WIFI_FAIL) {
3322         wpa_error_log0(MSG_ERROR, "wifi dev start or stop is running.");
3323         return EXT_WIFI_FAIL;
3324     }
3325 
3326     wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_STATION);
3327     if (wifi_dev == NULL) {
3328         wpa_error_log0(MSG_ERROR, "get wifi dev failed");
3329         return EXT_WIFI_FAIL;
3330     }
3331 
3332     os_intlock(&int_save);
3333     ret = snprintf_s(g_eap_identity, EXT_WIFI_IDENTITY_LEN + WPA_IDENTITY_EXTRA_LEN,
3334                      EXT_WIFI_IDENTITY_LEN + WPA_IDENTITY_EXTRA_LEN - 1, "\"%s\"", identity);
3335     if (ret < 0) {
3336         os_intrestore(int_save);
3337         wpa_error_log0(MSG_ERROR, "g_eap_identity snprintf_s failed");
3338         return EXT_WIFI_FAIL;
3339     }
3340     os_intrestore(int_save);
3341     return EXT_WIFI_OK;
3342 }
3343 
uapi_wifi_sta_ent_set_import_callback(ext_wifi_ent_import_callback * cb)3344 int uapi_wifi_sta_ent_set_import_callback(ext_wifi_ent_import_callback *cb)
3345 {
3346     int          ret;
3347     unsigned int int_save;
3348 
3349     os_intlock(&int_save);
3350     ret = tls_set_import_cb(cb);
3351     os_intrestore(int_save);
3352     return ret;
3353 }
3354 #endif /* LOS_CONFIG_WPA_ENTERPRISE */
3355 
3356