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 "wpa_supplicant/config.h"
31 #include "common/defs.h"
32 #include "v1_1/iwpa_callback.h"
33 #include "v1_1/iwpa_interface.h"
34
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
SplitCmdString(const char * startCmd,struct StWpaMainParam * pParam)44 static void SplitCmdString(const char *startCmd, struct StWpaMainParam *pParam)
45 {
46 if (pParam == NULL) {
47 return;
48 }
49 if (startCmd == NULL) {
50 pParam->argc = 0;
51 return;
52 }
53 const char *p = startCmd;
54 int i = 0;
55 int j = 0;
56 while (*p != '\0') {
57 if (*p == ' ') {
58 if (j <= MAX_WPA_MAIN_ARGV_LEN - 1) {
59 pParam->argv[i][j] = '\0';
60 } else {
61 pParam->argv[i][MAX_WPA_MAIN_ARGV_LEN - 1] = '\0';
62 }
63 ++i;
64 j = 0;
65 if (i >= MAX_WPA_MAIN_ARGC_NUM) {
66 break;
67 }
68 } else {
69 if (j < MAX_WPA_MAIN_ARGV_LEN - 1) {
70 pParam->argv[i][j] = *p;
71 ++j;
72 }
73 }
74 ++p;
75 }
76 if (i >= MAX_WPA_MAIN_ARGC_NUM) {
77 pParam->argc = MAX_WPA_MAIN_ARGC_NUM;
78 } else {
79 pParam->argc = i + 1;
80 }
81 return;
82 }
83
WpaThreadMain(void * p)84 static void *WpaThreadMain(void *p)
85 {
86 const char *startCmd;
87 struct StWpaMainParam param = {0};
88 char *tmpArgv[MAX_WPA_MAIN_ARGC_NUM] = {0};
89
90 if (p == NULL) {
91 HDF_LOGE("%{public}s: input parameter invalid!", __func__);
92 return NULL;
93 }
94 startCmd = (const char *)p;
95 SplitCmdString(startCmd, ¶m);
96 for (int i = 0; i < param.argc; i++) {
97 tmpArgv[i] = param.argv[i];
98 }
99 int ret = wpa_main(param.argc, tmpArgv);
100 HDF_LOGI("%{public}s: run wpa_main ret:%{public}d.", __func__, ret);
101 g_tid = 0;
102 return NULL;
103 }
104
MacToStr(const u8 * addr)105 const char *MacToStr(const u8 *addr)
106 {
107 const int macAddrIndexOne = 0;
108 const int macAddrIndexTwo = 1;
109 const int macAddrIndexThree = 2;
110 const int macAddrIndexFour = 3;
111 const int macAddrIndexFive = 4;
112 const int macAddrIndexSix = 5;
113 static char macToStr[WIFI_BSSID_LENGTH];
114 if (snprintf_s(macToStr, sizeof(macToStr), sizeof(macToStr)-1, "%02x:%02x:%02x:%02x:%02x:%02x",
115 addr[macAddrIndexOne], addr[macAddrIndexTwo], addr[macAddrIndexThree], addr[macAddrIndexFour],
116 addr[macAddrIndexFive], addr[macAddrIndexSix]) < 0) {
117 return NULL;
118 }
119 return macToStr;
120 }
121
FillData(uint8_t ** dst,uint32_t * dstLen,uint8_t * src,uint32_t srcLen)122 int32_t FillData(uint8_t **dst, uint32_t *dstLen, uint8_t *src, uint32_t srcLen)
123 {
124 if (src == NULL || dst == NULL || dstLen == NULL) {
125 HDF_LOGE("%{public}s: Invalid parameter!", __func__);
126 return HDF_ERR_INVALID_PARAM;
127 }
128 HDF_LOGD("%{public}s: srcLen =%{public}d ", __func__, srcLen);
129 if (srcLen > 0) {
130 *dst = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * srcLen);
131 if (*dst == NULL) {
132 HDF_LOGE("%{public}s: OsalMemCalloc fail!", __func__);
133 return HDF_FAILURE;
134 }
135 if (memcpy_s(*dst, srcLen, src, srcLen) != EOK) {
136 HDF_LOGE("%{public}s: memcpy_s fail!", __func__);
137 OsalMemFree(*dst);
138 *dst = NULL;
139 return HDF_FAILURE;
140 }
141 }
142 *dstLen = srcLen;
143 return HDF_SUCCESS;
144 }
145
HdfWpaStubDriver(void)146 struct HdfWpaStubData *HdfWpaStubDriver(void)
147 {
148 static struct HdfWpaStubData registerManager;
149 return ®isterManager;
150 }
151
HdfWpaDelRemoteObj(struct IWpaCallback * self)152 void HdfWpaDelRemoteObj(struct IWpaCallback *self)
153 {
154 struct HdfWpaRemoteNode *pos = NULL;
155 struct HdfWpaRemoteNode *tmp = NULL;
156 struct DListHead *head = &HdfWpaStubDriver()->remoteListHead;
157
158 DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, head, struct HdfWpaRemoteNode, node) {
159 if (pos->service->index == self->AsObject(self)->index) {
160 DListRemove(&(pos->node));
161 IWpaCallbackRelease(pos->callbackObj);
162 OsalMemFree(pos);
163 pos = NULL;
164 break;
165 }
166 }
167 IWpaCallbackRelease(self);
168 }
169
StartWpaSupplicant(const char * moduleName,const char * startCmd)170 static int32_t StartWpaSupplicant(const char *moduleName, const char *startCmd)
171 {
172 int32_t ret;
173 int32_t times = 0;
174
175 if (moduleName == NULL || startCmd == NULL) {
176 HDF_LOGE("%{public}s input parameter invalid!", __func__);
177 return HDF_ERR_INVALID_PARAM ;
178 }
179 while (g_tid != 0) {
180 HDF_LOGI("%{public}s: wpa_supplicant is already running!", __func__);
181 usleep(WPA_SLEEP_TIME);
182 times++;
183 if (times > MAX_WPA_WAIT_TIMES) {
184 HDF_LOGE("%{public}s: wait supplicant time out!", __func__);
185 return HDF_FAILURE;
186 }
187 }
188 ret = pthread_create(&g_tid, NULL, WpaThreadMain, (void *)startCmd);
189 if (ret != HDF_SUCCESS) {
190 HDF_LOGE("%{public}s: Create wpa thread failed, error code: %{public}d", __func__, ret);
191 return HDF_FAILURE;
192 }
193 pthread_setname_np(g_tid, "WpaMainThread");
194 HDF_LOGI("%{public}s: pthread_create successfully.", __func__);
195 usleep(WPA_SLEEP_TIME);
196 WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
197 if (pWpaInterface == NULL) {
198 HDF_LOGE("Get wpa interface failed!");
199 return HDF_FAILURE;
200 }
201 if (pWpaInterface->wpaCliConnect(pWpaInterface) < 0) {
202 HDF_LOGE("Failed to connect to wpa!");
203 return HDF_FAILURE;
204 }
205 return HDF_SUCCESS;
206 }
207
WpaInterfaceStart(struct IWpaInterface * self)208 int32_t WpaInterfaceStart(struct IWpaInterface *self)
209 {
210 int32_t ret;
211
212 (void)self;
213 HDF_LOGI("enter %{public}s: wpa_supplicant begin to start", __func__);
214 InitWifiWpaGlobalInterface();
215 WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
216 if (pWpaInterface == NULL) {
217 HDF_LOGI("fail get global interface");
218 return HDF_FAILURE;
219 }
220 pthread_mutex_lock(GetInterfaceLock());
221 ret = StartWpaSupplicant(WPA_SUPPLICANT_NAME, START_CMD);
222 if (ret != HDF_SUCCESS) {
223 HDF_LOGE("%{public}s: StartWpaSupplicant failed, error code: %{public}d", __func__, ret);
224 pthread_mutex_unlock(GetInterfaceLock());
225 return HDF_FAILURE;
226 }
227 pthread_mutex_unlock(GetInterfaceLock());
228 HDF_LOGI("%{public}s: wpa_supplicant start successfully!", __func__);
229 return HDF_SUCCESS;
230 }
231
StopWpaSupplicant(void)232 static int32_t StopWpaSupplicant(void)
233 {
234 /*Do nothing here,waiting for IWpaInterfaceReleaseInstance to destroy the wpa service. */
235 WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
236 if (pWpaInterface == NULL) {
237 HDF_LOGE("%{public}s: Get wpa global interface failed!", __func__);
238 return HDF_FAILURE;
239 }
240 int ret = pWpaInterface->wpaCliTerminate();
241 if (ret != 0) {
242 HDF_LOGE("%{public}s: wpaCliTerminate failed!", __func__);
243 } else {
244 HDF_LOGI("%{public}s: wpaCliTerminate suc!", __func__);
245 }
246 return HDF_SUCCESS;
247 }
248
WpaInterfaceStop(struct IWpaInterface * self)249 int32_t WpaInterfaceStop(struct IWpaInterface *self)
250 {
251 int32_t ret;
252 int32_t times = 0;
253
254 (void)self;
255 pthread_mutex_lock(GetInterfaceLock());
256 HDF_LOGI("enter %{public}s: wpa_supplicant begin to stop", __func__);
257 ret = StopWpaSupplicant();
258 if (ret != HDF_SUCCESS) {
259 HDF_LOGE("%{public}s: Wifi stop failed, error code: %{public}d", __func__, ret);
260 pthread_mutex_unlock(GetInterfaceLock());
261 return HDF_FAILURE;
262 }
263 while (g_tid != 0) {
264 HDF_LOGI("%{public}s: wpa_supplicant is not stop!", __func__);
265 usleep(WPA_SLEEP_TIME);
266 times++;
267 if (times > MAX_WPA_WAIT_TIMES) {
268 HDF_LOGE("%{public}s: wait supplicant stop time out!", __func__);
269 break;
270 }
271 }
272 ReleaseWifiStaInterface(0);
273 pthread_mutex_unlock(GetInterfaceLock());
274 HDF_LOGI("%{public}s: wpa_supplicant stop successfully!", __func__);
275 return HDF_SUCCESS;
276 }
277
WpaInterfaceAddWpaIface(struct IWpaInterface * self,const char * ifName,const char * confName)278 int32_t WpaInterfaceAddWpaIface(struct IWpaInterface *self, const char *ifName, const char *confName)
279 {
280 (void)self;
281 if (ifName == NULL || confName == NULL) {
282 HDF_LOGE("%{public}s input parameter invalid!", __func__);
283 return HDF_ERR_INVALID_PARAM ;
284 }
285 HDF_LOGI("enter %{public}s Ready to add iface, ifName: %{public}s, confName: %{public}s",
286 __func__, ifName, confName);
287 pthread_mutex_lock(GetInterfaceLock());
288 WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
289 if (pWpaInterface == NULL) {
290 pthread_mutex_unlock(GetInterfaceLock());
291 return HDF_FAILURE;
292 }
293 AddInterfaceArgv addInterface = {0};
294 if (strncmp(ifName, "wlan", strlen("wlan")) == 0) {
295 if (strcpy_s(addInterface.name, sizeof(addInterface.name) - 1, ifName) != EOK ||
296 strcpy_s(addInterface.confName, sizeof(addInterface.confName) - 1,
297 CONFIG_ROOR_DIR"/wpa_supplicant/wpa_supplicant.conf") != EOK) {
298 pthread_mutex_unlock(GetInterfaceLock());
299 return HDF_FAILURE;
300 }
301 } else if (strncmp(ifName, "p2p", strlen("p2p")) == 0) {
302 if (strcpy_s(addInterface.name, sizeof(addInterface.name) - 1, ifName) != EOK ||
303 strcpy_s(addInterface.confName, sizeof(addInterface.confName) - 1,
304 CONFIG_ROOR_DIR"/wpa_supplicant/p2p_supplicant.conf") != EOK) {
305 pthread_mutex_unlock(GetInterfaceLock());
306 return HDF_FAILURE;
307 }
308 } else if (strncmp(ifName, "chba0", strlen("chba0")) == 0) {
309 if (strcpy_s(addInterface.name, sizeof(addInterface.name) - 1, ifName) != EOK ||
310 strcpy_s(addInterface.confName, sizeof(addInterface.confName) - 1,
311 CONFIG_ROOR_DIR"/wpa_supplicant/p2p_supplicant.conf") != EOK) {
312 pthread_mutex_unlock(GetInterfaceLock());
313 return HDF_FAILURE;
314 }
315 } else {
316 pthread_mutex_unlock(GetInterfaceLock());
317 return HDF_FAILURE;
318 }
319 if (pWpaInterface->wpaCliAddIface(pWpaInterface, &addInterface, true) < 0) {
320 pthread_mutex_unlock(GetInterfaceLock());
321 return HDF_FAILURE;
322 }
323 pthread_mutex_unlock(GetInterfaceLock());
324 HDF_LOGI("%{public}s Add interface finish", __func__);
325 return HDF_SUCCESS;
326 }
327
WpaInterfaceRemoveWpaIface(struct IWpaInterface * self,const char * ifName)328 int32_t WpaInterfaceRemoveWpaIface(struct IWpaInterface *self, const char *ifName)
329 {
330 (void)self;
331 HDF_LOGI("enter %{public}s", __func__);
332 if (ifName == NULL) {
333 HDF_LOGE("%{public}s input parameter invalid!", __func__);
334 return HDF_ERR_INVALID_PARAM ;
335 }
336 HDF_LOGI("enter %{public}s Ready to Remove iface, ifName: %{public}s", __func__, ifName);
337 int ret = -1;
338 pthread_mutex_lock(GetInterfaceLock());
339 WifiWpaInterface *pWpaInterface = GetWifiWpaGlobalInterface();
340 if (pWpaInterface == NULL) {
341 HDF_LOGE("Get wpa interface failed!");
342 pthread_mutex_unlock(GetInterfaceLock());
343 return HDF_FAILURE;
344 }
345 ret = pWpaInterface->wpaCliRemoveIface(pWpaInterface, ifName);
346 pthread_mutex_unlock(GetInterfaceLock());
347 HDF_LOGI("%{public}s Remove wpa iface finish, ifName: %{public}s ret = %{public}d", __func__, ifName, ret);
348 return (ret == 0 ? HDF_SUCCESS : HDF_FAILURE);
349 }
350
WpaInterfaceScan(struct IWpaInterface * self,const char * ifName)351 int32_t WpaInterfaceScan(struct IWpaInterface *self, const char *ifName)
352 {
353 (void)self;
354 HDF_LOGI("enter %{public}s", __func__);
355 if (ifName == NULL) {
356 HDF_LOGE("%{public}s: input parameter invalid!", __func__);
357 return HDF_ERR_INVALID_PARAM;
358 }
359 pthread_mutex_lock(GetInterfaceLock());
360 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(ifName);
361 if (pStaIfc == NULL) {
362 pthread_mutex_unlock(GetInterfaceLock());
363 HDF_LOGE("%{public}s: pStaIfc = NULL", __func__);
364 return HDF_FAILURE;
365 }
366 ScanSettings settings = {0};
367 settings.scanStyle = SCAN_TYPE_LOW_SPAN;
368 int ret = pStaIfc->wpaCliCmdScan(pStaIfc, &settings);
369 if (ret < 0) {
370 pthread_mutex_unlock(GetInterfaceLock());
371 HDF_LOGE("%{public}s: StartScan fail! ret = %{public}d", __func__, ret);
372 return HDF_FAILURE;
373 }
374 if (ret == WIFI_HAL_SCAN_BUSY) {
375 pthread_mutex_unlock(GetInterfaceLock());
376 HDF_LOGE("%{public}s: StartScan return scan busy", __func__);
377 return HDF_FAILURE;
378 }
379 pthread_mutex_unlock(GetInterfaceLock());
380 HDF_LOGI("%{public}s: StartScan successfully!", __func__);
381 return HDF_SUCCESS;
382 }
383
WpaInterfaceScanResult(struct IWpaInterface * self,const char * ifName,unsigned char * resultBuf,uint32_t * resultBufLen)384 int32_t WpaInterfaceScanResult(struct IWpaInterface *self, const char *ifName, unsigned char *resultBuf,
385 uint32_t *resultBufLen)
386 {
387 HDF_LOGI("enter %{public}s", __func__);
388 (void)self;
389 if (ifName == NULL || resultBuf == NULL) {
390 HDF_LOGE("%{public}s: input parameter invalid!", __func__);
391 return HDF_ERR_INVALID_PARAM;
392 }
393 pthread_mutex_lock(GetInterfaceLock());
394 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(ifName);
395 if (pStaIfc == NULL) {
396 pthread_mutex_unlock(GetInterfaceLock());
397 HDF_LOGE("%{public}s: pStaIfc = NULL", __func__);
398 return HDF_FAILURE;
399 }
400 int ret = pStaIfc->wpaCliCmdScanInfo(pStaIfc, resultBuf, resultBufLen);
401 if (ret < 0) {
402 pthread_mutex_unlock(GetInterfaceLock());
403 HDF_LOGE("%{public}s: WpaCliCmdScanInfo2 fail! ret = %{public}d", __func__, ret);
404 return HDF_FAILURE;
405 }
406 pthread_mutex_unlock(GetInterfaceLock());
407 HDF_LOGI("%{public}s: Get scan result successfully!", __func__);
408 return HDF_SUCCESS;
409 }