• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "wpa_common_cmd.h"
16 #include "wpa_p2p_cmd.h"
17 #include "hdi_wpa_hal.h"
18 #include <securec.h>
19 #include <hdf_base.h>
20 #include <hdf_log.h>
21 #include <osal_time.h>
22 #include <osal_mem.h>
23 #include <arpa/inet.h>
24 #include "utils/common.h"
25 #include "wpa_supplicant_i.h"
26 #include "ctrl_iface.h"
27 #include "main.h"
28 #include "wps_supplicant.h"
29 #include "bssid_ignore.h"
30 #include "config.h"
31 #include "common/defs.h"
32 #include "v2_0/iwpa_callback.h"
33 #include "v2_0/iwpa_interface.h"
34 #include <dirent.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <dlfcn.h>
38 #include <string.h>
39 #include "hdi_wpa_common.h"
40 
41 pthread_t g_tid;
42 #define MAX_WPA_WAIT_TIMES 30
43 #define CTRL_LEN 128
44 
SplitCmdString(const char * startCmd,struct StWpaMainParam * pParam)45 static void SplitCmdString(const char *startCmd, struct StWpaMainParam *pParam)
46 {
47     if (pParam == NULL) {
48         return;
49     }
50     if (startCmd == NULL) {
51         pParam->argc = 0;
52         return;
53     }
54     const char *p = startCmd;
55     int i = 0;
56     int j = 0;
57     while (*p != '\0') {
58         if (*p == ' ') {
59             if (j <= MAX_WPA_MAIN_ARGV_LEN - 1) {
60                 pParam->argv[i][j] = '\0';
61             } else {
62                 pParam->argv[i][MAX_WPA_MAIN_ARGV_LEN - 1] = '\0';
63             }
64             ++i;
65             j = 0;
66             if (i >= MAX_WPA_MAIN_ARGC_NUM) {
67                 break;
68             }
69         } else {
70             if (j < MAX_WPA_MAIN_ARGV_LEN - 1) {
71                 pParam->argv[i][j] = *p;
72                 ++j;
73             }
74         }
75         ++p;
76     }
77     if (i >= MAX_WPA_MAIN_ARGC_NUM) {
78         pParam->argc = MAX_WPA_MAIN_ARGC_NUM;
79     } else {
80         pParam->argc = i + 1;
81     }
82     return;
83 }
84 
WpaThreadMain(void * p)85 static void *WpaThreadMain(void *p)
86 {
87     const char *startCmd;
88     struct StWpaMainParam param = {0};
89     char *tmpArgv[MAX_WPA_MAIN_ARGC_NUM] = {0};
90 
91     if (p == NULL) {
92         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
93         return NULL;
94     }
95     startCmd = (const char *)p;
96     SplitCmdString(startCmd, &param);
97     for (int i = 0; i < param.argc; i++) {
98         tmpArgv[i] = param.argv[i];
99     }
100     int ret = wpa_main(param.argc, tmpArgv);
101     HDF_LOGI("%{public}s: run wpa_main ret:%{public}d.", __func__, ret);
102     g_tid = 0;
103     return NULL;
104 }
105 
MacToStr(const u8 * addr)106 const char *MacToStr(const u8 *addr)
107 {
108     const int macAddrIndexOne = 0;
109     const int macAddrIndexTwo = 1;
110     const int macAddrIndexThree = 2;
111     const int macAddrIndexFour = 3;
112     const int macAddrIndexFive = 4;
113     const int macAddrIndexSix = 5;
114     static char macToStr[WIFI_BSSID_LENGTH];
115     if (snprintf_s(macToStr, sizeof(macToStr), sizeof(macToStr)-1, "%02x:%02x:%02x:%02x:%02x:%02x",
116         addr[macAddrIndexOne], addr[macAddrIndexTwo], addr[macAddrIndexThree], addr[macAddrIndexFour],
117         addr[macAddrIndexFive], addr[macAddrIndexSix]) < 0) {
118         return NULL;
119     }
120     return macToStr;
121 }
122 
FillData(uint8_t ** dst,uint32_t * dstLen,uint8_t * src,uint32_t srcLen)123 int32_t FillData(uint8_t **dst, uint32_t *dstLen, uint8_t *src, uint32_t srcLen)
124 {
125     if (src == NULL || dst == NULL || dstLen == NULL) {
126         HDF_LOGE("%{public}s: Invalid parameter!", __func__);
127         return HDF_ERR_INVALID_PARAM;
128     }
129     HDF_LOGD("%{public}s: srcLen =%{public}d ", __func__, srcLen);
130     if (srcLen > 0) {
131         *dst = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * srcLen);
132         if (*dst == NULL) {
133             HDF_LOGE("%{public}s: OsalMemCalloc fail!", __func__);
134             return HDF_FAILURE;
135         }
136         if (memcpy_s(*dst, srcLen, src, srcLen) != EOK) {
137             HDF_LOGE("%{public}s: memcpy_s fail!", __func__);
138             OsalMemFree(*dst);
139             *dst = NULL;
140             return HDF_FAILURE;
141         }
142     }
143     *dstLen = srcLen;
144     return HDF_SUCCESS;
145 }
146 
HdfWpaStubDriver(void)147 struct HdfWpaStubData *HdfWpaStubDriver(void)
148 {
149     static struct HdfWpaStubData registerManager;
150     return &registerManager;
151 }
152 
HdfWpaDelRemoteObj(struct IWpaCallback * self)153 void HdfWpaDelRemoteObj(struct IWpaCallback *self)
154 {
155     struct HdfWpaRemoteNode *pos = NULL;
156     struct HdfWpaRemoteNode *tmp = NULL;
157     struct DListHead *head = &HdfWpaStubDriver()->remoteListHead;
158 
159     DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, head, struct HdfWpaRemoteNode, node) {
160         if (pos->service->index == self->AsObject(self)->index) {
161             DListRemove(&(pos->node));
162             IWpaCallbackRelease(pos->callbackObj);
163             OsalMemFree(pos);
164             pos = NULL;
165             break;
166         }
167     }
168     IWpaCallbackRelease(self);
169 }
170 
StartWpaSupplicant(const char * moduleName,const char * startCmd)171 static int32_t StartWpaSupplicant(const char *moduleName, const char *startCmd)
172 {
173     int32_t ret;
174     int32_t times = 0;
175 
176     if (moduleName == NULL || startCmd == NULL) {
177         HDF_LOGE("%{public}s input parameter invalid!", __func__);
178         return HDF_ERR_INVALID_PARAM ;
179     }
180     while (g_tid != 0) {
181         HDF_LOGI("%{public}s: wpa_supplicant is already running!", __func__);
182         usleep(WPA_SLEEP_TIME);
183         times++;
184         if (times > MAX_WPA_WAIT_TIMES) {
185             HDF_LOGE("%{public}s: wait supplicant time out!", __func__);
186             return HDF_FAILURE;
187         }
188     }
189     ret = pthread_create(&g_tid, NULL, WpaThreadMain, (void *)startCmd);
190     if (ret != HDF_SUCCESS) {
191         HDF_LOGE("%{public}s: Create wpa thread failed, error code: %{public}d", __func__, ret);
192         return HDF_FAILURE;
193     }
194     pthread_setname_np(g_tid, "WpaMainThread");
195     HDF_LOGI("%{public}s: pthread_create successfully.", __func__);
196     usleep(WPA_SLEEP_TIME);
197     WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
198     if (pWpaInterface == NULL) {
199         HDF_LOGE("Get wpa interface failed!");
200         return HDF_FAILURE;
201     }
202     if (pWpaInterface->wpaCliConnect(pWpaInterface) < 0) {
203         HDF_LOGE("Failed to connect to wpa!");
204         return HDF_FAILURE;
205     }
206     return HDF_SUCCESS;
207 }
208 
HdfWpaInterfaceDriverInit(void)209 static void HdfWpaInterfaceDriverInit(void)
210 {
211     static bool flag = false;
212     if (!flag) {
213         HDF_LOGI("HdfWpaInterfaceDriverInit enter.");
214         DListHeadInit(&(HdfWpaStubDriver()->remoteListHead));
215         if (OsalMutexInit(&(HdfWpaStubDriver()->mutex)) != HDF_SUCCESS) {
216             HDF_LOGE("%{public}s: Mutex init failed", __func__);
217         }
218         flag = true;
219     }
220 }
221 
GetWpaStartCmd(void)222 static const char *GetWpaStartCmd(void)
223 {
224     if (IsUpdaterMode()) {
225         HdfWpaInterfaceDriverInit();
226         HDF_LOGI("updater mode");
227         return START_CMD_UPDATER;
228     }
229     return START_CMD;
230 }
231 
RemoveLostCtrl(void)232 static void RemoveLostCtrl(void)
233 {
234     DIR *dir = NULL;
235     char path[CTRL_LEN];
236     struct dirent *entry;
237 
238     dir = opendir(CONFIG_ROOR_DIR);
239     if (dir == NULL) {
240         HDF_LOGE("can not open wifi dir");
241         return;
242     }
243     while ((entry = readdir(dir)) != NULL) {
244         if (strncmp(entry->d_name, "wpa_ctrl_", strlen("wpa_ctrl_")) != 0) {
245             continue;
246         }
247         int ret = sprintf_s(path, sizeof(path), "%s/%s", CONFIG_ROOR_DIR, entry->d_name);
248         if (ret == -1) {
249             HDF_LOGE("sprintf_s dir name fail");
250             break;
251         }
252         if (entry->d_type != DT_DIR) {
253             remove(path);
254         }
255     }
256     closedir(dir);
257 }
258 
WpaInterfaceStart(struct IWpaInterface * self)259 int32_t WpaInterfaceStart(struct IWpaInterface *self)
260 {
261     int32_t ret;
262 
263     (void)self;
264     HDF_LOGI("enter %{public}s: wpa_supplicant begin to start", __func__);
265     InitWifiWpaGlobalInterface();
266     WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
267     if (pWpaInterface == NULL) {
268         HDF_LOGI("fail get global interface");
269         return HDF_FAILURE;
270     }
271     pthread_mutex_lock(GetInterfaceLock());
272     RemoveLostCtrl();
273     ret = StartWpaSupplicant(WPA_SUPPLICANT_NAME, GetWpaStartCmd());
274     if (ret != HDF_SUCCESS) {
275         HDF_LOGE("%{public}s: StartWpaSupplicant failed, error code: %{public}d", __func__, ret);
276         pthread_mutex_unlock(GetInterfaceLock());
277         return HDF_FAILURE;
278     }
279     pthread_mutex_unlock(GetInterfaceLock());
280     HDF_LOGI("%{public}s: wpa_supplicant start successfully!", __func__);
281     return HDF_SUCCESS;
282 }
283 
StopWpaSupplicant(void)284 static int32_t StopWpaSupplicant(void)
285 {
286     /*Do nothing here,waiting for IWpaInterfaceReleaseInstance to destroy the wpa service. */
287     WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
288     if (pWpaInterface == NULL) {
289         HDF_LOGE("%{public}s: Get wpa global interface failed!", __func__);
290         return HDF_FAILURE;
291     }
292     int ret = pWpaInterface->wpaCliTerminate();
293     if (ret != 0) {
294         HDF_LOGE("%{public}s: wpaCliTerminate failed!", __func__);
295     } else {
296         HDF_LOGI("%{public}s: wpaCliTerminate suc!", __func__);
297     }
298     return HDF_SUCCESS;
299 }
300 
WpaInterfaceStop(struct IWpaInterface * self)301 int32_t WpaInterfaceStop(struct IWpaInterface *self)
302 {
303     int32_t ret;
304     int32_t times = 0;
305 
306     (void)self;
307     pthread_mutex_lock(GetInterfaceLock());
308     HDF_LOGI("enter %{public}s: wpa_supplicant begin to stop", __func__);
309     ret = StopWpaSupplicant();
310     if (ret != HDF_SUCCESS) {
311         HDF_LOGE("%{public}s: Wifi stop failed, error code: %{public}d", __func__, ret);
312         pthread_mutex_unlock(GetInterfaceLock());
313         return HDF_FAILURE;
314     }
315     while (g_tid != 0) {
316         HDF_LOGI("%{public}s: wpa_supplicant is not stop!", __func__);
317         usleep(WPA_SLEEP_TIME);
318         times++;
319         if (times > MAX_WPA_WAIT_TIMES) {
320             HDF_LOGE("%{public}s: wait supplicant stop time out!", __func__);
321             break;
322         }
323     }
324     ReleaseWifiStaInterface(0);
325     pthread_mutex_unlock(GetInterfaceLock());
326     HDF_LOGI("%{public}s: wpa_supplicant stop successfully!", __func__);
327     return HDF_SUCCESS;
328 }
329 
GetWpaSupplicantConfPath(void)330 static const char *GetWpaSupplicantConfPath(void)
331 {
332     if (IsUpdaterMode()) {
333         HDF_LOGI("updater mode");
334         return CONFIG_ROOR_DIR_UPDATER"/wpa_supplicant/wpa_supplicant.conf";
335     }
336     return CONFIG_ROOR_DIR"/wpa_supplicant/wpa_supplicant.conf";
337 }
338 
WpaInterfaceAddWpaIface(struct IWpaInterface * self,const char * ifName,const char * confName)339 int32_t WpaInterfaceAddWpaIface(struct IWpaInterface *self, const char *ifName, const char *confName)
340 {
341     (void)self;
342     if (ifName == NULL || confName == NULL) {
343         HDF_LOGE("%{public}s input parameter invalid!", __func__);
344         return HDF_ERR_INVALID_PARAM ;
345     }
346     HDF_LOGI("enter %{public}s Ready to add iface, ifName: %{public}s, confName: %{public}s",
347         __func__, ifName, confName);
348     pthread_mutex_lock(GetInterfaceLock());
349     WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
350     if (pWpaInterface == NULL) {
351         pthread_mutex_unlock(GetInterfaceLock());
352         return HDF_FAILURE;
353     }
354     AddInterfaceArgv addInterface = {0};
355     if (strncmp(ifName, "wlan", strlen("wlan")) == 0) {
356         if (strcpy_s(addInterface.name, sizeof(addInterface.name) - 1, ifName) != EOK ||
357             strcpy_s(addInterface.confName, sizeof(addInterface.confName) - 1,
358             GetWpaSupplicantConfPath()) != EOK) {
359             pthread_mutex_unlock(GetInterfaceLock());
360             return HDF_FAILURE;
361         }
362     } else if (strncmp(ifName, "p2p", strlen("p2p")) == 0) {
363         if (strcpy_s(addInterface.name, sizeof(addInterface.name) - 1, ifName) != EOK ||
364             strcpy_s(addInterface.confName, sizeof(addInterface.confName) - 1,
365             CONFIG_ROOR_DIR"/wpa_supplicant/p2p_supplicant.conf") != EOK) {
366             pthread_mutex_unlock(GetInterfaceLock());
367             return HDF_FAILURE;
368         }
369     }  else if (strncmp(ifName, "chba0", strlen("chba0")) == 0) {
370         if (strcpy_s(addInterface.name, sizeof(addInterface.name) - 1, ifName) != EOK ||
371             strcpy_s(addInterface.confName, sizeof(addInterface.confName) - 1,
372                      CONFIG_ROOR_DIR"/wpa_supplicant/chba_supplicant.conf") != EOK) {
373             pthread_mutex_unlock(GetInterfaceLock());
374             return HDF_FAILURE;
375         }
376     } else {
377         pthread_mutex_unlock(GetInterfaceLock());
378         return HDF_FAILURE;
379     }
380     if (pWpaInterface->wpaCliAddIface(pWpaInterface, &addInterface, true) < 0) {
381         pthread_mutex_unlock(GetInterfaceLock());
382         return HDF_FAILURE;
383     }
384     pthread_mutex_unlock(GetInterfaceLock());
385     HDF_LOGI("%{public}s Add interface finish", __func__);
386     return HDF_SUCCESS;
387 }
388 
WpaInterfaceRemoveWpaIface(struct IWpaInterface * self,const char * ifName)389 int32_t WpaInterfaceRemoveWpaIface(struct IWpaInterface *self, const char *ifName)
390 {
391     (void)self;
392     HDF_LOGI("enter %{public}s", __func__);
393     if (ifName == NULL) {
394         HDF_LOGE("%{public}s input parameter invalid!", __func__);
395         return HDF_ERR_INVALID_PARAM ;
396     }
397     HDF_LOGI("enter %{public}s Ready to Remove iface, ifName: %{public}s", __func__, ifName);
398     int ret = -1;
399     pthread_mutex_lock(GetInterfaceLock());
400     WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
401     if (pWpaInterface == NULL) {
402         HDF_LOGE("Get wpa interface failed!");
403         pthread_mutex_unlock(GetInterfaceLock());
404         return HDF_FAILURE;
405     }
406     ret = pWpaInterface->wpaCliRemoveIface(pWpaInterface, ifName);
407     pthread_mutex_unlock(GetInterfaceLock());
408     HDF_LOGI("%{public}s Remove wpa iface finish, ifName: %{public}s ret = %{public}d", __func__, ifName, ret);
409     return (ret == 0 ? HDF_SUCCESS : HDF_FAILURE);
410 }
411 
WpaInterfaceScan(struct IWpaInterface * self,const char * ifName)412 int32_t WpaInterfaceScan(struct IWpaInterface *self, const char *ifName)
413 {
414     (void)self;
415     HDF_LOGI("enter %{public}s", __func__);
416     if (ifName == NULL) {
417         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
418         return HDF_ERR_INVALID_PARAM;
419     }
420     pthread_mutex_lock(GetInterfaceLock());
421     WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(ifName);
422     if (pStaIfc == NULL) {
423         pthread_mutex_unlock(GetInterfaceLock());
424         HDF_LOGE("%{public}s: pStaIfc = NULL", __func__);
425         return HDF_FAILURE;
426     }
427     ScanSettings settings = {0};
428     settings.scanStyle = SCAN_TYPE_LOW_SPAN;
429     int ret = pStaIfc->wpaCliCmdScan(pStaIfc, &settings);
430     if (ret < 0) {
431         pthread_mutex_unlock(GetInterfaceLock());
432         HDF_LOGE("%{public}s: StartScan fail! ret = %{public}d", __func__, ret);
433         return HDF_FAILURE;
434     }
435     if (ret == WIFI_HAL_SCAN_BUSY) {
436         pthread_mutex_unlock(GetInterfaceLock());
437         HDF_LOGE("%{public}s: StartScan return scan busy", __func__);
438         return HDF_FAILURE;
439     }
440     pthread_mutex_unlock(GetInterfaceLock());
441     HDF_LOGI("%{public}s: StartScan successfully!", __func__);
442     return HDF_SUCCESS;
443 }
444 
WpaInterfaceScanResult(struct IWpaInterface * self,const char * ifName,unsigned char * resultBuf,uint32_t * resultBufLen)445 int32_t WpaInterfaceScanResult(struct IWpaInterface *self, const char *ifName, unsigned char *resultBuf,
446     uint32_t *resultBufLen)
447 {
448     HDF_LOGI("enter %{public}s", __func__);
449     (void)self;
450     if (ifName == NULL || resultBuf == NULL) {
451         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
452         return HDF_ERR_INVALID_PARAM;
453     }
454     pthread_mutex_lock(GetInterfaceLock());
455     WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(ifName);
456     if (pStaIfc == NULL) {
457         pthread_mutex_unlock(GetInterfaceLock());
458         HDF_LOGE("%{public}s: pStaIfc = NULL", __func__);
459         return HDF_FAILURE;
460     }
461     int ret = pStaIfc->wpaCliCmdScanInfo(pStaIfc, resultBuf, resultBufLen);
462     if (ret < 0) {
463         pthread_mutex_unlock(GetInterfaceLock());
464         HDF_LOGE("%{public}s: WpaCliCmdScanInfo2 fail! ret = %{public}d", __func__, ret);
465         return HDF_FAILURE;
466     }
467     pthread_mutex_unlock(GetInterfaceLock());
468     HDF_LOGI("%{public}s: Get scan result successfully!", __func__);
469     return HDF_SUCCESS;
470 }