xref: /wlan-dirver/qca-wifi-host-cmn/umac/scan/dispatcher/src/wlan_scan_utils_api.c (revision 6d768494e5ce14eb1603a695c86739d12ecc6ec2) !
1 /*
2  * Copyright (c) 2017-2020 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * DOC: Defines scan utility functions
21  */
22 
23 #include <wlan_cmn.h>
24 #include <wlan_scan_ucfg_api.h>
25 #include <wlan_scan_utils_api.h>
26 #include <../../core/src/wlan_scan_cache_db.h>
27 #include <../../core/src/wlan_scan_main.h>
28 #include <wlan_reg_services_api.h>
29 
30 #define MAX_IE_LEN 1024
31 #define SHORT_SSID_LEN 4
32 #define NEIGHBOR_AP_LEN 1
33 #define BSS_PARAMS_LEN 1
34 
35 const char*
36 util_scan_get_ev_type_name(enum scan_event_type type)
37 {
38 	static const char * const event_name[] = {
39 		[SCAN_EVENT_TYPE_STARTED] = "STARTED",
40 		[SCAN_EVENT_TYPE_COMPLETED] = "COMPLETED",
41 		[SCAN_EVENT_TYPE_BSS_CHANNEL] = "HOME_CHANNEL",
42 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL] = "FOREIGN_CHANNEL",
43 		[SCAN_EVENT_TYPE_DEQUEUED] = "DEQUEUED",
44 		[SCAN_EVENT_TYPE_PREEMPTED] = "PREEMPTED",
45 		[SCAN_EVENT_TYPE_START_FAILED] = "START_FAILED",
46 		[SCAN_EVENT_TYPE_RESTARTED] = "RESTARTED",
47 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL_EXIT] = "FOREIGN_CHANNEL_EXIT",
48 		[SCAN_EVENT_TYPE_SUSPENDED] = "SUSPENDED",
49 		[SCAN_EVENT_TYPE_RESUMED] = "RESUMED",
50 		[SCAN_EVENT_TYPE_NLO_COMPLETE] = "NLO_COMPLETE",
51 		[SCAN_EVENT_TYPE_NLO_MATCH] = "NLO_MATCH",
52 		[SCAN_EVENT_TYPE_INVALID] = "INVALID",
53 		[SCAN_EVENT_TYPE_GPIO_TIMEOUT] = "GPIO_TIMEOUT",
54 		[SCAN_EVENT_TYPE_RADIO_MEASUREMENT_START] =
55 			"RADIO_MEASUREMENT_START",
56 		[SCAN_EVENT_TYPE_RADIO_MEASUREMENT_END] =
57 			"RADIO_MEASUREMENT_END",
58 		[SCAN_EVENT_TYPE_BSSID_MATCH] = "BSSID_MATCH",
59 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL_GET_NF] =
60 			"FOREIGN_CHANNEL_GET_NF",
61 	};
62 
63 	if (type >= SCAN_EVENT_TYPE_MAX)
64 		return "UNKNOWN";
65 
66 	return event_name[type];
67 }
68 
69 
70 const char*
71 util_scan_get_ev_reason_name(enum scan_completion_reason reason)
72 {
73 	static const char * const reason_name[] = {
74 		[SCAN_REASON_NONE] = "NONE",
75 		[SCAN_REASON_COMPLETED] = "COMPLETED",
76 		[SCAN_REASON_CANCELLED] = "CANCELLED",
77 		[SCAN_REASON_PREEMPTED] = "PREEMPTED",
78 		[SCAN_REASON_TIMEDOUT] = "TIMEDOUT",
79 		[SCAN_REASON_INTERNAL_FAILURE] = "INTERNAL_FAILURE",
80 		[SCAN_REASON_SUSPENDED] = "SUSPENDED",
81 		[SCAN_REASON_RUN_FAILED] = "RUN_FAILED",
82 		[SCAN_REASON_TERMINATION_FUNCTION] = "TERMINATION_FUNCTION",
83 		[SCAN_REASON_MAX_OFFCHAN_RETRIES] = "MAX_OFFCHAN_RETRIES",
84 		[SCAN_REASON_DFS_VIOLATION] = "DFS_NOL_VIOLATION",
85 	};
86 
87 	if (reason >= SCAN_REASON_MAX)
88 		return "UNKNOWN";
89 
90 	return reason_name[reason];
91 }
92 
93 qdf_time_t
94 util_get_last_scan_time(struct wlan_objmgr_vdev *vdev)
95 {
96 	uint8_t pdev_id;
97 	struct wlan_scan_obj *scan_obj;
98 
99 	if (!vdev) {
100 		scm_warn("null vdev");
101 		QDF_ASSERT(0);
102 		return 0;
103 	}
104 	pdev_id = wlan_scan_vdev_get_pdev_id(vdev);
105 	scan_obj = wlan_vdev_get_scan_obj(vdev);
106 
107 	if (scan_obj)
108 		return scan_obj->pdev_info[pdev_id].last_scan_time;
109 	else
110 		return 0;
111 }
112 
113 enum wlan_band util_scan_scm_chan_to_band(uint32_t chan)
114 {
115 	if (WLAN_CHAN_IS_2GHZ(chan))
116 		return WLAN_BAND_2_4_GHZ;
117 
118 	return WLAN_BAND_5_GHZ;
119 }
120 
121 enum wlan_band util_scan_scm_freq_to_band(uint16_t freq)
122 {
123 	if (WLAN_REG_IS_24GHZ_CH_FREQ(freq))
124 		return WLAN_BAND_2_4_GHZ;
125 
126 	return WLAN_BAND_5_GHZ;
127 }
128 
129 bool util_is_scan_entry_match(
130 	struct scan_cache_entry *entry1,
131 	struct scan_cache_entry *entry2)
132 {
133 
134 	if (entry1->cap_info.wlan_caps.ess !=
135 	   entry2->cap_info.wlan_caps.ess)
136 		return false;
137 
138 	if (entry1->cap_info.wlan_caps.ess &&
139 	   !qdf_mem_cmp(entry1->bssid.bytes,
140 	   entry2->bssid.bytes, QDF_MAC_ADDR_SIZE)) {
141 		/* Check for BSS */
142 		if (util_is_ssid_match(&entry1->ssid, &entry2->ssid) ||
143 		    util_scan_is_null_ssid(&entry1->ssid) ||
144 		    util_scan_is_null_ssid(&entry2->ssid))
145 			return true;
146 	} else if (entry1->cap_info.wlan_caps.ibss &&
147 	   (entry1->channel.chan_freq ==
148 	   entry2->channel.chan_freq)) {
149 		/*
150 		 * Same channel cannot have same SSID for
151 		 * different IBSS, so no need to check BSSID
152 		 */
153 		if (util_is_ssid_match(
154 		   &entry1->ssid, &entry2->ssid))
155 			return true;
156 	} else if (!entry1->cap_info.wlan_caps.ibss &&
157 	   !entry1->cap_info.wlan_caps.ess &&
158 	   !qdf_mem_cmp(entry1->bssid.bytes,
159 	   entry2->bssid.bytes, QDF_MAC_ADDR_SIZE)) {
160 		/* In case of P2P devices, ess and ibss will be set to zero */
161 		return true;
162 	}
163 
164 	return false;
165 }
166 
167 static bool util_is_pureg_rate(uint8_t *rates, uint8_t nrates)
168 {
169 	static const uint8_t g_rates[] = {12, 18, 24, 36, 48, 72, 96, 108};
170 	bool pureg = false;
171 	uint8_t i, j;
172 
173 	for (i = 0; i < nrates; i++) {
174 		for (j = 0; j < QDF_ARRAY_SIZE(g_rates); j++) {
175 			if (WLAN_RV(rates[i]) == g_rates[j]) {
176 				pureg = true;
177 				break;
178 			}
179 		}
180 		if (pureg)
181 			break;
182 	}
183 
184 	return pureg;
185 }
186 
187 #ifdef CONFIG_BAND_6GHZ
188 static struct he_oper_6g_param *util_scan_get_he_6g_params(uint8_t *he_ops)
189 {
190 	uint8_t len;
191 	uint32_t he_oper_params;
192 
193 	if (!he_ops)
194 		return NULL;
195 
196 	len = he_ops[1];
197 	he_ops += sizeof(struct ie_header);
198 
199 	if (len < WLAN_HEOP_FIXED_PARAM_LENGTH)
200 		return NULL;
201 
202 	/* element id extension */
203 	he_ops++;
204 	len--;
205 
206 	he_oper_params = LE_READ_4(he_ops);
207 	if (!(he_oper_params & WLAN_HEOP_6GHZ_INFO_PRESENT_MASK))
208 		return NULL;
209 
210 	/* fixed params - element id extension */
211 	he_ops += WLAN_HEOP_FIXED_PARAM_LENGTH - 1;
212 	len -= WLAN_HEOP_FIXED_PARAM_LENGTH - 1;
213 
214 	if (!len)
215 		return NULL;
216 
217 	/* vht oper params */
218 	if (he_oper_params & WLAN_HEOP_VHTOP_PRESENT_MASK) {
219 		if (len < WLAN_HEOP_VHTOP_LENGTH)
220 			return NULL;
221 		he_ops += WLAN_HEOP_VHTOP_LENGTH;
222 		len -= WLAN_HEOP_VHTOP_LENGTH;
223 	}
224 
225 	if (!len)
226 		return NULL;
227 
228 	if (he_oper_params & WLAN_HEOP_CO_LOCATED_BSS_MASK) {
229 		he_ops += WLAN_HEOP_CO_LOCATED_BSS_LENGTH;
230 		len -= WLAN_HEOP_CO_LOCATED_BSS_LENGTH;
231 	}
232 
233 	if (len < sizeof(struct he_oper_6g_param))
234 		return NULL;
235 
236 	return (struct he_oper_6g_param *)he_ops;
237 }
238 
239 static QDF_STATUS
240 util_scan_get_chan_from_he_6g_params(struct wlan_objmgr_pdev *pdev,
241 				     struct scan_cache_entry *scan_params,
242 				     qdf_freq_t *chan_freq,
243 				     bool *he_6g_dup_bcon, uint8_t band_mask)
244 {
245 	struct he_oper_6g_param *he_6g_params;
246 	uint8_t *he_ops;
247 	struct wlan_scan_obj *scan_obj;
248 	struct wlan_objmgr_psoc *psoc;
249 
250 	psoc = wlan_pdev_get_psoc(pdev);
251 	if (!psoc) {
252 		scm_err("psoc is NULL");
253 		return QDF_STATUS_E_INVAL;
254 	}
255 
256 	scan_obj = wlan_psoc_get_scan_obj(psoc);
257 	if (!scan_obj) {
258 		scm_err("scan_obj is NULL");
259 		return QDF_STATUS_E_INVAL;
260 	}
261 
262 	*he_6g_dup_bcon = false;
263 
264 	he_ops = util_scan_entry_heop(scan_params);
265 	if (!util_scan_entry_hecap(scan_params) || !he_ops)
266 		return QDF_STATUS_SUCCESS;
267 
268 	he_6g_params = util_scan_get_he_6g_params(he_ops);
269 	if (!he_6g_params)
270 		return QDF_STATUS_SUCCESS;
271 
272 	*chan_freq = wlan_reg_chan_band_to_freq(pdev,
273 						he_6g_params->primary_channel,
274 						band_mask);
275 	if (scan_obj->drop_bcn_on_invalid_freq &&
276 	    wlan_reg_is_disable_for_freq(pdev, *chan_freq)) {
277 		scm_debug_rl("%pM: Drop as invalid channel %d freq %d in HE 6Ghz params",
278 			     scan_params->bssid.bytes,
279 			     he_6g_params->primary_channel, *chan_freq);
280 		return QDF_STATUS_E_INVAL;
281 	}
282 	*he_6g_dup_bcon = he_6g_params->duplicate_beacon ? true : false;
283 
284 	return QDF_STATUS_SUCCESS;
285 }
286 
287 static enum wlan_phymode
288 util_scan_get_phymode_6g(struct wlan_objmgr_pdev *pdev,
289 			 struct scan_cache_entry *scan_params)
290 {
291 	struct he_oper_6g_param *he_6g_params;
292 	enum wlan_phymode phymode = WLAN_PHYMODE_11AXA_HE20;
293 	uint8_t *he_ops;
294 	uint8_t band_mask = BIT(REG_BAND_6G);
295 
296 	he_ops = util_scan_entry_heop(scan_params);
297 	if (!util_scan_entry_hecap(scan_params) || !he_ops)
298 		return phymode;
299 
300 	he_6g_params = util_scan_get_he_6g_params(he_ops);
301 	if (!he_6g_params)
302 		return phymode;
303 
304 	switch (he_6g_params->width) {
305 	case WLAN_HE_6GHZ_CHWIDTH_20:
306 		phymode = WLAN_PHYMODE_11AXA_HE20;
307 		break;
308 	case WLAN_HE_6GHZ_CHWIDTH_40:
309 		phymode = WLAN_PHYMODE_11AXA_HE40;
310 		break;
311 	case WLAN_HE_6GHZ_CHWIDTH_80:
312 		phymode = WLAN_PHYMODE_11AXA_HE80;
313 		break;
314 	case WLAN_HE_6GHZ_CHWIDTH_160_80_80:
315 		if (WLAN_IS_HE80_80(he_6g_params))
316 			phymode = WLAN_PHYMODE_11AXA_HE80_80;
317 		else if (WLAN_IS_HE160(he_6g_params))
318 			phymode = WLAN_PHYMODE_11AXA_HE160;
319 		else
320 			phymode = WLAN_PHYMODE_11AXA_HE80;
321 		break;
322 	default:
323 		scm_err("Invalid he_6g_params width: %d", he_6g_params->width);
324 		phymode = WLAN_PHYMODE_11AXA_HE20;
325 		break;
326 	}
327 
328 	if (he_6g_params->chan_freq_seg0)
329 		scan_params->channel.cfreq0 =
330 			wlan_reg_chan_band_to_freq(pdev,
331 					he_6g_params->chan_freq_seg0,
332 					band_mask);
333 	if (he_6g_params->chan_freq_seg1)
334 		scan_params->channel.cfreq1 =
335 			wlan_reg_chan_band_to_freq(pdev,
336 					he_6g_params->chan_freq_seg1,
337 					band_mask);
338 
339 	return phymode;
340 }
341 #else
342 static QDF_STATUS
343 util_scan_get_chan_from_he_6g_params(struct wlan_objmgr_pdev *pdev,
344 				     struct scan_cache_entry *scan_params,
345 				     qdf_freq_t *chan_freq,
346 				     bool *he_6g_dup_bcon,
347 				     uint8_t band_mask)
348 {
349 	return QDF_STATUS_SUCCESS;
350 }
351 static inline enum wlan_phymode
352 util_scan_get_phymode_6g(struct wlan_objmgr_pdev *pdev,
353 			 struct scan_cache_entry *scan_params)
354 {
355 	return WLAN_PHYMODE_AUTO;
356 }
357 #endif
358 
359 static inline
360 uint32_t util_scan_sec_chan_freq_from_htinfo(struct wlan_ie_htinfo_cmn *htinfo,
361 					     uint32_t primary_chan_freq)
362 {
363 	if (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_ABOVE)
364 		return primary_chan_freq + WLAN_CHAN_SPACING_20MHZ;
365 	else if (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_BELOW)
366 		return primary_chan_freq - WLAN_CHAN_SPACING_20MHZ;
367 
368 	return 0;
369 }
370 
371 static enum wlan_phymode
372 util_scan_get_phymode_5g(struct wlan_objmgr_pdev *pdev,
373 			 struct scan_cache_entry *scan_params)
374 {
375 	enum wlan_phymode phymode = WLAN_PHYMODE_AUTO;
376 	uint16_t ht_cap = 0;
377 	struct htcap_cmn_ie *htcap;
378 	struct wlan_ie_htinfo_cmn *htinfo;
379 	struct wlan_ie_vhtop *vhtop;
380 	uint8_t band_mask = BIT(REG_BAND_5G);
381 
382 	htcap = (struct htcap_cmn_ie *)
383 		util_scan_entry_htcap(scan_params);
384 	htinfo = (struct wlan_ie_htinfo_cmn *)
385 		util_scan_entry_htinfo(scan_params);
386 	vhtop = (struct wlan_ie_vhtop *)
387 		util_scan_entry_vhtop(scan_params);
388 
389 	if (!(htcap && htinfo))
390 		return WLAN_PHYMODE_11A;
391 
392 	if (htcap)
393 		ht_cap = le16toh(htcap->hc_cap);
394 
395 	if (ht_cap & WLAN_HTCAP_C_CHWIDTH40)
396 		phymode = WLAN_PHYMODE_11NA_HT40;
397 	else
398 		phymode = WLAN_PHYMODE_11NA_HT20;
399 
400 	scan_params->channel.cfreq0 =
401 		util_scan_sec_chan_freq_from_htinfo(htinfo,
402 						scan_params->channel.chan_freq);
403 
404 	if (util_scan_entry_vhtcap(scan_params) && vhtop) {
405 		switch (vhtop->vht_op_chwidth) {
406 		case WLAN_VHTOP_CHWIDTH_2040:
407 			if (ht_cap & WLAN_HTCAP_C_CHWIDTH40)
408 				phymode = WLAN_PHYMODE_11AC_VHT40;
409 			else
410 				phymode = WLAN_PHYMODE_11AC_VHT20;
411 			break;
412 		case WLAN_VHTOP_CHWIDTH_80:
413 			if (WLAN_IS_REVSIG_VHT80_80(vhtop))
414 				phymode = WLAN_PHYMODE_11AC_VHT80_80;
415 			else if (WLAN_IS_REVSIG_VHT160(vhtop))
416 				phymode = WLAN_PHYMODE_11AC_VHT160;
417 			else
418 				phymode = WLAN_PHYMODE_11AC_VHT80;
419 			break;
420 		case WLAN_VHTOP_CHWIDTH_160:
421 			phymode = WLAN_PHYMODE_11AC_VHT160;
422 			break;
423 		case WLAN_VHTOP_CHWIDTH_80_80:
424 			phymode = WLAN_PHYMODE_11AC_VHT80_80;
425 			break;
426 		default:
427 			scm_err("bad channel: %d",
428 					vhtop->vht_op_chwidth);
429 			phymode = WLAN_PHYMODE_11AC_VHT20;
430 			break;
431 		}
432 		if (vhtop->vht_op_ch_freq_seg1)
433 			scan_params->channel.cfreq0 =
434 				wlan_reg_chan_band_to_freq(pdev,
435 						vhtop->vht_op_ch_freq_seg1,
436 						band_mask);
437 		if (vhtop->vht_op_ch_freq_seg2)
438 			scan_params->channel.cfreq1 =
439 				wlan_reg_chan_band_to_freq(pdev,
440 						vhtop->vht_op_ch_freq_seg2,
441 						band_mask);
442 	}
443 
444 	if (!util_scan_entry_hecap(scan_params))
445 		return phymode;
446 
447 	/* for 5Ghz Check for HE, only if VHT cap and HE cap are present */
448 	if (!IS_WLAN_PHYMODE_VHT(phymode))
449 		return phymode;
450 
451 	switch (phymode) {
452 	case WLAN_PHYMODE_11AC_VHT20:
453 		phymode = WLAN_PHYMODE_11AXA_HE20;
454 		break;
455 	case WLAN_PHYMODE_11AC_VHT40:
456 		phymode = WLAN_PHYMODE_11AXA_HE40;
457 		break;
458 	case WLAN_PHYMODE_11AC_VHT80:
459 		phymode = WLAN_PHYMODE_11AXA_HE80;
460 		break;
461 	case WLAN_PHYMODE_11AC_VHT160:
462 		phymode = WLAN_PHYMODE_11AXA_HE160;
463 		break;
464 	case WLAN_PHYMODE_11AC_VHT80_80:
465 		phymode = WLAN_PHYMODE_11AXA_HE80_80;
466 		break;
467 	default:
468 		phymode = WLAN_PHYMODE_11AXA_HE20;
469 		break;
470 	}
471 
472 	return phymode;
473 }
474 
475 static enum wlan_phymode
476 util_scan_get_phymode_2g(struct scan_cache_entry *scan_params)
477 {
478 	enum wlan_phymode phymode = WLAN_PHYMODE_AUTO;
479 	uint16_t ht_cap = 0;
480 	struct htcap_cmn_ie *htcap;
481 	struct wlan_ie_htinfo_cmn *htinfo;
482 	struct wlan_ie_vhtop *vhtop;
483 
484 	htcap = (struct htcap_cmn_ie *)
485 		util_scan_entry_htcap(scan_params);
486 	htinfo = (struct wlan_ie_htinfo_cmn *)
487 		util_scan_entry_htinfo(scan_params);
488 	vhtop = (struct wlan_ie_vhtop *)
489 		util_scan_entry_vhtop(scan_params);
490 
491 	if (htcap)
492 		ht_cap = le16toh(htcap->hc_cap);
493 
494 	if (htcap && htinfo) {
495 		if ((ht_cap & WLAN_HTCAP_C_CHWIDTH40) &&
496 		   (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_ABOVE))
497 			phymode = WLAN_PHYMODE_11NG_HT40PLUS;
498 		else if ((ht_cap & WLAN_HTCAP_C_CHWIDTH40) &&
499 		   (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_BELOW))
500 			phymode = WLAN_PHYMODE_11NG_HT40MINUS;
501 		else
502 			phymode = WLAN_PHYMODE_11NG_HT20;
503 	} else if (util_scan_entry_xrates(scan_params)) {
504 		/* only 11G stations will have more than 8 rates */
505 		phymode = WLAN_PHYMODE_11G;
506 	} else {
507 		/* Some mischievous g-only APs do not set extended rates */
508 		if (util_scan_entry_rates(scan_params)) {
509 			if (util_is_pureg_rate(&scan_params->ie_list.rates[2],
510 			   scan_params->ie_list.rates[1]))
511 				phymode = WLAN_PHYMODE_11G;
512 			else
513 				phymode = WLAN_PHYMODE_11B;
514 		} else {
515 			phymode = WLAN_PHYMODE_11B;
516 		}
517 	}
518 
519 	/* Check for VHT only if HT cap is present */
520 	if (!IS_WLAN_PHYMODE_HT(phymode))
521 		return phymode;
522 
523 	scan_params->channel.cfreq0 =
524 		util_scan_sec_chan_freq_from_htinfo(htinfo,
525 						scan_params->channel.chan_freq);
526 
527 	if (util_scan_entry_vhtcap(scan_params) && vhtop) {
528 		switch (vhtop->vht_op_chwidth) {
529 		case WLAN_VHTOP_CHWIDTH_2040:
530 			if (phymode == WLAN_PHYMODE_11NG_HT40PLUS)
531 				phymode = WLAN_PHYMODE_11AC_VHT40PLUS_2G;
532 			else if (phymode == WLAN_PHYMODE_11NG_HT40MINUS)
533 				phymode = WLAN_PHYMODE_11AC_VHT40MINUS_2G;
534 			else
535 				phymode = WLAN_PHYMODE_11AC_VHT20_2G;
536 
537 			break;
538 		default:
539 			scm_info("bad vht_op_chwidth: %d",
540 				 vhtop->vht_op_chwidth);
541 			phymode = WLAN_PHYMODE_11AC_VHT20_2G;
542 			break;
543 		}
544 	}
545 
546 	if (!util_scan_entry_hecap(scan_params))
547 		return phymode;
548 
549 	if (phymode == WLAN_PHYMODE_11AC_VHT40PLUS_2G ||
550 	    phymode == WLAN_PHYMODE_11NG_HT40PLUS)
551 		phymode = WLAN_PHYMODE_11AXG_HE40PLUS;
552 	else if (phymode == WLAN_PHYMODE_11AC_VHT40MINUS_2G ||
553 		 phymode == WLAN_PHYMODE_11NG_HT40MINUS)
554 		phymode = WLAN_PHYMODE_11AXG_HE40MINUS;
555 	else
556 		phymode = WLAN_PHYMODE_11AXG_HE20;
557 
558 	return phymode;
559 }
560 
561 static enum wlan_phymode
562 util_scan_get_phymode(struct wlan_objmgr_pdev *pdev,
563 		      struct scan_cache_entry *scan_params)
564 {
565 	if (WLAN_REG_IS_24GHZ_CH_FREQ(scan_params->channel.chan_freq))
566 		return util_scan_get_phymode_2g(scan_params);
567 	else if (WLAN_REG_IS_6GHZ_CHAN_FREQ(scan_params->channel.chan_freq))
568 		return util_scan_get_phymode_6g(pdev, scan_params);
569 	else
570 		return util_scan_get_phymode_5g(pdev, scan_params);
571 }
572 
573 static QDF_STATUS
574 util_scan_parse_chan_switch_wrapper_ie(struct scan_cache_entry *scan_params,
575 	struct ie_header *sub_ie, qdf_size_t sub_ie_len)
576 {
577 	/* Walk through to check nothing is malformed */
578 	while (sub_ie_len >= sizeof(struct ie_header)) {
579 		/* At least one more header is present */
580 		sub_ie_len -= sizeof(struct ie_header);
581 
582 		if (sub_ie->ie_len == 0) {
583 			sub_ie += 1;
584 			continue;
585 		}
586 		if (sub_ie_len < sub_ie->ie_len) {
587 			scm_err("Incomplete corrupted IE:%x",
588 				WLAN_ELEMID_CHAN_SWITCH_WRAP);
589 			return QDF_STATUS_E_INVAL;
590 		}
591 		switch (sub_ie->ie_id) {
592 		case WLAN_ELEMID_COUNTRY:
593 			scan_params->ie_list.country = (uint8_t *)sub_ie;
594 			break;
595 		case WLAN_ELEMID_WIDE_BAND_CHAN_SWITCH:
596 			scan_params->ie_list.widebw = (uint8_t *)sub_ie;
597 			break;
598 		case WLAN_ELEMID_VHT_TX_PWR_ENVLP:
599 			scan_params->ie_list.txpwrenvlp = (uint8_t *)sub_ie;
600 			break;
601 		}
602 		/* Consume sub info element */
603 		sub_ie_len -= sub_ie->ie_len;
604 		/* go to next Sub IE */
605 		sub_ie = (struct ie_header *)
606 			(((uint8_t *) sub_ie) +
607 			sizeof(struct ie_header) + sub_ie->ie_len);
608 	}
609 
610 	return QDF_STATUS_SUCCESS;
611 }
612 
613 bool
614 util_scan_is_hidden_ssid(struct ie_ssid *ssid)
615 {
616 	uint8_t i;
617 
618 	/*
619 	 * We flag this as Hidden SSID if the Length is 0
620 	 * of the SSID only contains 0's
621 	 */
622 	if (!ssid || !ssid->ssid_len)
623 		return true;
624 
625 	for (i = 0; i < ssid->ssid_len; i++)
626 		if (ssid->ssid[i] != 0)
627 			return false;
628 
629 	/* All 0's */
630 	return true;
631 }
632 
633 static QDF_STATUS
634 util_scan_update_rnr(struct rnr_bss_info *rnr,
635 		     struct neighbor_ap_info_field *ap_info,
636 		     uint8_t *data)
637 {
638 	uint8_t tbtt_info_length;
639 
640 	tbtt_info_length = ap_info->tbtt_header.tbtt_info_length;
641 
642 	switch (tbtt_info_length) {
643 	case TBTT_NEIGHBOR_AP_OFFSET_ONLY:
644 		/* Dont store it skip*/
645 		break;
646 
647 	case TBTT_NEIGHBOR_AP_BSS_PARAM:
648 		/* Dont store it skip*/
649 		break;
650 
651 	case TBTT_NEIGHBOR_AP_SHORTSSID:
652 		rnr->channel_number = ap_info->channel_number;
653 		rnr->operating_class = ap_info->operting_class;
654 		qdf_mem_copy(&rnr->short_ssid, &data[1], SHORT_SSID_LEN);
655 		break;
656 
657 	case TBTT_NEIGHBOR_AP_S_SSID_BSS_PARAM:
658 		rnr->channel_number = ap_info->channel_number;
659 		rnr->operating_class = ap_info->operting_class;
660 		qdf_mem_copy(&rnr->short_ssid, &data[1], SHORT_SSID_LEN);
661 		rnr->bss_params = data[5];
662 		break;
663 
664 	case TBTT_NEIGHBOR_AP_BSSID:
665 		rnr->channel_number = ap_info->channel_number;
666 		rnr->operating_class = ap_info->operting_class;
667 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
668 		break;
669 
670 	case TBTT_NEIGHBOR_AP_BSSID_BSS_PARAM:
671 		rnr->channel_number = ap_info->channel_number;
672 		rnr->operating_class = ap_info->operting_class;
673 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
674 		rnr->bss_params = data[7];
675 		break;
676 
677 	case TBTT_NEIGHBOR_AP_BSSSID_S_SSID:
678 		rnr->channel_number = ap_info->channel_number;
679 		rnr->operating_class = ap_info->operting_class;
680 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
681 		qdf_mem_copy(&rnr->short_ssid, &data[7], SHORT_SSID_LEN);
682 		break;
683 
684 	case TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM:
685 		rnr->channel_number = ap_info->channel_number;
686 		rnr->operating_class = ap_info->operting_class;
687 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
688 		qdf_mem_copy(&rnr->short_ssid, &data[7], SHORT_SSID_LEN);
689 		rnr->bss_params = data[11];
690 		break;
691 
692 	default:
693 		scm_debug("Wrong fieldtype");
694 	}
695 
696 	return QDF_STATUS_SUCCESS;
697 }
698 
699 static QDF_STATUS
700 util_scan_parse_rnr_ie(struct scan_cache_entry *scan_entry,
701 		       struct ie_header *ie)
702 {
703 	uint32_t rnr_ie_len;
704 	uint16_t tbtt_count, tbtt_length, i, fieldtype;
705 	uint8_t *data;
706 	struct neighbor_ap_info_field *neighbor_ap_info;
707 
708 	rnr_ie_len = ie->ie_len;
709 	data = (uint8_t *)ie + sizeof(struct ie_header);
710 
711 	while (data < ((uint8_t *)ie + rnr_ie_len + 2)) {
712 		neighbor_ap_info = (struct neighbor_ap_info_field *)data;
713 		tbtt_count = neighbor_ap_info->tbtt_header.tbtt_info_count;
714 		tbtt_length = neighbor_ap_info->tbtt_header.tbtt_info_length;
715 		fieldtype = neighbor_ap_info->tbtt_header.tbbt_info_fieldtype;
716 		scm_debug("channel number %d, op class %d",
717 			  neighbor_ap_info->channel_number,
718 			  neighbor_ap_info->operting_class);
719 		scm_debug("tbtt_count %d, tbtt_length %d, fieldtype %d",
720 			  tbtt_count, tbtt_length, fieldtype);
721 		data += sizeof(struct neighbor_ap_info_field);
722 		for (i = 0; i < (tbtt_count + 1) ; i++) {
723 			if (i < MAX_RNR_BSS)
724 				util_scan_update_rnr(
725 					&scan_entry->rnr.bss_info[i],
726 					neighbor_ap_info,
727 					data);
728 			data += tbtt_length;
729 		}
730 	}
731 
732 	return QDF_STATUS_SUCCESS;
733 }
734 
735 static QDF_STATUS
736 util_scan_parse_extn_ie(struct scan_cache_entry *scan_params,
737 	struct ie_header *ie)
738 {
739 	struct extn_ie_header *extn_ie = (struct extn_ie_header *) ie;
740 
741 	switch (extn_ie->ie_extn_id) {
742 	case WLAN_EXTN_ELEMID_MAX_CHAN_SWITCH_TIME:
743 		scan_params->ie_list.mcst  = (uint8_t *)ie;
744 		break;
745 	case WLAN_EXTN_ELEMID_SRP:
746 		scan_params->ie_list.srp   = (uint8_t *)ie;
747 		break;
748 	case WLAN_EXTN_ELEMID_HECAP:
749 		scan_params->ie_list.hecap = (uint8_t *)ie;
750 		break;
751 	case WLAN_EXTN_ELEMID_HEOP:
752 		scan_params->ie_list.heop  = (uint8_t *)ie;
753 		break;
754 	case WLAN_EXTN_ELEMID_ESP:
755 		scan_params->ie_list.esp = (uint8_t *)ie;
756 		break;
757 	case WLAN_EXTN_ELEMID_MUEDCA:
758 		scan_params->ie_list.muedca = (uint8_t *)ie;
759 		break;
760 	case WLAN_EXTN_ELEMID_HE_6G_CAP:
761 		scan_params->ie_list.hecap_6g = (uint8_t *)ie;
762 		break;
763 	default:
764 		break;
765 	}
766 	return QDF_STATUS_SUCCESS;
767 }
768 
769 static QDF_STATUS
770 util_scan_parse_vendor_ie(struct scan_cache_entry *scan_params,
771 	struct ie_header *ie)
772 {
773 	if (!scan_params->ie_list.vendor)
774 		scan_params->ie_list.vendor = (uint8_t *)ie;
775 
776 	if (is_wpa_oui((uint8_t *)ie)) {
777 		scan_params->ie_list.wpa = (uint8_t *)ie;
778 	} else if (is_wps_oui((uint8_t *)ie)) {
779 		scan_params->ie_list.wps = (uint8_t *)ie;
780 		/* WCN IE should be a subset of WPS IE */
781 		if (is_wcn_oui((uint8_t *)ie))
782 			scan_params->ie_list.wcn = (uint8_t *)ie;
783 	} else if (is_wme_param((uint8_t *)ie)) {
784 		scan_params->ie_list.wmeparam = (uint8_t *)ie;
785 	} else if (is_wme_info((uint8_t *)ie)) {
786 		scan_params->ie_list.wmeinfo = (uint8_t *)ie;
787 	} else if (is_atheros_oui((uint8_t *)ie)) {
788 		scan_params->ie_list.athcaps = (uint8_t *)ie;
789 	} else if (is_atheros_extcap_oui((uint8_t *)ie)) {
790 		scan_params->ie_list.athextcaps = (uint8_t *)ie;
791 	} else if (is_sfa_oui((uint8_t *)ie)) {
792 		scan_params->ie_list.sfa = (uint8_t *)ie;
793 	} else if (is_p2p_oui((uint8_t *)ie)) {
794 		scan_params->ie_list.p2p = (uint8_t *)ie;
795 	} else if (is_qca_son_oui((uint8_t *)ie,
796 				  QCA_OUI_WHC_AP_INFO_SUBTYPE)) {
797 		scan_params->ie_list.sonadv = (uint8_t *)ie;
798 	} else if (is_ht_cap((uint8_t *)ie)) {
799 		/* we only care if there isn't already an HT IE (ANA) */
800 		if (!scan_params->ie_list.htcap) {
801 			if (ie->ie_len != (WLAN_VENDOR_HT_IE_OFFSET_LEN +
802 					   sizeof(struct htcap_cmn_ie)))
803 				return QDF_STATUS_E_INVAL;
804 			scan_params->ie_list.htcap =
805 			 (uint8_t *)&(((struct wlan_vendor_ie_htcap *)ie)->ie);
806 		}
807 	} else if (is_ht_info((uint8_t *)ie)) {
808 		/* we only care if there isn't already an HT IE (ANA) */
809 		if (!scan_params->ie_list.htinfo) {
810 			if (ie->ie_len != WLAN_VENDOR_HT_IE_OFFSET_LEN +
811 					  sizeof(struct wlan_ie_htinfo_cmn))
812 				return QDF_STATUS_E_INVAL;
813 			scan_params->ie_list.htinfo =
814 			  (uint8_t *)&(((struct wlan_vendor_ie_htinfo *)
815 			  ie)->hi_ie);
816 		}
817 	} else if (is_interop_vht((uint8_t *)ie) &&
818 	    !(scan_params->ie_list.vhtcap)) {
819 		uint8_t *vendor_ie = (uint8_t *)(ie);
820 
821 		if (ie->ie_len < ((WLAN_VENDOR_VHTCAP_IE_OFFSET +
822 				 sizeof(struct wlan_ie_vhtcaps)) -
823 				 sizeof(struct ie_header)))
824 			return QDF_STATUS_E_INVAL;
825 		vendor_ie = ((uint8_t *)(ie)) + WLAN_VENDOR_VHTCAP_IE_OFFSET;
826 		if (vendor_ie[1] != (sizeof(struct wlan_ie_vhtcaps)) -
827 				      sizeof(struct ie_header))
828 			return QDF_STATUS_E_INVAL;
829 		/* location where Interop Vht Cap IE and VHT OP IE Present */
830 		scan_params->ie_list.vhtcap = (((uint8_t *)(ie)) +
831 						WLAN_VENDOR_VHTCAP_IE_OFFSET);
832 		if (ie->ie_len > ((WLAN_VENDOR_VHTCAP_IE_OFFSET +
833 				 sizeof(struct wlan_ie_vhtcaps)) -
834 				 sizeof(struct ie_header))) {
835 			if (ie->ie_len < ((WLAN_VENDOR_VHTOP_IE_OFFSET +
836 					  sizeof(struct wlan_ie_vhtop)) -
837 					  sizeof(struct ie_header)))
838 				return QDF_STATUS_E_INVAL;
839 			vendor_ie = ((uint8_t *)(ie)) +
840 				    WLAN_VENDOR_VHTOP_IE_OFFSET;
841 			if (vendor_ie[1] != (sizeof(struct wlan_ie_vhtop) -
842 					     sizeof(struct ie_header)))
843 				return QDF_STATUS_E_INVAL;
844 			scan_params->ie_list.vhtop = (((uint8_t *)(ie)) +
845 						   WLAN_VENDOR_VHTOP_IE_OFFSET);
846 		}
847 	} else if (is_bwnss_oui((uint8_t *)ie)) {
848 		/*
849 		 * Bandwidth-NSS map has sub-type & version.
850 		 * hence copy data just after version byte
851 		 */
852 		scan_params->ie_list.bwnss_map = (((uint8_t *)ie) + 8);
853 	} else if (is_mbo_oce_oui((uint8_t *)ie)) {
854 		scan_params->ie_list.mbo_oce = (uint8_t *)ie;
855 	} else if (is_extender_oui((uint8_t *)ie)) {
856 		scan_params->ie_list.extender = (uint8_t *)ie;
857 	} else if (is_adaptive_11r_oui((uint8_t *)ie)) {
858 		if ((ie->ie_len < OUI_LENGTH) ||
859 		    (ie->ie_len > MAX_ADAPTIVE_11R_IE_LEN))
860 			return QDF_STATUS_E_INVAL;
861 
862 		scan_params->ie_list.adaptive_11r = (uint8_t *)ie +
863 						sizeof(struct ie_header);
864 	} else if (is_sae_single_pmk_oui((uint8_t *)ie)) {
865 		if ((ie->ie_len < OUI_LENGTH) ||
866 		    (ie->ie_len > MAX_SAE_SINGLE_PMK_IE_LEN)) {
867 			scm_debug("Invalid sae single pmk OUI");
868 			return QDF_STATUS_E_INVAL;
869 		}
870 		scan_params->ie_list.single_pmk = (uint8_t *)ie +
871 						sizeof(struct ie_header);
872 	}
873 	return QDF_STATUS_SUCCESS;
874 }
875 
876 static QDF_STATUS
877 util_scan_populate_bcn_ie_list(struct wlan_objmgr_pdev *pdev,
878 			       struct scan_cache_entry *scan_params,
879 			       qdf_freq_t *chan_freq, uint8_t band_mask)
880 {
881 	struct ie_header *ie, *sub_ie;
882 	uint32_t ie_len, sub_ie_len;
883 	QDF_STATUS status;
884 	uint8_t chan_idx;
885 	struct wlan_scan_obj *scan_obj;
886 	struct wlan_objmgr_psoc *psoc;
887 
888 	psoc = wlan_pdev_get_psoc(pdev);
889 	if (!psoc) {
890 		scm_err("psoc is NULL");
891 		return QDF_STATUS_E_INVAL;
892 	}
893 
894 	scan_obj = wlan_psoc_get_scan_obj(psoc);
895 	if (!scan_obj) {
896 		scm_err("scan_obj is NULL");
897 		return QDF_STATUS_E_INVAL;
898 	}
899 
900 	ie_len = util_scan_entry_ie_len(scan_params);
901 	ie = (struct ie_header *)
902 		  util_scan_entry_ie_data(scan_params);
903 
904 	while (ie_len >= sizeof(struct ie_header)) {
905 		ie_len -= sizeof(struct ie_header);
906 
907 		if (!ie->ie_len) {
908 			ie += 1;
909 			continue;
910 		}
911 
912 		if (ie_len < ie->ie_len) {
913 			scm_debug("Incomplete corrupted IE:%x",
914 				ie->ie_id);
915 			return QDF_STATUS_E_INVAL;
916 		}
917 
918 		switch (ie->ie_id) {
919 		case WLAN_ELEMID_SSID:
920 			if (ie->ie_len > (sizeof(struct ie_ssid) -
921 					  sizeof(struct ie_header)))
922 				goto err;
923 			scan_params->ie_list.ssid = (uint8_t *)ie;
924 			break;
925 		case WLAN_ELEMID_RATES:
926 			if (ie->ie_len > WLAN_SUPPORTED_RATES_IE_MAX_LEN)
927 				goto err;
928 			scan_params->ie_list.rates = (uint8_t *)ie;
929 			break;
930 		case WLAN_ELEMID_DSPARMS:
931 			if (ie->ie_len != WLAN_DS_PARAM_IE_MAX_LEN)
932 				return QDF_STATUS_E_INVAL;
933 			scan_params->ie_list.ds_param = (uint8_t *)ie;
934 			chan_idx =
935 				((struct ds_ie *)ie)->cur_chan;
936 			*chan_freq = wlan_reg_chan_band_to_freq(pdev, chan_idx,
937 								band_mask);
938 			/* Drop if invalid freq */
939 			if (scan_obj->drop_bcn_on_invalid_freq &&
940 			    wlan_reg_is_disable_for_freq(pdev, *chan_freq)) {
941 				scm_debug_rl("%pM: Drop as invalid channel %d freq %d in DS IE",
942 					     scan_params->bssid.bytes,
943 					     chan_idx, *chan_freq);
944 				return QDF_STATUS_E_INVAL;
945 			}
946 			break;
947 		case WLAN_ELEMID_TIM:
948 			if (ie->ie_len < WLAN_TIM_IE_MIN_LENGTH)
949 				goto err;
950 			scan_params->ie_list.tim = (uint8_t *)ie;
951 			scan_params->dtim_period =
952 				((struct wlan_tim_ie *)ie)->tim_period;
953 			break;
954 		case WLAN_ELEMID_COUNTRY:
955 			if (ie->ie_len < WLAN_COUNTRY_IE_MIN_LEN)
956 				goto err;
957 			scan_params->ie_list.country = (uint8_t *)ie;
958 			break;
959 		case WLAN_ELEMID_QBSS_LOAD:
960 			if (ie->ie_len != sizeof(struct qbss_load_ie) -
961 					  sizeof(struct ie_header)) {
962 				/*
963 				 * Expected QBSS IE length is 5Bytes; For some
964 				 * old cisco AP, QBSS IE length is 4Bytes, which
965 				 * doesn't match with latest spec, So ignore
966 				 * QBSS IE in such case.
967 				 */
968 				break;
969 			}
970 			scan_params->ie_list.qbssload = (uint8_t *)ie;
971 			break;
972 		case WLAN_ELEMID_CHANSWITCHANN:
973 			if (ie->ie_len != WLAN_CSA_IE_MAX_LEN)
974 				goto err;
975 			scan_params->ie_list.csa = (uint8_t *)ie;
976 			break;
977 		case WLAN_ELEMID_IBSSDFS:
978 			if (ie->ie_len < WLAN_IBSSDFS_IE_MIN_LEN)
979 				goto err;
980 			scan_params->ie_list.ibssdfs = (uint8_t *)ie;
981 			break;
982 		case WLAN_ELEMID_QUIET:
983 			if (ie->ie_len != WLAN_QUIET_IE_MAX_LEN)
984 				goto err;
985 			scan_params->ie_list.quiet = (uint8_t *)ie;
986 			break;
987 		case WLAN_ELEMID_ERP:
988 			if (ie->ie_len != (sizeof(struct erp_ie) -
989 					    sizeof(struct ie_header)))
990 				goto err;
991 			scan_params->erp = ((struct erp_ie *)ie)->value;
992 			break;
993 		case WLAN_ELEMID_HTCAP_ANA:
994 			if (ie->ie_len != sizeof(struct htcap_cmn_ie))
995 				goto err;
996 			scan_params->ie_list.htcap =
997 				(uint8_t *)&(((struct htcap_ie *)ie)->ie);
998 			break;
999 		case WLAN_ELEMID_RSN:
1000 			if (ie->ie_len < WLAN_RSN_IE_MIN_LEN)
1001 				goto err;
1002 			scan_params->ie_list.rsn = (uint8_t *)ie;
1003 			break;
1004 		case WLAN_ELEMID_XRATES:
1005 			scan_params->ie_list.xrates = (uint8_t *)ie;
1006 			break;
1007 		case WLAN_ELEMID_EXTCHANSWITCHANN:
1008 			if (ie->ie_len != WLAN_XCSA_IE_MAX_LEN)
1009 				goto err;
1010 			scan_params->ie_list.xcsa = (uint8_t *)ie;
1011 			break;
1012 		case WLAN_ELEMID_SECCHANOFFSET:
1013 			if (ie->ie_len != WLAN_SECCHANOFF_IE_MAX_LEN)
1014 				goto err;
1015 			scan_params->ie_list.secchanoff = (uint8_t *)ie;
1016 			break;
1017 		case WLAN_ELEMID_HTINFO_ANA:
1018 			if (ie->ie_len != sizeof(struct wlan_ie_htinfo_cmn))
1019 				goto err;
1020 			scan_params->ie_list.htinfo =
1021 			  (uint8_t *)&(((struct wlan_ie_htinfo *) ie)->hi_ie);
1022 			chan_idx = ((struct wlan_ie_htinfo_cmn *)
1023 				 (scan_params->ie_list.htinfo))->hi_ctrlchannel;
1024 			*chan_freq = wlan_reg_chan_band_to_freq(pdev, chan_idx,
1025 								band_mask);
1026 			/* Drop if invalid freq */
1027 			if (scan_obj->drop_bcn_on_invalid_freq &&
1028 			    wlan_reg_is_disable_for_freq(pdev, *chan_freq)) {
1029 				scm_debug_rl("%pM: Drop as invalid channel %d freq %d in HT_INFO IE",
1030 					     scan_params->bssid.bytes,
1031 					     chan_idx, *chan_freq);
1032 				return QDF_STATUS_E_INVAL;
1033 			}
1034 			break;
1035 		case WLAN_ELEMID_WAPI:
1036 			if (ie->ie_len < WLAN_WAPI_IE_MIN_LEN)
1037 				goto err;
1038 			scan_params->ie_list.wapi = (uint8_t *)ie;
1039 			break;
1040 		case WLAN_ELEMID_XCAPS:
1041 			if (ie->ie_len > WLAN_EXTCAP_IE_MAX_LEN)
1042 				goto err;
1043 			scan_params->ie_list.extcaps = (uint8_t *)ie;
1044 			break;
1045 		case WLAN_ELEMID_VHTCAP:
1046 			if (ie->ie_len != (sizeof(struct wlan_ie_vhtcaps) -
1047 					   sizeof(struct ie_header)))
1048 				goto err;
1049 			scan_params->ie_list.vhtcap = (uint8_t *)ie;
1050 			break;
1051 		case WLAN_ELEMID_VHTOP:
1052 			if (ie->ie_len != (sizeof(struct wlan_ie_vhtop) -
1053 					   sizeof(struct ie_header)))
1054 				goto err;
1055 			scan_params->ie_list.vhtop = (uint8_t *)ie;
1056 			break;
1057 		case WLAN_ELEMID_OP_MODE_NOTIFY:
1058 			if (ie->ie_len != WLAN_OPMODE_IE_MAX_LEN)
1059 				goto err;
1060 			scan_params->ie_list.opmode = (uint8_t *)ie;
1061 			break;
1062 		case WLAN_ELEMID_MOBILITY_DOMAIN:
1063 			if (ie->ie_len != WLAN_MOBILITY_DOMAIN_IE_MAX_LEN)
1064 				goto err;
1065 			scan_params->ie_list.mdie = (uint8_t *)ie;
1066 			break;
1067 		case WLAN_ELEMID_VENDOR:
1068 			status = util_scan_parse_vendor_ie(scan_params,
1069 							   ie);
1070 			if (QDF_IS_STATUS_ERROR(status))
1071 				goto err_status;
1072 			break;
1073 		case WLAN_ELEMID_CHAN_SWITCH_WRAP:
1074 			scan_params->ie_list.cswrp = (uint8_t *)ie;
1075 			/* Go to next sub IE */
1076 			sub_ie = (struct ie_header *)
1077 			(((uint8_t *)ie) + sizeof(struct ie_header));
1078 			sub_ie_len = ie->ie_len;
1079 			status =
1080 				util_scan_parse_chan_switch_wrapper_ie(
1081 					scan_params, sub_ie, sub_ie_len);
1082 			if (QDF_IS_STATUS_ERROR(status)) {
1083 				goto err_status;
1084 			}
1085 			break;
1086 		case WLAN_ELEMID_FILS_INDICATION:
1087 			if (ie->ie_len < WLAN_FILS_INDICATION_IE_MIN_LEN)
1088 				goto err;
1089 			scan_params->ie_list.fils_indication = (uint8_t *)ie;
1090 			break;
1091 		case WLAN_ELEMID_EXTN_ELEM:
1092 			status = util_scan_parse_extn_ie(scan_params, ie);
1093 			if (QDF_IS_STATUS_ERROR(status))
1094 				goto err_status;
1095 			break;
1096 		case WLAN_ELEMID_REDUCED_NEIGHBOR_REPORT:
1097 			if (ie->ie_len < WLAN_RNR_IE_MIN_LEN)
1098 				goto err;
1099 			scan_params->ie_list.rnrie = (uint8_t *)ie;
1100 			status = util_scan_parse_rnr_ie(scan_params, ie);
1101 			if (QDF_IS_STATUS_ERROR(status))
1102 				goto err_status;
1103 			break;
1104 		default:
1105 			break;
1106 		}
1107 
1108 		/* Consume info element */
1109 		ie_len -= ie->ie_len;
1110 		/* Go to next IE */
1111 		ie = (struct ie_header *)
1112 			(((uint8_t *) ie) +
1113 			sizeof(struct ie_header) +
1114 			ie->ie_len);
1115 	}
1116 
1117 	return QDF_STATUS_SUCCESS;
1118 
1119 err:
1120 	status = QDF_STATUS_E_INVAL;
1121 err_status:
1122 	scm_debug("failed to parse IE - id: %d, len: %d",
1123 		  ie->ie_id, ie->ie_len);
1124 
1125 	return status;
1126 }
1127 
1128 /**
1129  * util_scan_update_esp_data: update ESP params from beacon/probe response
1130  * @esp_information: pointer to wlan_esp_information
1131  * @scan_entry: new received entry
1132  *
1133  * The Estimated Service Parameters element is
1134  * used by a AP to provide information to another STA which
1135  * can then use the information as input to an algorithm to
1136  * generate an estimate of throughput between the two STAs.
1137  * The ESP Information List field contains from 1 to 4 ESP
1138  * Information fields(each field 24 bits), each corresponding
1139  * to an access category for which estimated service parameters
1140  * information is provided.
1141  *
1142  * Return: None
1143  */
1144 static void util_scan_update_esp_data(struct wlan_esp_ie *esp_information,
1145 		struct scan_cache_entry *scan_entry)
1146 {
1147 
1148 	uint8_t *data;
1149 	int i = 0;
1150 	uint64_t total_elements;
1151 	struct wlan_esp_info *esp_info;
1152 	struct wlan_esp_ie *esp_ie;
1153 
1154 	esp_ie = (struct wlan_esp_ie *)
1155 		util_scan_entry_esp_info(scan_entry);
1156 
1157 	total_elements  = esp_ie->esp_len;
1158 	data = (uint8_t *)esp_ie + 3;
1159 	do_div(total_elements, ESP_INFORMATION_LIST_LENGTH);
1160 
1161 	if (total_elements > MAX_ESP_INFORMATION_FIELD) {
1162 		scm_err("No of Air time fractions are greater than supported");
1163 		return;
1164 	}
1165 
1166 	for (i = 0; i < total_elements; i++) {
1167 		esp_info = (struct wlan_esp_info *)data;
1168 		if (esp_info->access_category == ESP_AC_BK) {
1169 			qdf_mem_copy(&esp_information->esp_info_AC_BK,
1170 					data, 3);
1171 			data = data + ESP_INFORMATION_LIST_LENGTH;
1172 			continue;
1173 		}
1174 		if (esp_info->access_category == ESP_AC_BE) {
1175 			qdf_mem_copy(&esp_information->esp_info_AC_BE,
1176 					data, 3);
1177 			data = data + ESP_INFORMATION_LIST_LENGTH;
1178 			continue;
1179 		}
1180 		if (esp_info->access_category == ESP_AC_VI) {
1181 			qdf_mem_copy(&esp_information->esp_info_AC_VI,
1182 					data, 3);
1183 			data = data + ESP_INFORMATION_LIST_LENGTH;
1184 			continue;
1185 		}
1186 		if (esp_info->access_category == ESP_AC_VO) {
1187 			qdf_mem_copy(&esp_information->esp_info_AC_VO,
1188 					data, 3);
1189 			data = data + ESP_INFORMATION_LIST_LENGTH;
1190 			break;
1191 		}
1192 	}
1193 }
1194 
1195 /**
1196  * util_scan_scm_update_bss_with_esp_dataa: calculate estimated air time
1197  * fraction
1198  * @scan_entry: new received entry
1199  *
1200  * This function process all Access category ESP params and provide
1201  * best effort air time fraction.
1202  * If best effort is not available, it will choose VI, VO and BK in sequence
1203  *
1204  */
1205 static void util_scan_scm_update_bss_with_esp_data(
1206 		struct scan_cache_entry *scan_entry)
1207 {
1208 	uint8_t air_time_fraction = 0;
1209 	struct wlan_esp_ie esp_information;
1210 
1211 	if (!scan_entry->ie_list.esp)
1212 		return;
1213 
1214 	util_scan_update_esp_data(&esp_information, scan_entry);
1215 
1216 	/*
1217 	 * If the ESP metric is transmitting multiple airtime fractions, then
1218 	 * follow the sequence AC_BE, AC_VI, AC_VO, AC_BK and pick whichever is
1219 	 * the first one available
1220 	 */
1221 	if (esp_information.esp_info_AC_BE.access_category
1222 			== ESP_AC_BE)
1223 		air_time_fraction =
1224 			esp_information.esp_info_AC_BE.
1225 			estimated_air_fraction;
1226 	else if (esp_information.esp_info_AC_VI.access_category
1227 			== ESP_AC_VI)
1228 		air_time_fraction =
1229 			esp_information.esp_info_AC_VI.
1230 			estimated_air_fraction;
1231 	else if (esp_information.esp_info_AC_VO.access_category
1232 			== ESP_AC_VO)
1233 		air_time_fraction =
1234 			esp_information.esp_info_AC_VO.
1235 			estimated_air_fraction;
1236 	else if (esp_information.esp_info_AC_BK.access_category
1237 			== ESP_AC_BK)
1238 		air_time_fraction =
1239 			esp_information.esp_info_AC_BK.
1240 				estimated_air_fraction;
1241 	scan_entry->air_time_fraction = air_time_fraction;
1242 }
1243 
1244 /**
1245  * util_scan_scm_calc_nss_supported_by_ap() - finds out nss from AP
1246  * @scan_entry: new received entry
1247  *
1248  * Return: number of nss advertised by AP
1249  */
1250 static int util_scan_scm_calc_nss_supported_by_ap(
1251 		struct scan_cache_entry *scan_params)
1252 {
1253 	struct htcap_cmn_ie *htcap;
1254 	struct wlan_ie_vhtcaps *vhtcaps;
1255 	uint8_t rx_mcs_map;
1256 
1257 	htcap = (struct htcap_cmn_ie *)
1258 		util_scan_entry_htcap(scan_params);
1259 	vhtcaps = (struct wlan_ie_vhtcaps *)
1260 		util_scan_entry_vhtcap(scan_params);
1261 	if (vhtcaps) {
1262 		rx_mcs_map = vhtcaps->rx_mcs_map;
1263 		if ((rx_mcs_map & 0xC0) != 0xC0)
1264 			return 4;
1265 
1266 		if ((rx_mcs_map & 0x30) != 0x30)
1267 			return 3;
1268 
1269 		if ((rx_mcs_map & 0x0C) != 0x0C)
1270 			return 2;
1271 	} else if (htcap) {
1272 		if (htcap->mcsset[3])
1273 			return 4;
1274 
1275 		if (htcap->mcsset[2])
1276 			return 3;
1277 
1278 		if (htcap->mcsset[1])
1279 			return 2;
1280 
1281 	}
1282 	return 1;
1283 }
1284 
1285 #ifdef WLAN_DFS_CHAN_HIDDEN_SSID
1286 QDF_STATUS
1287 util_scan_add_hidden_ssid(struct wlan_objmgr_pdev *pdev, qdf_nbuf_t bcnbuf)
1288 {
1289 	struct wlan_frame_hdr *hdr;
1290 	struct wlan_bcn_frame *bcn;
1291 	struct wlan_scan_obj *scan_obj;
1292 	struct wlan_ssid *conf_ssid;
1293 	struct  ie_header *ie;
1294 	uint32_t frame_len = qdf_nbuf_len(bcnbuf);
1295 	uint16_t bcn_ie_offset, ssid_ie_start_offset, ssid_ie_end_offset;
1296 	uint16_t tmplen, ie_length;
1297 	uint8_t *pbeacon, *tmp;
1298 	bool     set_ssid_flag = false;
1299 	struct ie_ssid *ssid;
1300 	uint8_t pdev_id;
1301 
1302 	if (!pdev) {
1303 		scm_warn("pdev: 0x%pK is NULL", pdev);
1304 		return QDF_STATUS_E_NULL_VALUE;
1305 	}
1306 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1307 	scan_obj = wlan_pdev_get_scan_obj(pdev);
1308 	if (!scan_obj) {
1309 		scm_warn("null scan_obj");
1310 		return QDF_STATUS_E_NULL_VALUE;
1311 	}
1312 
1313 	conf_ssid = &scan_obj->pdev_info[pdev_id].conf_ssid;
1314 
1315 	hdr = (struct wlan_frame_hdr *)qdf_nbuf_data(bcnbuf);
1316 
1317 	/* received bssid does not match configured bssid */
1318 	if (qdf_mem_cmp(hdr->i_addr3, scan_obj->pdev_info[pdev_id].conf_bssid,
1319 			QDF_MAC_ADDR_SIZE) ||
1320 			conf_ssid->length == 0) {
1321 		return QDF_STATUS_SUCCESS;
1322 	}
1323 
1324 	bcn = (struct wlan_bcn_frame *)(qdf_nbuf_data(bcnbuf) + sizeof(*hdr));
1325 	pbeacon = (uint8_t *)bcn;
1326 
1327 	ie = (struct ie_header *)(pbeacon +
1328 				  offsetof(struct wlan_bcn_frame, ie));
1329 
1330 	bcn_ie_offset = offsetof(struct wlan_bcn_frame, ie);
1331 	ie_length = (uint16_t)(frame_len - sizeof(*hdr) -
1332 			       bcn_ie_offset);
1333 
1334 	while (ie_length >=  sizeof(struct ie_header)) {
1335 		ie_length -= sizeof(struct ie_header);
1336 
1337 		bcn_ie_offset += sizeof(struct ie_header);
1338 
1339 		if (ie_length < ie->ie_len) {
1340 			scm_debug("Incomplete corrupted IE:%x", ie->ie_id);
1341 			return QDF_STATUS_E_INVAL;
1342 		}
1343 		if (ie->ie_id == WLAN_ELEMID_SSID) {
1344 			if (ie->ie_len > (sizeof(struct ie_ssid) -
1345 						 sizeof(struct ie_header))) {
1346 				return QDF_STATUS_E_INVAL;
1347 			}
1348 			ssid = (struct ie_ssid *)ie;
1349 			if (util_scan_is_hidden_ssid(ssid)) {
1350 				set_ssid_flag  = true;
1351 				ssid_ie_start_offset = bcn_ie_offset -
1352 					sizeof(struct ie_header);
1353 				ssid_ie_end_offset = bcn_ie_offset +
1354 					ie->ie_len;
1355 			}
1356 		}
1357 		if (ie->ie_len == 0) {
1358 			ie += 1;    /* next IE */
1359 			continue;
1360 		}
1361 		if (ie->ie_id == WLAN_ELEMID_VENDOR &&
1362 		    is_wps_oui((uint8_t *)ie)) {
1363 			set_ssid_flag = false;
1364 			break;
1365 		}
1366 		/* Consume info element */
1367 		ie_length -=  ie->ie_len;
1368 		/* Go to next IE */
1369 		ie = (struct ie_header *)(((uint8_t *)ie) +
1370 				sizeof(struct ie_header) +
1371 				ie->ie_len);
1372 	}
1373 
1374 	if (set_ssid_flag) {
1375 		/* Hidden SSID if the Length is 0 */
1376 		if (!ssid->ssid_len) {
1377 			/* increase the taillength by length of ssid */
1378 			if (qdf_nbuf_put_tail(bcnbuf,
1379 					      conf_ssid->length) == NULL) {
1380 				scm_debug("No enough tailroom");
1381 				return  QDF_STATUS_E_NOMEM;
1382 			}
1383 			/* length of the buffer to be copied */
1384 			tmplen = frame_len -
1385 				sizeof(*hdr) - ssid_ie_end_offset;
1386 			/*
1387 			 * tmp memory to copy the beacon info
1388 			 * after ssid ie.
1389 			 */
1390 			tmp = qdf_mem_malloc(tmplen * sizeof(u_int8_t));
1391 			if (!tmp)
1392 				return  QDF_STATUS_E_NOMEM;
1393 
1394 			/* Copy beacon data after ssid ie to tmp */
1395 			qdf_nbuf_copy_bits(bcnbuf, (sizeof(*hdr) +
1396 					   ssid_ie_end_offset), tmplen, tmp);
1397 			/* Add ssid length */
1398 			*(pbeacon + (ssid_ie_start_offset + 1))
1399 				= conf_ssid->length;
1400 			/* Insert the  SSID string */
1401 			qdf_mem_copy((pbeacon + ssid_ie_end_offset),
1402 				     conf_ssid->ssid, conf_ssid->length);
1403 			/* Copy rest of the beacon data */
1404 			qdf_mem_copy((pbeacon + ssid_ie_end_offset +
1405 				      conf_ssid->length), tmp, tmplen);
1406 			qdf_mem_free(tmp);
1407 
1408 			/* Hidden ssid with all 0's */
1409 		} else if (ssid->ssid_len == conf_ssid->length) {
1410 			/* Insert the  SSID string */
1411 			qdf_mem_copy((pbeacon + ssid_ie_start_offset +
1412 				      sizeof(struct ie_header)),
1413 				      conf_ssid->ssid, conf_ssid->length);
1414 		} else {
1415 			scm_debug("mismatch in hidden ssid length");
1416 			return QDF_STATUS_E_INVAL;
1417 		}
1418 	}
1419 	return QDF_STATUS_SUCCESS;
1420 }
1421 #endif /* WLAN_DFS_CHAN_HIDDEN_SSID */
1422 
1423 #ifdef WLAN_ADAPTIVE_11R
1424 /**
1425  * scm_fill_adaptive_11r_cap() - Check if the AP supports adaptive 11r
1426  * @scan_entry: Pointer to the scan entry
1427  *
1428  * Return: true if adaptive 11r is advertised else false
1429  */
1430 static void scm_fill_adaptive_11r_cap(struct scan_cache_entry *scan_entry)
1431 {
1432 	uint8_t *ie;
1433 	uint8_t data;
1434 	bool adaptive_11r;
1435 
1436 	ie = util_scan_entry_adaptive_11r(scan_entry);
1437 	if (!ie)
1438 		return;
1439 
1440 	data = *(ie + OUI_LENGTH);
1441 	adaptive_11r = (data & 0x1) ? true : false;
1442 
1443 	scan_entry->adaptive_11r_ap = adaptive_11r;
1444 }
1445 #else
1446 static void scm_fill_adaptive_11r_cap(struct scan_cache_entry *scan_entry)
1447 {
1448 	scan_entry->adaptive_11r_ap = false;
1449 }
1450 #endif
1451 
1452 static void util_scan_set_security(struct scan_cache_entry *scan_params)
1453 {
1454 	if (util_scan_entry_wpa(scan_params))
1455 		scan_params->security_type |= SCAN_SECURITY_TYPE_WPA;
1456 
1457 	if (util_scan_entry_rsn(scan_params))
1458 		scan_params->security_type |= SCAN_SECURITY_TYPE_RSN;
1459 	if (util_scan_entry_wapi(scan_params))
1460 		scan_params->security_type |= SCAN_SECURITY_TYPE_WAPI;
1461 
1462 	if (!scan_params->security_type &&
1463 	    scan_params->cap_info.wlan_caps.privacy)
1464 		scan_params->security_type |= SCAN_SECURITY_TYPE_WEP;
1465 }
1466 
1467 static QDF_STATUS
1468 util_scan_gen_scan_entry(struct wlan_objmgr_pdev *pdev,
1469 			 uint8_t *frame, qdf_size_t frame_len,
1470 			 uint32_t frm_subtype,
1471 			 struct mgmt_rx_event_params *rx_param,
1472 			 struct scan_mbssid_info *mbssid_info,
1473 			 qdf_list_t *scan_list)
1474 {
1475 	struct wlan_frame_hdr *hdr;
1476 	struct wlan_bcn_frame *bcn;
1477 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1478 	struct ie_ssid *ssid;
1479 	struct scan_cache_entry *scan_entry;
1480 	struct qbss_load_ie *qbss_load;
1481 	struct scan_cache_node *scan_node;
1482 	uint8_t i;
1483 	qdf_freq_t chan_freq = 0;
1484 	bool he_6g_dup_bcon = false;
1485 	uint8_t band_mask;
1486 
1487 	scan_entry = qdf_mem_malloc_atomic(sizeof(*scan_entry));
1488 	if (!scan_entry) {
1489 		scm_err("failed to allocate memory for scan_entry");
1490 		return QDF_STATUS_E_NOMEM;
1491 	}
1492 	scan_entry->raw_frame.ptr =
1493 			qdf_mem_malloc_atomic(frame_len);
1494 	if (!scan_entry->raw_frame.ptr) {
1495 		scm_err("failed to allocate memory for frame");
1496 		qdf_mem_free(scan_entry);
1497 		return QDF_STATUS_E_NOMEM;
1498 	}
1499 
1500 	bcn = (struct wlan_bcn_frame *)
1501 			   (frame + sizeof(*hdr));
1502 	hdr = (struct wlan_frame_hdr *)frame;
1503 
1504 	/* update timestamp in nanoseconds needed by kernel layers */
1505 	scan_entry->boottime_ns = qdf_get_bootbased_boottime_ns();
1506 
1507 	scan_entry->frm_subtype = frm_subtype;
1508 	qdf_mem_copy(scan_entry->bssid.bytes,
1509 		hdr->i_addr3, QDF_MAC_ADDR_SIZE);
1510 	/* Scr addr */
1511 	qdf_mem_copy(scan_entry->mac_addr.bytes,
1512 		hdr->i_addr2, QDF_MAC_ADDR_SIZE);
1513 	scan_entry->seq_num =
1514 		(le16toh(*(uint16_t *)hdr->i_seq) >> WLAN_SEQ_SEQ_SHIFT);
1515 
1516 	scan_entry->snr = rx_param->snr;
1517 	scan_entry->avg_snr = WLAN_SNR_IN(scan_entry->snr);
1518 	scan_entry->rssi_raw = rx_param->rssi;
1519 	scan_entry->avg_rssi = WLAN_RSSI_IN(scan_entry->rssi_raw);
1520 	scan_entry->tsf_delta = rx_param->tsf_delta;
1521 	scan_entry->pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1522 
1523 	/* Copy per chain rssi to scan entry */
1524 	qdf_mem_copy(scan_entry->per_chain_rssi, rx_param->rssi_ctl,
1525 		     WLAN_MGMT_TXRX_HOST_MAX_ANTENNA);
1526 	band_mask = BIT(wlan_reg_freq_to_band(rx_param->chan_freq));
1527 
1528 	if (!wlan_psoc_nif_fw_ext_cap_get(wlan_pdev_get_psoc(pdev),
1529 					  WLAN_SOC_CEXT_HW_DB2DBM)) {
1530 		for (i = 0; i < WLAN_MGMT_TXRX_HOST_MAX_ANTENNA; i++) {
1531 			if (scan_entry->per_chain_rssi[i] !=
1532 			    WLAN_INVALID_PER_CHAIN_SNR)
1533 				scan_entry->per_chain_rssi[i] +=
1534 						WLAN_NOISE_FLOOR_DBM_DEFAULT;
1535 			else
1536 				scan_entry->per_chain_rssi[i] =
1537 						WLAN_INVALID_PER_CHAIN_RSSI;
1538 		}
1539 	}
1540 
1541 	/* store jiffies */
1542 	scan_entry->rrm_parent_tsf = (uint32_t)qdf_system_ticks();
1543 
1544 	scan_entry->bcn_int = le16toh(bcn->beacon_interval);
1545 
1546 	/*
1547 	 * In case if the beacon dosnt have
1548 	 * valid beacon interval falback to def
1549 	 */
1550 	if (!scan_entry->bcn_int)
1551 		scan_entry->bcn_int = 100;
1552 	scan_entry->cap_info.value = le16toh(bcn->capability.value);
1553 	qdf_mem_copy(scan_entry->tsf_info.data,
1554 		bcn->timestamp, 8);
1555 	scan_entry->erp = ERP_NON_ERP_PRESENT;
1556 
1557 	scan_entry->scan_entry_time =
1558 		qdf_mc_timer_get_system_time();
1559 
1560 	scan_entry->raw_frame.len = frame_len;
1561 	qdf_mem_copy(scan_entry->raw_frame.ptr,
1562 		frame, frame_len);
1563 	status = util_scan_populate_bcn_ie_list(pdev, scan_entry, &chan_freq,
1564 						band_mask);
1565 	if (QDF_IS_STATUS_ERROR(status)) {
1566 		scm_debug("%pM: failed to parse beacon IE",
1567 			  scan_entry->bssid.bytes);
1568 		qdf_mem_free(scan_entry->raw_frame.ptr);
1569 		qdf_mem_free(scan_entry);
1570 		return QDF_STATUS_E_FAILURE;
1571 	}
1572 
1573 	ssid = (struct ie_ssid *)
1574 		scan_entry->ie_list.ssid;
1575 
1576 	if (ssid && (ssid->ssid_len > WLAN_SSID_MAX_LEN)) {
1577 		qdf_mem_free(scan_entry->raw_frame.ptr);
1578 		qdf_mem_free(scan_entry);
1579 		return QDF_STATUS_E_FAILURE;
1580 	}
1581 
1582 	if (scan_entry->ie_list.p2p)
1583 		scan_entry->is_p2p = true;
1584 
1585 	if (!chan_freq && util_scan_entry_hecap(scan_entry)) {
1586 		status = util_scan_get_chan_from_he_6g_params(pdev, scan_entry,
1587 							      &chan_freq,
1588 							      &he_6g_dup_bcon,
1589 							      band_mask);
1590 		if (QDF_IS_STATUS_ERROR(status)) {
1591 			qdf_mem_free(scan_entry->raw_frame.ptr);
1592 			qdf_mem_free(scan_entry);
1593 			return QDF_STATUS_E_FAILURE;
1594 		}
1595 	}
1596 
1597 	if (chan_freq)
1598 		scan_entry->channel.chan_freq = chan_freq;
1599 
1600 	/* If no channel info is present in beacon use meta channel */
1601 	if (!scan_entry->channel.chan_freq) {
1602 		scan_entry->channel.chan_freq = rx_param->chan_freq;
1603 	} else if (rx_param->chan_freq !=
1604 	   scan_entry->channel.chan_freq) {
1605 		if (!wlan_reg_is_49ghz_freq(scan_entry->channel.chan_freq) &&
1606 		    !he_6g_dup_bcon)
1607 			scan_entry->channel_mismatch = true;
1608 	}
1609 
1610 	if (util_scan_is_hidden_ssid(ssid)) {
1611 		scan_entry->ie_list.ssid = NULL;
1612 		scan_entry->is_hidden_ssid = true;
1613 	} else {
1614 		qdf_mem_copy(scan_entry->ssid.ssid,
1615 				ssid->ssid, ssid->ssid_len);
1616 		scan_entry->ssid.length = ssid->ssid_len;
1617 		scan_entry->hidden_ssid_timestamp =
1618 			scan_entry->scan_entry_time;
1619 	}
1620 	qdf_mem_copy(&scan_entry->mbssid_info, mbssid_info,
1621 		     sizeof(scan_entry->mbssid_info));
1622 
1623 	scan_entry->phy_mode = util_scan_get_phymode(pdev, scan_entry);
1624 
1625 	scan_entry->nss = util_scan_scm_calc_nss_supported_by_ap(scan_entry);
1626 	scm_fill_adaptive_11r_cap(scan_entry);
1627 	util_scan_set_security(scan_entry);
1628 
1629 	util_scan_scm_update_bss_with_esp_data(scan_entry);
1630 	qbss_load = (struct qbss_load_ie *)
1631 			util_scan_entry_qbssload(scan_entry);
1632 	if (qbss_load)
1633 		scan_entry->qbss_chan_load = qbss_load->qbss_chan_load;
1634 
1635 	scan_node = qdf_mem_malloc_atomic(sizeof(*scan_node));
1636 	if (!scan_node) {
1637 		qdf_mem_free(scan_entry->raw_frame.ptr);
1638 		qdf_mem_free(scan_entry);
1639 		return QDF_STATUS_E_FAILURE;
1640 	}
1641 
1642 	scan_node->entry = scan_entry;
1643 	qdf_list_insert_front(scan_list, &scan_node->node);
1644 
1645 	return status;
1646 }
1647 
1648 /**
1649  * util_scan_find_ie() - find information element
1650  * @eid: element id
1651  * @ies: pointer consisting of IEs
1652  * @len: IE length
1653  *
1654  * Return: NULL if the element ID is not found or
1655  * a pointer to the first byte of the requested
1656  * element
1657  */
1658 static uint8_t *util_scan_find_ie(uint8_t eid, uint8_t *ies,
1659 				  int32_t len)
1660 {
1661 	while (len >= 2 && len >= ies[1] + 2) {
1662 		if (ies[0] == eid)
1663 			return ies;
1664 		len -= ies[1] + 2;
1665 		ies += ies[1] + 2;
1666 	}
1667 
1668 	return NULL;
1669 }
1670 
1671 #ifdef WLAN_FEATURE_MBSSID
1672 static void util_gen_new_bssid(uint8_t *bssid, uint8_t max_bssid,
1673 			       uint8_t mbssid_index,
1674 			       uint8_t *new_bssid_addr)
1675 {
1676 	uint8_t lsb_n;
1677 	int i;
1678 
1679 	for (i = 0; i < QDF_MAC_ADDR_SIZE; i++)
1680 		new_bssid_addr[i] = bssid[i];
1681 
1682 	lsb_n = new_bssid_addr[5] & ((1 << max_bssid) - 1);
1683 
1684 	new_bssid_addr[5] &= ~((1 << max_bssid) - 1);
1685 	new_bssid_addr[5] |= (lsb_n + mbssid_index) % (1 << max_bssid);
1686 }
1687 
1688 static uint32_t util_gen_new_ie(uint8_t *ie, uint32_t ielen,
1689 				uint8_t *subelement,
1690 				size_t subie_len, uint8_t *new_ie)
1691 {
1692 	uint8_t *pos, *tmp;
1693 	const uint8_t *tmp_old, *tmp_new;
1694 	uint8_t *sub_copy;
1695 
1696 	/* copy subelement as we need to change its content to
1697 	 * mark an ie after it is processed.
1698 	 */
1699 	sub_copy = qdf_mem_malloc(subie_len);
1700 	if (!sub_copy)
1701 		return 0;
1702 	qdf_mem_copy(sub_copy, subelement, subie_len);
1703 
1704 	pos = &new_ie[0];
1705 
1706 	/* new ssid */
1707 	tmp_new = util_scan_find_ie(WLAN_ELEMID_SSID, sub_copy, subie_len);
1708 	if (tmp_new) {
1709 		qdf_mem_copy(pos, tmp_new, tmp_new[1] + 2);
1710 		pos += (tmp_new[1] + 2);
1711 	}
1712 
1713 	/* go through IEs in ie (skip SSID) and subelement,
1714 	 * merge them into new_ie
1715 	 */
1716 	tmp_old = util_scan_find_ie(WLAN_ELEMID_SSID, ie, ielen);
1717 	tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
1718 
1719 	while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
1720 		if (tmp_old[0] == 0) {
1721 			tmp_old++;
1722 			continue;
1723 		}
1724 
1725 		tmp = (uint8_t *)util_scan_find_ie(tmp_old[0], sub_copy,
1726 				subie_len);
1727 		if (!tmp) {
1728 			/* ie in old ie but not in subelement */
1729 			if (tmp_old[0] != WLAN_ELEMID_MULTIPLE_BSSID) {
1730 				qdf_mem_copy(pos, tmp_old, tmp_old[1] + 2);
1731 				pos += tmp_old[1] + 2;
1732 			}
1733 		} else {
1734 			/* ie in transmitting ie also in subelement,
1735 			 * copy from subelement and flag the ie in subelement
1736 			 * as copied (by setting eid field to 0xff). For
1737 			 * vendor ie, compare OUI + type + subType to
1738 			 * determine if they are the same ie.
1739 			 */
1740 			if (tmp_old[0] == WLAN_ELEMID_VENDOR) {
1741 				if (!qdf_mem_cmp(tmp_old + 2, tmp + 2, 5)) {
1742 					/* same vendor ie, copy from
1743 					 * subelement
1744 					 */
1745 					qdf_mem_copy(pos, tmp, tmp[1] + 2);
1746 					pos += tmp[1] + 2;
1747 					tmp[0] = 0xff;
1748 				} else {
1749 					qdf_mem_copy(pos, tmp_old,
1750 						     tmp_old[1] + 2);
1751 					pos += tmp_old[1] + 2;
1752 				}
1753 			} else {
1754 				/* copy ie from subelement into new ie */
1755 				qdf_mem_copy(pos, tmp, tmp[1] + 2);
1756 				pos += tmp[1] + 2;
1757 				tmp[0] = 0xff;
1758 			}
1759 		}
1760 
1761 		if (tmp_old + tmp_old[1] + 2 - ie == ielen)
1762 			break;
1763 
1764 		tmp_old += tmp_old[1] + 2;
1765 	}
1766 
1767 	/* go through subelement again to check if there is any ie not
1768 	 * copied to new ie, skip ssid, capability, bssid-index ie
1769 	 */
1770 	tmp_new = sub_copy;
1771 	while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
1772 		if (!(tmp_new[0] == WLAN_ELEMID_NONTX_BSSID_CAP ||
1773 		      tmp_new[0] == WLAN_ELEMID_SSID ||
1774 		      tmp_new[0] == WLAN_ELEMID_MULTI_BSSID_IDX ||
1775 		      tmp_new[0] == 0xff)) {
1776 			qdf_mem_copy(pos, tmp_new, tmp_new[1] + 2);
1777 			pos += tmp_new[1] + 2;
1778 		}
1779 		if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
1780 			break;
1781 		tmp_new += tmp_new[1] + 2;
1782 	}
1783 
1784 	qdf_mem_free(sub_copy);
1785 	return pos - new_ie;
1786 }
1787 
1788 static QDF_STATUS util_scan_parse_mbssid(struct wlan_objmgr_pdev *pdev,
1789 					 uint8_t *frame, qdf_size_t frame_len,
1790 					 uint32_t frm_subtype,
1791 					 struct mgmt_rx_event_params *rx_param,
1792 					 qdf_list_t *scan_list)
1793 {
1794 	struct wlan_bcn_frame *bcn;
1795 	struct wlan_frame_hdr *hdr;
1796 	struct scan_mbssid_info mbssid_info;
1797 	QDF_STATUS status;
1798 	uint8_t *pos, *subelement, *mbssid_end_pos;
1799 	uint8_t *tmp, *mbssid_index_ie;
1800 	uint32_t subie_len, new_ie_len;
1801 	uint8_t new_bssid[QDF_MAC_ADDR_SIZE], bssid[QDF_MAC_ADDR_SIZE];
1802 	uint8_t *new_ie;
1803 	uint8_t *ie, *new_frame = NULL;
1804 	uint64_t ielen, new_frame_len;
1805 
1806 	hdr = (struct wlan_frame_hdr *)frame;
1807 	bcn = (struct wlan_bcn_frame *)(frame + sizeof(struct wlan_frame_hdr));
1808 	ie = (uint8_t *)&bcn->ie;
1809 	ielen = (uint16_t)(frame_len -
1810 		sizeof(struct wlan_frame_hdr) -
1811 		offsetof(struct wlan_bcn_frame, ie));
1812 	qdf_mem_copy(bssid, hdr->i_addr3, QDF_MAC_ADDR_SIZE);
1813 
1814 	if (!util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID, ie, ielen))
1815 		return QDF_STATUS_E_FAILURE;
1816 
1817 	pos = ie;
1818 
1819 	new_ie = qdf_mem_malloc(MAX_IE_LEN);
1820 	if (!new_ie)
1821 		return QDF_STATUS_E_NOMEM;
1822 
1823 	while (pos < ie + ielen + 2) {
1824 		tmp = util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID, pos,
1825 					ielen - (pos - ie));
1826 		if (!tmp)
1827 			break;
1828 
1829 		mbssid_info.profile_count = 1 << tmp[2];
1830 		mbssid_end_pos = tmp + tmp[1] + 2;
1831 		/* Skip Element ID, Len, MaxBSSID Indicator */
1832 		if (tmp[1] < 4)
1833 			break;
1834 		for (subelement = tmp + 3; subelement < mbssid_end_pos - 1;
1835 		     subelement += 2 + subelement[1]) {
1836 			subie_len = subelement[1];
1837 			if (mbssid_end_pos - subelement < 2 + subie_len)
1838 				break;
1839 			if (subelement[0] != 0 || subelement[1] < 4) {
1840 				/* not a valid BSS profile */
1841 				continue;
1842 			}
1843 
1844 			if (subelement[2] != WLAN_ELEMID_NONTX_BSSID_CAP ||
1845 			    subelement[3] != 2) {
1846 				/* The first element within the Nontransmitted
1847 				 * BSSID Profile is not the Nontransmitted
1848 				 * BSSID Capability element.
1849 				 */
1850 				continue;
1851 			}
1852 
1853 			/* found a Nontransmitted BSSID Profile */
1854 			mbssid_index_ie =
1855 				util_scan_find_ie(WLAN_ELEMID_MULTI_BSSID_IDX,
1856 						  subelement + 2, subie_len);
1857 			if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1858 			    mbssid_index_ie[2] == 0) {
1859 				/* No valid Multiple BSSID-Index element */
1860 				continue;
1861 			}
1862 			qdf_mem_copy(&mbssid_info.trans_bssid, bssid,
1863 				     QDF_MAC_ADDR_SIZE);
1864 			mbssid_info.profile_num = mbssid_index_ie[2];
1865 			util_gen_new_bssid(bssid, tmp[2], mbssid_index_ie[2],
1866 					   new_bssid);
1867 			new_ie_len = util_gen_new_ie(ie, ielen, subelement + 2,
1868 						     subie_len, new_ie);
1869 			if (!new_ie_len)
1870 				continue;
1871 
1872 			new_frame_len = frame_len - ielen + new_ie_len;
1873 			new_frame = qdf_mem_malloc(new_frame_len);
1874 			if (!new_frame) {
1875 				qdf_mem_free(new_ie);
1876 				return QDF_STATUS_E_NOMEM;
1877 			}
1878 
1879 			/*
1880 			 * Copy the header(24byte), timestamp(8 byte),
1881 			 * beaconinterval(2byte) and capability(2byte)
1882 			 */
1883 			qdf_mem_copy(new_frame, frame, 36);
1884 			/* Copy the new ie generated from MBSSID profile*/
1885 			hdr = (struct wlan_frame_hdr *)new_frame;
1886 			qdf_mem_copy(hdr->i_addr2, new_bssid,
1887 				     QDF_MAC_ADDR_SIZE);
1888 			qdf_mem_copy(hdr->i_addr3, new_bssid,
1889 				     QDF_MAC_ADDR_SIZE);
1890 			/* Copy the new ie generated from MBSSID profile*/
1891 			qdf_mem_copy(new_frame +
1892 					offsetof(struct wlan_bcn_frame, ie) +
1893 					sizeof(struct wlan_frame_hdr),
1894 					new_ie, new_ie_len);
1895 			status = util_scan_gen_scan_entry(pdev, new_frame,
1896 							  new_frame_len,
1897 							  frm_subtype,
1898 							  rx_param,
1899 							  &mbssid_info,
1900 							  scan_list);
1901 			if (QDF_IS_STATUS_ERROR(status)) {
1902 				qdf_mem_free(new_frame);
1903 				scm_err("failed to generate a scan entry");
1904 				break;
1905 			}
1906 			/* scan entry makes its own copy so free the frame*/
1907 			qdf_mem_free(new_frame);
1908 		}
1909 
1910 		pos = mbssid_end_pos;
1911 	}
1912 	qdf_mem_free(new_ie);
1913 
1914 	return QDF_STATUS_SUCCESS;
1915 }
1916 #else
1917 static QDF_STATUS util_scan_parse_mbssid(struct wlan_objmgr_pdev *pdev,
1918 					 uint8_t *frame, qdf_size_t frame_len,
1919 					 uint32_t frm_subtype,
1920 					 struct mgmt_rx_event_params *rx_param,
1921 					 qdf_list_t *scan_list)
1922 {
1923 	return QDF_STATUS_SUCCESS;
1924 }
1925 #endif
1926 
1927 static QDF_STATUS
1928 util_scan_parse_beacon_frame(struct wlan_objmgr_pdev *pdev,
1929 			     uint8_t *frame,
1930 			     qdf_size_t frame_len,
1931 			     uint32_t frm_subtype,
1932 			     struct mgmt_rx_event_params *rx_param,
1933 			     qdf_list_t *scan_list)
1934 {
1935 	struct wlan_bcn_frame *bcn;
1936 	struct wlan_frame_hdr *hdr;
1937 	uint8_t *mbssid_ie = NULL;
1938 	uint32_t ie_len = 0;
1939 	QDF_STATUS status;
1940 	struct scan_mbssid_info mbssid_info = { 0 };
1941 
1942 	hdr = (struct wlan_frame_hdr *)frame;
1943 	bcn = (struct wlan_bcn_frame *)
1944 			   (frame + sizeof(struct wlan_frame_hdr));
1945 	ie_len = (uint16_t)(frame_len -
1946 		sizeof(struct wlan_frame_hdr) -
1947 		offsetof(struct wlan_bcn_frame, ie));
1948 
1949 	mbssid_ie = util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID,
1950 				      (uint8_t *)&bcn->ie, ie_len);
1951 	if (mbssid_ie) {
1952 		qdf_mem_copy(&mbssid_info.trans_bssid,
1953 			     hdr->i_addr3, QDF_MAC_ADDR_SIZE);
1954 		mbssid_info.profile_count = 1 << mbssid_ie[2];
1955 	}
1956 
1957 	status = util_scan_gen_scan_entry(pdev, frame, frame_len,
1958 					  frm_subtype, rx_param,
1959 					  &mbssid_info,
1960 					  scan_list);
1961 
1962 	/*
1963 	 * IF MBSSID IE is present in the beacon then
1964 	 * scan component will create a new entry for
1965 	 * each BSSID found in the MBSSID
1966 	 */
1967 	if (mbssid_ie)
1968 		status = util_scan_parse_mbssid(pdev, frame, frame_len,
1969 						frm_subtype, rx_param,
1970 						scan_list);
1971 
1972 	if (QDF_IS_STATUS_ERROR(status))
1973 		scm_debug_rl("Failed to create a scan entry");
1974 
1975 	return status;
1976 }
1977 
1978 qdf_list_t *
1979 util_scan_unpack_beacon_frame(struct wlan_objmgr_pdev *pdev, uint8_t *frame,
1980 			      qdf_size_t frame_len, uint32_t frm_subtype,
1981 			      struct mgmt_rx_event_params *rx_param)
1982 {
1983 	qdf_list_t *scan_list;
1984 	QDF_STATUS status;
1985 
1986 	scan_list = qdf_mem_malloc_atomic(sizeof(*scan_list));
1987 	if (!scan_list) {
1988 		scm_err("failed to allocate scan_list");
1989 		return NULL;
1990 	}
1991 	qdf_list_create(scan_list, MAX_SCAN_CACHE_SIZE);
1992 
1993 	status = util_scan_parse_beacon_frame(pdev, frame, frame_len,
1994 					      frm_subtype, rx_param,
1995 					      scan_list);
1996 	if (QDF_IS_STATUS_ERROR(status)) {
1997 		ucfg_scan_purge_results(scan_list);
1998 		return NULL;
1999 	}
2000 
2001 	return scan_list;
2002 }
2003 
2004 QDF_STATUS
2005 util_scan_entry_update_mlme_info(struct wlan_objmgr_pdev *pdev,
2006 	struct scan_cache_entry *scan_entry)
2007 {
2008 
2009 	if (!pdev || !scan_entry) {
2010 		scm_err("pdev 0x%pK, scan_entry: 0x%pK", pdev, scan_entry);
2011 		return QDF_STATUS_E_INVAL;
2012 	}
2013 
2014 	return scm_update_scan_mlme_info(pdev, scan_entry);
2015 }
2016 
2017 bool util_is_scan_completed(struct scan_event *event, bool *success)
2018 {
2019 	if ((event->type == SCAN_EVENT_TYPE_COMPLETED) ||
2020 	    (event->type == SCAN_EVENT_TYPE_DEQUEUED) ||
2021 	    (event->type == SCAN_EVENT_TYPE_START_FAILED)) {
2022 		if ((event->type == SCAN_EVENT_TYPE_COMPLETED) &&
2023 		    (event->reason == SCAN_REASON_COMPLETED))
2024 			*success = true;
2025 		else
2026 			*success = false;
2027 
2028 		return true;
2029 	}
2030 
2031 	*success = false;
2032 	return false;
2033 }
2034 
2035