1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @brief File containing WPA supplicant interface specific definitions for the
9  * Zephyr OS layer of the Wi-Fi driver.
10  */
11 #include <stdlib.h>
12 
13 #include <zephyr/device.h>
14 #include <zephyr/logging/log.h>
15 
16 #include "fmac_main.h"
17 #include "fmac_util.h"
18 #include "wifi_mgmt.h"
19 #include "wpa_supp_if.h"
20 
21 LOG_MODULE_DECLARE(wifi_nrf, CONFIG_WIFI_NRF70_LOG_LEVEL);
22 
23 K_SEM_DEFINE(wait_for_event_sem, 0, 1);
24 K_SEM_DEFINE(wait_for_scan_resp_sem, 0, 1);
25 static K_MUTEX_DEFINE(mgmt_tx_lock);
26 
27 #define ACTION_FRAME_RESP_TIMEOUT_MS 5000
28 #define SET_IFACE_EVENT_TIMEOUT_MS 5000
29 #define CARR_ON_TIMEOUT_MS 5000
30 
get_nrf_wifi_auth_type(int wpa_auth_alg)31 static int get_nrf_wifi_auth_type(int wpa_auth_alg)
32 {
33 	if (wpa_auth_alg & WPA_AUTH_ALG_OPEN) {
34 		return NRF_WIFI_AUTHTYPE_OPEN_SYSTEM;
35 	}
36 	if (wpa_auth_alg & WPA_AUTH_ALG_SHARED) {
37 		return NRF_WIFI_AUTHTYPE_SHARED_KEY;
38 	}
39 	if (wpa_auth_alg & WPA_AUTH_ALG_LEAP) {
40 		return NRF_WIFI_AUTHTYPE_NETWORK_EAP;
41 	}
42 	if (wpa_auth_alg & WPA_AUTH_ALG_FT) {
43 		return NRF_WIFI_AUTHTYPE_FT;
44 	}
45 	if (wpa_auth_alg & WPA_AUTH_ALG_SAE) {
46 		return NRF_WIFI_AUTHTYPE_SAE;
47 	}
48 
49 	return NRF_WIFI_AUTHTYPE_MAX;
50 }
51 
wpa_alg_to_cipher_suite(enum wpa_alg alg,size_t key_len)52 static unsigned int wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
53 {
54 	switch (alg) {
55 	case WPA_ALG_WEP:
56 		if (key_len == 5) {
57 			return RSN_CIPHER_SUITE_WEP40;
58 		}
59 		return RSN_CIPHER_SUITE_WEP104;
60 	case WPA_ALG_TKIP:
61 		return RSN_CIPHER_SUITE_TKIP;
62 	case WPA_ALG_CCMP:
63 		return RSN_CIPHER_SUITE_CCMP;
64 	case WPA_ALG_GCMP:
65 		return RSN_CIPHER_SUITE_GCMP;
66 	case WPA_ALG_CCMP_256:
67 		return RSN_CIPHER_SUITE_CCMP_256;
68 	case WPA_ALG_GCMP_256:
69 		return RSN_CIPHER_SUITE_GCMP_256;
70 	case WPA_ALG_BIP_CMAC_128:
71 		return RSN_CIPHER_SUITE_AES_128_CMAC;
72 	case WPA_ALG_BIP_GMAC_128:
73 		return RSN_CIPHER_SUITE_BIP_GMAC_128;
74 	case WPA_ALG_BIP_GMAC_256:
75 		return RSN_CIPHER_SUITE_BIP_GMAC_256;
76 	case WPA_ALG_BIP_CMAC_256:
77 		return RSN_CIPHER_SUITE_BIP_CMAC_256;
78 	case WPA_ALG_SMS4:
79 		return RSN_CIPHER_SUITE_SMS4;
80 	case WPA_ALG_KRK:
81 		return RSN_CIPHER_SUITE_KRK;
82 	case WPA_ALG_NONE:
83 		LOG_ERR("%s: Unexpected encryption algorithm %d", __func__, alg);
84 		return 0;
85 	}
86 
87 	LOG_ERR("%s: Unsupported encryption algorithm %d", __func__, alg);
88 	return 0;
89 }
90 
drv2supp_chan_width(int width)91 static enum chan_width drv2supp_chan_width(int width)
92 {
93 	switch (width) {
94 	case NRF_WIFI_CHAN_WIDTH_20_NOHT:
95 		return CHAN_WIDTH_20_NOHT;
96 	case NRF_WIFI_CHAN_WIDTH_20:
97 		return CHAN_WIDTH_20;
98 	case NRF_WIFI_CHAN_WIDTH_40:
99 		return CHAN_WIDTH_40;
100 	case NRF_WIFI_CHAN_WIDTH_80:
101 		return CHAN_WIDTH_80;
102 	case NRF_WIFI_CHAN_WIDTH_80P80:
103 		return CHAN_WIDTH_80P80;
104 	case NRF_WIFI_CHAN_WIDTH_160:
105 		return CHAN_WIDTH_160;
106 	default:
107 		break;
108 	}
109 	return CHAN_WIDTH_UNKNOWN;
110 }
111 
nrf_wifi_wpa_supp_event_proc_scan_start(void * if_priv)112 void nrf_wifi_wpa_supp_event_proc_scan_start(void *if_priv)
113 {
114 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
115 
116 	vif_ctx_zep = if_priv;
117 
118 	if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.scan_start) {
119 		vif_ctx_zep->supp_callbk_fns.scan_start(vif_ctx_zep->supp_drv_if_ctx);
120 	}
121 }
122 
nrf_wifi_wpa_supp_event_proc_scan_done(void * if_priv,struct nrf_wifi_umac_event_trigger_scan * scan_done_event,unsigned int event_len,int aborted)123 void nrf_wifi_wpa_supp_event_proc_scan_done(void *if_priv,
124 					struct nrf_wifi_umac_event_trigger_scan *scan_done_event,
125 					unsigned int event_len,
126 					int aborted)
127 {
128 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
129 	union wpa_event_data event;
130 	struct scan_info *info = NULL;
131 
132 	vif_ctx_zep = if_priv;
133 
134 	memset(&event, 0, sizeof(event));
135 
136 	info = &event.scan_info;
137 
138 	info->aborted = aborted;
139 	info->external_scan = 0;
140 	info->nl_scan_event = 1;
141 
142 	if (vif_ctx_zep->supp_drv_if_ctx &&
143 		vif_ctx_zep->supp_callbk_fns.scan_done) {
144 		vif_ctx_zep->supp_callbk_fns.scan_done(vif_ctx_zep->supp_drv_if_ctx,
145 			&event);
146 	}
147 	k_work_cancel_delayable(&vif_ctx_zep->scan_timeout_work);
148 	vif_ctx_zep->scan_in_progress = false;
149 	k_sem_give(&wait_for_scan_resp_sem);
150 }
151 
nrf_wifi_wpa_supp_event_proc_scan_res(void * if_priv,struct nrf_wifi_umac_event_new_scan_results * scan_res,unsigned int event_len,bool more_res)152 void nrf_wifi_wpa_supp_event_proc_scan_res(void *if_priv,
153 					struct nrf_wifi_umac_event_new_scan_results *scan_res,
154 					unsigned int event_len,
155 					bool more_res)
156 {
157 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
158 	struct wpa_scan_res *r = NULL;
159 	const unsigned char *ie = NULL;
160 	const unsigned char *beacon_ie = NULL;
161 	unsigned int ie_len = 0;
162 	unsigned int beacon_ie_len = 0;
163 	unsigned char *pos = NULL;
164 
165 	vif_ctx_zep = if_priv;
166 
167 	if (scan_res->valid_fields & NRF_WIFI_EVENT_NEW_SCAN_RESULTS_IES_VALID) {
168 		ie = scan_res->ies;
169 		ie_len = scan_res->ies_len;
170 	}
171 
172 	if (scan_res->valid_fields & NRF_WIFI_EVENT_NEW_SCAN_RESULTS_BEACON_IES_VALID) {
173 		beacon_ie = scan_res->ies + scan_res->ies_len;
174 		beacon_ie_len = scan_res->beacon_ies_len;
175 	}
176 
177 	r = k_calloc(sizeof(*r) + ie_len + beacon_ie_len, sizeof(char));
178 
179 	if (!r) {
180 		LOG_ERR("%s: Unable to allocate memory for scan result", __func__);
181 		return;
182 	}
183 
184 	if (scan_res->valid_fields & NRF_WIFI_EVENT_NEW_SCAN_RESULTS_MAC_ADDR_VALID) {
185 		memcpy(r->bssid, scan_res->mac_addr, ETH_ALEN);
186 	}
187 
188 	r->freq = scan_res->frequency;
189 
190 	if (scan_res->valid_fields & NRF_WIFI_EVENT_NEW_SCAN_RESULTS_BEACON_INTERVAL_VALID) {
191 		r->beacon_int = scan_res->beacon_interval;
192 	}
193 
194 	r->caps = scan_res->capability;
195 
196 	r->flags |= WPA_SCAN_NOISE_INVALID;
197 
198 	if (scan_res->signal.signal_type == NRF_WIFI_SIGNAL_TYPE_MBM) {
199 		r->level = scan_res->signal.signal.mbm_signal;
200 		r->level /= 100; /* mBm to dBm */
201 		r->flags |= (WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID);
202 	} else if (scan_res->signal.signal_type == NRF_WIFI_SIGNAL_TYPE_UNSPEC) {
203 		r->level = scan_res->signal.signal.unspec_signal;
204 		r->flags |= WPA_SCAN_QUAL_INVALID;
205 	} else {
206 		r->flags |= (WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID);
207 	}
208 
209 	if (scan_res->valid_fields & NRF_WIFI_EVENT_NEW_SCAN_RESULTS_IES_TSF_VALID) {
210 		r->tsf = scan_res->ies_tsf;
211 	}
212 
213 	if (scan_res->valid_fields & NRF_WIFI_EVENT_NEW_SCAN_RESULTS_BEACON_IES_TSF_VALID) {
214 		if (scan_res->beacon_ies_tsf > r->tsf) {
215 			r->tsf = scan_res->beacon_ies_tsf;
216 		}
217 	}
218 
219 	if (scan_res->seen_ms_ago) {
220 		r->age = scan_res->seen_ms_ago;
221 	}
222 
223 	r->ie_len = ie_len;
224 
225 	pos = (unsigned char *)(r + 1);
226 
227 	if (ie) {
228 		memcpy(pos, ie, ie_len);
229 
230 		pos += ie_len;
231 	}
232 
233 	r->beacon_ie_len = beacon_ie_len;
234 
235 	if (beacon_ie) {
236 		memcpy(pos, beacon_ie, beacon_ie_len);
237 	}
238 
239 	if (scan_res->valid_fields & NRF_WIFI_EVENT_NEW_SCAN_RESULTS_STATUS_VALID) {
240 		switch (scan_res->status) {
241 		case NRF_WIFI_BSS_STATUS_ASSOCIATED:
242 			r->flags |= WPA_SCAN_ASSOCIATED;
243 			break;
244 		default:
245 			break;
246 		}
247 	}
248 
249 	if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.scan_res) {
250 		vif_ctx_zep->supp_callbk_fns.scan_res(vif_ctx_zep->supp_drv_if_ctx, r, more_res);
251 	}
252 
253 	if (!more_res) {
254 		vif_ctx_zep->scan_in_progress = false;
255 	}
256 
257 	k_free(r);
258 }
259 
nrf_wifi_wpa_supp_event_proc_auth_resp(void * if_priv,struct nrf_wifi_umac_event_mlme * auth_resp,unsigned int event_len)260 void nrf_wifi_wpa_supp_event_proc_auth_resp(void *if_priv,
261 					    struct nrf_wifi_umac_event_mlme *auth_resp,
262 					    unsigned int event_len)
263 {
264 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
265 	union wpa_event_data event;
266 	const struct ieee80211_mgmt *mgmt = NULL;
267 	const unsigned char *frame = NULL;
268 	unsigned int frame_len = 0;
269 
270 	vif_ctx_zep = if_priv;
271 
272 	frame = auth_resp->frame.frame;
273 	frame_len = auth_resp->frame.frame_len;
274 	mgmt = (const struct ieee80211_mgmt *)frame;
275 
276 	if (frame_len < 4 + (2 * NRF_WIFI_ETH_ADDR_LEN)) {
277 		LOG_ERR("%s: MLME event too short", __func__);
278 		return;
279 	}
280 
281 
282 	if (frame_len < 24 + sizeof(mgmt->u.auth)) {
283 		LOG_ERR("%s: Authentication response frame too short", __func__);
284 		return;
285 	}
286 
287 	memset(&event, 0, sizeof(event));
288 
289 	memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
290 
291 	event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
292 
293 	event.auth.auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
294 
295 	event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
296 
297 	if (frame_len > 24 + sizeof(mgmt->u.auth)) {
298 		event.auth.ies = mgmt->u.auth.variable;
299 		event.auth.ies_len = (frame_len - 24 - sizeof(mgmt->u.auth));
300 	}
301 
302 	if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.auth_resp) {
303 		vif_ctx_zep->supp_callbk_fns.auth_resp(vif_ctx_zep->supp_drv_if_ctx, &event);
304 	}
305 }
306 
nrf_wifi_wpa_supp_event_proc_assoc_resp(void * if_priv,struct nrf_wifi_umac_event_mlme * assoc_resp,unsigned int event_len)307 void nrf_wifi_wpa_supp_event_proc_assoc_resp(void *if_priv,
308 					     struct nrf_wifi_umac_event_mlme *assoc_resp,
309 					     unsigned int event_len)
310 {
311 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
312 	union wpa_event_data event;
313 	const struct ieee80211_mgmt *mgmt = NULL;
314 	const unsigned char *frame = NULL;
315 	unsigned int frame_len = 0;
316 	unsigned short status = WLAN_STATUS_UNSPECIFIED_FAILURE;
317 
318 	vif_ctx_zep = if_priv;
319 
320 	frame = assoc_resp->frame.frame;
321 	frame_len = assoc_resp->frame.frame_len;
322 	mgmt = (const struct ieee80211_mgmt *)frame;
323 
324 	if (frame_len < 24 + sizeof(mgmt->u.assoc_resp)) {
325 		LOG_ERR("%s: Association response frame too short", __func__);
326 		return;
327 	}
328 
329 	memset(&event, 0, sizeof(event));
330 
331 	status = le_to_host16(mgmt->u.assoc_resp.status_code);
332 
333 	if (status != WLAN_STATUS_SUCCESS) {
334 		event.assoc_reject.bssid = mgmt->bssid;
335 
336 		if (frame_len > 24 + sizeof(mgmt->u.assoc_resp)) {
337 			event.assoc_reject.resp_ies = (unsigned char *)mgmt->u.assoc_resp.variable;
338 			event.assoc_reject.resp_ies_len =
339 				(frame_len - 24 - sizeof(mgmt->u.assoc_resp));
340 		}
341 
342 		event.assoc_reject.status_code = status;
343 	} else {
344 		event.assoc_info.addr = mgmt->bssid;
345 		event.assoc_info.resp_frame = frame;
346 		event.assoc_info.resp_frame_len = frame_len;
347 		event.assoc_info.freq = vif_ctx_zep->assoc_freq;
348 
349 		if (frame_len > 24 + sizeof(mgmt->u.assoc_resp)) {
350 			event.assoc_info.resp_ies = (unsigned char *)mgmt->u.assoc_resp.variable;
351 			event.assoc_info.resp_ies_len =
352 				(frame_len - 24 - sizeof(mgmt->u.assoc_resp));
353 		}
354 
355 		if (assoc_resp->req_ie_len > 0) {
356 			event.assoc_info.req_ies = (unsigned char *)assoc_resp->req_ie;
357 			event.assoc_info.req_ies_len = assoc_resp->req_ie_len;
358 		}
359 
360 	}
361 
362 	if (vif_ctx_zep->supp_drv_if_ctx &&
363 		vif_ctx_zep->supp_callbk_fns.assoc_resp) {
364 		vif_ctx_zep->supp_callbk_fns.assoc_resp(vif_ctx_zep->supp_drv_if_ctx,
365 			&event, status);
366 	}
367 }
368 
nrf_wifi_wpa_supp_event_proc_deauth(void * if_priv,struct nrf_wifi_umac_event_mlme * deauth,unsigned int event_len)369 void nrf_wifi_wpa_supp_event_proc_deauth(void *if_priv,
370 					 struct nrf_wifi_umac_event_mlme *deauth,
371 					 unsigned int event_len)
372 {
373 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
374 	union wpa_event_data event;
375 	const struct ieee80211_mgmt *mgmt = NULL;
376 	const unsigned char *frame = NULL;
377 	unsigned int frame_len = 0;
378 
379 	vif_ctx_zep = if_priv;
380 
381 	frame = deauth->frame.frame;
382 	frame_len = deauth->frame.frame_len;
383 	mgmt = (const struct ieee80211_mgmt *)frame;
384 
385 	if (frame_len < 24 + sizeof(mgmt->u.deauth)) {
386 		LOG_ERR("%s: Deauthentication frame too short", __func__);
387 		return;
388 	}
389 
390 	memset(&event, 0, sizeof(event));
391 
392 	event.deauth_info.addr = &mgmt->sa[0];
393 	event.deauth_info.reason_code = le_to_host16(mgmt->u.deauth.reason_code);
394 	if (frame + frame_len > mgmt->u.deauth.variable) {
395 		event.deauth_info.ie = mgmt->u.deauth.variable;
396 		event.deauth_info.ie_len = (frame + frame_len - mgmt->u.deauth.variable);
397 	}
398 
399 	if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.deauth) {
400 		vif_ctx_zep->supp_callbk_fns.deauth(vif_ctx_zep->supp_drv_if_ctx,
401 			&event, mgmt);
402 	}
403 }
404 
nrf_wifi_wpa_supp_event_proc_disassoc(void * if_priv,struct nrf_wifi_umac_event_mlme * disassoc,unsigned int event_len)405 void nrf_wifi_wpa_supp_event_proc_disassoc(void *if_priv,
406 					   struct nrf_wifi_umac_event_mlme *disassoc,
407 					   unsigned int event_len)
408 {
409 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
410 	union wpa_event_data event;
411 	const struct ieee80211_mgmt *mgmt = NULL;
412 	const unsigned char *frame = NULL;
413 	unsigned int frame_len = 0;
414 
415 	vif_ctx_zep = if_priv;
416 
417 	frame = disassoc->frame.frame;
418 	frame_len = disassoc->frame.frame_len;
419 	mgmt = (const struct ieee80211_mgmt *)frame;
420 
421 	if (frame_len < 24 + sizeof(mgmt->u.disassoc)) {
422 		LOG_ERR("%s: Disassociation frame too short", __func__);
423 		return;
424 	}
425 
426 	memset(&event, 0, sizeof(event));
427 
428 	event.disassoc_info.addr = &mgmt->sa[0];
429 	event.disassoc_info.reason_code = le_to_host16(mgmt->u.disassoc.reason_code);
430 	if (frame + frame_len > mgmt->u.disassoc.variable) {
431 		event.disassoc_info.ie = mgmt->u.disassoc.variable;
432 		event.disassoc_info.ie_len = (frame + frame_len - mgmt->u.disassoc.variable);
433 	}
434 
435 	if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.disassoc)	{
436 		vif_ctx_zep->supp_callbk_fns.disassoc(vif_ctx_zep->supp_drv_if_ctx, &event);
437 	}
438 
439 	(void) nrf_wifi_twt_teardown_flows(vif_ctx_zep, 0, NRF_WIFI_MAX_TWT_FLOWS);
440 }
441 
nrf_wifi_wpa_supp_dev_init(void * supp_drv_if_ctx,const char * iface_name,struct zep_wpa_supp_dev_callbk_fns * supp_callbk_fns)442 void *nrf_wifi_wpa_supp_dev_init(void *supp_drv_if_ctx, const char *iface_name,
443 				 struct zep_wpa_supp_dev_callbk_fns *supp_callbk_fns)
444 {
445 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
446 	const struct device *device = DEVICE_DT_GET(DT_CHOSEN(zephyr_wifi));
447 
448 	if (!device) {
449 		LOG_ERR("%s: Interface %s not found", __func__, iface_name);
450 		return NULL;
451 	}
452 
453 	vif_ctx_zep = device->data;
454 
455 	if (!vif_ctx_zep || !vif_ctx_zep->rpu_ctx_zep) {
456 		LOG_ERR("%s: Interface %s not properly initialized", __func__, iface_name);
457 		return NULL;
458 	}
459 
460 	/* Needed to make sure that during initialization, commands like setting regdomain
461 	 * does not access it.
462 	 */
463 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
464 	vif_ctx_zep->supp_drv_if_ctx = supp_drv_if_ctx;
465 
466 	memcpy(&vif_ctx_zep->supp_callbk_fns, supp_callbk_fns,
467 	       sizeof(vif_ctx_zep->supp_callbk_fns));
468 
469 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
470 	return vif_ctx_zep;
471 }
472 
nrf_wifi_wpa_supp_dev_deinit(void * if_priv)473 void nrf_wifi_wpa_supp_dev_deinit(void *if_priv)
474 {
475 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
476 
477 	vif_ctx_zep = if_priv;
478 
479 	vif_ctx_zep->supp_drv_if_ctx = NULL;
480 }
481 
nrf_wifi_wpa_supp_scan2(void * if_priv,struct wpa_driver_scan_params * params)482 int nrf_wifi_wpa_supp_scan2(void *if_priv, struct wpa_driver_scan_params *params)
483 {
484 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
485 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
486 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
487 	struct nrf_wifi_umac_scan_info *scan_info = NULL;
488 	int indx = 0;
489 	int ret = -1;
490 	int num_freqs = 0;
491 
492 	if (!if_priv || !params) {
493 		LOG_ERR("%s: Invalid params", __func__);
494 		return ret;
495 	}
496 
497 	vif_ctx_zep = if_priv;
498 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
499 	if (!rpu_ctx_zep->rpu_ctx) {
500 		LOG_ERR("%s: RPU context not initialized", __func__);
501 		return ret;
502 	}
503 
504 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
505 	if (!rpu_ctx_zep->rpu_ctx) {
506 		LOG_DBG("%s: RPU context not initialized", __func__);
507 		goto out;
508 	}
509 
510 	if (vif_ctx_zep->scan_in_progress) {
511 		LOG_ERR("%s: Scan already in progress", __func__);
512 		ret = -EBUSY;
513 		goto out;
514 	}
515 
516 	if (params->freqs) {
517 		for (indx = 0; params->freqs[indx]; indx++) {
518 			num_freqs++;
519 		}
520 	}
521 
522 	scan_info = k_calloc(sizeof(*scan_info) + (num_freqs * sizeof(unsigned int)),
523 			     sizeof(char));
524 
525 	if (!scan_info) {
526 		LOG_ERR("%s: Unable to allocate memory for scan info", __func__);
527 		ret = -ENOMEM;
528 		goto out;
529 	}
530 
531 	memset(scan_info, 0x0, sizeof(*scan_info));
532 
533 	if (params->freqs) {
534 		for (indx = 0; params->freqs[indx]; indx++) {
535 			scan_info->scan_params.center_frequency[indx] =
536 				params->freqs[indx];
537 		}
538 		scan_info->scan_params.num_scan_channels = indx;
539 	}
540 
541 	if (params->filter_ssids) {
542 		scan_info->scan_params.num_scan_ssids = params->num_filter_ssids;
543 
544 		for (indx = 0; indx < params->num_filter_ssids; indx++) {
545 			memcpy(scan_info->scan_params.scan_ssids[indx].nrf_wifi_ssid,
546 			       params->filter_ssids[indx].ssid,
547 			       params->filter_ssids[indx].ssid_len);
548 
549 			scan_info->scan_params.scan_ssids[indx].nrf_wifi_ssid_len =
550 				params->filter_ssids[indx].ssid_len;
551 		}
552 	}
553 
554 	scan_info->scan_reason = SCAN_CONNECT;
555 
556 	/* Copy extra_ies */
557 	if (params->extra_ies_len && params->extra_ies_len <= NRF_WIFI_MAX_IE_LEN) {
558 		memcpy(scan_info->scan_params.ie.ie, params->extra_ies, params->extra_ies_len);
559 		scan_info->scan_params.ie.ie_len = params->extra_ies_len;
560 	} else if (params->extra_ies_len) {
561 		LOG_ERR("%s: extra_ies_len %d is greater than max IE len %d",
562 			__func__, params->extra_ies_len, NRF_WIFI_MAX_IE_LEN);
563 		goto out;
564 	}
565 
566 	status = nrf_wifi_fmac_scan(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, scan_info);
567 
568 	if (status != NRF_WIFI_STATUS_SUCCESS) {
569 		LOG_ERR("%s: Scan trigger failed", __func__);
570 		goto out;
571 	}
572 
573 	vif_ctx_zep->scan_type = SCAN_CONNECT;
574 	vif_ctx_zep->scan_in_progress = true;
575 
576 	k_work_schedule(&vif_ctx_zep->scan_timeout_work,
577 		K_SECONDS(CONFIG_WIFI_NRF70_SCAN_TIMEOUT_S));
578 
579 	ret = 0;
580 out:
581 	if (scan_info) {
582 		k_free(scan_info);
583 	}
584 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
585 	return ret;
586 }
587 
nrf_wifi_wpa_supp_scan_abort(void * if_priv)588 int nrf_wifi_wpa_supp_scan_abort(void *if_priv)
589 {
590 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
591 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
592 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
593 	int sem_ret;
594 
595 	vif_ctx_zep = if_priv;
596 	if (!vif_ctx_zep) {
597 		LOG_ERR("%s: vif_ctx_zep is NULL", __func__);
598 		return -1;
599 	}
600 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
601 	if (!rpu_ctx_zep) {
602 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
603 		return -1;
604 	}
605 
606 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
607 	if (!rpu_ctx_zep->rpu_ctx) {
608 		LOG_DBG("%s: RPU context not initialized", __func__);
609 		goto out;
610 	}
611 
612 	if (!vif_ctx_zep->scan_in_progress) {
613 		LOG_ERR("%s:Ignore scan abort, no scan in progress", __func__);
614 		goto out;
615 	}
616 
617 	status = nrf_wifi_fmac_abort_scan(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx);
618 
619 	if (status != NRF_WIFI_STATUS_SUCCESS) {
620 		LOG_ERR("%s: nrf_wifi_fmac_abort_scan failed", __func__);
621 		goto out;
622 	}
623 
624 	k_sem_reset(&wait_for_scan_resp_sem);
625 	sem_ret = k_sem_take(&wait_for_scan_resp_sem, K_MSEC(RPU_RESP_EVENT_TIMEOUT));
626 	if (sem_ret) {
627 		LOG_ERR("%s: Timedout waiting for scan abort response, ret = %d",
628 			    __func__, sem_ret);
629 		status = NRF_WIFI_STATUS_FAIL;
630 		goto out;
631 	}
632 
633 out:
634 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
635 	return status;
636 }
637 
nrf_wifi_wpa_supp_scan_results_get(void * if_priv)638 int nrf_wifi_wpa_supp_scan_results_get(void *if_priv)
639 {
640 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
641 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
642 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
643 	int ret = -1;
644 
645 	if (!if_priv) {
646 		LOG_ERR("%s: Invalid params", __func__);
647 		return ret;
648 	}
649 
650 	vif_ctx_zep = if_priv;
651 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
652 	if (!rpu_ctx_zep) {
653 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
654 		return ret;
655 	}
656 
657 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
658 	if (!rpu_ctx_zep->rpu_ctx) {
659 		LOG_DBG("%s: RPU context not initialized", __func__);
660 		goto out;
661 	}
662 
663 	status = nrf_wifi_fmac_scan_res_get(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx,
664 					    SCAN_CONNECT);
665 
666 	if (status != NRF_WIFI_STATUS_SUCCESS) {
667 		LOG_ERR("%s: nrf_wifi_fmac_scan_res_get failed", __func__);
668 		goto out;
669 	}
670 
671 	ret = 0;
672 out:
673 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
674 	return ret;
675 }
676 
nrf_wifi_wpa_supp_deauthenticate(void * if_priv,const char * addr,unsigned short reason_code)677 int nrf_wifi_wpa_supp_deauthenticate(void *if_priv, const char *addr, unsigned short reason_code)
678 {
679 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
680 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
681 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
682 	struct nrf_wifi_umac_disconn_info deauth_info;
683 	int ret = -1;
684 
685 	if ((!if_priv) || (!addr)) {
686 		LOG_ERR("%s: Invalid params", __func__);
687 		return ret;
688 	}
689 
690 	vif_ctx_zep = if_priv;
691 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
692 	if (!rpu_ctx_zep) {
693 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
694 		return ret;
695 	}
696 
697 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
698 	if (!rpu_ctx_zep->rpu_ctx) {
699 		LOG_DBG("%s: RPU context not initialized", __func__);
700 		goto out;
701 	}
702 
703 	memset(&deauth_info, 0, sizeof(deauth_info));
704 
705 	deauth_info.reason_code = reason_code;
706 
707 	memcpy(deauth_info.mac_addr, addr, sizeof(deauth_info.mac_addr));
708 
709 	status = nrf_wifi_fmac_deauth(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &deauth_info);
710 
711 	if (status != NRF_WIFI_STATUS_SUCCESS) {
712 		LOG_ERR("%s: nrf_wifi_fmac_deauth failed", __func__);
713 		goto out;
714 	}
715 
716 	ret = nrf_wifi_twt_teardown_flows(vif_ctx_zep, 0, NRF_WIFI_MAX_TWT_FLOWS);
717 out:
718 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
719 	return ret;
720 }
721 
nrf_wifi_wpa_supp_add_key(struct nrf_wifi_umac_key_info * key_info,enum wpa_alg alg,int key_idx,int defkey,const unsigned char * seq,size_t seq_len,const unsigned char * key,size_t key_len)722 int nrf_wifi_wpa_supp_add_key(struct nrf_wifi_umac_key_info *key_info, enum wpa_alg alg,
723 			      int key_idx,
724 			      int defkey, const unsigned char *seq, size_t seq_len,
725 			      const unsigned char *key, size_t key_len)
726 {
727 	unsigned int suite = 0;
728 
729 	suite = wpa_alg_to_cipher_suite(alg, key_len);
730 
731 	if (!suite) {
732 		return -1;
733 	}
734 
735 	if (defkey && alg == WPA_ALG_BIP_CMAC_128) {
736 		key_info->nrf_wifi_flags = NRF_WIFI_KEY_DEFAULT_MGMT;
737 	} else if (defkey) {
738 		key_info->nrf_wifi_flags = NRF_WIFI_KEY_DEFAULT;
739 	}
740 
741 	key_info->key_idx = key_idx;
742 	key_info->cipher_suite = suite;
743 
744 	if (key && key_len) {
745 		memcpy(key_info->key.nrf_wifi_key, key, key_len);
746 		key_info->key.nrf_wifi_key_len = key_len;
747 	}
748 	if (seq && seq_len) {
749 		memcpy(key_info->seq.nrf_wifi_seq, seq, seq_len);
750 		key_info->seq.nrf_wifi_seq_len = seq_len;
751 	}
752 
753 	return 0;
754 }
755 
nrf_wifi_wpa_supp_authenticate(void * if_priv,struct wpa_driver_auth_params * params,struct wpa_bss * curr_bss)756 int nrf_wifi_wpa_supp_authenticate(void *if_priv, struct wpa_driver_auth_params *params,
757 				struct wpa_bss *curr_bss)
758 {
759 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
760 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
761 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
762 	struct nrf_wifi_umac_auth_info auth_info;
763 	int ret = -1;
764 	int type;
765 	int count = 0;
766 	int max_ie_len = 0;
767 
768 	if ((!if_priv) || (!params)) {
769 		LOG_ERR("%s: Invalid params", __func__);
770 		return ret;
771 	}
772 
773 	vif_ctx_zep = if_priv;
774 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
775 	if (!rpu_ctx_zep) {
776 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
777 		return ret;
778 	}
779 
780 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
781 	if (!rpu_ctx_zep->rpu_ctx) {
782 		LOG_DBG("%s: RPU context not initialized", __func__);
783 		goto out;
784 	}
785 
786 #ifdef CONFIG_NRF70_RAW_DATA_RX
787 	if (vif_ctx_zep->if_type == NRF_WIFI_IFTYPE_MONITOR) {
788 		LOG_ERR("%s: Interface is in Monitor mode -  cannot connect to a BSS", __func__);
789 		goto out;
790 	}
791 #endif /* CONFIG_NRF70_RAW_DATA_RX */
792 
793 	memset(&auth_info, 0, sizeof(auth_info));
794 
795 
796 	if (params->bssid) {
797 		memcpy(auth_info.nrf_wifi_bssid, params->bssid, ETH_ALEN);
798 	}
799 
800 	if (params->freq) {
801 		auth_info.frequency = params->freq;
802 	}
803 
804 	if (params->ssid) {
805 		memcpy(auth_info.ssid.nrf_wifi_ssid, params->ssid, params->ssid_len);
806 
807 		auth_info.ssid.nrf_wifi_ssid_len = params->ssid_len;
808 	}
809 
810 	if (params->ie) {
811 		max_ie_len = (params->ie_len > NRF_WIFI_MAX_IE_LEN) ?
812 			     NRF_WIFI_MAX_IE_LEN : params->ie_len;
813 		memcpy(&auth_info.ie.ie, params->ie, max_ie_len);
814 		auth_info.ie.ie_len = max_ie_len;
815 	} else {
816 		auth_info.scan_width = 0; /* hard coded */
817 		auth_info.nrf_wifi_signal = curr_bss->level;
818 		auth_info.capability = curr_bss->caps;
819 		auth_info.beacon_interval = curr_bss->beacon_int;
820 		auth_info.tsf = curr_bss->tsf;
821 		auth_info.from_beacon = 0; /* hard coded */
822 	}
823 
824 	if (params->auth_data) {
825 		auth_info.sae.sae_data_len = params->auth_data_len;
826 
827 		memcpy(auth_info.sae.sae_data, params->auth_data, params->auth_data_len);
828 	}
829 
830 	type = get_nrf_wifi_auth_type(params->auth_alg);
831 
832 	if (type != NRF_WIFI_AUTHTYPE_MAX) {
833 		auth_info.auth_type = type;
834 	}
835 
836 	if (type == NRF_WIFI_AUTHTYPE_SHARED_KEY) {
837 		size_t key_len = params->wep_key_len[params->wep_tx_keyidx];
838 		struct nrf_wifi_umac_key_info *key_info = &auth_info.key_info;
839 
840 		key_info->cipher_suite = wpa_alg_to_cipher_suite(params->auth_alg, key_len);
841 		memcpy(key_info->key.nrf_wifi_key,
842 			   params->wep_key[params->wep_tx_keyidx],
843 			   key_len);
844 		key_info->key.nrf_wifi_key_len = key_len;
845 		key_info->valid_fields |= NRF_WIFI_KEY_VALID | NRF_WIFI_KEY_IDX_VALID |
846 			NRF_WIFI_CIPHER_SUITE_VALID;
847 	}
848 
849 	if (params->local_state_change) {
850 		auth_info.nrf_wifi_flags |= NRF_WIFI_CMD_AUTHENTICATE_LOCAL_STATE_CHANGE;
851 	}
852 
853 	status = nrf_wifi_fmac_auth(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &auth_info);
854 
855 	if (status != NRF_WIFI_STATUS_SUCCESS) {
856 		LOG_ERR("%s: MLME command failed (auth): count=%d ret=%d", __func__, count, ret);
857 		count++;
858 		ret = -1;
859 	} else {
860 		LOG_DBG("%s:Authentication request sent successfully", __func__);
861 		ret = 0;
862 	}
863 out:
864 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
865 	return ret;
866 }
867 
nrf_wifi_wpa_supp_associate(void * if_priv,struct wpa_driver_associate_params * params)868 int nrf_wifi_wpa_supp_associate(void *if_priv, struct wpa_driver_associate_params *params)
869 {
870 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
871 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
872 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
873 	struct nrf_wifi_umac_assoc_info assoc_info;
874 	int ret = -1;
875 
876 	if ((!if_priv) || (!params)) {
877 		LOG_ERR("%s: Invalid params", __func__);
878 		return ret;
879 	}
880 
881 	vif_ctx_zep = if_priv;
882 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
883 	if (!rpu_ctx_zep->rpu_ctx) {
884 		LOG_DBG("%s: RPU context not initialized", __func__);
885 		return ret;
886 	}
887 
888 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
889 	if (!rpu_ctx_zep->rpu_ctx) {
890 		LOG_DBG("%s: RPU context not initialized", __func__);
891 		goto out;
892 	}
893 
894 	memset(&assoc_info, 0, sizeof(assoc_info));
895 
896 	if (params->bssid) {
897 		memcpy(assoc_info.nrf_wifi_bssid, params->bssid, sizeof(assoc_info.nrf_wifi_bssid));
898 	}
899 
900 	if (params->freq.freq) {
901 		assoc_info.center_frequency = params->freq.freq;
902 		vif_ctx_zep->assoc_freq = params->freq.freq;
903 	} else {
904 		vif_ctx_zep->assoc_freq = 0;
905 	}
906 
907 	if (params->prev_bssid) {
908 		assoc_info.prev_bssid_flag = 1;
909 		memcpy(assoc_info.prev_bssid, params->prev_bssid, ETH_ALEN);
910 	}
911 
912 	if (params->ssid) {
913 		assoc_info.ssid.nrf_wifi_ssid_len = params->ssid_len;
914 
915 		memcpy(assoc_info.ssid.nrf_wifi_ssid, params->ssid, params->ssid_len);
916 
917 	}
918 
919 	if (params->wpa_ie) {
920 		assoc_info.wpa_ie.ie_len = params->wpa_ie_len;
921 		memcpy(assoc_info.wpa_ie.ie, params->wpa_ie, params->wpa_ie_len);
922 	}
923 
924 	assoc_info.control_port = 1;
925 
926 	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED) {
927 		assoc_info.use_mfp = NRF_WIFI_MFP_REQUIRED;
928 	}
929 
930 	if (params->bss_max_idle_period) {
931 		assoc_info.bss_max_idle_time = params->bss_max_idle_period;
932 	}
933 
934 	status = nrf_wifi_fmac_assoc(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &assoc_info);
935 
936 	if (status != NRF_WIFI_STATUS_SUCCESS) {
937 		LOG_ERR("%s: MLME command failed (assoc)", __func__);
938 	} else {
939 		LOG_DBG("%s: Association request sent successfully", __func__);
940 		ret = 0;
941 	}
942 
943 out:
944 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
945 	return ret;
946 }
947 
nrf_wifi_wpa_supp_set_key(void * if_priv,const unsigned char * ifname,enum wpa_alg alg,const unsigned char * addr,int key_idx,int set_tx,const unsigned char * seq,size_t seq_len,const unsigned char * key,size_t key_len,enum key_flag key_flag)948 int nrf_wifi_wpa_supp_set_key(void *if_priv, const unsigned char *ifname, enum wpa_alg alg,
949 			      const unsigned char *addr, int key_idx, int set_tx,
950 			      const unsigned char *seq, size_t seq_len, const unsigned char *key,
951 			      size_t key_len, enum key_flag key_flag)
952 {
953 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
954 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
955 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
956 	struct nrf_wifi_umac_key_info key_info;
957 	const unsigned char *mac_addr = NULL;
958 	unsigned int suite;
959 	int ret = -1;
960 
961 
962 	if ((!if_priv) || (!ifname)) {
963 		LOG_ERR("%s: Invalid params", __func__);
964 		return ret;
965 	}
966 
967 	vif_ctx_zep = if_priv;
968 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
969 	if (!rpu_ctx_zep->rpu_ctx) {
970 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
971 		return ret;
972 	}
973 
974 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
975 	if (!rpu_ctx_zep->rpu_ctx) {
976 		LOG_DBG("%s: RPU context not initialized", __func__);
977 		goto out;
978 	}
979 
980 	/* Can happen in a positive case where "net if down" is completed, but WPA
981 	 * supplicant is still deleting keys.
982 	 */
983 	if (!rpu_ctx_zep->rpu_ctx) {
984 		goto out;
985 	}
986 
987 	memset(&key_info, 0, sizeof(key_info));
988 
989 	if (alg != WPA_ALG_NONE) {
990 		suite = wpa_alg_to_cipher_suite(alg, key_len);
991 
992 		if (!suite) {
993 			goto out;
994 		}
995 
996 		memcpy(key_info.key.nrf_wifi_key, key, key_len);
997 
998 		key_info.key.nrf_wifi_key_len = key_len;
999 		key_info.cipher_suite = suite;
1000 
1001 		key_info.valid_fields |= (NRF_WIFI_CIPHER_SUITE_VALID | NRF_WIFI_KEY_VALID);
1002 	}
1003 
1004 	if (seq && seq_len) {
1005 		memcpy(key_info.seq.nrf_wifi_seq, seq, seq_len);
1006 
1007 		key_info.seq.nrf_wifi_seq_len = seq_len;
1008 		key_info.valid_fields |= NRF_WIFI_SEQ_VALID;
1009 	}
1010 
1011 
1012 	/* TODO: Implement/check set_tx */
1013 	if (addr && !is_broadcast_ether_addr(addr)) {
1014 		mac_addr = addr;
1015 		key_info.key_type = NRF_WIFI_KEYTYPE_PAIRWISE;
1016 		key_info.valid_fields |= NRF_WIFI_KEY_TYPE_VALID;
1017 	} else if (addr && is_broadcast_ether_addr(addr)) {
1018 		mac_addr = NULL;
1019 		key_info.key_type = NRF_WIFI_KEYTYPE_GROUP;
1020 		key_info.valid_fields |= NRF_WIFI_KEY_TYPE_VALID;
1021 		key_info.nrf_wifi_flags |= NRF_WIFI_KEY_DEFAULT_TYPE_MULTICAST;
1022 	}
1023 
1024 	key_info.key_idx = key_idx;
1025 	key_info.valid_fields |= NRF_WIFI_KEY_IDX_VALID;
1026 
1027 	if (alg == WPA_ALG_NONE) {
1028 		status = nrf_wifi_fmac_del_key(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx,
1029 					       &key_info, mac_addr);
1030 
1031 		if (status != NRF_WIFI_STATUS_SUCCESS) {
1032 			LOG_ERR("%s: nrf_wifi_fmac_del_key failed", __func__);
1033 		} else {
1034 			ret = 0;
1035 		}
1036 	} else {
1037 		status = nrf_wifi_fmac_add_key(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx,
1038 					       &key_info, mac_addr);
1039 
1040 		if (status != NRF_WIFI_STATUS_SUCCESS) {
1041 			LOG_ERR("%s: nrf_wifi_fmac_add_key failed", __func__);
1042 		} else {
1043 			ret = 0;
1044 		}
1045 	}
1046 
1047 	/*
1048 	 * If we failed or don't need to set the default TX key (below),
1049 	 * we're done here.
1050 	 */
1051 	if (ret || !set_tx || alg == WPA_ALG_NONE) {
1052 		goto out;
1053 	}
1054 
1055 
1056 	memset(&key_info, 0, sizeof(key_info));
1057 
1058 	key_info.key_idx = key_idx;
1059 	key_info.valid_fields |= NRF_WIFI_KEY_IDX_VALID;
1060 
1061 	if (alg == WPA_ALG_BIP_CMAC_128 || alg == WPA_ALG_BIP_GMAC_128 ||
1062 	    alg == WPA_ALG_BIP_GMAC_256 || alg == WPA_ALG_BIP_CMAC_256) {
1063 		key_info.nrf_wifi_flags = NRF_WIFI_KEY_DEFAULT_MGMT;
1064 	} else {
1065 		key_info.nrf_wifi_flags = NRF_WIFI_KEY_DEFAULT;
1066 	}
1067 
1068 	if (addr && is_broadcast_ether_addr(addr)) {
1069 		key_info.nrf_wifi_flags |= NRF_WIFI_KEY_DEFAULT_TYPE_MULTICAST;
1070 	} else if (addr) {
1071 		key_info.nrf_wifi_flags |= NRF_WIFI_KEY_DEFAULT_TYPE_UNICAST;
1072 	}
1073 
1074 	status = nrf_wifi_fmac_set_key(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &key_info);
1075 
1076 	if (status != NRF_WIFI_STATUS_SUCCESS) {
1077 		LOG_ERR("%s: nrf_wifi_fmac_set_key failed", __func__);
1078 		ret = -1;
1079 	} else {
1080 		ret = 0;
1081 	}
1082 out:
1083 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1084 	return ret;
1085 }
1086 
nrf_wifi_wpa_set_supp_port(void * if_priv,int authorized,char * bssid)1087 int nrf_wifi_wpa_set_supp_port(void *if_priv, int authorized, char *bssid)
1088 {
1089 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1090 	struct nrf_wifi_umac_chg_sta_info chg_sta_info;
1091 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1092 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
1093 	int ret = -1;
1094 
1095 	if (!if_priv || !bssid) {
1096 		LOG_ERR("%s: Invalid params", __func__);
1097 		return ret;
1098 	}
1099 
1100 	vif_ctx_zep = if_priv;
1101 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1102 	if (!rpu_ctx_zep) {
1103 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1104 		return ret;
1105 	}
1106 
1107 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1108 	if (!rpu_ctx_zep->rpu_ctx) {
1109 		LOG_DBG("%s: RPU context not initialized", __func__);
1110 		goto out;
1111 	}
1112 
1113 	if (vif_ctx_zep->if_op_state != NRF_WIFI_FMAC_IF_OP_STATE_UP) {
1114 		LOG_DBG("%s: Interface not UP, ignoring", __func__);
1115 		ret = 0;
1116 		goto out;
1117 	}
1118 
1119 	memset(&chg_sta_info, 0x0, sizeof(chg_sta_info));
1120 
1121 	memcpy(chg_sta_info.mac_addr, bssid, ETH_ALEN);
1122 
1123 	vif_ctx_zep->authorized = authorized;
1124 
1125 	if (authorized) {
1126 		/* BIT(NL80211_STA_FLAG_AUTHORIZED) */
1127 		chg_sta_info.sta_flags2.nrf_wifi_mask = 1 << 1;
1128 		/* BIT(NL80211_STA_FLAG_AUTHORIZED) */
1129 		chg_sta_info.sta_flags2.nrf_wifi_set = 1 << 1;
1130 	}
1131 
1132 	status = nrf_wifi_fmac_chg_sta(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &chg_sta_info);
1133 
1134 	if (status != NRF_WIFI_STATUS_SUCCESS) {
1135 		LOG_ERR("%s: nrf_wifi_fmac_chg_sta failed", __func__);
1136 		ret = -1;
1137 		goto out;
1138 	}
1139 
1140 	ret = 0;
1141 out:
1142 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1143 	return ret;
1144 }
1145 
nrf_wifi_wpa_supp_signal_poll(void * if_priv,struct wpa_signal_info * si,unsigned char * bssid)1146 int nrf_wifi_wpa_supp_signal_poll(void *if_priv, struct wpa_signal_info *si, unsigned char *bssid)
1147 {
1148 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1149 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1150 	struct nrf_wifi_fmac_dev_ctx *fmac_dev_ctx = NULL;
1151 	enum nrf_wifi_status ret = NRF_WIFI_STATUS_FAIL;
1152 	int sem_ret;
1153 	int rssi_record_elapsed_time_ms = 0;
1154 
1155 	if (!if_priv || !si || !bssid) {
1156 		LOG_ERR("%s: Invalid params", __func__);
1157 		return ret;
1158 	}
1159 
1160 	vif_ctx_zep = if_priv;
1161 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1162 	if (!rpu_ctx_zep) {
1163 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1164 		return ret;
1165 	}
1166 
1167 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1168 	if (!rpu_ctx_zep->rpu_ctx) {
1169 		LOG_DBG("%s: RPU context not initialized", __func__);
1170 		goto out;
1171 	}
1172 
1173 	fmac_dev_ctx = rpu_ctx_zep->rpu_ctx;
1174 
1175 	vif_ctx_zep->signal_info = si;
1176 
1177 	rssi_record_elapsed_time_ms =
1178 		nrf_wifi_osal_time_elapsed_us(vif_ctx_zep->rssi_record_timestamp_us) / 1000;
1179 
1180 	if (rssi_record_elapsed_time_ms > CONFIG_NRF70_RSSI_STALE_TIMEOUT_MS) {
1181 		ret = nrf_wifi_fmac_get_station(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, bssid);
1182 		if (ret != NRF_WIFI_STATUS_SUCCESS) {
1183 			LOG_ERR("%s: Failed to send get station info command", __func__);
1184 			goto out;
1185 		}
1186 
1187 		sem_ret = k_sem_take(&wait_for_event_sem, K_MSEC(RPU_RESP_EVENT_TIMEOUT));
1188 		if (sem_ret) {
1189 			LOG_ERR("%s: Failed to get station info, ret = %d", __func__, sem_ret);
1190 			ret = NRF_WIFI_STATUS_FAIL;
1191 			goto out;
1192 		}
1193 	} else {
1194 		si->data.signal = (int)vif_ctx_zep->rssi;
1195 	}
1196 
1197 	ret = nrf_wifi_fmac_get_interface(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx);
1198 	if (ret != NRF_WIFI_STATUS_SUCCESS) {
1199 		LOG_ERR("%s: Failed to send get interface info command", __func__);
1200 		goto out;
1201 	}
1202 
1203 	sem_ret = k_sem_take(&wait_for_event_sem, K_MSEC(RPU_RESP_EVENT_TIMEOUT));
1204 	if (sem_ret) {
1205 		LOG_ERR("%s: Failed to get interface info, ret = %d", __func__, sem_ret);
1206 		ret = NRF_WIFI_STATUS_FAIL;
1207 		goto out;
1208 	}
1209 	vif_ctx_zep->signal_info->frequency = vif_ctx_zep->assoc_freq;
1210 out:
1211 	vif_ctx_zep->signal_info = NULL;
1212 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1213 	return ret;
1214 }
1215 
nrf_wifi_wpa_supp_event_proc_get_sta(void * if_priv,struct nrf_wifi_umac_event_new_station * info,unsigned int event_len)1216 void nrf_wifi_wpa_supp_event_proc_get_sta(void *if_priv,
1217 					   struct nrf_wifi_umac_event_new_station *info,
1218 					   unsigned int event_len)
1219 {
1220 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1221 	struct wpa_signal_info *signal_info = NULL;
1222 
1223 	if (!if_priv || !info) {
1224 		LOG_ERR("%s: Invalid params", __func__);
1225 		goto out;
1226 	}
1227 
1228 	vif_ctx_zep = if_priv;
1229 	if (!vif_ctx_zep) {
1230 		LOG_ERR("%s: vif_ctx_zep is NULL", __func__);
1231 		goto out;
1232 	}
1233 
1234 #ifdef CONFIG_NRF70_AP_MODE
1235 	vif_ctx_zep->inactive_time_sec = info->sta_info.inactive_time;
1236 #endif /* CONFIG_NRF70_AP_MODE */
1237 
1238 	signal_info = vif_ctx_zep->signal_info;
1239 	/* Semaphore timedout */
1240 	if (!signal_info) {
1241 		goto out;
1242 	}
1243 
1244 	if (info->sta_info.valid_fields & NRF_WIFI_STA_INFO_SIGNAL_VALID) {
1245 		signal_info->data.signal = info->sta_info.signal;
1246 	} else {
1247 		signal_info->data.signal = 0;
1248 	}
1249 
1250 	if (info->sta_info.valid_fields & NRF_WIFI_STA_INFO_SIGNAL_AVG_VALID) {
1251 		signal_info->data.avg_signal = info->sta_info.signal_avg;
1252 	} else {
1253 		signal_info->data.avg_signal = 0;
1254 	}
1255 
1256 	if (info->sta_info.valid_fields & NRF_WIFI_STA_INFO_RX_BEACON_SIGNAL_AVG_VALID) {
1257 		signal_info->data.avg_beacon_signal = info->sta_info.rx_beacon_signal_avg;
1258 	} else {
1259 		signal_info->data.avg_beacon_signal = 0;
1260 	}
1261 
1262 	signal_info->data.current_tx_rate = 0;
1263 
1264 	if (info->sta_info.valid_fields & NRF_WIFI_STA_INFO_TX_BITRATE_VALID) {
1265 		if (info->sta_info.tx_bitrate.valid_fields & NRF_WIFI_RATE_INFO_BITRATE_VALID) {
1266 			signal_info->data.current_tx_rate = info->sta_info.tx_bitrate.bitrate * 100;
1267 		}
1268 	}
1269 out:
1270 	k_sem_give(&wait_for_event_sem);
1271 }
1272 
nrf_wifi_wpa_supp_event_proc_get_if(void * if_priv,struct nrf_wifi_interface_info * info,unsigned int event_len)1273 void nrf_wifi_wpa_supp_event_proc_get_if(void *if_priv,
1274 					   struct nrf_wifi_interface_info *info,
1275 					   unsigned int event_len)
1276 {
1277 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1278 	struct nrf_wifi_chan_definition *chan_def_info = NULL;
1279 	struct wpa_signal_info *signal_info = NULL;
1280 
1281 	if (!if_priv || !info) {
1282 		LOG_ERR("%s: Invalid params", __func__);
1283 		k_sem_give(&wait_for_event_sem);
1284 		return;
1285 	}
1286 
1287 	vif_ctx_zep = if_priv;
1288 	signal_info = vif_ctx_zep->signal_info;
1289 
1290 	/* Semaphore timedout */
1291 	if (!signal_info) {
1292 		LOG_DBG("%s: Get interface Semaphore timedout", __func__);
1293 		return;
1294 	}
1295 
1296 	chan_def_info = (struct nrf_wifi_chan_definition *)(&info->chan_def);
1297 	signal_info->chanwidth = drv2supp_chan_width(chan_def_info->width);
1298 	signal_info->center_frq1 = chan_def_info->center_frequency1;
1299 	signal_info->center_frq2 = chan_def_info->center_frequency2;
1300 
1301 	k_sem_give(&wait_for_event_sem);
1302 }
1303 
nrf_wifi_wpa_supp_event_mgmt_tx_status(void * if_priv,struct nrf_wifi_umac_event_mlme * mlme_event,unsigned int event_len)1304 void nrf_wifi_wpa_supp_event_mgmt_tx_status(void *if_priv,
1305 		struct nrf_wifi_umac_event_mlme *mlme_event,
1306 		unsigned int event_len)
1307 {
1308 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1309 	bool acked = false;
1310 
1311 	if (!if_priv) {
1312 		LOG_ERR("%s: Missing interface context", __func__);
1313 		return;
1314 	}
1315 
1316 	vif_ctx_zep = if_priv;
1317 
1318 	if (!mlme_event) {
1319 		LOG_ERR("%s: Missing MLME event data", __func__);
1320 		return;
1321 	}
1322 
1323 	acked = mlme_event->nrf_wifi_flags & NRF_WIFI_EVENT_MLME_ACK ? true : false;
1324 	LOG_DBG("%s: Mgmt frame %llx tx status: %s",
1325 		    __func__, mlme_event->cookie, acked ? "ACK" : "NOACK");
1326 
1327 	if (vif_ctx_zep->supp_drv_if_ctx &&
1328 		vif_ctx_zep->supp_callbk_fns.mgmt_tx_status) {
1329 		vif_ctx_zep->supp_callbk_fns.mgmt_tx_status(vif_ctx_zep->supp_drv_if_ctx,
1330 				mlme_event->frame.frame,
1331 				mlme_event->frame.frame_len,
1332 				acked);
1333 	}
1334 }
1335 
nrf_wifi_wpa_supp_event_proc_unprot_mgmt(void * if_priv,struct nrf_wifi_umac_event_mlme * unprot_mgmt,unsigned int event_len)1336 void nrf_wifi_wpa_supp_event_proc_unprot_mgmt(void *if_priv,
1337 		struct nrf_wifi_umac_event_mlme *unprot_mgmt,
1338 		unsigned int event_len)
1339 {
1340 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1341 	union wpa_event_data event;
1342 	const struct ieee80211_mgmt *mgmt = NULL;
1343 	const unsigned char *frame = NULL;
1344 	unsigned int frame_len = 0;
1345 	int cmd_evnt = 0;
1346 
1347 	vif_ctx_zep = if_priv;
1348 
1349 	frame = unprot_mgmt->frame.frame;
1350 	frame_len = unprot_mgmt->frame.frame_len;
1351 
1352 	mgmt = (const struct ieee80211_mgmt *)frame;
1353 	cmd_evnt = ((struct nrf_wifi_umac_hdr *)unprot_mgmt)->cmd_evnt;
1354 
1355 	if (frame_len < 24 + sizeof(mgmt->u.deauth)) {
1356 		LOG_ERR("%s: Unprotected mgmt frame too short", __func__);
1357 		return;
1358 	}
1359 
1360 	memset(&event, 0, sizeof(event));
1361 
1362 	event.unprot_deauth.sa = &mgmt->sa[0];
1363 	event.unprot_deauth.da = &mgmt->da[0];
1364 
1365 	if (cmd_evnt == NRF_WIFI_UMAC_EVENT_UNPROT_DEAUTHENTICATE) {
1366 		event.unprot_deauth.reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1367 		if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.unprot_deauth) {
1368 			vif_ctx_zep->supp_callbk_fns.unprot_deauth(vif_ctx_zep->supp_drv_if_ctx,
1369 									  &event);
1370 		}
1371 	} else if (cmd_evnt == NRF_WIFI_UMAC_EVENT_UNPROT_DISASSOCIATE) {
1372 		event.unprot_disassoc.reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1373 		if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.unprot_disassoc) {
1374 			vif_ctx_zep->supp_callbk_fns.unprot_disassoc(vif_ctx_zep->supp_drv_if_ctx,
1375 										&event);
1376 		}
1377 	}
1378 }
1379 
nrf_wifi_nl80211_send_mlme(void * if_priv,const u8 * data,size_t data_len,int noack,unsigned int freq,int no_cck,int offchanok,unsigned int wait_time,int cookie)1380 int nrf_wifi_nl80211_send_mlme(void *if_priv, const u8 *data,
1381 		size_t data_len, int noack,
1382 		unsigned int freq, int no_cck,
1383 		int offchanok,
1384 		unsigned int wait_time,
1385 		int cookie)
1386 {
1387 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
1388 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1389 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1390 	struct nrf_wifi_umac_mgmt_tx_info *mgmt_tx_info = NULL;
1391 	unsigned int timeout = 0;
1392 
1393 	if (!if_priv || !data) {
1394 		LOG_ERR("%s: Invalid params", __func__);
1395 		return -1;
1396 	}
1397 
1398 	vif_ctx_zep = if_priv;
1399 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1400 	if (!rpu_ctx_zep) {
1401 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1402 		return -1;
1403 	}
1404 
1405 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1406 	if (!rpu_ctx_zep->rpu_ctx) {
1407 		LOG_DBG("%s: RPU context not initialized", __func__);
1408 		goto out;
1409 	}
1410 
1411 	k_mutex_lock(&mgmt_tx_lock, K_FOREVER);
1412 
1413 	mgmt_tx_info = k_calloc(sizeof(*mgmt_tx_info), sizeof(char));
1414 
1415 	if (!mgmt_tx_info) {
1416 		LOG_ERR("%s: Unable to allocate memory", __func__);
1417 		goto out;
1418 	}
1419 
1420 	if (offchanok) {
1421 		mgmt_tx_info->nrf_wifi_flags |= NRF_WIFI_CMD_FRAME_OFFCHANNEL_TX_OK;
1422 	}
1423 
1424 	if (noack) {
1425 		mgmt_tx_info->nrf_wifi_flags |= NRF_WIFI_CMD_FRAME_DONT_WAIT_FOR_ACK;
1426 	}
1427 
1428 	if (no_cck) {
1429 		mgmt_tx_info->nrf_wifi_flags |= NRF_WIFI_CMD_FRAME_TX_NO_CCK_RATE;
1430 	}
1431 
1432 	if (freq) {
1433 		mgmt_tx_info->frequency = freq;
1434 	}
1435 
1436 	if (wait_time) {
1437 		mgmt_tx_info->dur = wait_time;
1438 	}
1439 
1440 	if (data_len) {
1441 		memcpy(mgmt_tx_info->frame.frame, data, data_len);
1442 		mgmt_tx_info->frame.frame_len = data_len;
1443 	}
1444 
1445 	mgmt_tx_info->freq_params.frequency = freq;
1446 	mgmt_tx_info->freq_params.channel_width = NRF_WIFI_CHAN_WIDTH_20;
1447 	mgmt_tx_info->freq_params.center_frequency1 = freq;
1448 	mgmt_tx_info->freq_params.center_frequency2 = 0;
1449 	mgmt_tx_info->freq_params.channel_type = NRF_WIFI_CHAN_HT20;
1450 
1451 	/* Going to RPU */
1452 	mgmt_tx_info->host_cookie = cookie;
1453 	vif_ctx_zep->cookie_resp_received = false;
1454 
1455 	LOG_DBG("%s: Sending frame to RPU: cookie=%d wait_time=%d no_ack=%d", __func__,
1456 		    cookie, wait_time, noack);
1457 	status = nrf_wifi_fmac_mgmt_tx(rpu_ctx_zep->rpu_ctx,
1458 			vif_ctx_zep->vif_idx,
1459 			mgmt_tx_info);
1460 
1461 	if (status == NRF_WIFI_STATUS_FAIL) {
1462 		LOG_ERR("%s: nrf_wifi_fmac_mgmt_tx failed", __func__);
1463 		goto out;
1464 	}
1465 
1466 	/* Both are needed as we use this to send_action where noack is hardcoded
1467 	 * to 0 always.
1468 	 */
1469 	if (wait_time || !noack) {
1470 		if (!noack && !wait_time) {
1471 			wait_time = ACTION_FRAME_RESP_TIMEOUT_MS;
1472 		}
1473 
1474 		while (!vif_ctx_zep->cookie_resp_received &&
1475 			timeout++ < wait_time) {
1476 			k_sleep(K_MSEC(1));
1477 		}
1478 
1479 		if (!vif_ctx_zep->cookie_resp_received) {
1480 			LOG_ERR("%s: cookie response not received (%dms)", __func__,
1481 				timeout);
1482 			status = NRF_WIFI_STATUS_FAIL;
1483 			goto out;
1484 		}
1485 		status = NRF_WIFI_STATUS_SUCCESS;
1486 	}
1487 
1488 out:
1489 	if (mgmt_tx_info) {
1490 		k_free(mgmt_tx_info);
1491 	}
1492 	k_mutex_unlock(&mgmt_tx_lock);
1493 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1494 	return status;
1495 }
1496 
nrf_wifi_parse_sband(struct nrf_wifi_event_supported_band * event,struct wpa_supp_event_supported_band * band)1497 enum nrf_wifi_status nrf_wifi_parse_sband(
1498 	struct nrf_wifi_event_supported_band *event,
1499 	struct wpa_supp_event_supported_band *band
1500 	)
1501 {
1502 	int count;
1503 
1504 	if (event && (event->nrf_wifi_n_bitrates == 0 || event->nrf_wifi_n_channels == 0)) {
1505 		return NRF_WIFI_STATUS_FAIL;
1506 	}
1507 	memset(band, 0, sizeof(*band));
1508 
1509 	band->wpa_supp_n_channels = event->nrf_wifi_n_channels;
1510 	band->wpa_supp_n_bitrates = event->nrf_wifi_n_bitrates;
1511 
1512 	for (count = 0; count < band->wpa_supp_n_channels; count++) {
1513 		struct wpa_supp_event_channel *chan = &band->channels[count];
1514 
1515 		if (count >= WPA_SUPP_SBAND_MAX_CHANNELS) {
1516 			LOG_ERR("%s: Failed to add channel", __func__);
1517 			break;
1518 		}
1519 
1520 		chan->wpa_supp_flags = event->channels[count].nrf_wifi_flags;
1521 		chan->wpa_supp_max_power = event->channels[count].nrf_wifi_max_power;
1522 		chan->wpa_supp_time = event->channels[count].nrf_wifi_time;
1523 		chan->dfs_cac_msec = event->channels[count].dfs_cac_msec;
1524 		chan->ch_valid = event->channels[count].ch_valid;
1525 		chan->center_frequency = event->channels[count].center_frequency;
1526 		chan->dfs_state = event->channels[count].dfs_state;
1527 	}
1528 
1529 	for (count = 0; count < band->wpa_supp_n_bitrates; count++) {
1530 		struct wpa_supp_event_rate *rate = &band->bitrates[count];
1531 
1532 		if (count >= WPA_SUPP_SBAND_MAX_RATES) {
1533 			LOG_ERR("%s: Failed to add bitrate", __func__);
1534 			break;
1535 		}
1536 
1537 		rate->wpa_supp_flags = event->bitrates[count].nrf_wifi_flags;
1538 		rate->wpa_supp_bitrate = event->bitrates[count].nrf_wifi_bitrate;
1539 	}
1540 
1541 	band->ht_cap.wpa_supp_ht_supported = event->ht_cap.nrf_wifi_ht_supported;
1542 	band->ht_cap.wpa_supp_cap = event->ht_cap.nrf_wifi_cap;
1543 	band->ht_cap.mcs.wpa_supp_rx_highest = event->ht_cap.mcs.nrf_wifi_rx_highest;
1544 
1545 	for (count = 0; count < WPA_SUPP_HT_MCS_MASK_LEN; count++) {
1546 		band->ht_cap.mcs.wpa_supp_rx_mask[count] =
1547 			event->ht_cap.mcs.nrf_wifi_rx_mask[count];
1548 	}
1549 
1550 	band->ht_cap.mcs.wpa_supp_tx_params = event->ht_cap.mcs.nrf_wifi_tx_params;
1551 
1552 	for (count = 0; count < NRF_WIFI_HT_MCS_RES_LEN; count++) {
1553 
1554 		if (count >= WPA_SUPP_HT_MCS_RES_LEN) {
1555 			LOG_ERR("%s: Failed to add reserved bytes", __func__);
1556 			break;
1557 		}
1558 
1559 		band->ht_cap.mcs.wpa_supp_reserved[count] =
1560 			event->ht_cap.mcs.nrf_wifi_reserved[count];
1561 	}
1562 
1563 	band->ht_cap.wpa_supp_ampdu_factor = event->ht_cap.nrf_wifi_ampdu_factor;
1564 	band->ht_cap.wpa_supp_ampdu_density = event->ht_cap.nrf_wifi_ampdu_density;
1565 
1566 	band->vht_cap.wpa_supp_vht_supported = event->vht_cap.nrf_wifi_vht_supported;
1567 	band->vht_cap.wpa_supp_cap = event->vht_cap.nrf_wifi_cap;
1568 
1569 	band->vht_cap.vht_mcs.rx_mcs_map = event->vht_cap.vht_mcs.rx_mcs_map;
1570 	band->vht_cap.vht_mcs.rx_highest = event->vht_cap.vht_mcs.rx_highest;
1571 	band->vht_cap.vht_mcs.tx_mcs_map = event->vht_cap.vht_mcs.tx_mcs_map;
1572 	band->vht_cap.vht_mcs.tx_highest = event->vht_cap.vht_mcs.tx_highest;
1573 
1574 	band->band = event->band;
1575 
1576 	return WLAN_STATUS_SUCCESS;
1577 }
1578 
nrf_wifi_wpa_supp_event_get_wiphy(void * if_priv,struct nrf_wifi_event_get_wiphy * wiphy_info,unsigned int event_len)1579 void nrf_wifi_wpa_supp_event_get_wiphy(void *if_priv,
1580 		struct nrf_wifi_event_get_wiphy *wiphy_info,
1581 		unsigned int event_len)
1582 {
1583 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1584 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1585 	struct wpa_supp_event_supported_band band;
1586 
1587 	if (!if_priv || !wiphy_info || !event_len) {
1588 		LOG_ERR("%s: Invalid parameters", __func__);
1589 		return;
1590 	}
1591 
1592 	vif_ctx_zep = if_priv;
1593 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1594 
1595 	memset(&band, 0, sizeof(band));
1596 
1597 	for (int i = 0; i < NRF_WIFI_EVENT_GET_WIPHY_NUM_BANDS; i++) {
1598 		if (nrf_wifi_parse_sband(&wiphy_info->sband[i], &band) != WLAN_STATUS_SUCCESS) {
1599 			continue;
1600 		}
1601 		if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.get_wiphy_res) {
1602 			vif_ctx_zep->supp_callbk_fns.get_wiphy_res(vif_ctx_zep->supp_drv_if_ctx,
1603 									&band);
1604 		}
1605 	}
1606 
1607 	if ((wiphy_info->params_valid & NRF_WIFI_GET_WIPHY_VALID_EXTENDED_CAPABILITIES) &&
1608 	    rpu_ctx_zep->extended_capa == NULL) {
1609 
1610 		rpu_ctx_zep->extended_capa = k_malloc(wiphy_info->extended_capabilities_len);
1611 
1612 		if (rpu_ctx_zep->extended_capa) {
1613 			memcpy(rpu_ctx_zep->extended_capa, wiphy_info->extended_capabilities,
1614 			       wiphy_info->extended_capabilities_len);
1615 		}
1616 
1617 		rpu_ctx_zep->extended_capa_mask = k_malloc(wiphy_info->extended_capabilities_len);
1618 
1619 		if (rpu_ctx_zep->extended_capa_mask) {
1620 			memcpy(rpu_ctx_zep->extended_capa_mask,
1621 			       wiphy_info->extended_capabilities_mask,
1622 			       wiphy_info->extended_capabilities_len);
1623 		} else {
1624 			free(rpu_ctx_zep->extended_capa);
1625 			rpu_ctx_zep->extended_capa = NULL;
1626 			rpu_ctx_zep->extended_capa_len = 0;
1627 		}
1628 	}
1629 
1630 	if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.get_wiphy_res) {
1631 		vif_ctx_zep->supp_callbk_fns.get_wiphy_res(vif_ctx_zep->supp_drv_if_ctx, NULL);
1632 	}
1633 }
1634 
nrf_wifi_supp_get_wiphy(void * if_priv)1635 int nrf_wifi_supp_get_wiphy(void *if_priv)
1636 {
1637 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
1638 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1639 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1640 
1641 	if (!if_priv) {
1642 		LOG_ERR("%s: Missing interface context", __func__);
1643 		return -1;
1644 	}
1645 
1646 	vif_ctx_zep = if_priv;
1647 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1648 	if (!rpu_ctx_zep) {
1649 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1650 		return -1;
1651 	}
1652 
1653 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1654 	if (!rpu_ctx_zep->rpu_ctx) {
1655 		LOG_DBG("%s: RPU context not initialized", __func__);
1656 		goto out;
1657 	}
1658 
1659 	status = nrf_wifi_fmac_get_wiphy(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx);
1660 
1661 	if (status != NRF_WIFI_STATUS_SUCCESS) {
1662 		LOG_ERR("%s: nrf_wifi_fmac_get_wiphy failed", __func__);
1663 		goto out;
1664 	}
1665 out:
1666 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1667 	return status;
1668 }
1669 
nrf_wifi_supp_register_frame(void * if_priv,u16 type,const u8 * match,size_t match_len,bool multicast)1670 int nrf_wifi_supp_register_frame(void *if_priv,
1671 			u16 type, const u8 *match, size_t match_len,
1672 			bool multicast)
1673 {
1674 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
1675 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1676 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1677 	struct nrf_wifi_umac_mgmt_frame_info frame_info;
1678 
1679 	if (!if_priv || !match || !match_len) {
1680 		LOG_ERR("%s: Invalid parameters", __func__);
1681 		return -1;
1682 	}
1683 
1684 	vif_ctx_zep = if_priv;
1685 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1686 	if (!rpu_ctx_zep) {
1687 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1688 		return -1;
1689 	}
1690 
1691 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1692 	if (!rpu_ctx_zep->rpu_ctx) {
1693 		LOG_DBG("%s: RPU context not initialized", __func__);
1694 		goto out;
1695 	}
1696 
1697 	memset(&frame_info, 0, sizeof(frame_info));
1698 
1699 	frame_info.frame_type = type;
1700 	frame_info.frame_match.frame_match_len = match_len;
1701 	memcpy(frame_info.frame_match.frame_match, match, match_len);
1702 
1703 	status = nrf_wifi_fmac_register_frame(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx,
1704 			&frame_info);
1705 
1706 	if (status != NRF_WIFI_STATUS_SUCCESS) {
1707 		LOG_ERR("%s: nrf_wifi_fmac_register_frame failed", __func__);
1708 		goto out;
1709 	}
1710 out:
1711 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1712 	return status;
1713 }
1714 
nrf_wifi_wpa_supp_event_mgmt_rx_callbk_fn(void * if_priv,struct nrf_wifi_umac_event_mlme * mlme_event,unsigned int event_len)1715 void nrf_wifi_wpa_supp_event_mgmt_rx_callbk_fn(void *if_priv,
1716 					       struct nrf_wifi_umac_event_mlme *mlme_event,
1717 					       unsigned int event_len)
1718 {
1719 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1720 
1721 	if (!if_priv) {
1722 		LOG_ERR("%s: Missing interface context", __func__);
1723 		return;
1724 	}
1725 
1726 	vif_ctx_zep = if_priv;
1727 
1728 	if (!mlme_event || !event_len) {
1729 		LOG_ERR("%s: Missing MLME event data", __func__);
1730 		return;
1731 	}
1732 
1733 	if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.mgmt_rx) {
1734 		vif_ctx_zep->supp_callbk_fns.mgmt_rx(vif_ctx_zep->supp_drv_if_ctx,
1735 				mlme_event->frame.frame,
1736 				mlme_event->frame.frame_len,
1737 				mlme_event->frequency,
1738 				mlme_event->rx_signal_dbm);
1739 	}
1740 }
1741 
nrf_wifi_supp_get_capa(void * if_priv,struct wpa_driver_capa * capa)1742 int nrf_wifi_supp_get_capa(void *if_priv, struct wpa_driver_capa *capa)
1743 {
1744 	enum nrf_wifi_status status = NRF_WIFI_STATUS_SUCCESS;
1745 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1746 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1747 
1748 	if (!if_priv || !capa) {
1749 		LOG_ERR("%s: Invalid parameters", __func__);
1750 		return -1;
1751 	}
1752 
1753 	memset(capa, 0, sizeof(*capa));
1754 
1755 	vif_ctx_zep = if_priv;
1756 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1757 	if (!rpu_ctx_zep) {
1758 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1759 		return -1;
1760 	}
1761 
1762 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1763 	if (!rpu_ctx_zep->rpu_ctx) {
1764 		LOG_DBG("%s: RPU context not initialized", __func__);
1765 		goto out;
1766 	}
1767 
1768 	/* TODO: Get these from RPU*/
1769 	/* Use SME */
1770 	capa->flags = 0;
1771 	capa->flags |= WPA_DRIVER_FLAGS_SME;
1772 	capa->flags |= WPA_DRIVER_FLAGS_SAE;
1773 	capa->flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
1774 	capa->rrm_flags |= WPA_DRIVER_FLAGS_SUPPORT_RRM;
1775 	capa->rrm_flags |= WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT;
1776 	if (IS_ENABLED(CONFIG_NRF70_AP_MODE)) {
1777 		capa->flags |= WPA_DRIVER_FLAGS_AP;
1778 	}
1779 
1780 	capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1781 			WPA_DRIVER_CAPA_ENC_WEP104 |
1782 			WPA_DRIVER_CAPA_ENC_TKIP |
1783 			WPA_DRIVER_CAPA_ENC_CCMP |
1784 			WPA_DRIVER_CAPA_ENC_CCMP |
1785 			WPA_DRIVER_CAPA_ENC_CCMP_256 |
1786 			WPA_DRIVER_CAPA_ENC_GCMP_256;
1787 
1788 	if (rpu_ctx_zep->extended_capa && rpu_ctx_zep->extended_capa_mask) {
1789 		capa->extended_capa = rpu_ctx_zep->extended_capa;
1790 		capa->extended_capa_mask = rpu_ctx_zep->extended_capa_mask;
1791 		capa->extended_capa_len = rpu_ctx_zep->extended_capa_len;
1792 	}
1793 out:
1794 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1795 	return status;
1796 }
1797 
1798 
nrf_wifi_wpa_supp_event_mac_chgd(void * if_priv)1799 void nrf_wifi_wpa_supp_event_mac_chgd(void *if_priv)
1800 {
1801 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1802 
1803 	if (!if_priv) {
1804 		LOG_ERR("%s: Invalid parameters", __func__);
1805 		return;
1806 	}
1807 
1808 	vif_ctx_zep = if_priv;
1809 
1810 	if (vif_ctx_zep->supp_drv_if_ctx && vif_ctx_zep->supp_callbk_fns.mac_changed) {
1811 		vif_ctx_zep->supp_callbk_fns.mac_changed(vif_ctx_zep->supp_drv_if_ctx);
1812 	}
1813 
1814 }
1815 
1816 
nrf_wifi_supp_get_conn_info(void * if_priv,struct wpa_conn_info * info)1817 int nrf_wifi_supp_get_conn_info(void *if_priv, struct wpa_conn_info *info)
1818 {
1819 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1820 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1821 	struct nrf_wifi_fmac_dev_ctx *fmac_dev_ctx = NULL;
1822 	enum nrf_wifi_status ret = NRF_WIFI_STATUS_FAIL;
1823 	int sem_ret;
1824 
1825 	if (!if_priv || !info) {
1826 		LOG_ERR("%s: Invalid params", __func__);
1827 		return ret;
1828 	}
1829 
1830 	vif_ctx_zep = if_priv;
1831 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1832 	if (!rpu_ctx_zep) {
1833 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1834 		return ret;
1835 	}
1836 
1837 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1838 	if (!rpu_ctx_zep->rpu_ctx) {
1839 		LOG_DBG("%s: RPU context not initialized", __func__);
1840 		goto out;
1841 	}
1842 
1843 	fmac_dev_ctx = rpu_ctx_zep->rpu_ctx;
1844 
1845 	vif_ctx_zep->conn_info = info;
1846 	ret = nrf_wifi_fmac_get_conn_info(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx);
1847 	if (ret != NRF_WIFI_STATUS_SUCCESS) {
1848 		LOG_ERR("%s: nrf_wifi_fmac_get_conn_info failed", __func__);
1849 		goto out;
1850 	}
1851 
1852 	sem_ret = k_sem_take(&wait_for_event_sem, K_MSEC(RPU_RESP_EVENT_TIMEOUT));
1853 	if (sem_ret) {
1854 		LOG_ERR("%s: Timeout: failed to get connection info, ret = %d", __func__, sem_ret);
1855 		ret = NRF_WIFI_STATUS_FAIL;
1856 		goto out;
1857 	}
1858 
1859 out:
1860 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1861 	return ret;
1862 }
1863 
nrf_wifi_supp_set_country(void * if_priv,const char * alpha2)1864 int nrf_wifi_supp_set_country(void *if_priv, const char *alpha2)
1865 {
1866 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1867 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1868 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
1869 	struct nrf_wifi_fmac_reg_info reg_domain_info = {0};
1870 
1871 	if (!if_priv || !alpha2) {
1872 		LOG_ERR("%s: Invalid params", __func__);
1873 		return -1;
1874 	}
1875 
1876 	vif_ctx_zep = if_priv;
1877 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1878 	if (!rpu_ctx_zep) {
1879 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1880 		return -1;
1881 	}
1882 
1883 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1884 	if (!rpu_ctx_zep->rpu_ctx) {
1885 		LOG_DBG("%s: RPU context not initialized", __func__);
1886 		goto out;
1887 	}
1888 
1889 	memcpy(reg_domain_info.alpha2, alpha2, NRF_WIFI_COUNTRY_CODE_LEN);
1890 
1891 	status = nrf_wifi_fmac_set_reg(rpu_ctx_zep->rpu_ctx, &reg_domain_info);
1892 	if (status != NRF_WIFI_STATUS_SUCCESS) {
1893 		LOG_ERR("%s: nrf_wifi_fmac_set_reg failed", __func__);
1894 		goto out;
1895 	}
1896 out:
1897 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1898 	return status;
1899 }
1900 
nrf_wifi_supp_get_country(void * if_priv,char * alpha2)1901 int nrf_wifi_supp_get_country(void *if_priv, char *alpha2)
1902 {
1903 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1904 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1905 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
1906 	struct nrf_wifi_fmac_reg_info reg_domain_info = {0};
1907 
1908 	if (!if_priv || !alpha2) {
1909 		LOG_ERR("%s: Invalid params", __func__);
1910 		return -1;
1911 	}
1912 
1913 	vif_ctx_zep = if_priv;
1914 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1915 	if (!rpu_ctx_zep) {
1916 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1917 		return -1;
1918 	}
1919 
1920 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1921 	if (!rpu_ctx_zep->rpu_ctx) {
1922 		LOG_DBG("%s: RPU context not initialized", __func__);
1923 		goto out;
1924 	}
1925 
1926 	status = nrf_wifi_fmac_get_reg(rpu_ctx_zep->rpu_ctx, &reg_domain_info);
1927 	if (status != NRF_WIFI_STATUS_SUCCESS) {
1928 		LOG_ERR("%s: nrf_wifi_fmac_get_reg failed", __func__);
1929 		goto out;
1930 	}
1931 
1932 	memcpy(alpha2, reg_domain_info.alpha2, NRF_WIFI_COUNTRY_CODE_LEN);
1933 out:
1934 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
1935 	return status;
1936 }
1937 
nrf_wifi_supp_event_proc_get_conn_info(void * if_priv,struct nrf_wifi_umac_event_conn_info * info,unsigned int event_len)1938 void nrf_wifi_supp_event_proc_get_conn_info(void *if_priv,
1939 					   struct nrf_wifi_umac_event_conn_info *info,
1940 					   unsigned int event_len)
1941 {
1942 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
1943 	struct wpa_conn_info *conn_info = NULL;
1944 
1945 	if (!if_priv || !info) {
1946 		LOG_ERR("%s: Invalid params", __func__);
1947 		k_sem_give(&wait_for_event_sem);
1948 		return;
1949 	}
1950 	vif_ctx_zep = if_priv;
1951 	conn_info = vif_ctx_zep->conn_info;
1952 
1953 	conn_info->beacon_interval = info->beacon_interval;
1954 	conn_info->dtim_period = info->dtim_interval;
1955 	conn_info->twt_capable = info->twt_capable;
1956 	k_sem_give(&wait_for_event_sem);
1957 }
1958 
1959 #ifdef CONFIG_NRF70_AP_MODE
nrf_wifi_vif_state_change(struct nrf_wifi_vif_ctx_zep * vif_ctx_zep,enum nrf_wifi_fmac_if_op_state state)1960 static int nrf_wifi_vif_state_change(struct nrf_wifi_vif_ctx_zep *vif_ctx_zep,
1961 	enum nrf_wifi_fmac_if_op_state state)
1962 {
1963 	struct nrf_wifi_umac_chg_vif_state_info vif_state_info = {0};
1964 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
1965 	unsigned int timeout = 0;
1966 	struct nrf_wifi_fmac_vif_ctx *vif_ctx = NULL;
1967 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
1968 	struct nrf_wifi_fmac_dev_ctx_def *def_dev_ctx = NULL;
1969 	int ret = -1;
1970 
1971 	if (!vif_ctx_zep) {
1972 		LOG_ERR("%s: Invalid params", __func__);
1973 		return ret;
1974 	}
1975 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
1976 	if (!rpu_ctx_zep) {
1977 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
1978 		return ret;
1979 	}
1980 
1981 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
1982 	if (!rpu_ctx_zep->rpu_ctx) {
1983 		LOG_DBG("%s: RPU context not initialized", __func__);
1984 		goto out;
1985 	}
1986 
1987 	def_dev_ctx = wifi_dev_priv(rpu_ctx_zep->rpu_ctx);
1988 	vif_ctx = def_dev_ctx->vif_ctx[vif_ctx_zep->vif_idx];
1989 
1990 	vif_state_info.state = state;
1991 	vif_state_info.if_index = vif_ctx_zep->vif_idx;
1992 	vif_ctx->ifflags = false;
1993 	status = nrf_wifi_fmac_chg_vif_state(rpu_ctx_zep->rpu_ctx,
1994 					     vif_ctx_zep->vif_idx,
1995 					     &vif_state_info);
1996 
1997 	if (status != NRF_WIFI_STATUS_SUCCESS) {
1998 		LOG_ERR("%s: nrf_wifi_fmac_chg_vif_state failed",
1999 			__func__);
2000 		goto out;
2001 	}
2002 
2003 	while (!vif_ctx->ifflags && timeout++ < SET_IFACE_EVENT_TIMEOUT_MS) {
2004 		k_sleep(K_MSEC(1));
2005 	}
2006 
2007 	if (!vif_ctx->ifflags) {
2008 		LOG_ERR("%s: set interface state event not received (%dms)", __func__, timeout);
2009 		goto out;
2010 	}
2011 	ret = 0;
2012 out:
2013 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2014 	return ret;
2015 }
2016 
nrf_wifi_iftype_change(struct nrf_wifi_vif_ctx_zep * vif_ctx_zep,int iftype)2017 static int nrf_wifi_iftype_change(struct nrf_wifi_vif_ctx_zep *vif_ctx_zep, int iftype)
2018 {
2019 	struct nrf_wifi_umac_chg_vif_attr_info chg_vif_info = {0};
2020 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2021 	int ret = -1;
2022 	unsigned int timeout = 0;
2023 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2024 
2025 	if (!vif_ctx_zep) {
2026 		LOG_ERR("%s: Invalid params", __func__);
2027 		return ret;
2028 	}
2029 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2030 	if (!rpu_ctx_zep) {
2031 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2032 		return ret;
2033 	}
2034 
2035 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2036 	if (!rpu_ctx_zep->rpu_ctx) {
2037 		LOG_DBG("%s: RPU context not initialized", __func__);
2038 		goto out;
2039 	}
2040 
2041 	ret = nrf_wifi_vif_state_change(vif_ctx_zep, NRF_WIFI_FMAC_IF_OP_STATE_DOWN);
2042 	if (ret) {
2043 		LOG_ERR("%s: Failed to set interface down", __func__);
2044 		goto out;
2045 	}
2046 
2047 	chg_vif_info.iftype = iftype;
2048 	vif_ctx_zep->set_if_event_received = false;
2049 	vif_ctx_zep->set_if_status = 0;
2050 	status = nrf_wifi_fmac_chg_vif(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &chg_vif_info);
2051 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2052 		LOG_ERR("%s: nrf_wifi_fmac_chg_vif failed", __func__);
2053 		goto out;
2054 	}
2055 
2056 	while (!vif_ctx_zep->set_if_event_received &&
2057 		  timeout++ < SET_IFACE_EVENT_TIMEOUT_MS) {
2058 		k_sleep(K_MSEC(1));
2059 	}
2060 
2061 	if (!vif_ctx_zep->set_if_event_received) {
2062 		LOG_ERR("%s: set interface event not received (%dms)", __func__, timeout);
2063 		goto out;
2064 	}
2065 
2066 	if (vif_ctx_zep->set_if_status != NRF_WIFI_STATUS_SUCCESS) {
2067 		LOG_ERR("%s: set interface failed: %d", __func__, vif_ctx_zep->set_if_status);
2068 		goto out;
2069 	}
2070 
2071 	ret = nrf_wifi_vif_state_change(vif_ctx_zep, NRF_WIFI_FMAC_IF_OP_STATE_UP);
2072 	if (ret) {
2073 		LOG_ERR("%s: Failed to set interface up", __func__);
2074 		goto out;
2075 	}
2076 	ret = 0;
2077 out:
2078 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2079 	return ret;
2080 }
2081 
nrf_wifi_wait_for_carrier_status(struct nrf_wifi_vif_ctx_zep * vif_ctx_zep,enum nrf_wifi_fmac_if_carr_state carrier_status)2082 static int nrf_wifi_wait_for_carrier_status(struct nrf_wifi_vif_ctx_zep *vif_ctx_zep,
2083 	enum nrf_wifi_fmac_if_carr_state carrier_status)
2084 {
2085 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2086 	int ret = -1;
2087 	unsigned int timeout = 0;
2088 
2089 	if (!vif_ctx_zep) {
2090 		LOG_ERR("%s: Invalid params", __func__);
2091 		return ret;
2092 	}
2093 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2094 	if (!rpu_ctx_zep) {
2095 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2096 		return ret;
2097 	}
2098 
2099 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2100 	if (!rpu_ctx_zep->rpu_ctx) {
2101 		LOG_DBG("%s: RPU context not initialized", __func__);
2102 		goto out;
2103 	}
2104 
2105 	while (vif_ctx_zep->if_carr_state != carrier_status &&
2106 	       timeout++ < CARR_ON_TIMEOUT_MS) {
2107 		k_sleep(K_MSEC(1));
2108 	}
2109 
2110 	if (vif_ctx_zep->if_carr_state != carrier_status) {
2111 		LOG_ERR("%s: Carrier %s event not received in %dms", __func__,
2112 			carrier_status == NRF_WIFI_FMAC_IF_CARR_STATE_ON ? "ON" : "OFF",
2113 			timeout);
2114 		goto out;
2115 	}
2116 
2117 	ret = 0;
2118 out:
2119 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2120 	return ret;
2121 }
2122 
nrf_wifi_wpa_supp_init_ap(void * if_priv,struct wpa_driver_associate_params * params)2123 int nrf_wifi_wpa_supp_init_ap(void *if_priv, struct wpa_driver_associate_params *params)
2124 {
2125 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2126 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2127 
2128 	int ret = -1;
2129 
2130 	if (!if_priv || !params) {
2131 		LOG_ERR("%s: Invalid params", __func__);
2132 		return ret;
2133 	}
2134 
2135 	if (params->mode != IEEE80211_MODE_AP) {
2136 		LOG_ERR("%s: Invalid mode", __func__);
2137 		return ret;
2138 	}
2139 
2140 	vif_ctx_zep = if_priv;
2141 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2142 	if (!rpu_ctx_zep) {
2143 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2144 		return ret;
2145 	}
2146 
2147 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2148 	if (!rpu_ctx_zep->rpu_ctx) {
2149 		LOG_DBG("%s: RPU context not initialized", __func__);
2150 		goto out;
2151 	}
2152 
2153 	ret = nrf_wifi_iftype_change(vif_ctx_zep, NRF_WIFI_IFTYPE_AP);
2154 	if (ret) {
2155 		LOG_ERR("%s: Failed to set interface type to AP: %d", __func__, ret);
2156 		goto out;
2157 	}
2158 
2159 out:
2160 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2161 	return ret;
2162 }
2163 
wpas_cipher_to_nrf(int cipher)2164 static int wpas_cipher_to_nrf(int cipher)
2165 {
2166 	switch (cipher) {
2167 	case WPA_CIPHER_NONE:
2168 		return 0;
2169 	case WPA_CIPHER_WEP40:
2170 		return NRF_WIFI_FMAC_CIPHER_SUITE_WEP40;
2171 	case WPA_CIPHER_WEP104:
2172 		return NRF_WIFI_FMAC_CIPHER_SUITE_WEP104;
2173 	case WPA_CIPHER_TKIP:
2174 		return NRF_WIFI_FMAC_CIPHER_SUITE_TKIP;
2175 	case WPA_CIPHER_CCMP:
2176 		return NRF_WIFI_FMAC_CIPHER_SUITE_CCMP;
2177 	case WPA_CIPHER_CCMP_256:
2178 		return NRF_WIFI_FMAC_CIPHER_SUITE_CCMP_256;
2179 	default:
2180 		return -1;
2181 	}
2182 }
2183 
nrf_wifi_set_beacon_data(const struct wpa_driver_ap_params * params,struct nrf_wifi_beacon_data * beacon_data)2184 static int nrf_wifi_set_beacon_data(const struct wpa_driver_ap_params *params,
2185 		struct nrf_wifi_beacon_data *beacon_data)
2186 {
2187 	int ret = -1;
2188 
2189 	if (params->head_len > ARRAY_SIZE(beacon_data->head)) {
2190 		LOG_ERR("%s: head_len too big", __func__);
2191 		goto out;
2192 	}
2193 
2194 	if (params->tail_len > ARRAY_SIZE(beacon_data->tail)) {
2195 		LOG_ERR("%s: tail_len too big", __func__);
2196 		goto out;
2197 	}
2198 
2199 	if (params->proberesp_len > ARRAY_SIZE(beacon_data->probe_resp)) {
2200 		LOG_ERR("%s: proberesp_len too big", __func__);
2201 		goto out;
2202 	}
2203 
2204 	beacon_data->head_len = params->head_len;
2205 	beacon_data->tail_len = params->tail_len;
2206 	beacon_data->probe_resp_len = params->proberesp_len;
2207 
2208 	if (params->head_len) {
2209 		memcpy(&beacon_data->head, params->head,
2210 		       params->head_len);
2211 	}
2212 
2213 	if (params->tail_len) {
2214 		memcpy(&beacon_data->tail, params->tail,
2215 		       params->tail_len);
2216 	}
2217 
2218 	if (params->proberesp_len) {
2219 		memcpy(&beacon_data->probe_resp, params->proberesp_ies,
2220 		       params->proberesp_len);
2221 	}
2222 
2223 	ret = 0;
2224 out:
2225 	return ret;
2226 }
2227 
nrf_wifi_supp_register_mgmt_frame(void * if_priv,u16 frame_type,size_t match_len,const u8 * match)2228 int nrf_wifi_supp_register_mgmt_frame(void *if_priv,
2229 	u16 frame_type, size_t match_len, const u8 *match)
2230 {
2231 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2232 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2233 	struct nrf_wifi_umac_mgmt_frame_info mgmt_frame_info = {0};
2234 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2235 	int ret = -1;
2236 
2237 	if (!if_priv || (match_len && !match)) {
2238 		LOG_ERR("%s: Invalid params", __func__);
2239 		return ret;
2240 	}
2241 
2242 	vif_ctx_zep = if_priv;
2243 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2244 	if (!rpu_ctx_zep) {
2245 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2246 		return ret;
2247 	}
2248 
2249 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2250 	if (!rpu_ctx_zep->rpu_ctx) {
2251 		LOG_DBG("%s: RPU context not initialized", __func__);
2252 		goto out;
2253 	}
2254 
2255 	mgmt_frame_info.frame_type = frame_type;
2256 	mgmt_frame_info.frame_match.frame_match_len = match_len;
2257 	if (match_len >= NRF_WIFI_FRAME_MATCH_MAX_LEN) {
2258 		LOG_ERR("%s: match_len too big: %d (max %d)", __func__, match_len,
2259 			NRF_WIFI_FRAME_MATCH_MAX_LEN);
2260 		goto out;
2261 	}
2262 	memcpy(mgmt_frame_info.frame_match.frame_match, match, match_len);
2263 
2264 	status = nrf_wifi_fmac_mgmt_frame_reg(rpu_ctx_zep->rpu_ctx,
2265 					      vif_ctx_zep->vif_idx,
2266 					      &mgmt_frame_info);
2267 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2268 		LOG_ERR("%s: nrf_wifi_fmac_mgmt_frame_reg failed", __func__);
2269 		goto out;
2270 	}
2271 
2272 	ret = 0;
2273 out:
2274 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2275 	return ret;
2276 }
2277 
2278 /* As per current design default is always STA */
is_ap_dynamic_iface(struct nrf_wifi_vif_ctx_zep * vif_ctx_zep)2279 static int is_ap_dynamic_iface(struct nrf_wifi_vif_ctx_zep *vif_ctx_zep)
2280 {
2281 	return (vif_ctx_zep->vif_idx != 0);
2282 }
2283 
nrf_wifi_set_bss(struct nrf_wifi_vif_ctx_zep * vif_ctx_zep,struct wpa_driver_ap_params * params)2284 static int nrf_wifi_set_bss(struct nrf_wifi_vif_ctx_zep *vif_ctx_zep,
2285 	 struct wpa_driver_ap_params *params)
2286 {
2287 	struct nrf_wifi_umac_bss_info bss_info = {0};
2288 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2289 	int ret = -1;
2290 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2291 	int i;
2292 
2293 	if (!vif_ctx_zep || !params) {
2294 		LOG_ERR("%s: Invalid params", __func__);
2295 		return ret;
2296 	}
2297 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2298 	if (!rpu_ctx_zep) {
2299 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2300 		return ret;
2301 	}
2302 
2303 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2304 	if (!rpu_ctx_zep->rpu_ctx) {
2305 		LOG_DBG("%s: RPU context not initialized", __func__);
2306 		goto out;
2307 	}
2308 
2309 	if (params->basic_rates) {
2310 		for (i = 0; params->basic_rates[i] != -1; i++) {
2311 			if (i >= ARRAY_SIZE(bss_info.basic_rates)) {
2312 				LOG_ERR("%s: basic_rates too big: %d (max %d)", __func__, i,
2313 					ARRAY_SIZE(bss_info.basic_rates));
2314 				goto out;
2315 			}
2316 			bss_info.basic_rates[i] = params->basic_rates[i];
2317 		}
2318 		bss_info.num_basic_rates = i;
2319 	}
2320 	bss_info.p2p_go_ctwindow = params->p2p_go_ctwindow;
2321 	bss_info.ht_opmode = params->ht_opmode;
2322 	bss_info.nrf_wifi_cts = params->cts_protect;
2323 	bss_info.preamble = params->preamble;
2324 	bss_info.nrf_wifi_slot = params->short_slot_time;
2325 	bss_info.ap_isolate = params->isolate;
2326 
2327 	status = nrf_wifi_fmac_set_bss(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &bss_info);
2328 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2329 		LOG_ERR("%s: nrf_wifi_fmac_set_bss failed", __func__);
2330 		goto out;
2331 	}
2332 
2333 	ret = 0;
2334 out:
2335 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2336 	return ret;
2337 }
2338 
2339 
wpa_supp_chan_width_to_nrf(enum hostapd_hw_mode mode,int bandwidth,int cfreq1,int cfreq2)2340 static enum nrf_wifi_chan_width wpa_supp_chan_width_to_nrf(enum hostapd_hw_mode mode,
2341 	int bandwidth, int cfreq1, int cfreq2)
2342 {
2343 	switch (bandwidth) {
2344 	case 20:
2345 		if (mode == HOSTAPD_MODE_IEEE80211B) {
2346 			return NRF_WIFI_CHAN_WIDTH_20_NOHT;
2347 		} else {
2348 			return NRF_WIFI_CHAN_WIDTH_20;
2349 		};
2350 	case 40:
2351 		return NRF_WIFI_CHAN_WIDTH_40;
2352 	case 80:
2353 		if (cfreq2) {
2354 			return NRF_WIFI_CHAN_WIDTH_80P80;
2355 		} else {
2356 			return NRF_WIFI_CHAN_WIDTH_80;
2357 		}
2358 	case 160:
2359 		return NRF_WIFI_CHAN_WIDTH_160;
2360 	};
2361 
2362 	return NRF_WIFI_CHAN_WIDTH_20;
2363 }
2364 
nrf_wifi_wpa_supp_start_ap(void * if_priv,struct wpa_driver_ap_params * params)2365 int nrf_wifi_wpa_supp_start_ap(void *if_priv, struct wpa_driver_ap_params *params)
2366 {
2367 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2368 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2369 	int ret = -1;
2370 	struct nrf_wifi_umac_start_ap_info start_ap_info = {0};
2371 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2372 	int ch_width = 0;
2373 
2374 	if (!if_priv || !params) {
2375 		LOG_ERR("%s: Invalid params", __func__);
2376 		return ret;
2377 	}
2378 
2379 	vif_ctx_zep = if_priv;
2380 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2381 	if (!rpu_ctx_zep) {
2382 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2383 		return ret;
2384 	}
2385 
2386 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2387 	if (!rpu_ctx_zep->rpu_ctx) {
2388 		LOG_DBG("%s: RPU context not initialized", __func__);
2389 		goto out;
2390 	}
2391 
2392 	nrf_wifi_set_beacon_data(params, &start_ap_info.beacon_data);
2393 	start_ap_info.beacon_interval = params->beacon_int;
2394 	start_ap_info.dtim_period = params->dtim_period;
2395 	start_ap_info.ssid.nrf_wifi_ssid_len = params->ssid_len;
2396 	memcpy(start_ap_info.ssid.nrf_wifi_ssid, params->ssid, params->ssid_len);
2397 	for (int i = 0; i < 32; i++) {
2398 		if ((params->pairwise_ciphers & BIT(i)) &&
2399 		    start_ap_info.connect_common_info.num_cipher_suites_pairwise < 7) {
2400 			if (wpas_cipher_to_nrf(i) < 0) {
2401 				LOG_DBG("%s: Unsupported cipher %d ignored", __func__, i);
2402 				continue;
2403 			}
2404 			start_ap_info.connect_common_info.cipher_suites_pairwise[i] =
2405 				wpas_cipher_to_nrf(i);
2406 			start_ap_info.connect_common_info.num_cipher_suites_pairwise++;
2407 		}
2408 	}
2409 
2410 	ch_width = wpa_supp_chan_width_to_nrf(params->freq->mode, params->freq->bandwidth,
2411 			params->freq->center_freq1, params->freq->center_freq2);
2412 
2413 	start_ap_info.freq_params.frequency = params->freq->freq;
2414 	start_ap_info.freq_params.channel_width = ch_width;
2415 	start_ap_info.freq_params.center_frequency1 = params->freq->center_freq1;
2416 	start_ap_info.freq_params.center_frequency2 = params->freq->center_freq2;
2417 	start_ap_info.freq_params.channel_type = params->freq->ht_enabled ? NRF_WIFI_CHAN_HT20 :
2418 						NRF_WIFI_CHAN_NO_HT;
2419 	start_ap_info.freq_params.valid_fields = NRF_WIFI_SET_FREQ_PARAMS_FREQ_VALID |
2420 		NRF_WIFI_SET_FREQ_PARAMS_CHANNEL_WIDTH_VALID |
2421 		NRF_WIFI_SET_FREQ_PARAMS_CENTER_FREQ1_VALID |
2422 		NRF_WIFI_SET_FREQ_PARAMS_CENTER_FREQ2_VALID |
2423 		NRF_WIFI_SET_FREQ_PARAMS_CHANNEL_TYPE_VALID;
2424 
2425 	vif_ctx_zep->if_carr_state = NRF_WIFI_FMAC_IF_CARR_STATE_OFF;
2426 	status = nrf_wifi_fmac_start_ap(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &start_ap_info);
2427 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2428 		LOG_ERR("%s: nrf_wifi_fmac_start_ap failed", __func__);
2429 		goto out;
2430 	}
2431 
2432 	ret = nrf_wifi_wait_for_carrier_status(vif_ctx_zep, NRF_WIFI_FMAC_IF_CARR_STATE_ON);
2433 	if (ret) {
2434 		goto out;
2435 	}
2436 
2437 	ret = nrf_wifi_set_bss(vif_ctx_zep, params);
2438 	if (ret) {
2439 		LOG_ERR("%s: Failed to set BSS", __func__);
2440 		goto out;
2441 	}
2442 
2443 	ret = 0;
2444 out:
2445 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2446 	return ret;
2447 }
2448 
nrf_wifi_wpa_supp_change_beacon(void * if_priv,struct wpa_driver_ap_params * params)2449 int nrf_wifi_wpa_supp_change_beacon(void *if_priv, struct wpa_driver_ap_params *params)
2450 {
2451 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2452 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2453 	int ret = -1;
2454 	struct nrf_wifi_umac_set_beacon_info chg_bcn_info = {0};
2455 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2456 
2457 	if (!if_priv || !params) {
2458 		LOG_ERR("%s: Invalid params", __func__);
2459 		return ret;
2460 	}
2461 
2462 	vif_ctx_zep = if_priv;
2463 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2464 	if (!rpu_ctx_zep) {
2465 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2466 		return ret;
2467 	}
2468 
2469 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2470 	if (!rpu_ctx_zep->rpu_ctx) {
2471 		LOG_DBG("%s: RPU context not initialized", __func__);
2472 		goto out;
2473 	}
2474 
2475 	nrf_wifi_set_beacon_data(params, &chg_bcn_info.beacon_data);
2476 
2477 	status = nrf_wifi_fmac_chg_bcn(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &chg_bcn_info);
2478 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2479 		LOG_ERR("%s: nrf_wifi_fmac_chg_bcn failed", __func__);
2480 		goto out;
2481 	}
2482 
2483 	ret = 0;
2484 out:
2485 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2486 	return ret;
2487 }
2488 
nrf_wifi_wpa_supp_stop_ap(void * if_priv)2489 int nrf_wifi_wpa_supp_stop_ap(void *if_priv)
2490 {
2491 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2492 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2493 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2494 	int ret = -1;
2495 
2496 	if (!if_priv) {
2497 		LOG_ERR("%s: Invalid params", __func__);
2498 		return ret;
2499 	}
2500 
2501 	vif_ctx_zep = if_priv;
2502 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2503 	if (!rpu_ctx_zep) {
2504 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2505 		return ret;
2506 	}
2507 
2508 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2509 	if (!rpu_ctx_zep->rpu_ctx) {
2510 		LOG_DBG("%s: RPU context not initialized", __func__);
2511 		goto out;
2512 	}
2513 
2514 	status = nrf_wifi_fmac_stop_ap(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx);
2515 
2516 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2517 		LOG_ERR("%s: nrf_wifi_fmac_stop_ap failed", __func__);
2518 		goto out;
2519 	}
2520 
2521 	ret = nrf_wifi_wait_for_carrier_status(vif_ctx_zep, NRF_WIFI_FMAC_IF_CARR_STATE_OFF);
2522 	if (ret) {
2523 		goto out;
2524 	}
2525 
2526 	ret = 0;
2527 out:
2528 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2529 	return ret;
2530 }
2531 
nrf_wifi_wpa_supp_deinit_ap(void * if_priv)2532 int nrf_wifi_wpa_supp_deinit_ap(void *if_priv)
2533 {
2534 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2535 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2536 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2537 	int ret = -1;
2538 
2539 	if (!if_priv) {
2540 		LOG_ERR("%s: Invalid params", __func__);
2541 		return ret;
2542 	}
2543 
2544 	vif_ctx_zep = if_priv;
2545 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2546 	if (!rpu_ctx_zep) {
2547 		LOG_DBG("%s: rpu_ctx_zep is NULL", __func__);
2548 		return ret;
2549 	}
2550 
2551 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2552 	if (!rpu_ctx_zep->rpu_ctx) {
2553 		LOG_DBG("%s: RPU context not initialized", __func__);
2554 		goto out;
2555 	}
2556 
2557 	status = nrf_wifi_wpa_supp_stop_ap(if_priv);
2558 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2559 		LOG_ERR("%s: Failed to stop AP", __func__);
2560 		goto out;
2561 	}
2562 
2563 	if (!is_ap_dynamic_iface(vif_ctx_zep)) {
2564 		ret = nrf_wifi_iftype_change(vif_ctx_zep, NRF_WIFI_IFTYPE_STATION);
2565 		if (ret) {
2566 			LOG_ERR("%s: Failed to set interface type to STATION: %d", __func__, ret);
2567 			goto out;
2568 		}
2569 	}
2570 	ret = 0;
2571 out:
2572 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2573 	return ret;
2574 }
2575 
nrf_wifi_sta_flags_to_nrf(int wpas_sta_flags)2576 int nrf_wifi_sta_flags_to_nrf(int wpas_sta_flags)
2577 {
2578 	int nrf_sta_flags = 0;
2579 
2580 	if (wpas_sta_flags & WPA_STA_AUTHORIZED) {
2581 		nrf_sta_flags |= NRF_WIFI_STA_FLAG_AUTHORIZED;
2582 	}
2583 	if (wpas_sta_flags & WPA_STA_WMM) {
2584 		nrf_sta_flags |= NRF_WIFI_STA_FLAG_WME;
2585 	}
2586 	if (wpas_sta_flags & WPA_STA_SHORT_PREAMBLE) {
2587 		nrf_sta_flags |= NRF_WIFI_STA_FLAG_SHORT_PREAMBLE;
2588 	}
2589 	if (wpas_sta_flags & WPA_STA_MFP) {
2590 		nrf_sta_flags |= NRF_WIFI_STA_FLAG_MFP;
2591 	}
2592 	if (wpas_sta_flags & WPA_STA_TDLS_PEER) {
2593 		nrf_sta_flags |= NRF_WIFI_STA_FLAG_TDLS_PEER;
2594 	}
2595 	/* Note: Do not set flags > NRF_WIFI_STA_FLAG_TDLS_PEER, else
2596 	 * nrf_wifi_fmac_chg_sta will fail. This is equivalent to not
2597 	 * setting WPA_DRIVER_FLAGS_FULL_AP_CLIENT_STATE flag.
2598 	 */
2599 
2600 	return nrf_sta_flags;
2601 }
2602 
nrf_wifi_wpa_supp_sta_add(void * if_priv,struct hostapd_sta_add_params * params)2603 int nrf_wifi_wpa_supp_sta_add(void *if_priv, struct hostapd_sta_add_params *params)
2604 {
2605 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2606 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2607 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2608 	struct nrf_wifi_umac_add_sta_info sta_info = {0};
2609 	int ret = -1;
2610 	int i;
2611 
2612 	if (!if_priv || !params) {
2613 		LOG_ERR("%s: Invalid params", __func__);
2614 		return ret;
2615 	}
2616 
2617 	vif_ctx_zep = if_priv;
2618 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2619 	if (!rpu_ctx_zep) {
2620 		LOG_ERR("%s: rpu_ctx_zep is NULL", __func__);
2621 		return ret;
2622 	}
2623 
2624 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2625 	if (!rpu_ctx_zep->rpu_ctx) {
2626 		LOG_DBG("%s: RPU context not initialized", __func__);
2627 		goto out;
2628 	}
2629 
2630 	sta_info.nrf_wifi_listen_interval = params->listen_interval;
2631 	sta_info.aid = params->aid;
2632 	sta_info.sta_capability = params->capability;
2633 	sta_info.supp_rates.nrf_wifi_num_rates = params->supp_rates_len;
2634 	/* TODO: sta_info.supp_rates.band */
2635 	for (i = 0; i < params->supp_rates_len; i++) {
2636 		sta_info.supp_rates.rates[i] = params->supp_rates[i] & 0x7f;
2637 	}
2638 
2639 	sta_info.ext_capability.ext_capability_len = params->ext_capab_len;
2640 	if (params->ext_capab_len >= NRF_WIFI_EXT_CAPABILITY_MAX_LEN) {
2641 		LOG_ERR("%s: ext_capab_len too big: %d (max %d)", __func__,
2642 			params->ext_capab_len, NRF_WIFI_EXT_CAPABILITY_MAX_LEN);
2643 		goto out;
2644 	}
2645 	memcpy(sta_info.ext_capability.ext_capability, params->ext_capab,
2646 	       params->ext_capab_len);
2647 
2648 	sta_info.supported_channels.supported_channels_len = params->supp_channels_len;
2649 	if (params->supp_channels_len >= NRF_WIFI_SUPPORTED_CHANNELS_MAX_LEN) {
2650 		LOG_ERR("%s: supp_channels_len too big: %d (max %d)", __func__,
2651 			params->supp_channels_len, NRF_WIFI_SUPPORTED_CHANNELS_MAX_LEN);
2652 		goto out;
2653 	}
2654 	memcpy(sta_info.supported_channels.supported_channels, params->supp_channels,
2655 	       params->supp_channels_len);
2656 
2657 	sta_info.supported_oper_classes.supported_oper_classes_len = params->supp_oper_classes_len;
2658 	if (params->supp_oper_classes_len >= NRF_WIFI_OPER_CLASSES_MAX_LEN) {
2659 		LOG_ERR("%s: supp_oper_classes_len too big: %d (max %d)", __func__,
2660 			params->supp_oper_classes_len, NRF_WIFI_OPER_CLASSES_MAX_LEN);
2661 		goto out;
2662 	}
2663 	memcpy(sta_info.supported_oper_classes.supported_oper_classes, params->supp_oper_classes,
2664 	       params->supp_oper_classes_len);
2665 
2666 	sta_info.sta_flags2.nrf_wifi_set = nrf_wifi_sta_flags_to_nrf(params->flags);
2667 	sta_info.sta_flags2.nrf_wifi_mask = sta_info.sta_flags2.nrf_wifi_set |
2668 		nrf_wifi_sta_flags_to_nrf(params->flags_mask);
2669 
2670 	if (params->ht_capabilities) {
2671 		memcpy(sta_info.ht_capability,
2672 			   params->ht_capabilities,
2673 			   sizeof(sta_info.ht_capability));
2674 	}
2675 
2676 	if (params->vht_capabilities) {
2677 		memcpy(sta_info.vht_capability,
2678 			   params->vht_capabilities,
2679 			   sizeof(sta_info.vht_capability));
2680 	}
2681 
2682 	memcpy(sta_info.mac_addr, params->addr, sizeof(sta_info.mac_addr));
2683 
2684 	LOG_DBG("%s: %x, %x", __func__,
2685 		sta_info.sta_flags2.nrf_wifi_set, sta_info.sta_flags2.nrf_wifi_mask);
2686 
2687 	if (params->set) {
2688 		status = nrf_wifi_fmac_chg_sta(rpu_ctx_zep->rpu_ctx,
2689 					vif_ctx_zep->vif_idx,
2690 					(struct nrf_wifi_umac_chg_sta_info *)&sta_info);
2691 	} else {
2692 		status = nrf_wifi_fmac_add_sta(rpu_ctx_zep->rpu_ctx,
2693 					vif_ctx_zep->vif_idx,
2694 					&sta_info);
2695 	}
2696 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2697 		LOG_ERR("%s: nrf_wifi_fmac_add_sta failed", __func__);
2698 		goto out;
2699 	}
2700 
2701 	ret = 0;
2702 out:
2703 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2704 	return ret;
2705 }
2706 
nrf_wifi_wpa_supp_sta_remove(void * if_priv,const u8 * addr)2707 int nrf_wifi_wpa_supp_sta_remove(void *if_priv, const u8 *addr)
2708 {
2709 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2710 	struct nrf_wifi_umac_del_sta_info del_sta = {0};
2711 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2712 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2713 	int ret = -1;
2714 
2715 	if (!if_priv || !addr) {
2716 		LOG_ERR("%s: Invalid params", __func__);
2717 		return ret;
2718 	}
2719 
2720 	vif_ctx_zep = if_priv;
2721 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2722 	if (!rpu_ctx_zep) {
2723 		LOG_DBG("%s: rpu_ctx_zep is NULL", __func__);
2724 		return ret;
2725 	}
2726 
2727 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2728 	if (!rpu_ctx_zep->rpu_ctx) {
2729 		LOG_DBG("%s: RPU context not initialized", __func__);
2730 		goto out;
2731 	}
2732 
2733 	memcpy(del_sta.mac_addr, addr, sizeof(del_sta.mac_addr));
2734 
2735 	status = nrf_wifi_fmac_del_sta(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &del_sta);
2736 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2737 		LOG_ERR("%s: nrf_wifi_fmac_del_sta failed", __func__);
2738 		goto out;
2739 	}
2740 
2741 	ret = 0;
2742 
2743 out:
2744 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2745 	return ret;
2746 }
2747 
nrf_wifi_wpa_supp_sta_set_flags(void * if_priv,const u8 * addr,unsigned int total_flags,unsigned int flags_or,unsigned int flags_and)2748 int nrf_wifi_wpa_supp_sta_set_flags(void *if_priv, const u8 *addr,
2749 			unsigned int total_flags, unsigned int flags_or,
2750 			unsigned int flags_and)
2751 {
2752 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2753 	struct nrf_wifi_umac_chg_sta_info chg_sta = {0};
2754 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2755 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2756 	int ret = -1;
2757 
2758 	if (!if_priv || !addr) {
2759 		LOG_ERR("%s: Invalid params", __func__);
2760 		return ret;
2761 	}
2762 
2763 	vif_ctx_zep = if_priv;
2764 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2765 	if (!rpu_ctx_zep) {
2766 		LOG_DBG("%s: rpu_ctx_zep is NULL", __func__);
2767 		return ret;
2768 	}
2769 
2770 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2771 	if (!rpu_ctx_zep->rpu_ctx) {
2772 		LOG_DBG("%s: RPU context not initialized", __func__);
2773 		goto out;
2774 	}
2775 
2776 	memcpy(chg_sta.mac_addr, addr, sizeof(chg_sta.mac_addr));
2777 
2778 	chg_sta.sta_flags2.nrf_wifi_mask = nrf_wifi_sta_flags_to_nrf(flags_or | ~flags_and);
2779 	chg_sta.sta_flags2.nrf_wifi_set = nrf_wifi_sta_flags_to_nrf(flags_or);
2780 
2781 	LOG_DBG("%s %x, %x", __func__,
2782 		chg_sta.sta_flags2.nrf_wifi_set, chg_sta.sta_flags2.nrf_wifi_mask);
2783 
2784 	status = nrf_wifi_fmac_chg_sta(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx, &chg_sta);
2785 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2786 		LOG_ERR("%s: nrf_wifi_fmac_chg_sta failed", __func__);
2787 		goto out;
2788 	}
2789 
2790 	ret = 0;
2791 
2792 out:
2793 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2794 	return ret;
2795 }
2796 
nrf_wifi_wpa_supp_sta_get_inact_sec(void * if_priv,const u8 * addr)2797 int nrf_wifi_wpa_supp_sta_get_inact_sec(void *if_priv, const u8 *addr)
2798 {
2799 	struct nrf_wifi_vif_ctx_zep *vif_ctx_zep = NULL;
2800 	struct nrf_wifi_ctx_zep *rpu_ctx_zep = NULL;
2801 	enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL;
2802 	int ret = -1, sem_ret;
2803 	int inactive_time_sec = -1;
2804 
2805 	if (!if_priv || !addr) {
2806 		LOG_ERR("%s: Invalid params", __func__);
2807 		return ret;
2808 	}
2809 
2810 	vif_ctx_zep = if_priv;
2811 	rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep;
2812 	if (!rpu_ctx_zep) {
2813 		LOG_DBG("%s: rpu_ctx_zep is NULL", __func__);
2814 		return ret;
2815 	}
2816 
2817 	k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER);
2818 	if (!rpu_ctx_zep->rpu_ctx) {
2819 		LOG_DBG("%s: RPU context not initialized", __func__);
2820 		goto out;
2821 	}
2822 
2823 	status = nrf_wifi_fmac_get_station(rpu_ctx_zep->rpu_ctx, vif_ctx_zep->vif_idx,
2824 					(unsigned char *) addr);
2825 	if (status != NRF_WIFI_STATUS_SUCCESS) {
2826 		LOG_ERR("%s: nrf_wifi_fmac_get_station failed", __func__);
2827 		goto out;
2828 	}
2829 
2830 	sem_ret = k_sem_take(&wait_for_event_sem, K_MSEC(RPU_RESP_EVENT_TIMEOUT));
2831 	if (sem_ret) {
2832 		LOG_ERR("%s: Timed out to get station info, ret = %d", __func__, sem_ret);
2833 		ret = NRF_WIFI_STATUS_FAIL;
2834 		goto out;
2835 	}
2836 
2837 	inactive_time_sec = vif_ctx_zep->inactive_time_sec;
2838 out:
2839 	k_mutex_unlock(&vif_ctx_zep->vif_lock);
2840 	return inactive_time_sec;
2841 }
2842 #endif /* CONFIG_NRF70_AP_MODE */
2843