xref: /wlan-dirver/qca-wifi-host-cmn/umac/scan/dispatcher/src/wlan_scan_utils_api.c (revision f3d3f64f12c13ea69171f005888f79bf13e50340)
1 /*
2  * Copyright (c) 2017-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * DOC: Defines scan utility functions
22  */
23 
24 #include <wlan_cmn.h>
25 #include <wlan_scan_ucfg_api.h>
26 #include <wlan_scan_utils_api.h>
27 #include <../../core/src/wlan_scan_cache_db.h>
28 #include <../../core/src/wlan_scan_main.h>
29 #include <wlan_reg_services_api.h>
30 #if defined(WLAN_SAE_SINGLE_PMK) && defined(WLAN_FEATURE_ROAM_OFFLOAD)
31 #include <wlan_mlme_api.h>
32 #endif
33 #ifdef WLAN_FEATURE_11BE_MLO
34 #include <wlan_utility.h>
35 #include "wlan_mlo_mgr_public_structs.h"
36 #include <utils_mlo.h>
37 #endif
38 #include "wlan_psoc_mlme_api.h"
39 #include "reg_services_public_struct.h"
40 #ifdef WLAN_FEATURE_ACTION_OUI
41 #include <wlan_action_oui_main.h>
42 #include <wlan_action_oui_public_struct.h>
43 #endif
44 #include <wlan_crypto_global_api.h>
45 
46 #define MAX_IE_LEN 1024
47 #define SHORT_SSID_LEN 4
48 #define NEIGHBOR_AP_LEN 1
49 #define BSS_PARAMS_LEN 1
50 
51 const char*
52 util_scan_get_ev_type_name(enum scan_event_type type)
53 {
54 	static const char * const event_name[] = {
55 		[SCAN_EVENT_TYPE_STARTED] = "STARTED",
56 		[SCAN_EVENT_TYPE_COMPLETED] = "COMPLETED",
57 		[SCAN_EVENT_TYPE_BSS_CHANNEL] = "HOME_CHANNEL",
58 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL] = "FOREIGN_CHANNEL",
59 		[SCAN_EVENT_TYPE_DEQUEUED] = "DEQUEUED",
60 		[SCAN_EVENT_TYPE_PREEMPTED] = "PREEMPTED",
61 		[SCAN_EVENT_TYPE_START_FAILED] = "START_FAILED",
62 		[SCAN_EVENT_TYPE_RESTARTED] = "RESTARTED",
63 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL_EXIT] = "FOREIGN_CHANNEL_EXIT",
64 		[SCAN_EVENT_TYPE_SUSPENDED] = "SUSPENDED",
65 		[SCAN_EVENT_TYPE_RESUMED] = "RESUMED",
66 		[SCAN_EVENT_TYPE_NLO_COMPLETE] = "NLO_COMPLETE",
67 		[SCAN_EVENT_TYPE_NLO_MATCH] = "NLO_MATCH",
68 		[SCAN_EVENT_TYPE_INVALID] = "INVALID",
69 		[SCAN_EVENT_TYPE_GPIO_TIMEOUT] = "GPIO_TIMEOUT",
70 		[SCAN_EVENT_TYPE_RADIO_MEASUREMENT_START] =
71 			"RADIO_MEASUREMENT_START",
72 		[SCAN_EVENT_TYPE_RADIO_MEASUREMENT_END] =
73 			"RADIO_MEASUREMENT_END",
74 		[SCAN_EVENT_TYPE_BSSID_MATCH] = "BSSID_MATCH",
75 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL_GET_NF] =
76 			"FOREIGN_CHANNEL_GET_NF",
77 	};
78 
79 	if (type >= SCAN_EVENT_TYPE_MAX)
80 		return "UNKNOWN";
81 
82 	return event_name[type];
83 }
84 
85 
86 const char*
87 util_scan_get_ev_reason_name(enum scan_completion_reason reason)
88 {
89 	static const char * const reason_name[] = {
90 		[SCAN_REASON_NONE] = "NONE",
91 		[SCAN_REASON_COMPLETED] = "COMPLETED",
92 		[SCAN_REASON_CANCELLED] = "CANCELLED",
93 		[SCAN_REASON_PREEMPTED] = "PREEMPTED",
94 		[SCAN_REASON_TIMEDOUT] = "TIMEDOUT",
95 		[SCAN_REASON_INTERNAL_FAILURE] = "INTERNAL_FAILURE",
96 		[SCAN_REASON_SUSPENDED] = "SUSPENDED",
97 		[SCAN_REASON_RUN_FAILED] = "RUN_FAILED",
98 		[SCAN_REASON_TERMINATION_FUNCTION] = "TERMINATION_FUNCTION",
99 		[SCAN_REASON_MAX_OFFCHAN_RETRIES] = "MAX_OFFCHAN_RETRIES",
100 		[SCAN_REASON_DFS_VIOLATION] = "DFS_NOL_VIOLATION",
101 	};
102 
103 	if (reason >= SCAN_REASON_MAX)
104 		return "UNKNOWN";
105 
106 	return reason_name[reason];
107 }
108 
109 qdf_time_t
110 util_get_last_scan_time(struct wlan_objmgr_vdev *vdev)
111 {
112 	uint8_t pdev_id;
113 	struct wlan_scan_obj *scan_obj;
114 
115 	if (!vdev) {
116 		scm_warn("null vdev");
117 		QDF_ASSERT(0);
118 		return 0;
119 	}
120 	pdev_id = wlan_scan_vdev_get_pdev_id(vdev);
121 	scan_obj = wlan_vdev_get_scan_obj(vdev);
122 
123 	if (scan_obj)
124 		return scan_obj->pdev_info[pdev_id].last_scan_time;
125 	else
126 		return 0;
127 }
128 
129 #ifdef WLAN_FEATURE_11BE_MLO
130 uint32_t util_scan_entry_t2lm_len(struct scan_cache_entry *scan_entry)
131 {
132 	int i = 0;
133 	uint32_t len = 0;
134 
135 	if (!scan_entry || !scan_entry->ie_list.t2lm[0])
136 		return 0;
137 
138 	for (i = 0; i < WLAN_MAX_T2LM_IE; i++) {
139 		if (scan_entry->ie_list.t2lm[i])
140 			len += scan_entry->ie_list.t2lm[i][TAG_LEN_POS] +
141 				sizeof(struct ie_header);
142 	}
143 
144 	return len;
145 }
146 #endif
147 
148 bool util_is_rsnxe_h2e_capable(const uint8_t *rsnxe)
149 {
150 	const uint8_t *rsnxe_caps;
151 	uint8_t cap_len;
152 
153 	if (!rsnxe)
154 		return false;
155 
156 	rsnxe_caps = wlan_crypto_parse_rsnxe_ie(rsnxe, &cap_len);
157 	if (!rsnxe_caps)
158 		return false;
159 
160 	return *rsnxe_caps & WLAN_CRYPTO_RSNX_CAP_SAE_H2E;
161 }
162 
163 bool util_scan_entry_sae_h2e_capable(struct scan_cache_entry *scan_entry)
164 {
165 	const uint8_t *rsnxe;
166 
167 	/* If RSN caps are not there, then return false */
168 	if (!util_scan_entry_rsn(scan_entry))
169 		return false;
170 
171 	/* If not SAE AKM no need to check H2E capability */
172 	if (!WLAN_CRYPTO_IS_AKM_SAE(scan_entry->neg_sec_info.key_mgmt))
173 		return false;
174 
175 	rsnxe = util_scan_entry_rsnxe(scan_entry);
176 	return util_is_rsnxe_h2e_capable(rsnxe);
177 }
178 
179 enum wlan_band util_scan_scm_freq_to_band(uint16_t freq)
180 {
181 	if (WLAN_REG_IS_24GHZ_CH_FREQ(freq))
182 		return WLAN_BAND_2_4_GHZ;
183 
184 	return WLAN_BAND_5_GHZ;
185 }
186 
187 bool util_is_scan_entry_match(
188 	struct scan_cache_entry *entry1,
189 	struct scan_cache_entry *entry2)
190 {
191 
192 	if (entry1->cap_info.wlan_caps.ess !=
193 	   entry2->cap_info.wlan_caps.ess)
194 		return false;
195 
196 	if (entry1->cap_info.wlan_caps.ess &&
197 	   !qdf_mem_cmp(entry1->bssid.bytes,
198 	   entry2->bssid.bytes, QDF_MAC_ADDR_SIZE)) {
199 		/* Check for BSS */
200 		if (util_is_ssid_match(&entry1->ssid, &entry2->ssid) ||
201 		    util_scan_is_null_ssid(&entry1->ssid) ||
202 		    util_scan_is_null_ssid(&entry2->ssid))
203 			return true;
204 	} else if (entry1->cap_info.wlan_caps.ibss &&
205 	   (entry1->channel.chan_freq ==
206 	   entry2->channel.chan_freq)) {
207 		/*
208 		 * Same channel cannot have same SSID for
209 		 * different IBSS, so no need to check BSSID
210 		 */
211 		if (util_is_ssid_match(
212 		   &entry1->ssid, &entry2->ssid))
213 			return true;
214 	} else if (!entry1->cap_info.wlan_caps.ibss &&
215 	   !entry1->cap_info.wlan_caps.ess &&
216 	   !qdf_mem_cmp(entry1->bssid.bytes,
217 	   entry2->bssid.bytes, QDF_MAC_ADDR_SIZE)) {
218 		/* In case of P2P devices, ess and ibss will be set to zero */
219 		return true;
220 	}
221 
222 	return false;
223 }
224 
225 static bool util_is_pureg_rate(uint8_t *rates, uint8_t nrates)
226 {
227 	static const uint8_t g_rates[] = {12, 18, 24, 36, 48, 72, 96, 108};
228 	bool pureg = false;
229 	uint8_t i, j;
230 
231 	for (i = 0; i < nrates; i++) {
232 		for (j = 0; j < QDF_ARRAY_SIZE(g_rates); j++) {
233 			if (WLAN_RV(rates[i]) == g_rates[j]) {
234 				pureg = true;
235 				break;
236 			}
237 		}
238 		if (pureg)
239 			break;
240 	}
241 
242 	return pureg;
243 }
244 
245 #ifdef WLAN_FEATURE_11BE
246 static enum wlan_phymode
247 util_scan_get_phymode_11be(struct wlan_objmgr_pdev *pdev,
248 			   struct scan_cache_entry *scan_params,
249 			   enum wlan_phymode phymode,
250 			   uint8_t band_mask)
251 {
252 	struct wlan_ie_ehtops *eht_ops;
253 	uint8_t width;
254 
255 	eht_ops = (struct wlan_ie_ehtops *)util_scan_entry_ehtop(scan_params);
256 	if (!util_scan_entry_ehtcap(scan_params) || !eht_ops)
257 		return phymode;
258 
259 	if (QDF_GET_BITS(eht_ops->ehtop_param,
260 			 EHTOP_INFO_PRESENT_IDX, EHTOP_INFO_PRESENT_BITS)) {
261 		width = QDF_GET_BITS(eht_ops->control,
262 				     EHTOP_INFO_CHAN_WIDTH_IDX,
263 				     EHTOP_INFO_CHAN_WIDTH_BITS);
264 		switch (width) {
265 		case WLAN_EHT_CHWIDTH_20:
266 			phymode = WLAN_PHYMODE_11BEA_EHT20;
267 			break;
268 		case WLAN_EHT_CHWIDTH_40:
269 			phymode = WLAN_PHYMODE_11BEA_EHT40;
270 			break;
271 		case WLAN_EHT_CHWIDTH_80:
272 			phymode = WLAN_PHYMODE_11BEA_EHT80;
273 			break;
274 		case WLAN_EHT_CHWIDTH_160:
275 			phymode = WLAN_PHYMODE_11BEA_EHT160;
276 			break;
277 		case WLAN_EHT_CHWIDTH_320:
278 			phymode = WLAN_PHYMODE_11BEA_EHT320;
279 			break;
280 		default:
281 			scm_debug("Invalid eht_ops width: %d", width);
282 			phymode = WLAN_PHYMODE_11BEA_EHT20;
283 			break;
284 		}
285 	} else {
286 		switch (phymode) {
287 		case WLAN_PHYMODE_11AXA_HE20:
288 			phymode = WLAN_PHYMODE_11BEA_EHT20;
289 			break;
290 		case WLAN_PHYMODE_11AXG_HE20:
291 			phymode = WLAN_PHYMODE_11BEG_EHT20;
292 			break;
293 		case WLAN_PHYMODE_11AXA_HE40:
294 			phymode = WLAN_PHYMODE_11BEA_EHT40;
295 			break;
296 		case WLAN_PHYMODE_11AXG_HE40:
297 			phymode = WLAN_PHYMODE_11BEG_EHT40;
298 			break;
299 		case WLAN_PHYMODE_11AXA_HE80:
300 			phymode = WLAN_PHYMODE_11BEA_EHT80;
301 			break;
302 		case WLAN_PHYMODE_11AXA_HE160:
303 			phymode = WLAN_PHYMODE_11BEA_EHT160;
304 			break;
305 		default:
306 			break;
307 		}
308 	}
309 
310 	if (QDF_GET_BITS(eht_ops->ehtop_param,
311 			 EHTOP_INFO_PRESENT_IDX, EHTOP_INFO_PRESENT_BITS)) {
312 		scan_params->channel.cfreq0 =
313 			wlan_reg_chan_band_to_freq(pdev,
314 						   eht_ops->ccfs0,
315 						   band_mask);
316 		scan_params->channel.cfreq1 =
317 			wlan_reg_chan_band_to_freq(pdev,
318 						   eht_ops->ccfs1,
319 						   band_mask);
320 	}
321 
322 	if (QDF_GET_BITS(eht_ops->ehtop_param,
323 			 EHTOP_PARAM_DISABLED_SC_BITMAP_PRESENT_IDX,
324 			 EHTOP_PARAM_DISABLED_SC_BITMAP_PRESENT_BITS)) {
325 		scan_params->channel.puncture_bitmap =
326 			QDF_GET_BITS(eht_ops->disabled_sub_chan_bitmap[0],
327 				     0, 8);
328 		scan_params->channel.puncture_bitmap |=
329 			QDF_GET_BITS(eht_ops->disabled_sub_chan_bitmap[1],
330 				     0, 8) << 8;
331 	} else {
332 		scan_params->channel.puncture_bitmap = 0;
333 	}
334 
335 	return phymode;
336 }
337 #else
338 static enum wlan_phymode
339 util_scan_get_phymode_11be(struct wlan_objmgr_pdev *pdev,
340 			   struct scan_cache_entry *scan_params,
341 			   enum wlan_phymode phymode,
342 			   uint8_t band_mask)
343 {
344 	return phymode;
345 }
346 #endif
347 
348 #ifdef CONFIG_BAND_6GHZ
349 static struct he_oper_6g_param *util_scan_get_he_6g_params(uint8_t *he_ops)
350 {
351 	uint8_t len;
352 	uint32_t he_oper_params;
353 
354 	if (!he_ops)
355 		return NULL;
356 
357 	len = he_ops[1];
358 	he_ops += sizeof(struct ie_header);
359 
360 	if (len < WLAN_HEOP_FIXED_PARAM_LENGTH)
361 		return NULL;
362 
363 	/* element id extension */
364 	he_ops++;
365 	len--;
366 
367 	he_oper_params = LE_READ_4(he_ops);
368 	if (!(he_oper_params & WLAN_HEOP_6GHZ_INFO_PRESENT_MASK))
369 		return NULL;
370 
371 	/* fixed params - element id extension */
372 	he_ops += WLAN_HEOP_FIXED_PARAM_LENGTH - 1;
373 	len -= WLAN_HEOP_FIXED_PARAM_LENGTH - 1;
374 
375 	if (!len)
376 		return NULL;
377 
378 	/* vht oper params */
379 	if (he_oper_params & WLAN_HEOP_VHTOP_PRESENT_MASK) {
380 		if (len < WLAN_HEOP_VHTOP_LENGTH)
381 			return NULL;
382 		he_ops += WLAN_HEOP_VHTOP_LENGTH;
383 		len -= WLAN_HEOP_VHTOP_LENGTH;
384 	}
385 
386 	if (!len)
387 		return NULL;
388 
389 	if (he_oper_params & WLAN_HEOP_CO_LOCATED_BSS_MASK) {
390 		he_ops += WLAN_HEOP_CO_LOCATED_BSS_LENGTH;
391 		len -= WLAN_HEOP_CO_LOCATED_BSS_LENGTH;
392 	}
393 
394 	if (len < sizeof(struct he_oper_6g_param))
395 		return NULL;
396 
397 	return (struct he_oper_6g_param *)he_ops;
398 }
399 
400 #ifdef WLAN_FEATURE_11BE
401 /*
402  * util_scan_is_out_of_band_leak_eht() - Check if eht beacon out of BSS BW
403  * @pdev: pointer to pdev.
404  * @scan_params: scan entry generated by beacon/probe rsp
405  * @band_mask: band mask of frequency beacon/probe rsp received
406  * @current_freq: frequency beacon/probe rsp received
407  *
408  * 1. If BSS BW <= 80MHz
409  * If Absolute value of (Current Channel Channel Center Frequency Segment 0) <=
410  * BSS BW/2 then eht beacon in BSS operating BW
411  * else eht beacon out of BSS operating BW
412  *
413  * 2. If BSS BW > 80MHz
414  * If Absolute value of (Current Channel Channel Center Frequency Segment 1) <=
415  * BSS BW/2 then eht beacon in BSS operating BW
416  * else eht beacon out of BSS operating BW
417  *
418  * Return: bool, whether eht beacon out of BSS operating BW
419  */
420 static bool
421 util_scan_is_out_of_band_leak_eht(struct wlan_objmgr_pdev *pdev,
422 				  struct scan_cache_entry *scan_params,
423 				  uint8_t band_mask,
424 				  qdf_freq_t current_freq)
425 {
426 	struct wlan_ie_ehtops *eht_ops;
427 	uint8_t ch_width;
428 	uint32_t bw;
429 	uint32_t freq_diff;
430 	qdf_freq_t freq_seg0;
431 	qdf_freq_t freq_seg1;
432 
433 	eht_ops = (struct wlan_ie_ehtops *)util_scan_entry_ehtop(scan_params);
434 	if (!util_scan_entry_ehtcap(scan_params) || !eht_ops)
435 		return false;
436 
437 	if (!QDF_GET_BITS(eht_ops->ehtop_param,
438 			  EHTOP_INFO_PRESENT_IDX, EHTOP_INFO_PRESENT_BITS))
439 		return false;
440 
441 	ch_width = QDF_GET_BITS(eht_ops->control,
442 				EHTOP_INFO_CHAN_WIDTH_IDX,
443 				EHTOP_INFO_CHAN_WIDTH_BITS);
444 	freq_seg0 = wlan_reg_chan_band_to_freq(pdev, eht_ops->ccfs0,
445 					       band_mask);
446 	freq_seg1 = wlan_reg_chan_band_to_freq(pdev, eht_ops->ccfs1,
447 					       band_mask);
448 	if (ch_width == WLAN_EHT_CHWIDTH_320)
449 		bw = BW_320_MHZ;
450 	else if (ch_width == WLAN_EHT_CHWIDTH_160)
451 		bw = BW_160_MHZ;
452 	else if (ch_width == WLAN_EHT_CHWIDTH_80)
453 		bw = BW_80_MHZ;
454 	else  if (ch_width == WLAN_EHT_CHWIDTH_40)
455 		bw = BW_40_MHZ;
456 	else  if (ch_width == WLAN_EHT_CHWIDTH_20)
457 		bw = BW_20_MHZ;
458 	else
459 		bw = BW_20_MHZ;
460 
461 	if (bw <= BW_80_MHZ)
462 		freq_diff = abs(freq_seg0 - current_freq);
463 	else
464 		freq_diff = abs(freq_seg1 - current_freq);
465 	if (freq_diff <= bw / 2)
466 		return false;
467 
468 	scm_debug("Leaked freq:%u ch width:%u freq0:%u freq1:%u",
469 		  current_freq, bw, freq_seg0, freq_seg1);
470 	return true;
471 }
472 #else
473 static bool
474 util_scan_is_out_of_band_leak_eht(struct wlan_objmgr_pdev *pdev,
475 				  struct scan_cache_entry *scan_params,
476 				  uint8_t band_mask,
477 				  qdf_freq_t current_freq)
478 {
479 	return false;
480 }
481 #endif
482 
483 /*
484  * util_scan_is_out_of_band_leak_he() - Check if HE beacon out of BSS BW
485  * @pdev: pointer to pdev.
486  * @he_6g_params: HE 6 GHz params
487  * @band_mask: band mask of frequency beacon/probe rsp received
488  * @current_freq: frequency beacon/probe rsp received
489  *
490  * 1. If BSS BW <= 80MHz
491  * If Absolute value of (Current Channel Channel Center Frequency Segment 0) <=
492  * BSS BW/2 then HE beacon in BSS operating BW
493  *
494  * 2. If BSS BW is 160MHz
495  * If Absolute value of (Current Channel Channel Center Frequency Segment 1) <=
496  * BSS BW/2 then HE beacon in BSS operating BW
497  *
498  * 3. If BSS BW is 80+80MHz
499  * If absolute value of (Current Channel - Channel Center Frequency Segment 0)
500  * <= 40 or absolute value of (Current Channel - Channel Center Frequency
501  *  Segment 1) <= 40, then HE beacon in BSS operating BW
502  *
503  * Return: bool, whether HE beacon out of BSS operating BW
504  */
505 static bool
506 util_scan_is_out_of_band_leak_he(struct wlan_objmgr_pdev *pdev,
507 				 struct he_oper_6g_param *he_6g_params,
508 				 uint8_t band_mask,
509 				 qdf_freq_t current_freq)
510 {
511 	uint8_t ch_width;
512 	uint32_t bw;
513 	uint32_t freq_diff;
514 	qdf_freq_t freq_seg0;
515 	qdf_freq_t freq_seg1;
516 
517 	ch_width = he_6g_params->width;
518 	freq_seg0 = wlan_reg_chan_band_to_freq(pdev,
519 					       he_6g_params->chan_freq_seg0,
520 					       band_mask);
521 	freq_seg1 = wlan_reg_chan_band_to_freq(pdev,
522 					       he_6g_params->chan_freq_seg1,
523 					       band_mask);
524 	if (ch_width == WLAN_HE_6GHZ_CHWIDTH_160_80_80)
525 		bw = BW_160_MHZ;
526 	else if (ch_width == WLAN_HE_6GHZ_CHWIDTH_80)
527 		bw = BW_80_MHZ;
528 	else  if (ch_width == WLAN_HE_6GHZ_CHWIDTH_40)
529 		bw = BW_40_MHZ;
530 	else  if (ch_width == WLAN_HE_6GHZ_CHWIDTH_20)
531 		bw = BW_20_MHZ;
532 	else
533 		bw = BW_20_MHZ;
534 
535 	if (bw <= BW_80_MHZ) {
536 		freq_diff = abs(freq_seg0 - current_freq);
537 		if (freq_diff <= bw / 2)
538 			return false;
539 	} else if (WLAN_IS_HE160(he_6g_params)) {
540 		freq_diff = abs(freq_seg1 - current_freq);
541 		if (freq_diff <= bw / 2)
542 			return false;
543 	} else if (WLAN_IS_HE80_80(he_6g_params)) {
544 		freq_diff = abs(freq_seg0 - current_freq);
545 		if (freq_diff <= BW_40_MHZ)
546 			return false;
547 		freq_diff = abs(freq_seg1 - current_freq);
548 		if (freq_diff <= BW_40_MHZ)
549 			return false;
550 	}
551 
552 	scm_debug("Leaked freq:%u ch width:%u freq0:%u freq1:%u",
553 		  current_freq, bw, freq_seg0, freq_seg1);
554 
555 	return true;
556 }
557 
558 /*
559  * util_scan_get_chan_from_he_6g_params() - Get chan info from 6 GHz param
560  * @pdev: pointer to pdev.
561  * @scan_params: scan entry generated by beacon/probe rsp
562  * @chan_freq: output parameter, primary freq from 6 GHz he params
563  * @is_6g_dup_bcon: output parameter, bool, if false, invalid 6g duplicated
564 	beacon out of BSS operating BW or not duplicated beacon, can drop if
565 	channel mismatch
566  * @band_mask: band mask of frequency beacon/probe rsp received
567  * @current_freq: frequency beacon/probe rsp received
568  *
569  * Return: QDF_STATUS
570  */
571 static QDF_STATUS
572 util_scan_get_chan_from_he_6g_params(struct wlan_objmgr_pdev *pdev,
573 				     struct scan_cache_entry *scan_params,
574 				     qdf_freq_t *chan_freq,
575 				     bool *is_6g_dup_bcon, uint8_t band_mask,
576 				     qdf_freq_t current_freq)
577 {
578 	struct he_oper_6g_param *he_6g_params;
579 	uint8_t *he_ops;
580 	struct wlan_scan_obj *scan_obj;
581 	struct wlan_objmgr_psoc *psoc;
582 	bool is_out_of_band_leak = true;
583 
584 	psoc = wlan_pdev_get_psoc(pdev);
585 	if (!psoc) {
586 		scm_err("psoc is NULL");
587 		return QDF_STATUS_E_INVAL;
588 	}
589 
590 	scan_obj = wlan_psoc_get_scan_obj(psoc);
591 	if (!scan_obj) {
592 		scm_err("scan_obj is NULL");
593 		return QDF_STATUS_E_INVAL;
594 	}
595 
596 	*is_6g_dup_bcon = false;
597 
598 	he_ops = util_scan_entry_heop(scan_params);
599 	if (!util_scan_entry_hecap(scan_params) || !he_ops)
600 		return QDF_STATUS_SUCCESS;
601 
602 	he_6g_params = util_scan_get_he_6g_params(he_ops);
603 	if (!he_6g_params)
604 		return QDF_STATUS_SUCCESS;
605 
606 	*chan_freq = wlan_reg_chan_band_to_freq(pdev,
607 						he_6g_params->primary_channel,
608 						band_mask);
609 	if (scan_obj->drop_bcn_on_invalid_freq &&
610 	    !wlan_reg_is_freq_enabled(pdev, *chan_freq, REG_BEST_PWR_MODE)) {
611 		scm_debug_rl(QDF_MAC_ADDR_FMT": Drop as invalid channel %d freq %d in HE 6Ghz params",
612 			     QDF_MAC_ADDR_REF(scan_params->bssid.bytes),
613 			     he_6g_params->primary_channel, *chan_freq);
614 		return QDF_STATUS_E_INVAL;
615 	}
616 
617 	if (!he_6g_params->duplicate_beacon) {
618 		*is_6g_dup_bcon = false;
619 		return QDF_STATUS_SUCCESS;
620 	}
621 	is_out_of_band_leak =
622 		util_scan_is_out_of_band_leak_eht(pdev, scan_params, band_mask,
623 						  current_freq);
624 	if (is_out_of_band_leak) {
625 		*is_6g_dup_bcon = false;
626 		return QDF_STATUS_SUCCESS;
627 	}
628 	is_out_of_band_leak =
629 		util_scan_is_out_of_band_leak_he(pdev, he_6g_params, band_mask,
630 						 current_freq);
631 	if (is_out_of_band_leak) {
632 		*is_6g_dup_bcon = false;
633 		return QDF_STATUS_SUCCESS;
634 	}
635 
636 	*is_6g_dup_bcon = true;
637 
638 	return QDF_STATUS_SUCCESS;
639 }
640 
641 static enum wlan_phymode
642 util_scan_get_phymode_6g(struct wlan_objmgr_pdev *pdev,
643 			 struct scan_cache_entry *scan_params)
644 {
645 	struct he_oper_6g_param *he_6g_params;
646 	enum wlan_phymode phymode = WLAN_PHYMODE_11AXA_HE20;
647 	uint8_t *he_ops;
648 	uint8_t band_mask = BIT(REG_BAND_6G);
649 
650 	he_ops = util_scan_entry_heop(scan_params);
651 	if (!util_scan_entry_hecap(scan_params) || !he_ops)
652 		return phymode;
653 
654 	he_6g_params = util_scan_get_he_6g_params(he_ops);
655 	if (!he_6g_params)
656 		return phymode;
657 
658 	switch (he_6g_params->width) {
659 	case WLAN_HE_6GHZ_CHWIDTH_20:
660 		phymode = WLAN_PHYMODE_11AXA_HE20;
661 		break;
662 	case WLAN_HE_6GHZ_CHWIDTH_40:
663 		phymode = WLAN_PHYMODE_11AXA_HE40;
664 		break;
665 	case WLAN_HE_6GHZ_CHWIDTH_80:
666 		phymode = WLAN_PHYMODE_11AXA_HE80;
667 		break;
668 	case WLAN_HE_6GHZ_CHWIDTH_160_80_80:
669 		if (WLAN_IS_HE80_80(he_6g_params))
670 			phymode = WLAN_PHYMODE_11AXA_HE80_80;
671 		else if (WLAN_IS_HE160(he_6g_params))
672 			phymode = WLAN_PHYMODE_11AXA_HE160;
673 		else
674 			phymode = WLAN_PHYMODE_11AXA_HE80;
675 		break;
676 	default:
677 		scm_err("Invalid he_6g_params width: %d", he_6g_params->width);
678 		phymode = WLAN_PHYMODE_11AXA_HE20;
679 		break;
680 	}
681 
682 	if (he_6g_params->chan_freq_seg0)
683 		scan_params->channel.cfreq0 =
684 			wlan_reg_chan_band_to_freq(pdev,
685 					he_6g_params->chan_freq_seg0,
686 					band_mask);
687 	if (he_6g_params->chan_freq_seg1)
688 		scan_params->channel.cfreq1 =
689 			wlan_reg_chan_band_to_freq(pdev,
690 					he_6g_params->chan_freq_seg1,
691 					band_mask);
692 
693 	phymode = util_scan_get_phymode_11be(pdev, scan_params,
694 					     phymode, band_mask);
695 
696 	return phymode;
697 }
698 
699 uint8_t
700 util_scan_get_6g_oper_channel(uint8_t *he_op_ie)
701 {
702 	struct he_oper_6g_param *he_6g_params;
703 
704 	he_6g_params = util_scan_get_he_6g_params(he_op_ie);
705 	if (!he_6g_params)
706 		return 0;
707 
708 	return he_6g_params->primary_channel;
709 }
710 
711 #else
712 static QDF_STATUS
713 util_scan_get_chan_from_he_6g_params(struct wlan_objmgr_pdev *pdev,
714 				     struct scan_cache_entry *scan_params,
715 				     qdf_freq_t *chan_freq,
716 				     bool *is_6g_dup_bcon,
717 				     uint8_t band_mask,
718 				     qdf_freq_t current_freq)
719 {
720 	return QDF_STATUS_SUCCESS;
721 }
722 static inline enum wlan_phymode
723 util_scan_get_phymode_6g(struct wlan_objmgr_pdev *pdev,
724 			 struct scan_cache_entry *scan_params)
725 {
726 	return WLAN_PHYMODE_AUTO;
727 }
728 #endif
729 
730 static inline
731 uint32_t util_scan_sec_chan_freq_from_htinfo(struct wlan_ie_htinfo_cmn *htinfo,
732 					     uint32_t primary_chan_freq)
733 {
734 	if (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_ABOVE)
735 		return primary_chan_freq + WLAN_CHAN_SPACING_20MHZ;
736 	else if (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_BELOW)
737 		return primary_chan_freq - WLAN_CHAN_SPACING_20MHZ;
738 
739 	return 0;
740 }
741 
742 static enum wlan_phymode
743 util_scan_get_phymode_5g(struct wlan_objmgr_pdev *pdev,
744 			 struct scan_cache_entry *scan_params)
745 {
746 	enum wlan_phymode phymode = WLAN_PHYMODE_AUTO;
747 	uint16_t ht_cap = 0;
748 	struct htcap_cmn_ie *htcap;
749 	struct wlan_ie_htinfo_cmn *htinfo;
750 	struct wlan_ie_vhtop *vhtop;
751 	uint8_t band_mask = BIT(REG_BAND_5G);
752 
753 	htcap = (struct htcap_cmn_ie *)
754 		util_scan_entry_htcap(scan_params);
755 	htinfo = (struct wlan_ie_htinfo_cmn *)
756 		util_scan_entry_htinfo(scan_params);
757 	vhtop = (struct wlan_ie_vhtop *)
758 		util_scan_entry_vhtop(scan_params);
759 
760 	if (!(htcap && htinfo))
761 		return WLAN_PHYMODE_11A;
762 
763 	if (htcap)
764 		ht_cap = le16toh(htcap->hc_cap);
765 
766 	if ((ht_cap & WLAN_HTCAP_C_CHWIDTH40) &&
767 	    (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_ABOVE ||
768 	     htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_BELOW))
769 		phymode = WLAN_PHYMODE_11NA_HT40;
770 	else
771 		phymode = WLAN_PHYMODE_11NA_HT20;
772 
773 	scan_params->channel.cfreq0 =
774 		util_scan_sec_chan_freq_from_htinfo(htinfo,
775 						scan_params->channel.chan_freq);
776 
777 	if (util_scan_entry_vhtcap(scan_params) && vhtop) {
778 		switch (vhtop->vht_op_chwidth) {
779 		case WLAN_VHTOP_CHWIDTH_2040:
780 			if (phymode == WLAN_PHYMODE_11NA_HT40)
781 				phymode = WLAN_PHYMODE_11AC_VHT40;
782 			else
783 				phymode = WLAN_PHYMODE_11AC_VHT20;
784 			break;
785 		case WLAN_VHTOP_CHWIDTH_80:
786 			if (WLAN_IS_REVSIG_VHT80_80(vhtop))
787 				phymode = WLAN_PHYMODE_11AC_VHT80_80;
788 			else if (WLAN_IS_REVSIG_VHT160(vhtop))
789 				phymode = WLAN_PHYMODE_11AC_VHT160;
790 			else
791 				phymode = WLAN_PHYMODE_11AC_VHT80;
792 			break;
793 		case WLAN_VHTOP_CHWIDTH_160:
794 			phymode = WLAN_PHYMODE_11AC_VHT160;
795 			break;
796 		case WLAN_VHTOP_CHWIDTH_80_80:
797 			phymode = WLAN_PHYMODE_11AC_VHT80_80;
798 			break;
799 		default:
800 			scm_debug("bad channel: %d",
801 				  vhtop->vht_op_chwidth);
802 			phymode = WLAN_PHYMODE_11AC_VHT20;
803 			break;
804 		}
805 		if (vhtop->vht_op_ch_freq_seg1)
806 			scan_params->channel.cfreq0 =
807 				wlan_reg_chan_band_to_freq(pdev,
808 						vhtop->vht_op_ch_freq_seg1,
809 						band_mask);
810 		if (vhtop->vht_op_ch_freq_seg2)
811 			scan_params->channel.cfreq1 =
812 				wlan_reg_chan_band_to_freq(pdev,
813 						vhtop->vht_op_ch_freq_seg2,
814 						band_mask);
815 	}
816 
817 	if (!util_scan_entry_hecap(scan_params))
818 		return phymode;
819 
820 	/* for 5Ghz Check for HE, only if VHT cap and HE cap are present */
821 	if (!IS_WLAN_PHYMODE_VHT(phymode))
822 		return phymode;
823 
824 	switch (phymode) {
825 	case WLAN_PHYMODE_11AC_VHT20:
826 		phymode = WLAN_PHYMODE_11AXA_HE20;
827 		break;
828 	case WLAN_PHYMODE_11AC_VHT40:
829 		phymode = WLAN_PHYMODE_11AXA_HE40;
830 		break;
831 	case WLAN_PHYMODE_11AC_VHT80:
832 		phymode = WLAN_PHYMODE_11AXA_HE80;
833 		break;
834 	case WLAN_PHYMODE_11AC_VHT160:
835 		phymode = WLAN_PHYMODE_11AXA_HE160;
836 		break;
837 	case WLAN_PHYMODE_11AC_VHT80_80:
838 		phymode = WLAN_PHYMODE_11AXA_HE80_80;
839 		break;
840 	default:
841 		phymode = WLAN_PHYMODE_11AXA_HE20;
842 		break;
843 	}
844 
845 	phymode = util_scan_get_phymode_11be(pdev, scan_params,
846 					     phymode, band_mask);
847 
848 	return phymode;
849 }
850 
851 #ifdef WLAN_FEATURE_11BE
852 static enum wlan_phymode
853 util_scan_get_phymode_2g_11be(struct scan_cache_entry *scan_params,
854 			      enum wlan_phymode  phymode)
855 {
856 	if (!util_scan_entry_ehtcap(scan_params))
857 		return phymode;
858 
859 	if (phymode == WLAN_PHYMODE_11AXG_HE40PLUS)
860 		phymode = WLAN_PHYMODE_11BEG_EHT40PLUS;
861 	else if (phymode == WLAN_PHYMODE_11AXG_HE40MINUS)
862 		phymode = WLAN_PHYMODE_11BEG_EHT40MINUS;
863 	else
864 		phymode = WLAN_PHYMODE_11BEG_EHT20;
865 
866 	return phymode;
867 }
868 #else
869 static enum wlan_phymode
870 util_scan_get_phymode_2g_11be(struct scan_cache_entry *scan_params,
871 			      enum wlan_phymode  phymode)
872 {
873 	return phymode;
874 }
875 #endif
876 
877 static enum wlan_phymode
878 util_scan_get_phymode_2g(struct scan_cache_entry *scan_params)
879 {
880 	enum wlan_phymode phymode = WLAN_PHYMODE_AUTO;
881 	uint16_t ht_cap = 0;
882 	struct htcap_cmn_ie *htcap;
883 	struct wlan_ie_htinfo_cmn *htinfo;
884 	struct wlan_ie_vhtop *vhtop;
885 
886 	htcap = (struct htcap_cmn_ie *)
887 		util_scan_entry_htcap(scan_params);
888 	htinfo = (struct wlan_ie_htinfo_cmn *)
889 		util_scan_entry_htinfo(scan_params);
890 	vhtop = (struct wlan_ie_vhtop *)
891 		util_scan_entry_vhtop(scan_params);
892 
893 	if (htcap)
894 		ht_cap = le16toh(htcap->hc_cap);
895 
896 	if (htcap && htinfo) {
897 		if ((ht_cap & WLAN_HTCAP_C_CHWIDTH40) &&
898 		   (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_ABOVE))
899 			phymode = WLAN_PHYMODE_11NG_HT40PLUS;
900 		else if ((ht_cap & WLAN_HTCAP_C_CHWIDTH40) &&
901 		   (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_BELOW))
902 			phymode = WLAN_PHYMODE_11NG_HT40MINUS;
903 		else
904 			phymode = WLAN_PHYMODE_11NG_HT20;
905 	} else if (util_scan_entry_xrates(scan_params)) {
906 		/* only 11G stations will have more than 8 rates */
907 		phymode = WLAN_PHYMODE_11G;
908 	} else {
909 		/* Some mischievous g-only APs do not set extended rates */
910 		if (util_scan_entry_rates(scan_params)) {
911 			if (util_is_pureg_rate(&scan_params->ie_list.rates[2],
912 			   scan_params->ie_list.rates[1]))
913 				phymode = WLAN_PHYMODE_11G;
914 			else
915 				phymode = WLAN_PHYMODE_11B;
916 		} else {
917 			phymode = WLAN_PHYMODE_11B;
918 		}
919 	}
920 
921 	/* Check for VHT only if HT cap is present */
922 	if (!IS_WLAN_PHYMODE_HT(phymode))
923 		return phymode;
924 
925 	scan_params->channel.cfreq0 =
926 		util_scan_sec_chan_freq_from_htinfo(htinfo,
927 						scan_params->channel.chan_freq);
928 
929 	if (util_scan_entry_vhtcap(scan_params) && vhtop) {
930 		switch (vhtop->vht_op_chwidth) {
931 		case WLAN_VHTOP_CHWIDTH_2040:
932 			if (phymode == WLAN_PHYMODE_11NG_HT40PLUS)
933 				phymode = WLAN_PHYMODE_11AC_VHT40PLUS_2G;
934 			else if (phymode == WLAN_PHYMODE_11NG_HT40MINUS)
935 				phymode = WLAN_PHYMODE_11AC_VHT40MINUS_2G;
936 			else
937 				phymode = WLAN_PHYMODE_11AC_VHT20_2G;
938 
939 			break;
940 		default:
941 			scm_info("bad vht_op_chwidth: %d",
942 				 vhtop->vht_op_chwidth);
943 			phymode = WLAN_PHYMODE_11AC_VHT20_2G;
944 			break;
945 		}
946 	}
947 
948 	if (!util_scan_entry_hecap(scan_params))
949 		return phymode;
950 
951 	if (phymode == WLAN_PHYMODE_11AC_VHT40PLUS_2G ||
952 	    phymode == WLAN_PHYMODE_11NG_HT40PLUS)
953 		phymode = WLAN_PHYMODE_11AXG_HE40PLUS;
954 	else if (phymode == WLAN_PHYMODE_11AC_VHT40MINUS_2G ||
955 		 phymode == WLAN_PHYMODE_11NG_HT40MINUS)
956 		phymode = WLAN_PHYMODE_11AXG_HE40MINUS;
957 	else
958 		phymode = WLAN_PHYMODE_11AXG_HE20;
959 
960 	phymode = util_scan_get_phymode_2g_11be(scan_params, phymode);
961 
962 	return phymode;
963 }
964 
965 static enum wlan_phymode
966 util_scan_get_phymode(struct wlan_objmgr_pdev *pdev,
967 		      struct scan_cache_entry *scan_params)
968 {
969 	if (WLAN_REG_IS_24GHZ_CH_FREQ(scan_params->channel.chan_freq))
970 		return util_scan_get_phymode_2g(scan_params);
971 	else if (WLAN_REG_IS_6GHZ_CHAN_FREQ(scan_params->channel.chan_freq))
972 		return util_scan_get_phymode_6g(pdev, scan_params);
973 	else
974 		return util_scan_get_phymode_5g(pdev, scan_params);
975 }
976 
977 static QDF_STATUS
978 util_scan_parse_chan_switch_wrapper_ie(struct scan_cache_entry *scan_params,
979 	struct ie_header *sub_ie, qdf_size_t sub_ie_len)
980 {
981 	/* Walk through to check nothing is malformed */
982 	while (sub_ie_len >= sizeof(struct ie_header)) {
983 		/* At least one more header is present */
984 		sub_ie_len -= sizeof(struct ie_header);
985 
986 		if (sub_ie->ie_len == 0) {
987 			sub_ie += 1;
988 			continue;
989 		}
990 		if (sub_ie_len < sub_ie->ie_len) {
991 			scm_debug_rl(QDF_MAC_ADDR_FMT": Incomplete corrupted IE:%x",
992 				     QDF_MAC_ADDR_REF(scan_params->bssid.bytes),
993 				     WLAN_ELEMID_CHAN_SWITCH_WRAP);
994 			return QDF_STATUS_E_INVAL;
995 		}
996 		switch (sub_ie->ie_id) {
997 		case WLAN_ELEMID_COUNTRY:
998 			if (sub_ie->ie_len < WLAN_COUNTRY_IE_MIN_LEN)
999 				return QDF_STATUS_E_INVAL;
1000 			scan_params->ie_list.country = (uint8_t *)sub_ie;
1001 			break;
1002 		case WLAN_ELEMID_WIDE_BAND_CHAN_SWITCH:
1003 			if (sub_ie->ie_len < WLAN_WIDE_BW_CHAN_SWITCH_IE_LEN)
1004 				return QDF_STATUS_E_INVAL;
1005 			scan_params->ie_list.widebw = (uint8_t *)sub_ie;
1006 			break;
1007 		case WLAN_ELEMID_VHT_TX_PWR_ENVLP:
1008 			if (sub_ie->ie_len > WLAN_TPE_IE_MAX_LEN)
1009 				return QDF_STATUS_E_INVAL;
1010 			scan_params->ie_list.txpwrenvlp = (uint8_t *)sub_ie;
1011 			break;
1012 #ifdef WLAN_FEATURE_11BE
1013 		case WLAN_EXTN_ELEMID_BW_IND:
1014 			if (sub_ie->ie_len > WLAN_BW_IND_IE_MAX_LEN)
1015 				return QDF_STATUS_E_INVAL;
1016 			scan_params->ie_list.bw_ind = (uint8_t *)sub_ie;
1017 			break;
1018 #endif
1019 		}
1020 		/* Consume sub info element */
1021 		sub_ie_len -= sub_ie->ie_len;
1022 		/* go to next Sub IE */
1023 		sub_ie = (struct ie_header *)
1024 			(((uint8_t *) sub_ie) +
1025 			sizeof(struct ie_header) + sub_ie->ie_len);
1026 	}
1027 
1028 	return QDF_STATUS_SUCCESS;
1029 }
1030 
1031 bool
1032 util_scan_is_hidden_ssid(struct ie_ssid *ssid)
1033 {
1034 	uint8_t i;
1035 
1036 	/*
1037 	 * We flag this as Hidden SSID if the Length is 0
1038 	 * of the SSID only contains 0's
1039 	 */
1040 	if (!ssid || !ssid->ssid_len)
1041 		return true;
1042 
1043 	for (i = 0; i < ssid->ssid_len; i++)
1044 		if (ssid->ssid[i] != 0)
1045 			return false;
1046 
1047 	/* All 0's */
1048 	return true;
1049 }
1050 
1051 #ifdef WLAN_FEATURE_11BE_MLO
1052 static void util_scan_update_rnr_mld(struct rnr_bss_info *rnr, uint8_t *data,
1053 				     uint8_t tbtt_info_length)
1054 {
1055 	bool mld_info_present = false;
1056 
1057 	switch (tbtt_info_length) {
1058 	case TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM_20MHZ_PSD_MLD_PARAM:
1059 		qdf_mem_copy(&rnr->mld_info, &data[13],
1060 			     sizeof(struct rnr_mld_info));
1061 		mld_info_present = true;
1062 		break;
1063 	};
1064 
1065 	rnr->mld_info_valid = mld_info_present;
1066 }
1067 #else
1068 static inline void
1069 util_scan_update_rnr_mld(struct rnr_bss_info *rnr, uint8_t *data,
1070 			 uint8_t tbtt_info_length)
1071 {
1072 }
1073 #endif
1074 
1075 static QDF_STATUS
1076 util_scan_update_rnr(struct rnr_bss_info *rnr,
1077 		     struct neighbor_ap_info_field *ap_info,
1078 		     uint8_t *data)
1079 {
1080 	uint8_t tbtt_info_length;
1081 
1082 	tbtt_info_length = ap_info->tbtt_header.tbtt_info_length;
1083 
1084 	/*
1085 	 * Max TBTT sub-element length in RNR IE is 255 bytes and AP can send
1086 	 * data above defined length and the bytes in excess to this length
1087 	 * shall be treated as reserved.
1088 	 *
1089 	 * Limit the TBTT sub-element read operation to current supported
1090 	 * length i.e TBTT_NEIGHBOR_AP_PARAM_MAX
1091 	 */
1092 	if (tbtt_info_length > TBTT_NEIGHBOR_AP_PARAM_MAX)
1093 		tbtt_info_length = TBTT_NEIGHBOR_AP_PARAM_MAX;
1094 
1095 	switch (tbtt_info_length) {
1096 	case TBTT_NEIGHBOR_AP_OFFSET_ONLY:
1097 		/* Dont store it skip*/
1098 		break;
1099 
1100 	case TBTT_NEIGHBOR_AP_BSS_PARAM:
1101 		/* Dont store it skip*/
1102 		break;
1103 
1104 	case TBTT_NEIGHBOR_AP_S_SSID_BSS_PARAM:
1105 		rnr->bss_params = data[5];
1106 		fallthrough;
1107 	case TBTT_NEIGHBOR_AP_SHORTSSID:
1108 		rnr->channel_number = ap_info->channel_number;
1109 		rnr->operating_class = ap_info->operting_class;
1110 		qdf_mem_copy(&rnr->short_ssid, &data[1], SHORT_SSID_LEN);
1111 		break;
1112 
1113 	case TBTT_NEIGHBOR_AP_BSSID_BSS_PARAM_20MHZ_PSD:
1114 		rnr->psd_20mhz = data[8];
1115 		fallthrough;
1116 	case TBTT_NEIGHBOR_AP_BSSID_BSS_PARAM:
1117 		rnr->bss_params = data[7];
1118 		fallthrough;
1119 	case TBTT_NEIGHBOR_AP_BSSID:
1120 		rnr->channel_number = ap_info->channel_number;
1121 		rnr->operating_class = ap_info->operting_class;
1122 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
1123 		break;
1124 
1125 	case TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM_20MHZ_PSD_MLD_PARAM:
1126 		util_scan_update_rnr_mld(rnr, data, tbtt_info_length);
1127 		fallthrough;
1128 	case TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM_20MHZ_PSD:
1129 		rnr->psd_20mhz = data[12];
1130 		fallthrough;
1131 	case TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM:
1132 		rnr->bss_params = data[11];
1133 		fallthrough;
1134 	case TBTT_NEIGHBOR_AP_BSSSID_S_SSID:
1135 		rnr->channel_number = ap_info->channel_number;
1136 		rnr->operating_class = ap_info->operting_class;
1137 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
1138 		qdf_mem_copy(&rnr->short_ssid, &data[7], SHORT_SSID_LEN);
1139 		break;
1140 
1141 	default:
1142 		scm_debug("Wrong fieldtype");
1143 	}
1144 
1145 	return QDF_STATUS_SUCCESS;
1146 }
1147 
1148 static QDF_STATUS
1149 util_scan_parse_rnr_ie(struct scan_cache_entry *scan_entry,
1150 		       struct ie_header *ie)
1151 {
1152 	uint32_t rnr_ie_len;
1153 	uint16_t tbtt_count, tbtt_length, i, fieldtype, idx;
1154 	uint8_t *data;
1155 	struct neighbor_ap_info_field *neighbor_ap_info;
1156 
1157 	rnr_ie_len = ie->ie_len;
1158 	data = (uint8_t *)ie + sizeof(struct ie_header);
1159 	idx = scan_entry->rnr.count;
1160 
1161 	while (data < ((uint8_t *)ie + rnr_ie_len + 2)) {
1162 		neighbor_ap_info = (struct neighbor_ap_info_field *)data;
1163 		tbtt_count = neighbor_ap_info->tbtt_header.tbtt_info_count;
1164 		tbtt_length = neighbor_ap_info->tbtt_header.tbtt_info_length;
1165 		fieldtype = neighbor_ap_info->tbtt_header.tbbt_info_fieldtype;
1166 		scm_debug("chan %d, opclass %d tbtt_cnt %d, tbtt_len %d, fieldtype %d",
1167 			  neighbor_ap_info->channel_number,
1168 			  neighbor_ap_info->operting_class,
1169 			  tbtt_count, tbtt_length, fieldtype);
1170 		data += sizeof(struct neighbor_ap_info_field);
1171 
1172 		if (tbtt_count > TBTT_INFO_COUNT)
1173 			break;
1174 
1175 		for (i = 0; i < (tbtt_count + 1) &&
1176 		     data < ((uint8_t *)ie + rnr_ie_len + 2); i++) {
1177 			if ((i < MAX_RNR_BSS) && (idx < MAX_RNR_BSS))
1178 				util_scan_update_rnr(
1179 					&scan_entry->rnr.bss_info[idx++],
1180 					neighbor_ap_info,
1181 					data);
1182 			data += tbtt_length;
1183 		}
1184 	}
1185 
1186 	scan_entry->rnr.count = idx;
1187 
1188 	return QDF_STATUS_SUCCESS;
1189 }
1190 
1191 #ifdef WLAN_FEATURE_11BE_MLO
1192 static void
1193 util_scan_parse_t2lm_ie(struct scan_cache_entry *scan_params,
1194 			struct extn_ie_header *extn_ie)
1195 {
1196 	uint8_t t2lm_idx = 0;
1197 
1198 	if (extn_ie->ie_extn_id == WLAN_EXTN_ELEMID_T2LM)
1199 		for (t2lm_idx = 0; t2lm_idx < WLAN_MAX_T2LM_IE; t2lm_idx++) {
1200 			if (!scan_params->ie_list.t2lm[t2lm_idx]) {
1201 				scan_params->ie_list.t2lm[t2lm_idx] =
1202 					(uint8_t *)extn_ie;
1203 				return;
1204 		}
1205 	}
1206 }
1207 #endif
1208 
1209 #ifdef WLAN_FEATURE_11BE
1210 #ifdef WLAN_FEATURE_11BE_MLO
1211 static void util_scan_parse_ml_ie(struct scan_cache_entry *scan_params,
1212 				  struct extn_ie_header *extn_ie)
1213 {
1214 	uint8_t *ml_ie;
1215 	uint32_t ml_ie_len;
1216 	enum wlan_ml_variant ml_variant;
1217 	QDF_STATUS ret;
1218 
1219 	if (extn_ie->ie_extn_id != WLAN_EXTN_ELEMID_MULTI_LINK)
1220 		return;
1221 
1222 	ml_ie = (uint8_t *)extn_ie;
1223 	ml_ie_len = ml_ie[TAG_LEN_POS];
1224 
1225 	/* Adding the size of IE header to ML IE length */
1226 	ml_ie_len += sizeof(struct ie_header);
1227 	ret = util_get_mlie_variant(ml_ie, ml_ie_len, (int *)&ml_variant);
1228 	if (ret) {
1229 		scm_err("Unable to get ml variant");
1230 		return;
1231 	}
1232 
1233 	switch (ml_variant) {
1234 	case WLAN_ML_VARIANT_BASIC:
1235 		scan_params->ie_list.multi_link_bv = (uint8_t *)extn_ie;
1236 		break;
1237 	case WLAN_ML_VARIANT_RECONFIG:
1238 		scan_params->ie_list.multi_link_rv = (uint8_t *)extn_ie;
1239 		break;
1240 	default:
1241 		break;
1242 	}
1243 }
1244 #else
1245 static void util_scan_parse_ml_ie(struct scan_cache_entry *scan_params,
1246 				  struct extn_ie_header *extn_ie)
1247 {
1248 }
1249 #endif
1250 static void util_scan_parse_eht_ie(struct scan_cache_entry *scan_params,
1251 				   struct extn_ie_header *extn_ie)
1252 {
1253 	switch (extn_ie->ie_extn_id) {
1254 	case WLAN_EXTN_ELEMID_EHTCAP:
1255 		scan_params->ie_list.ehtcap = (uint8_t *)extn_ie;
1256 		break;
1257 	case WLAN_EXTN_ELEMID_EHTOP:
1258 		scan_params->ie_list.ehtop  = (uint8_t *)extn_ie;
1259 		break;
1260 	default:
1261 		break;
1262 	}
1263 
1264 	util_scan_parse_ml_ie(scan_params, extn_ie);
1265 	util_scan_parse_t2lm_ie(scan_params, extn_ie);
1266 }
1267 #else
1268 static void util_scan_parse_eht_ie(struct scan_cache_entry *scan_params,
1269 				   struct extn_ie_header *extn_ie)
1270 {
1271 }
1272 #endif
1273 
1274 static QDF_STATUS
1275 util_scan_parse_extn_ie(struct scan_cache_entry *scan_params,
1276 			struct ie_header *ie)
1277 {
1278 	struct extn_ie_header *extn_ie = (struct extn_ie_header *) ie;
1279 
1280 	switch (extn_ie->ie_extn_id) {
1281 	case WLAN_EXTN_ELEMID_MAX_CHAN_SWITCH_TIME:
1282 		if (extn_ie->ie_len != WLAN_MAX_CHAN_SWITCH_TIME_IE_LEN)
1283 			return QDF_STATUS_E_INVAL;
1284 		scan_params->ie_list.mcst  = (uint8_t *)ie;
1285 		break;
1286 	case WLAN_EXTN_ELEMID_SRP:
1287 		if (extn_ie->ie_len > WLAN_MAX_SRP_IE_LEN)
1288 			return QDF_STATUS_E_INVAL;
1289 		scan_params->ie_list.srp   = (uint8_t *)ie;
1290 		break;
1291 	case WLAN_EXTN_ELEMID_HECAP:
1292 		if ((extn_ie->ie_len < WLAN_MIN_HECAP_IE_LEN) ||
1293 		    (extn_ie->ie_len > WLAN_MAX_HECAP_IE_LEN))
1294 			return QDF_STATUS_E_INVAL;
1295 		scan_params->ie_list.hecap = (uint8_t *)ie;
1296 		break;
1297 	case WLAN_EXTN_ELEMID_HEOP:
1298 		if (extn_ie->ie_len > WLAN_MAX_HEOP_IE_LEN)
1299 			return QDF_STATUS_E_INVAL;
1300 		scan_params->ie_list.heop  = (uint8_t *)ie;
1301 		break;
1302 	case WLAN_EXTN_ELEMID_ESP:
1303 		scan_params->ie_list.esp = (uint8_t *)ie;
1304 		break;
1305 	case WLAN_EXTN_ELEMID_MUEDCA:
1306 		if (extn_ie->ie_len > WLAN_MAX_MUEDCA_IE_LEN)
1307 			return QDF_STATUS_E_INVAL;
1308 		scan_params->ie_list.muedca = (uint8_t *)ie;
1309 		break;
1310 	case WLAN_EXTN_ELEMID_HE_6G_CAP:
1311 		if (extn_ie->ie_len > WLAN_MAX_HE_6G_CAP_IE_LEN)
1312 			return QDF_STATUS_E_INVAL;
1313 		scan_params->ie_list.hecap_6g = (uint8_t *)ie;
1314 		break;
1315 	default:
1316 		break;
1317 	}
1318 	util_scan_parse_eht_ie(scan_params, extn_ie);
1319 
1320 	return QDF_STATUS_SUCCESS;
1321 }
1322 
1323 static QDF_STATUS
1324 util_scan_parse_vendor_ie(struct scan_cache_entry *scan_params,
1325 	struct ie_header *ie)
1326 {
1327 	if (!scan_params->ie_list.vendor)
1328 		scan_params->ie_list.vendor = (uint8_t *)ie;
1329 
1330 	if (is_wpa_oui((uint8_t *)ie)) {
1331 		scan_params->ie_list.wpa = (uint8_t *)ie;
1332 	} else if (is_wps_oui((uint8_t *)ie)) {
1333 		scan_params->ie_list.wps = (uint8_t *)ie;
1334 		/* WCN IE should be a subset of WPS IE */
1335 		if (is_wcn_oui((uint8_t *)ie))
1336 			scan_params->ie_list.wcn = (uint8_t *)ie;
1337 	} else if (is_wme_param((uint8_t *)ie)) {
1338 		if (ie->ie_len > WLAN_VENDOR_WME_IE_LEN)
1339 			return QDF_STATUS_E_INVAL;
1340 
1341 		scan_params->ie_list.wmeparam = (uint8_t *)ie;
1342 	} else if (is_wme_info((uint8_t *)ie)) {
1343 		scan_params->ie_list.wmeinfo = (uint8_t *)ie;
1344 	} else if (is_atheros_oui((uint8_t *)ie)) {
1345 		if (ie->ie_len > WLAN_VENDOR_ATHCAPS_IE_LEN)
1346 			return QDF_STATUS_E_INVAL;
1347 
1348 		scan_params->ie_list.athcaps = (uint8_t *)ie;
1349 	} else if (is_atheros_extcap_oui((uint8_t *)ie)) {
1350 		if (ie->ie_len > WLAN_VENDOR_ATH_EXTCAP_IE_LEN)
1351 			return QDF_STATUS_E_INVAL;
1352 
1353 		scan_params->ie_list.athextcaps = (uint8_t *)ie;
1354 	} else if (is_sfa_oui((uint8_t *)ie)) {
1355 		if (ie->ie_len > WLAN_VENDOR_SFA_IE_LEN)
1356 			return QDF_STATUS_E_INVAL;
1357 
1358 		scan_params->ie_list.sfa = (uint8_t *)ie;
1359 	} else if (is_p2p_oui((uint8_t *)ie)) {
1360 		scan_params->ie_list.p2p = (uint8_t *)ie;
1361 	} else if (is_qca_son_oui((uint8_t *)ie,
1362 				  QCA_OUI_WHC_AP_INFO_SUBTYPE)) {
1363 
1364 		scan_params->ie_list.sonadv = (uint8_t *)ie;
1365 	} else if (is_ht_cap((uint8_t *)ie)) {
1366 		/* we only care if there isn't already an HT IE (ANA) */
1367 		if (!scan_params->ie_list.htcap) {
1368 			if (ie->ie_len != (WLAN_VENDOR_HT_IE_OFFSET_LEN +
1369 					   sizeof(struct htcap_cmn_ie)))
1370 				return QDF_STATUS_E_INVAL;
1371 			scan_params->ie_list.htcap =
1372 			 (uint8_t *)&(((struct wlan_vendor_ie_htcap *)ie)->ie);
1373 		}
1374 	} else if (is_ht_info((uint8_t *)ie)) {
1375 		/* we only care if there isn't already an HT IE (ANA) */
1376 		if (!scan_params->ie_list.htinfo) {
1377 			if (ie->ie_len != WLAN_VENDOR_HT_IE_OFFSET_LEN +
1378 					  sizeof(struct wlan_ie_htinfo_cmn))
1379 				return QDF_STATUS_E_INVAL;
1380 			scan_params->ie_list.htinfo =
1381 			  (uint8_t *)&(((struct wlan_vendor_ie_htinfo *)
1382 			  ie)->hi_ie);
1383 		}
1384 	} else if (is_interop_vht((uint8_t *)ie) &&
1385 	    !(scan_params->ie_list.vhtcap)) {
1386 		uint8_t *vendor_ie = (uint8_t *)(ie);
1387 
1388 		if (ie->ie_len < ((WLAN_VENDOR_VHTCAP_IE_OFFSET +
1389 				 sizeof(struct wlan_ie_vhtcaps)) -
1390 				 sizeof(struct ie_header)))
1391 			return QDF_STATUS_E_INVAL;
1392 		vendor_ie = ((uint8_t *)(ie)) + WLAN_VENDOR_VHTCAP_IE_OFFSET;
1393 		if (vendor_ie[1] != (sizeof(struct wlan_ie_vhtcaps)) -
1394 				      sizeof(struct ie_header))
1395 			return QDF_STATUS_E_INVAL;
1396 		/* location where Interop Vht Cap IE and VHT OP IE Present */
1397 		scan_params->ie_list.vhtcap = (((uint8_t *)(ie)) +
1398 						WLAN_VENDOR_VHTCAP_IE_OFFSET);
1399 		if (ie->ie_len > ((WLAN_VENDOR_VHTCAP_IE_OFFSET +
1400 				 sizeof(struct wlan_ie_vhtcaps)) -
1401 				 sizeof(struct ie_header))) {
1402 			if (ie->ie_len < ((WLAN_VENDOR_VHTOP_IE_OFFSET +
1403 					  sizeof(struct wlan_ie_vhtop)) -
1404 					  sizeof(struct ie_header)))
1405 				return QDF_STATUS_E_INVAL;
1406 			vendor_ie = ((uint8_t *)(ie)) +
1407 				    WLAN_VENDOR_VHTOP_IE_OFFSET;
1408 			if (vendor_ie[1] != (sizeof(struct wlan_ie_vhtop) -
1409 					     sizeof(struct ie_header)))
1410 				return QDF_STATUS_E_INVAL;
1411 			scan_params->ie_list.vhtop = (((uint8_t *)(ie)) +
1412 						   WLAN_VENDOR_VHTOP_IE_OFFSET);
1413 		}
1414 	} else if (is_bwnss_oui((uint8_t *)ie)) {
1415 		/*
1416 		 * Bandwidth-NSS map has sub-type & version.
1417 		 * hence copy data just after version byte
1418 		 */
1419 		if (ie->ie_len > WLAN_BWNSS_MAP_OFFSET)
1420 			scan_params->ie_list.bwnss_map = (((uint8_t *)ie) + 8);
1421 	} else if (is_mbo_oce_oui((uint8_t *)ie)) {
1422 		scan_params->ie_list.mbo_oce = (uint8_t *)ie;
1423 	} else if (is_extender_oui((uint8_t *)ie)) {
1424 		scan_params->ie_list.extender = (uint8_t *)ie;
1425 	} else if (is_adaptive_11r_oui((uint8_t *)ie)) {
1426 		if ((ie->ie_len < OUI_LENGTH) ||
1427 		    (ie->ie_len > MAX_ADAPTIVE_11R_IE_LEN))
1428 			return QDF_STATUS_E_INVAL;
1429 
1430 		scan_params->ie_list.adaptive_11r = (uint8_t *)ie +
1431 						sizeof(struct ie_header);
1432 	} else if (is_sae_single_pmk_oui((uint8_t *)ie)) {
1433 		if ((ie->ie_len < OUI_LENGTH) ||
1434 		    (ie->ie_len > MAX_SAE_SINGLE_PMK_IE_LEN)) {
1435 			scm_debug("Invalid sae single pmk OUI");
1436 			return QDF_STATUS_E_INVAL;
1437 		}
1438 		scan_params->ie_list.single_pmk = (uint8_t *)ie +
1439 						sizeof(struct ie_header);
1440 	} else if (is_qcn_oui((uint8_t *)ie)) {
1441 		scan_params->ie_list.qcn = (uint8_t *)ie;
1442 	}
1443 
1444 	return QDF_STATUS_SUCCESS;
1445 }
1446 
1447 static QDF_STATUS
1448 util_scan_populate_bcn_ie_list(struct wlan_objmgr_pdev *pdev,
1449 			       struct scan_cache_entry *scan_params,
1450 			       qdf_freq_t *chan_freq, uint8_t band_mask)
1451 {
1452 	struct ie_header *ie, *sub_ie;
1453 	uint32_t ie_len, sub_ie_len;
1454 	QDF_STATUS status;
1455 	uint8_t chan_idx;
1456 	struct wlan_scan_obj *scan_obj;
1457 	struct wlan_objmgr_psoc *psoc;
1458 	uint8_t tpe_idx = 0;
1459 
1460 	psoc = wlan_pdev_get_psoc(pdev);
1461 	if (!psoc) {
1462 		scm_err("psoc is NULL");
1463 		return QDF_STATUS_E_INVAL;
1464 	}
1465 
1466 	scan_obj = wlan_psoc_get_scan_obj(psoc);
1467 	if (!scan_obj) {
1468 		scm_err("scan_obj is NULL");
1469 		return QDF_STATUS_E_INVAL;
1470 	}
1471 
1472 	ie_len = util_scan_entry_ie_len(scan_params);
1473 	ie = (struct ie_header *)
1474 		  util_scan_entry_ie_data(scan_params);
1475 
1476 	while (ie_len >= sizeof(struct ie_header)) {
1477 		ie_len -= sizeof(struct ie_header);
1478 
1479 		if (!ie->ie_len) {
1480 			ie += 1;
1481 			continue;
1482 		}
1483 
1484 		if (ie_len < ie->ie_len) {
1485 			if (scan_obj->allow_bss_with_incomplete_ie) {
1486 				scm_debug(QDF_MAC_ADDR_FMT": Scan allowed with incomplete corrupted IE:%x, ie_len: %d, ie->ie_len: %d, stop processing further",
1487 					  QDF_MAC_ADDR_REF(scan_params->bssid.bytes),
1488 					  ie->ie_id, ie_len, ie->ie_len);
1489 				break;
1490 			}
1491 			scm_debug(QDF_MAC_ADDR_FMT": Scan not allowed with incomplete corrupted IE:%x, ie_len: %d, ie->ie_len: %d, stop processing further",
1492 				  QDF_MAC_ADDR_REF(scan_params->bssid.bytes),
1493 				  ie->ie_id, ie_len, ie->ie_len);
1494 			return QDF_STATUS_E_INVAL;
1495 		}
1496 
1497 		switch (ie->ie_id) {
1498 		case WLAN_ELEMID_SSID:
1499 			if (ie->ie_len > (sizeof(struct ie_ssid) -
1500 					  sizeof(struct ie_header)))
1501 				goto err;
1502 			scan_params->ie_list.ssid = (uint8_t *)ie;
1503 			break;
1504 		case WLAN_ELEMID_RATES:
1505 			if (ie->ie_len > WLAN_SUPPORTED_RATES_IE_MAX_LEN)
1506 				goto err;
1507 			scan_params->ie_list.rates = (uint8_t *)ie;
1508 			break;
1509 		case WLAN_ELEMID_DSPARMS:
1510 			if (ie->ie_len != WLAN_DS_PARAM_IE_MAX_LEN)
1511 				return QDF_STATUS_E_INVAL;
1512 			scan_params->ie_list.ds_param = (uint8_t *)ie;
1513 			chan_idx = ((struct ds_ie *)ie)->cur_chan;
1514 			*chan_freq = wlan_reg_chan_band_to_freq(pdev, chan_idx,
1515 								band_mask);
1516 			/* Drop if invalid freq */
1517 			if (scan_obj->drop_bcn_on_invalid_freq &&
1518 			    !wlan_reg_is_freq_enabled(pdev,
1519 						      *chan_freq,
1520 						      REG_CURRENT_PWR_MODE)) {
1521 				scm_debug(QDF_MAC_ADDR_FMT": Drop as invalid chan %d in DS IE, freq %d, band_mask %d",
1522 					  QDF_MAC_ADDR_REF(
1523 						  scan_params->bssid.bytes),
1524 					  chan_idx, *chan_freq, band_mask);
1525 				return QDF_STATUS_E_INVAL;
1526 			}
1527 			break;
1528 		case WLAN_ELEMID_TIM:
1529 			if (ie->ie_len < WLAN_TIM_IE_MIN_LENGTH)
1530 				goto err;
1531 			scan_params->ie_list.tim = (uint8_t *)ie;
1532 			scan_params->dtim_period =
1533 				((struct wlan_tim_ie *)ie)->tim_period;
1534 			break;
1535 		case WLAN_ELEMID_COUNTRY:
1536 			if (ie->ie_len < WLAN_COUNTRY_IE_MIN_LEN)
1537 				goto err;
1538 			scan_params->ie_list.country = (uint8_t *)ie;
1539 			break;
1540 		case WLAN_ELEMID_QBSS_LOAD:
1541 			if (ie->ie_len != sizeof(struct qbss_load_ie) -
1542 					  sizeof(struct ie_header)) {
1543 				/*
1544 				 * Expected QBSS IE length is 5Bytes; For some
1545 				 * old cisco AP, QBSS IE length is 4Bytes, which
1546 				 * doesn't match with latest spec, So ignore
1547 				 * QBSS IE in such case.
1548 				 */
1549 				break;
1550 			}
1551 			scan_params->ie_list.qbssload = (uint8_t *)ie;
1552 			break;
1553 		case WLAN_ELEMID_CHANSWITCHANN:
1554 			if (ie->ie_len != WLAN_CSA_IE_MAX_LEN)
1555 				goto err;
1556 			scan_params->ie_list.csa = (uint8_t *)ie;
1557 			break;
1558 		case WLAN_ELEMID_IBSSDFS:
1559 			if (ie->ie_len < WLAN_IBSSDFS_IE_MIN_LEN)
1560 				goto err;
1561 			scan_params->ie_list.ibssdfs = (uint8_t *)ie;
1562 			break;
1563 		case WLAN_ELEMID_QUIET:
1564 			if (ie->ie_len != WLAN_QUIET_IE_MAX_LEN)
1565 				goto err;
1566 			scan_params->ie_list.quiet = (uint8_t *)ie;
1567 			break;
1568 		case WLAN_ELEMID_ERP:
1569 			if (ie->ie_len != (sizeof(struct erp_ie) -
1570 					    sizeof(struct ie_header)))
1571 				goto err;
1572 			scan_params->erp = ((struct erp_ie *)ie)->value;
1573 			break;
1574 		case WLAN_ELEMID_HTCAP_ANA:
1575 			if (ie->ie_len == sizeof(struct htcap_cmn_ie)) {
1576 				scan_params->ie_list.htcap =
1577 				(uint8_t *)&(((struct htcap_ie *)ie)->ie);
1578 			}
1579 			break;
1580 		case WLAN_ELEMID_RSN:
1581 			/*
1582 			 * For security cert TC, RSNIE length can be 1 but if
1583 			 * beacon is dropped, old entry will remain in scan
1584 			 * cache and cause cert TC failure as connection with
1585 			 * old entry with valid RSN IE will pass.
1586 			 * So instead of dropping the frame, do not store the
1587 			 * RSN pointer so that old entry is overwritten.
1588 			 */
1589 			if (ie->ie_len >= WLAN_RSN_IE_MIN_LEN)
1590 				scan_params->ie_list.rsn = (uint8_t *)ie;
1591 			break;
1592 		case WLAN_ELEMID_XRATES:
1593 			if (ie->ie_len > WLAN_EXT_SUPPORTED_RATES_IE_MAX_LEN)
1594 				goto err;
1595 			scan_params->ie_list.xrates = (uint8_t *)ie;
1596 			break;
1597 		case WLAN_ELEMID_EXTCHANSWITCHANN:
1598 			if (ie->ie_len != WLAN_XCSA_IE_MAX_LEN)
1599 				goto err;
1600 			scan_params->ie_list.xcsa = (uint8_t *)ie;
1601 			break;
1602 		case WLAN_ELEMID_SECCHANOFFSET:
1603 			if (ie->ie_len != WLAN_SECCHANOFF_IE_MAX_LEN)
1604 				goto err;
1605 			scan_params->ie_list.secchanoff = (uint8_t *)ie;
1606 			break;
1607 		case WLAN_ELEMID_HTINFO_ANA:
1608 			if (ie->ie_len != sizeof(struct wlan_ie_htinfo_cmn))
1609 				goto err;
1610 			scan_params->ie_list.htinfo =
1611 			  (uint8_t *)&(((struct wlan_ie_htinfo *) ie)->hi_ie);
1612 			chan_idx = ((struct wlan_ie_htinfo_cmn *)
1613 				 (scan_params->ie_list.htinfo))->hi_ctrlchannel;
1614 			*chan_freq = wlan_reg_chan_band_to_freq(pdev, chan_idx,
1615 								band_mask);
1616 			/* Drop if invalid freq */
1617 			if (scan_obj->drop_bcn_on_invalid_freq &&
1618 			    !wlan_reg_is_freq_enabled(pdev,
1619 						      *chan_freq,
1620 						      REG_CURRENT_PWR_MODE)) {
1621 				scm_debug_rl(QDF_MAC_ADDR_FMT": Drop as invalid channel %d freq %d in HT_INFO IE",
1622 					     QDF_MAC_ADDR_REF(scan_params->bssid.bytes),
1623 					     chan_idx, *chan_freq);
1624 				return QDF_STATUS_E_INVAL;
1625 			}
1626 			break;
1627 		case WLAN_ELEMID_WAPI:
1628 			if (ie->ie_len < WLAN_WAPI_IE_MIN_LEN)
1629 				goto err;
1630 			scan_params->ie_list.wapi = (uint8_t *)ie;
1631 			break;
1632 		case WLAN_ELEMID_XCAPS:
1633 			if (ie->ie_len > WLAN_EXTCAP_IE_MAX_LEN)
1634 				goto err;
1635 			scan_params->ie_list.extcaps = (uint8_t *)ie;
1636 			break;
1637 		case WLAN_ELEMID_VHTCAP:
1638 			if (ie->ie_len != (sizeof(struct wlan_ie_vhtcaps) -
1639 					   sizeof(struct ie_header)))
1640 				goto err;
1641 			scan_params->ie_list.vhtcap = (uint8_t *)ie;
1642 			break;
1643 		case WLAN_ELEMID_VHTOP:
1644 			if (ie->ie_len != (sizeof(struct wlan_ie_vhtop) -
1645 					   sizeof(struct ie_header)))
1646 				goto err;
1647 			scan_params->ie_list.vhtop = (uint8_t *)ie;
1648 			break;
1649 		case WLAN_ELEMID_OP_MODE_NOTIFY:
1650 			if (ie->ie_len != WLAN_OPMODE_IE_MAX_LEN)
1651 				goto err;
1652 			scan_params->ie_list.opmode = (uint8_t *)ie;
1653 			break;
1654 		case WLAN_ELEMID_MOBILITY_DOMAIN:
1655 			if (ie->ie_len != WLAN_MOBILITY_DOMAIN_IE_MAX_LEN)
1656 				goto err;
1657 			scan_params->ie_list.mdie = (uint8_t *)ie;
1658 			break;
1659 		case WLAN_ELEMID_VENDOR:
1660 			status = util_scan_parse_vendor_ie(scan_params,
1661 							   ie);
1662 			if (QDF_IS_STATUS_ERROR(status))
1663 				goto err_status;
1664 			break;
1665 		case WLAN_ELEMID_VHT_TX_PWR_ENVLP:
1666 			if (ie->ie_len < WLAN_TPE_IE_MIN_LEN)
1667 				goto err;
1668 			if (tpe_idx >= WLAN_MAX_NUM_TPE_IE)
1669 				goto err;
1670 			scan_params->ie_list.tpe[tpe_idx++] = (uint8_t *)ie;
1671 			break;
1672 		case WLAN_ELEMID_CHAN_SWITCH_WRAP:
1673 			scan_params->ie_list.cswrp = (uint8_t *)ie;
1674 			/* Go to next sub IE */
1675 			sub_ie = (struct ie_header *)
1676 			(((uint8_t *)ie) + sizeof(struct ie_header));
1677 			sub_ie_len = ie->ie_len;
1678 			status =
1679 				util_scan_parse_chan_switch_wrapper_ie(
1680 					scan_params, sub_ie, sub_ie_len);
1681 			if (QDF_IS_STATUS_ERROR(status)) {
1682 				goto err_status;
1683 			}
1684 			break;
1685 		case WLAN_ELEMID_FILS_INDICATION:
1686 			if (ie->ie_len < WLAN_FILS_INDICATION_IE_MIN_LEN)
1687 				goto err;
1688 			scan_params->ie_list.fils_indication = (uint8_t *)ie;
1689 			break;
1690 		case WLAN_ELEMID_RSNXE:
1691 			if (!ie->ie_len)
1692 				goto err;
1693 			scan_params->ie_list.rsnxe = (uint8_t *)ie;
1694 			break;
1695 		case WLAN_ELEMID_EXTN_ELEM:
1696 			status = util_scan_parse_extn_ie(scan_params, ie);
1697 			if (QDF_IS_STATUS_ERROR(status))
1698 				goto err_status;
1699 			break;
1700 		case WLAN_ELEMID_REDUCED_NEIGHBOR_REPORT:
1701 			if (ie->ie_len < WLAN_RNR_IE_MIN_LEN)
1702 				goto err;
1703 			scan_params->ie_list.rnrie = (uint8_t *)ie;
1704 			status = util_scan_parse_rnr_ie(scan_params, ie);
1705 			if (QDF_IS_STATUS_ERROR(status))
1706 				goto err_status;
1707 			break;
1708 		default:
1709 			break;
1710 		}
1711 
1712 		/* Consume info element */
1713 		ie_len -= ie->ie_len;
1714 		/* Go to next IE */
1715 		ie = (struct ie_header *)
1716 			(((uint8_t *) ie) +
1717 			sizeof(struct ie_header) +
1718 			ie->ie_len);
1719 	}
1720 
1721 	return QDF_STATUS_SUCCESS;
1722 
1723 err:
1724 	status = QDF_STATUS_E_INVAL;
1725 err_status:
1726 	scm_debug(QDF_MAC_ADDR_FMT ": failed to parse IE - id: %d, len: %d",
1727 		  QDF_MAC_ADDR_REF(scan_params->bssid.bytes),
1728 		  ie->ie_id, ie->ie_len);
1729 
1730 	return status;
1731 }
1732 
1733 /**
1734  * util_scan_update_esp_data: update ESP params from beacon/probe response
1735  * @esp_information: pointer to wlan_esp_information
1736  * @scan_entry: new received entry
1737  *
1738  * The Estimated Service Parameters element is
1739  * used by a AP to provide information to another STA which
1740  * can then use the information as input to an algorithm to
1741  * generate an estimate of throughput between the two STAs.
1742  * The ESP Information List field contains from 1 to 4 ESP
1743  * Information fields(each field 24 bits), each corresponding
1744  * to an access category for which estimated service parameters
1745  * information is provided.
1746  *
1747  * Return: None
1748  */
1749 static void util_scan_update_esp_data(struct wlan_esp_ie *esp_information,
1750 		struct scan_cache_entry *scan_entry)
1751 {
1752 
1753 	uint8_t *data;
1754 	int i = 0;
1755 	uint64_t total_elements;
1756 	struct wlan_esp_info *esp_info;
1757 	struct wlan_esp_ie *esp_ie;
1758 
1759 	esp_ie = (struct wlan_esp_ie *)
1760 		util_scan_entry_esp_info(scan_entry);
1761 
1762 	// Ignore ESP_ID_EXTN element
1763 	total_elements  = esp_ie->esp_len - 1;
1764 	data = (uint8_t *)esp_ie + 3;
1765 	do_div(total_elements, ESP_INFORMATION_LIST_LENGTH);
1766 
1767 	if (total_elements > MAX_ESP_INFORMATION_FIELD) {
1768 		scm_err("No of Air time fractions are greater than supported");
1769 		return;
1770 	}
1771 
1772 	for (i = 0; i < total_elements &&
1773 	     data < ((uint8_t *)esp_ie + esp_ie->esp_len); i++) {
1774 		esp_info = (struct wlan_esp_info *)data;
1775 		if (esp_info->access_category == ESP_AC_BK) {
1776 			qdf_mem_copy(&esp_information->esp_info_AC_BK,
1777 					data, 3);
1778 			data = data + ESP_INFORMATION_LIST_LENGTH;
1779 			continue;
1780 		}
1781 		if (esp_info->access_category == ESP_AC_BE) {
1782 			qdf_mem_copy(&esp_information->esp_info_AC_BE,
1783 					data, 3);
1784 			data = data + ESP_INFORMATION_LIST_LENGTH;
1785 			continue;
1786 		}
1787 		if (esp_info->access_category == ESP_AC_VI) {
1788 			qdf_mem_copy(&esp_information->esp_info_AC_VI,
1789 					data, 3);
1790 			data = data + ESP_INFORMATION_LIST_LENGTH;
1791 			continue;
1792 		}
1793 		if (esp_info->access_category == ESP_AC_VO) {
1794 			qdf_mem_copy(&esp_information->esp_info_AC_VO,
1795 					data, 3);
1796 			data = data + ESP_INFORMATION_LIST_LENGTH;
1797 			break;
1798 		}
1799 	}
1800 }
1801 
1802 /**
1803  * util_scan_scm_update_bss_with_esp_data: calculate estimated air time
1804  * fraction
1805  * @scan_entry: new received entry
1806  *
1807  * This function process all Access category ESP params and provide
1808  * best effort air time fraction.
1809  * If best effort is not available, it will choose VI, VO and BK in sequence
1810  *
1811  */
1812 static void util_scan_scm_update_bss_with_esp_data(
1813 		struct scan_cache_entry *scan_entry)
1814 {
1815 	uint8_t air_time_fraction = 0;
1816 	struct wlan_esp_ie esp_information;
1817 
1818 	if (!scan_entry->ie_list.esp)
1819 		return;
1820 
1821 	util_scan_update_esp_data(&esp_information, scan_entry);
1822 
1823 	/*
1824 	 * If the ESP metric is transmitting multiple airtime fractions, then
1825 	 * follow the sequence AC_BE, AC_VI, AC_VO, AC_BK and pick whichever is
1826 	 * the first one available
1827 	 */
1828 	if (esp_information.esp_info_AC_BE.access_category
1829 			== ESP_AC_BE)
1830 		air_time_fraction =
1831 			esp_information.esp_info_AC_BE.
1832 			estimated_air_fraction;
1833 	else if (esp_information.esp_info_AC_VI.access_category
1834 			== ESP_AC_VI)
1835 		air_time_fraction =
1836 			esp_information.esp_info_AC_VI.
1837 			estimated_air_fraction;
1838 	else if (esp_information.esp_info_AC_VO.access_category
1839 			== ESP_AC_VO)
1840 		air_time_fraction =
1841 			esp_information.esp_info_AC_VO.
1842 			estimated_air_fraction;
1843 	else if (esp_information.esp_info_AC_BK.access_category
1844 			== ESP_AC_BK)
1845 		air_time_fraction =
1846 			esp_information.esp_info_AC_BK.
1847 				estimated_air_fraction;
1848 	scan_entry->air_time_fraction = air_time_fraction;
1849 }
1850 
1851 /**
1852  * util_scan_scm_calc_nss_supported_by_ap() - finds out nss from AP
1853  * @scan_params: new received entry
1854  *
1855  * Return: number of nss advertised by AP
1856  */
1857 static int util_scan_scm_calc_nss_supported_by_ap(
1858 		struct scan_cache_entry *scan_params)
1859 {
1860 	struct htcap_cmn_ie *htcap;
1861 	struct wlan_ie_vhtcaps *vhtcaps;
1862 	uint8_t *he_cap;
1863 	uint8_t *end_ptr = NULL;
1864 	uint16_t rx_mcs_map = 0;
1865 	uint8_t *mcs_map_offset;
1866 
1867 	htcap = (struct htcap_cmn_ie *)
1868 		util_scan_entry_htcap(scan_params);
1869 	vhtcaps = (struct wlan_ie_vhtcaps *)
1870 		util_scan_entry_vhtcap(scan_params);
1871 	he_cap = util_scan_entry_hecap(scan_params);
1872 
1873 	if (he_cap) {
1874 		/* Using rx mcs map related to 80MHz or lower as in some
1875 		 * cases higher mcs may support lesser NSS than that
1876 		 * of lowe mcs. Thus giving max NSS capability.
1877 		 */
1878 		end_ptr = he_cap + he_cap[1] + sizeof(struct ie_header);
1879 		mcs_map_offset = (he_cap + sizeof(struct extn_ie_header) +
1880 				  WLAN_HE_MACCAP_LEN + WLAN_HE_PHYCAP_LEN);
1881 		if ((mcs_map_offset + WLAN_HE_MCS_MAP_LEN) <= end_ptr) {
1882 			rx_mcs_map = *(uint16_t *)mcs_map_offset;
1883 		} else {
1884 			rx_mcs_map = WLAN_INVALID_RX_MCS_MAP;
1885 			scm_debug("mcs_map_offset exceeds he cap len");
1886 		}
1887 	} else if (vhtcaps) {
1888 		rx_mcs_map = vhtcaps->rx_mcs_map;
1889 	}
1890 
1891 	if (he_cap || vhtcaps) {
1892 		if ((rx_mcs_map & 0xC000) != 0xC000)
1893 			return 8;
1894 
1895 		if ((rx_mcs_map & 0x3000) != 0x3000)
1896 			return 7;
1897 
1898 		if ((rx_mcs_map & 0x0C00) != 0x0C00)
1899 			return 6;
1900 
1901 		if ((rx_mcs_map & 0x0300) != 0x0300)
1902 			return 5;
1903 
1904 		if ((rx_mcs_map & 0x00C0) != 0x00C0)
1905 			return 4;
1906 
1907 		if ((rx_mcs_map & 0x0030) != 0x0030)
1908 			return 3;
1909 
1910 		if ((rx_mcs_map & 0x000C) != 0x000C)
1911 			return 2;
1912 	} else if (htcap) {
1913 		if (htcap->mcsset[3])
1914 			return 4;
1915 
1916 		if (htcap->mcsset[2])
1917 			return 3;
1918 
1919 		if (htcap->mcsset[1])
1920 			return 2;
1921 
1922 	}
1923 	return 1;
1924 }
1925 
1926 #ifdef WLAN_DFS_CHAN_HIDDEN_SSID
1927 QDF_STATUS
1928 util_scan_add_hidden_ssid(struct wlan_objmgr_pdev *pdev, qdf_nbuf_t bcnbuf)
1929 {
1930 	struct wlan_frame_hdr *hdr;
1931 	struct wlan_bcn_frame *bcn;
1932 	struct wlan_scan_obj *scan_obj;
1933 	struct wlan_ssid *conf_ssid;
1934 	struct  ie_header *ie;
1935 	uint32_t frame_len = qdf_nbuf_len(bcnbuf);
1936 	uint16_t bcn_ie_offset, ssid_ie_start_offset, ssid_ie_end_offset;
1937 	uint16_t tmplen, ie_length;
1938 	uint8_t *pbeacon, *tmp;
1939 	bool     set_ssid_flag = false;
1940 	struct ie_ssid ssid = {0};
1941 	uint8_t pdev_id;
1942 
1943 	if (!pdev) {
1944 		scm_warn("pdev: 0x%pK is NULL", pdev);
1945 		return QDF_STATUS_E_NULL_VALUE;
1946 	}
1947 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1948 	scan_obj = wlan_pdev_get_scan_obj(pdev);
1949 	if (!scan_obj) {
1950 		scm_warn("null scan_obj");
1951 		return QDF_STATUS_E_NULL_VALUE;
1952 	}
1953 
1954 	conf_ssid = &scan_obj->pdev_info[pdev_id].conf_ssid;
1955 
1956 	hdr = (struct wlan_frame_hdr *)qdf_nbuf_data(bcnbuf);
1957 
1958 	/* received bssid does not match configured bssid */
1959 	if (qdf_mem_cmp(hdr->i_addr3, scan_obj->pdev_info[pdev_id].conf_bssid,
1960 			QDF_MAC_ADDR_SIZE) ||
1961 			conf_ssid->length == 0) {
1962 		return QDF_STATUS_SUCCESS;
1963 	}
1964 
1965 	bcn = (struct wlan_bcn_frame *)(qdf_nbuf_data(bcnbuf) + sizeof(*hdr));
1966 	pbeacon = (uint8_t *)bcn;
1967 
1968 	ie = (struct ie_header *)(pbeacon +
1969 				  offsetof(struct wlan_bcn_frame, ie));
1970 
1971 	bcn_ie_offset = offsetof(struct wlan_bcn_frame, ie);
1972 	ie_length = (uint16_t)(frame_len - sizeof(*hdr) -
1973 			       bcn_ie_offset);
1974 
1975 	while (ie_length >=  sizeof(struct ie_header)) {
1976 		ie_length -= sizeof(struct ie_header);
1977 
1978 		bcn_ie_offset += sizeof(struct ie_header);
1979 
1980 		if (ie_length < ie->ie_len) {
1981 			scm_debug("Incomplete corrupted IE:%x", ie->ie_id);
1982 			return QDF_STATUS_E_INVAL;
1983 		}
1984 		if (ie->ie_id == WLAN_ELEMID_SSID) {
1985 			if (ie->ie_len > (sizeof(struct ie_ssid) -
1986 						 sizeof(struct ie_header))) {
1987 				return QDF_STATUS_E_INVAL;
1988 			}
1989 			ssid.ssid_id = ie->ie_id;
1990 			ssid.ssid_len = ie->ie_len;
1991 
1992 			if (ssid.ssid_len)
1993 				qdf_mem_copy(ssid.ssid,
1994 					     ie + sizeof(struct ie_header),
1995 					     ssid.ssid_len);
1996 
1997 			if (util_scan_is_hidden_ssid(&ssid)) {
1998 				set_ssid_flag  = true;
1999 				ssid_ie_start_offset = bcn_ie_offset -
2000 					sizeof(struct ie_header);
2001 				ssid_ie_end_offset = bcn_ie_offset +
2002 					ie->ie_len;
2003 			}
2004 		}
2005 		if (ie->ie_len == 0) {
2006 			ie += 1;    /* next IE */
2007 			continue;
2008 		}
2009 		if (ie->ie_id == WLAN_ELEMID_VENDOR &&
2010 		    is_wps_oui((uint8_t *)ie)) {
2011 			set_ssid_flag = false;
2012 			break;
2013 		}
2014 		/* Consume info element */
2015 		ie_length -=  ie->ie_len;
2016 		/* Go to next IE */
2017 		ie = (struct ie_header *)(((uint8_t *)ie) +
2018 				sizeof(struct ie_header) +
2019 				ie->ie_len);
2020 	}
2021 
2022 	if (set_ssid_flag) {
2023 		/* Hidden SSID if the Length is 0 */
2024 		if (!ssid.ssid_len) {
2025 			/* increase the taillength by length of ssid */
2026 			if (qdf_nbuf_put_tail(bcnbuf,
2027 					      conf_ssid->length) == NULL) {
2028 				scm_debug("No enough tailroom");
2029 				return  QDF_STATUS_E_NOMEM;
2030 			}
2031 			/*
2032 			 * "qdf_nbuf_put_tail" might change the data pointer of
2033 			 * the skb. Therefore use the new data area.
2034 			 */
2035 			pbeacon = (qdf_nbuf_data(bcnbuf) + sizeof(*hdr));
2036 			/* length of the buffer to be copied */
2037 			tmplen = frame_len -
2038 				sizeof(*hdr) - ssid_ie_end_offset;
2039 			/*
2040 			 * tmp memory to copy the beacon info
2041 			 * after ssid ie.
2042 			 */
2043 			tmp = qdf_mem_malloc(tmplen * sizeof(u_int8_t));
2044 			if (!tmp)
2045 				return  QDF_STATUS_E_NOMEM;
2046 
2047 			/* Copy beacon data after ssid ie to tmp */
2048 			qdf_nbuf_copy_bits(bcnbuf, (sizeof(*hdr) +
2049 					   ssid_ie_end_offset), tmplen, tmp);
2050 			/* Add ssid length */
2051 			*(pbeacon + (ssid_ie_start_offset + 1))
2052 				= conf_ssid->length;
2053 			/* Insert the  SSID string */
2054 			qdf_mem_copy((pbeacon + ssid_ie_end_offset),
2055 				     conf_ssid->ssid, conf_ssid->length);
2056 			/* Copy rest of the beacon data */
2057 			qdf_mem_copy((pbeacon + ssid_ie_end_offset +
2058 				      conf_ssid->length), tmp, tmplen);
2059 			qdf_mem_free(tmp);
2060 
2061 			/* Hidden ssid with all 0's */
2062 		} else if (ssid.ssid_len == conf_ssid->length) {
2063 			/* Insert the  SSID string */
2064 			qdf_mem_copy((pbeacon + ssid_ie_start_offset +
2065 				      sizeof(struct ie_header)),
2066 				      conf_ssid->ssid, conf_ssid->length);
2067 		} else {
2068 			scm_debug("mismatch in hidden ssid length");
2069 			return QDF_STATUS_E_INVAL;
2070 		}
2071 	}
2072 	return QDF_STATUS_SUCCESS;
2073 }
2074 #endif /* WLAN_DFS_CHAN_HIDDEN_SSID */
2075 
2076 #ifdef WLAN_ADAPTIVE_11R
2077 /**
2078  * scm_fill_adaptive_11r_cap() - Check if the AP supports adaptive 11r
2079  * @scan_entry: Pointer to the scan entry
2080  *
2081  * Return: true if adaptive 11r is advertised else false
2082  */
2083 static void scm_fill_adaptive_11r_cap(struct scan_cache_entry *scan_entry)
2084 {
2085 	uint8_t *ie;
2086 	uint8_t data;
2087 	bool adaptive_11r;
2088 
2089 	ie = util_scan_entry_adaptive_11r(scan_entry);
2090 	if (!ie)
2091 		return;
2092 
2093 	data = *(ie + OUI_LENGTH);
2094 	adaptive_11r = (data & 0x1) ? true : false;
2095 
2096 	scan_entry->adaptive_11r_ap = adaptive_11r;
2097 }
2098 #else
2099 static void scm_fill_adaptive_11r_cap(struct scan_cache_entry *scan_entry)
2100 {
2101 	scan_entry->adaptive_11r_ap = false;
2102 }
2103 #endif
2104 
2105 static void util_scan_set_security(struct scan_cache_entry *scan_params)
2106 {
2107 	if (util_scan_entry_wpa(scan_params))
2108 		scan_params->security_type |= SCAN_SECURITY_TYPE_WPA;
2109 
2110 	if (util_scan_entry_rsn(scan_params))
2111 		scan_params->security_type |= SCAN_SECURITY_TYPE_RSN;
2112 	if (util_scan_entry_wapi(scan_params))
2113 		scan_params->security_type |= SCAN_SECURITY_TYPE_WAPI;
2114 
2115 	if (!scan_params->security_type &&
2116 	    scan_params->cap_info.wlan_caps.privacy)
2117 		scan_params->security_type |= SCAN_SECURITY_TYPE_WEP;
2118 }
2119 
2120 #ifdef WLAN_FEATURE_11BE_MLO
2121 /*
2122  * Multi link IE field offsets
2123  *  ------------------------------------------------------------------------
2124  * | EID(1) | Len (1) | EID_EXT (1) | ML_CONTROL (2) | CMN_INFO (var) | ... |
2125  *  ------------------------------------------------------------------------
2126  */
2127 #define ML_CONTROL_OFFSET 3
2128 #define ML_CMN_INFO_OFFSET ML_CONTROL_OFFSET + 2
2129 
2130 #define CMN_INFO_LINK_ID_PRESENT_BIT      BIT(4)
2131 #define LINK_INFO_MAC_ADDR_PRESENT_BIT    BIT(5)
2132 
2133 /* This function is implemented as per IEEE802.11be D1.0, there is no difference
2134  * in presence bitmap for beacon, probe response and probe request frames.
2135  * This code is to be revisited for future drafts if the presence bitmap values
2136  * changes for the beacon, probe response and probe request frames.
2137  */
2138 static uint8_t util_get_link_info_offset(uint8_t *ml_ie, bool *is_ml_ie_valid)
2139 {
2140 	qdf_size_t ml_ie_len = 0;
2141 	qdf_size_t parsed_ie_len = 0;
2142 	struct wlan_ie_multilink *mlie_fixed;
2143 	uint16_t mlcontrol;
2144 	uint16_t presencebm;
2145 	qdf_size_t actual_len;
2146 
2147 	if (!ml_ie) {
2148 		scm_err("ml_ie is null");
2149 		return 0;
2150 	}
2151 
2152 	if (!is_ml_ie_valid) {
2153 		scm_err_rl("is_ml_ie_valid is null");
2154 		return 0;
2155 	}
2156 
2157 	ml_ie_len = ml_ie[TAG_LEN_POS];
2158 	if (!ml_ie_len) {
2159 		scm_err("ml_ie_len is zero");
2160 		return 0;
2161 	}
2162 
2163 	if (ml_ie_len < sizeof(struct wlan_ie_multilink)) {
2164 		scm_err_rl("Length %zu octets is smaller than required for the fixed portion of Multi-Link element (%zu octets)",
2165 			   ml_ie_len, sizeof(struct wlan_ie_multilink));
2166 		return 0;
2167 	}
2168 
2169 	mlie_fixed = (struct wlan_ie_multilink *)ml_ie;
2170 	mlcontrol = le16toh(mlie_fixed->mlcontrol);
2171 	presencebm = QDF_GET_BITS(mlcontrol, WLAN_ML_CTRL_PBM_IDX,
2172 				  WLAN_ML_CTRL_PBM_BITS);
2173 
2174 	parsed_ie_len += sizeof(*mlie_fixed);
2175 
2176 	parsed_ie_len += WLAN_ML_BV_CINFO_LENGTH_SIZE;
2177 	parsed_ie_len += QDF_MAC_ADDR_SIZE;
2178 
2179 	/* Check if Link ID info is present */
2180 	if (presencebm & WLAN_ML_BV_CTRL_PBM_LINKIDINFO_P)
2181 		parsed_ie_len += WLAN_ML_BV_CINFO_LINKIDINFO_SIZE;
2182 
2183 	/* Check if BSS parameter change count is present */
2184 	if (presencebm & WLAN_ML_BV_CTRL_PBM_BSSPARAMCHANGECNT_P)
2185 		parsed_ie_len += WLAN_ML_BSSPARAMCHNGCNT_SIZE;
2186 
2187 	/* Check if Medium Sync Delay Info is present */
2188 	if (presencebm & WLAN_ML_BV_CTRL_PBM_MEDIUMSYNCDELAYINFO_P)
2189 		parsed_ie_len += WLAN_ML_BV_CINFO_MEDMSYNCDELAYINFO_SIZE;
2190 
2191 	/* Check if EML cap is present */
2192 	if (presencebm & WLAN_ML_BV_CTRL_PBM_EMLCAP_P)
2193 		parsed_ie_len += WLAN_ML_BV_CINFO_EMLCAP_SIZE;
2194 
2195 	/* Check if MLD cap and op is present */
2196 	if (presencebm & WLAN_ML_BV_CTRL_PBM_MLDCAPANDOP_P)
2197 		parsed_ie_len += WLAN_ML_BV_CINFO_MLDCAPANDOP_SIZE;
2198 
2199 	/* Check if MLD ID is present */
2200 	if (presencebm & WLAN_ML_BV_CTRL_PBM_MLDID_P)
2201 		parsed_ie_len += WLAN_ML_BV_CINFO_MLDID_SIZE;
2202 
2203 	/* Check if Extended MLD Cap and Op is present */
2204 	if (presencebm & WLAN_ML_BV_CTRL_PBM_EXT_MLDCAPANDOP_P)
2205 		parsed_ie_len += WLAN_ML_BV_CINFO_EXT_MLDCAPANDOP_SIZE;
2206 
2207 	/* Offset calculation starts from the beginning of the ML IE (including
2208 	 * EID) hence, adding the size of IE header to ML IE length.
2209 	 */
2210 	actual_len = ml_ie_len + sizeof(struct ie_header);
2211 	if (parsed_ie_len <= actual_len) {
2212 		*is_ml_ie_valid = true;
2213 	} else {
2214 		*is_ml_ie_valid = false;
2215 		scm_err("Invalid ML IE, expect min len: %zu, actual len: %zu",
2216 			parsed_ie_len, actual_len);
2217 	}
2218 	if (parsed_ie_len < actual_len)
2219 		return parsed_ie_len;
2220 
2221 	return 0;
2222 }
2223 
2224 static void
2225 util_get_ml_bv_partner_link_info(struct wlan_objmgr_pdev *pdev,
2226 				 struct scan_cache_entry *scan_entry)
2227 {
2228 	uint8_t *ml_ie = scan_entry->ie_list.multi_link_bv;
2229 	uint8_t *end_ptr = NULL;
2230 	bool is_ml_ie_valid;
2231 	uint8_t offset = util_get_link_info_offset(ml_ie, &is_ml_ie_valid);
2232 	uint16_t sta_ctrl;
2233 	uint8_t *stactrl_offset = NULL, *ielist_offset;
2234 	uint8_t perstaprof_len = 0, perstaprof_stainfo_len = 0, ielist_len = 0;
2235 	struct partner_link_info *link_info = NULL;
2236 	uint8_t eid = 0;
2237 	uint8_t link_idx = 0;
2238 	uint8_t rnr_idx = 0;
2239 	struct rnr_bss_info *rnr = NULL;
2240 	qdf_size_t ml_ie_len = ml_ie[TAG_LEN_POS] + sizeof(struct ie_header);
2241 	uint16_t freq;
2242 	struct scan_cache_entry *tmp_entry;
2243 	struct qdf_mac_addr bcast_addr = QDF_MAC_ADDR_BCAST_INIT;
2244 	struct scan_mbssid_info *mbssid;
2245 
2246 	/* Update partner info  from RNR IE */
2247 	while ((rnr_idx < MAX_RNR_BSS) && (rnr_idx < scan_entry->rnr.count)) {
2248 		if (link_idx >= (MLD_MAX_LINKS - 1))
2249 			break;
2250 		rnr = &scan_entry->rnr.bss_info[rnr_idx];
2251 		if (rnr->mld_info_valid && !rnr->mld_info.mld_id) {
2252 			mbssid = &scan_entry->mbssid_info;
2253 			freq =
2254 			     wlan_reg_chan_opclass_to_freq(rnr->channel_number,
2255 							   rnr->operating_class,
2256 							   true);
2257 
2258 			if ((!scan_entry->mbssid_info.profile_count) &&
2259 			    !(rnr->bss_params & TBTT_BSS_PARAM_TRANS_BSSID_BIT)) {
2260 				tmp_entry =
2261 				       scm_scan_get_scan_entry_by_mac_freq(pdev,
2262 							     &rnr->bssid, freq);
2263 				if (tmp_entry) {
2264 					qdf_mem_copy(mbssid,
2265 						     &tmp_entry->mbssid_info,
2266 						     sizeof(*mbssid));
2267 					util_scan_free_cache_entry(tmp_entry);
2268 				} else {
2269 					qdf_mem_copy(mbssid->non_trans_bssid,
2270 						     rnr->bssid.bytes,
2271 						     QDF_MAC_ADDR_SIZE);
2272 					qdf_mem_copy(mbssid->trans_bssid,
2273 						     bcast_addr.bytes,
2274 						     QDF_MAC_ADDR_SIZE);
2275 				}
2276 			}
2277 
2278 			link_info = &scan_entry->ml_info.link_info[link_idx];
2279 			qdf_mem_copy(&link_info->link_addr,
2280 				     &rnr->bssid, QDF_MAC_ADDR_SIZE);
2281 
2282 			link_info->link_id = rnr->mld_info.link_id;
2283 			link_info->freq = freq;
2284 
2285 			if (!link_info->freq)
2286 				scm_debug("freq 0 rnr channel %u op_class %u",
2287 					  rnr->channel_number,
2288 					  rnr->operating_class);
2289 			link_info->op_class = rnr->operating_class;
2290 			link_idx++;
2291 		}
2292 		rnr_idx++;
2293 	}
2294 
2295 	scan_entry->ml_info.num_links = link_idx;
2296 	if (!offset ||
2297 	    (offset + sizeof(struct wlan_ml_bv_linfo_perstaprof) >= ml_ie_len)) {
2298 		scm_err_rl("incorrect offset value %d", offset);
2299 		return;
2300 	}
2301 
2302 	/* TODO: loop through all the STA info fields */
2303 
2304 	/*
2305 	 * Per-STA Profile subelement format of the Basic Multi-Link element
2306 	 *
2307 	 * |---------------|--------|-------------|----------|-------------|
2308 	 * | Subelement ID | Length | STA control | STA info | STA profile |
2309 	 * |---------------|--------|-------------|----------|-------------|
2310 	 * Octets:  1         1           2         variable     variable
2311 	 */
2312 
2313 	/* Sub element ID 0 represents Per-STA Profile */
2314 	if (ml_ie[offset] == 0) {
2315 		perstaprof_len = ml_ie[offset + 1];
2316 		stactrl_offset = &ml_ie[offset + 2];
2317 		end_ptr = &ml_ie[offset] + perstaprof_len + 2;
2318 
2319 		if (!(end_ptr <= (ml_ie + ml_ie_len))) {
2320 			if (ml_ie[TAG_LEN_POS] >= 255)
2321 				scm_debug("Possible fragmentation in ml_ie. Ignore the processing");
2322 			else
2323 				scm_debug("perstaprof exceeds ML IE boundary. Ignore the processing");
2324 			return;
2325 		}
2326 
2327 		/* Skip sub element ID and length fields */
2328 		offset += 2;
2329 
2330 		sta_ctrl = *(uint16_t *)(ml_ie + offset);
2331 
2332 		/* Skip STA control field */
2333 		offset += 2;
2334 
2335 		/*
2336 		 * offset points to the beginning of the STA Info field which
2337 		 * indicates the number of octets in the STA Info field,
2338 		 * including one octet for the STA Info Length subfield.
2339 		 */
2340 		perstaprof_stainfo_len = ml_ie[offset];
2341 
2342 		/* Skip STA Info Length field */
2343 		offset += perstaprof_stainfo_len;
2344 		if (offset >= ml_ie_len) {
2345 			scm_err_rl("incorrect offset value %d", offset);
2346 			return;
2347 		}
2348 
2349 		/*
2350 		 * To point to the ie_list offset move past the STA Info
2351 		 * field.
2352 		 */
2353 		ielist_offset = &ml_ie[offset];
2354 
2355 		/*
2356 		 * Ensure that the STA Control Field + STA Info Field
2357 		 * is smaller than the per-STA profile when incrementing
2358 		 * the pointer to avoid underflow during subtraction.
2359 		 */
2360 		if ((perstaprof_stainfo_len +
2361 		     WLAN_ML_BV_LINFO_PERSTAPROF_STACTRL_SIZE) <
2362 							perstaprof_len) {
2363 			if (!(ielist_offset <= end_ptr))
2364 				ielist_len = 0;
2365 			else
2366 				ielist_len = perstaprof_len -
2367 					(WLAN_ML_BV_LINFO_PERSTAPROF_STACTRL_SIZE +
2368 					 perstaprof_stainfo_len);
2369 		} else {
2370 			scm_debug("No STA profile IE list found");
2371 			ielist_len = 0;
2372 		}
2373 
2374 		link_info = NULL;
2375 		for (link_idx = 0; link_idx < scan_entry->ml_info.num_links;
2376 		     link_idx++) {
2377 			if (scan_entry->ml_info.link_info[link_idx].link_id ==
2378 							(sta_ctrl & 0xF)) {
2379 				link_info = &scan_entry->ml_info.link_info[link_idx];
2380 			}
2381                 }
2382 
2383 		/* Get the pointers to CSA, ECSA, Max Channel Switch Time IE. */
2384 		if (link_info) {
2385 			link_info->csa_ie = wlan_get_ie_ptr_from_eid
2386 				(WLAN_ELEMID_CHANSWITCHANN, ielist_offset,
2387 				 ielist_len);
2388 
2389 			link_info->ecsa_ie = wlan_get_ie_ptr_from_eid
2390 				(WLAN_ELEMID_EXTCHANSWITCHANN, ielist_offset,
2391 				 ielist_len);
2392 
2393 			eid = WLAN_EXTN_ELEMID_MAX_CHAN_SWITCH_TIME;
2394 			link_info->max_cst_ie = wlan_get_ext_ie_ptr_from_ext_id
2395 					(&eid, 1, ielist_offset, ielist_len);
2396 		}
2397 	}
2398 }
2399 
2400 static void util_scan_update_ml_info(struct wlan_objmgr_pdev *pdev,
2401 				     struct scan_cache_entry *scan_entry)
2402 {
2403 	uint8_t *ml_ie = scan_entry->ie_list.multi_link_bv;
2404 	uint16_t multi_link_ctrl;
2405 	uint8_t offset;
2406 	uint8_t mlie_min_len;
2407 	bool is_ml_ie_valid = true;
2408 	uint8_t *end_ptr = NULL;
2409 
2410 	if (!scan_entry->ie_list.ehtcap && scan_entry->ie_list.multi_link_bv) {
2411 		scan_entry->ie_list.multi_link_bv = NULL;
2412 		return;
2413 	}
2414 	if (!scan_entry->ie_list.multi_link_bv)
2415 		return;
2416 
2417 	mlie_min_len = util_get_link_info_offset(ml_ie, &is_ml_ie_valid);
2418 	if (!is_ml_ie_valid) {
2419 		scan_entry->ie_list.multi_link_bv = NULL;
2420 		return;
2421 	}
2422 
2423 	end_ptr = ml_ie + ml_ie[TAG_LEN_POS] + sizeof(struct ie_header);
2424 
2425 	multi_link_ctrl = *(uint16_t *)(ml_ie + ML_CONTROL_OFFSET);
2426 
2427 	/* TODO: update ml_info based on ML IE */
2428 
2429 	offset = ML_CMN_INFO_OFFSET;
2430 
2431 	/* Increment the offset to account for the Common Info Length */
2432 	offset += WLAN_ML_BV_CINFO_LENGTH_SIZE;
2433 
2434 	if ((ml_ie + offset + QDF_MAC_ADDR_SIZE) <= end_ptr) {
2435 		qdf_mem_copy(&scan_entry->ml_info.mld_mac_addr,
2436 			     ml_ie + offset, QDF_MAC_ADDR_SIZE);
2437 		offset += QDF_MAC_ADDR_SIZE;
2438 	}
2439 
2440 	/* TODO: Decode it from ML IE */
2441 	scan_entry->ml_info.num_links = 0;
2442 
2443 	/**
2444 	 * Copy Link ID & MAC address of the scan cache entry as first entry
2445 	 * in the partner info list
2446 	 */
2447 	if (multi_link_ctrl & CMN_INFO_LINK_ID_PRESENT_BIT) {
2448 		if (&ml_ie[offset] < end_ptr)
2449 			scan_entry->ml_info.self_link_id = ml_ie[offset] & 0x0F;
2450 	}
2451 
2452 	util_get_ml_bv_partner_link_info(pdev, scan_entry);
2453 }
2454 #else
2455 static void util_scan_update_ml_info(struct wlan_objmgr_pdev *pdev,
2456 				     struct scan_cache_entry *scan_entry)
2457 {
2458 }
2459 #endif
2460 
2461 static QDF_STATUS
2462 util_scan_gen_scan_entry(struct wlan_objmgr_pdev *pdev,
2463 			 uint8_t *frame, qdf_size_t frame_len,
2464 			 uint32_t frm_subtype,
2465 			 struct mgmt_rx_event_params *rx_param,
2466 			 struct scan_mbssid_info *mbssid_info,
2467 			 qdf_list_t *scan_list)
2468 {
2469 	struct wlan_frame_hdr *hdr;
2470 	struct wlan_bcn_frame *bcn;
2471 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2472 	struct ie_ssid *ssid;
2473 	struct scan_cache_entry *scan_entry;
2474 	struct qbss_load_ie *qbss_load;
2475 	struct scan_cache_node *scan_node;
2476 	uint8_t i;
2477 	qdf_freq_t chan_freq = 0;
2478 	bool is_6g_dup_bcon = false;
2479 	uint8_t band_mask;
2480 	qdf_freq_t recv_freq = 0;
2481 
2482 	scan_entry = qdf_mem_malloc_atomic(sizeof(*scan_entry));
2483 	if (!scan_entry) {
2484 		scm_err("failed to allocate memory for scan_entry");
2485 		return QDF_STATUS_E_NOMEM;
2486 	}
2487 
2488 	scan_entry->raw_frame.ptr =
2489 			qdf_mem_malloc_atomic(frame_len);
2490 	if (!scan_entry->raw_frame.ptr) {
2491 		scm_err("failed to allocate memory for frame");
2492 		qdf_mem_free(scan_entry);
2493 		return QDF_STATUS_E_NOMEM;
2494 	}
2495 
2496 	bcn = (struct wlan_bcn_frame *)
2497 			   (frame + sizeof(*hdr));
2498 	hdr = (struct wlan_frame_hdr *)frame;
2499 
2500 	/* update timestamp in nanoseconds needed by kernel layers */
2501 	scan_entry->boottime_ns = qdf_get_bootbased_boottime_ns();
2502 
2503 	scan_entry->frm_subtype = frm_subtype;
2504 	qdf_mem_copy(scan_entry->bssid.bytes,
2505 		hdr->i_addr3, QDF_MAC_ADDR_SIZE);
2506 	/* Scr addr */
2507 	qdf_mem_copy(scan_entry->mac_addr.bytes,
2508 		hdr->i_addr2, QDF_MAC_ADDR_SIZE);
2509 	scan_entry->seq_num =
2510 		(le16toh(*(uint16_t *)hdr->i_seq) >> WLAN_SEQ_SEQ_SHIFT);
2511 
2512 	scan_entry->snr = rx_param->snr;
2513 	scan_entry->avg_snr = WLAN_SNR_IN(scan_entry->snr);
2514 	scan_entry->rssi_raw = rx_param->rssi;
2515 	scan_entry->avg_rssi = WLAN_RSSI_IN(scan_entry->rssi_raw);
2516 	scan_entry->tsf_delta = rx_param->tsf_delta;
2517 	scan_entry->pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
2518 
2519 	recv_freq = rx_param->chan_freq;
2520 	/* Copy per chain rssi to scan entry */
2521 	qdf_mem_copy(scan_entry->per_chain_rssi, rx_param->rssi_ctl,
2522 		     WLAN_MGMT_TXRX_HOST_MAX_ANTENNA);
2523 	band_mask = BIT(wlan_reg_freq_to_band(recv_freq));
2524 
2525 	if (!wlan_psoc_nif_fw_ext_cap_get(wlan_pdev_get_psoc(pdev),
2526 					  WLAN_SOC_CEXT_HW_DB2DBM)) {
2527 		for (i = 0; i < WLAN_MGMT_TXRX_HOST_MAX_ANTENNA; i++) {
2528 			if (scan_entry->per_chain_rssi[i] !=
2529 			    WLAN_INVALID_PER_CHAIN_SNR)
2530 				scan_entry->per_chain_rssi[i] +=
2531 						WLAN_NOISE_FLOOR_DBM_DEFAULT;
2532 			else
2533 				scan_entry->per_chain_rssi[i] =
2534 						WLAN_INVALID_PER_CHAIN_RSSI;
2535 		}
2536 	}
2537 
2538 	/* store jiffies */
2539 	scan_entry->rrm_parent_tsf = (uint32_t)qdf_system_ticks();
2540 
2541 	scan_entry->bcn_int = le16toh(bcn->beacon_interval);
2542 
2543 	/*
2544 	 * In case if the beacon doesn't have
2545 	 * valid beacon interval falback to def
2546 	 */
2547 	if (!scan_entry->bcn_int)
2548 		scan_entry->bcn_int = 100;
2549 	scan_entry->cap_info.value = le16toh(bcn->capability.value);
2550 	qdf_mem_copy(scan_entry->tsf_info.data,
2551 		bcn->timestamp, 8);
2552 	scan_entry->erp = ERP_NON_ERP_PRESENT;
2553 
2554 	scan_entry->scan_entry_time =
2555 		qdf_mc_timer_get_system_time();
2556 
2557 	scan_entry->raw_frame.len = frame_len;
2558 	qdf_mem_copy(scan_entry->raw_frame.ptr,
2559 		frame, frame_len);
2560 	status = util_scan_populate_bcn_ie_list(pdev, scan_entry, &chan_freq,
2561 						band_mask);
2562 	if (QDF_IS_STATUS_ERROR(status)) {
2563 		qdf_mem_free(scan_entry->raw_frame.ptr);
2564 		qdf_mem_free(scan_entry);
2565 		return QDF_STATUS_E_FAILURE;
2566 	}
2567 
2568 	ssid = (struct ie_ssid *)
2569 		scan_entry->ie_list.ssid;
2570 
2571 	if (ssid && (ssid->ssid_len > WLAN_SSID_MAX_LEN)) {
2572 		qdf_mem_free(scan_entry->raw_frame.ptr);
2573 		qdf_mem_free(scan_entry);
2574 		return QDF_STATUS_E_FAILURE;
2575 	}
2576 
2577 	if (scan_entry->ie_list.p2p)
2578 		scan_entry->is_p2p = true;
2579 
2580 	if (!chan_freq && util_scan_entry_hecap(scan_entry)) {
2581 		status = util_scan_get_chan_from_he_6g_params(pdev, scan_entry,
2582 							      &chan_freq,
2583 							      &is_6g_dup_bcon,
2584 							      band_mask,
2585 							      recv_freq);
2586 		if (QDF_IS_STATUS_ERROR(status)) {
2587 			qdf_mem_free(scan_entry->raw_frame.ptr);
2588 			qdf_mem_free(scan_entry);
2589 			return QDF_STATUS_E_FAILURE;
2590 		}
2591 	}
2592 
2593 	if (chan_freq)
2594 		scan_entry->channel.chan_freq = chan_freq;
2595 
2596 	/* If no channel info is present in beacon use meta channel */
2597 	if (!scan_entry->channel.chan_freq) {
2598 		scan_entry->channel.chan_freq = recv_freq;
2599 	} else if (recv_freq !=
2600 	   scan_entry->channel.chan_freq) {
2601 		if (!wlan_reg_is_49ghz_freq(scan_entry->channel.chan_freq) &&
2602 		    !is_6g_dup_bcon)
2603 			scan_entry->channel_mismatch = true;
2604 	}
2605 
2606 	if (util_scan_is_hidden_ssid(ssid)) {
2607 		scan_entry->ie_list.ssid = NULL;
2608 		scan_entry->is_hidden_ssid = true;
2609 	} else {
2610 		qdf_mem_copy(scan_entry->ssid.ssid,
2611 				ssid->ssid, ssid->ssid_len);
2612 		scan_entry->ssid.length = ssid->ssid_len;
2613 		scan_entry->hidden_ssid_timestamp =
2614 			scan_entry->scan_entry_time;
2615 	}
2616 	qdf_mem_copy(&scan_entry->mbssid_info, mbssid_info,
2617 		     sizeof(scan_entry->mbssid_info));
2618 
2619 	scan_entry->phy_mode = util_scan_get_phymode(pdev, scan_entry);
2620 
2621 	scan_entry->nss = util_scan_scm_calc_nss_supported_by_ap(scan_entry);
2622 	scm_fill_adaptive_11r_cap(scan_entry);
2623 	util_scan_set_security(scan_entry);
2624 
2625 	util_scan_scm_update_bss_with_esp_data(scan_entry);
2626 	qbss_load = (struct qbss_load_ie *)
2627 			util_scan_entry_qbssload(scan_entry);
2628 	if (qbss_load)
2629 		scan_entry->qbss_chan_load = qbss_load->qbss_chan_load;
2630 
2631 	scan_node = qdf_mem_malloc_atomic(sizeof(*scan_node));
2632 	if (!scan_node) {
2633 		qdf_mem_free(scan_entry->raw_frame.ptr);
2634 		qdf_mem_free(scan_entry);
2635 		scm_err("failed to allocate memory for scan_node");
2636 		return QDF_STATUS_E_FAILURE;
2637 	}
2638 
2639 	util_scan_update_ml_info(pdev, scan_entry);
2640 
2641 	scan_node->entry = scan_entry;
2642 	qdf_list_insert_front(scan_list, &scan_node->node);
2643 
2644 	return status;
2645 }
2646 
2647 #ifdef WLAN_FEATURE_MBSSID
2648 /*
2649  * util_is_noninh_ie() - find the noninhertance information element
2650  * in the received frame's IE list, so that we can stop inheriting that IE
2651  * in the caller function.
2652  *
2653  * @elem_id: Element ID in the received frame's IE, which is being processed.
2654  * @non_inh_list: pointer to the non inherited list of element IDs or
2655  *                list of extension element IDs.
2656  * @len: Length of non inheritance IE list
2657  *
2658  * Return: False if the element ID is not found or else return true
2659  */
2660 static bool util_is_noninh_ie(uint8_t elem_id,
2661 			      uint8_t *non_inh_list,
2662 			      int8_t len)
2663 {
2664 	int count;
2665 
2666 	for (count = 0; count < len; count++) {
2667 		if (elem_id == non_inh_list[count])
2668 			return true;
2669 	}
2670 
2671 	return false;
2672 }
2673 
2674 /*
2675  * util_scan_find_noninheritance_ie() - find noninheritance information element
2676  * This block of code is to identify if there is any non-inheritance element
2677  * present as part of the nontransmitted BSSID profile.
2678  * @elem_id: element id
2679  * @ies: pointer consisting of IEs
2680  * @len: IE length
2681  *
2682  * Return: NULL if the element ID is not found or if IE pointer is NULL else
2683  * pointer to the first byte of the requested element
2684  */
2685 static uint8_t
2686 *util_scan_find_noninheritance_ie(uint8_t elem_id, uint8_t *ies,
2687 				  int32_t len)
2688 {
2689 	if (!ies)
2690 		return NULL;
2691 
2692 	while (len >= MIN_IE_LEN && len >= ies[TAG_LEN_POS] + MIN_IE_LEN) {
2693 		if ((ies[ID_POS] == elem_id) &&
2694 		    (ies[ELEM_ID_EXTN_POS] ==
2695 		     WLAN_EXTN_ELEMID_NONINHERITANCE)) {
2696 			return ies;
2697 		}
2698 		len -= ies[TAG_LEN_POS] + MIN_IE_LEN;
2699 		ies += ies[TAG_LEN_POS] + MIN_IE_LEN;
2700 	}
2701 
2702 	return NULL;
2703 }
2704 #endif
2705 
2706 /*
2707  * util_scan_find_ie() - find information element
2708  * @eid: element id
2709  * @ies: pointer consisting of IEs
2710  * @len: IE length
2711  *
2712  * Return: NULL if the element ID is not found or if IE pointer is NULL else
2713  * pointer to the first byte of the requested element
2714  */
2715 static uint8_t *util_scan_find_ie(uint8_t eid, uint8_t *ies,
2716 				  int32_t len)
2717 {
2718 	if (!ies)
2719 		return NULL;
2720 
2721 	while (len >= 2 && len >= ies[1] + 2) {
2722 		if (ies[0] == eid)
2723 			return ies;
2724 		len -= ies[1] + 2;
2725 		ies += ies[1] + 2;
2726 	}
2727 
2728 	return NULL;
2729 }
2730 
2731 #ifdef WLAN_FEATURE_MBSSID
2732 static void util_gen_new_bssid(uint8_t *bssid, uint8_t max_bssid,
2733 			       uint8_t mbssid_index,
2734 			       uint8_t *new_bssid_addr)
2735 {
2736 	uint8_t lsb_n;
2737 	int i;
2738 
2739 	for (i = 0; i < QDF_MAC_ADDR_SIZE; i++)
2740 		new_bssid_addr[i] = bssid[i];
2741 
2742 	lsb_n = new_bssid_addr[5] & ((1 << max_bssid) - 1);
2743 
2744 	new_bssid_addr[5] &= ~((1 << max_bssid) - 1);
2745 	new_bssid_addr[5] |= (lsb_n + mbssid_index) % (1 << max_bssid);
2746 }
2747 
2748 /*
2749  * util_parse_noninheritance_list() - This block of code will be executed only
2750  * if there is a valid non inheritance IE present in the nontx profile.
2751  * Host need not inherit those list of element IDs and list of element ID
2752  * extensions from the transmitted BSSID profile.
2753  * Since non-inheritance element is an element ID extension, it should
2754  * be part of extension element. So first we need to find if there are
2755  * any extension element present in the nontransmitted BSSID profile.
2756  * @extn_elem: If valid, it points to the element ID field of
2757  * extension element tag in the nontransmitted BSSID profile.
2758  * It may or may not have non inheritance tag present.
2759  *      _____________________________________________
2760  *     |         |       |       |List of|List of    |
2761  *     | Element |Length |Element|Element|Element ID |
2762  *     |  ID     |       |ID extn| IDs   |Extension  |
2763  *     |_________|_______|_______|_______|___________|
2764  * List of Element IDs:
2765  *      __________________
2766  *     |         |        |
2767  *     |  Length |Element |
2768  *     |         |ID List |
2769  *     |_________|________|
2770  * List of Element ID Extensions:
2771  *      __________________________
2772  *     |         |                |
2773  *     |  Length |Element ID      |
2774  *     |         |extension List  |
2775  *     |_________|________________|
2776  * @elem_list: Element ID list
2777  * @extn_elem_list: Element ID exiension list
2778  * @non_inheritance_ie: Non inheritance IE information
2779  */
2780 
2781 static void util_parse_noninheritance_list(uint8_t *extn_elem,
2782 					   uint8_t **elem_list,
2783 					   uint8_t **extn_elem_list,
2784 					   struct non_inheritance_ie *ninh)
2785 {
2786 	int8_t extn_rem_len = 0;
2787 
2788 	if (extn_elem[ELEM_ID_LIST_LEN_POS] < extn_elem[TAG_LEN_POS]) {
2789 		/*
2790 		 * extn_rem_len represents the number of bytes after
2791 		 * the length subfield of list of Element IDs.
2792 		 * So here, extn_rem_len should be equal to
2793 		 * Element ID list + Length subfield of Element ID
2794 		 * extension list + Element ID extension list.
2795 		 *
2796 		 * Here we have taken two pointers pointing to the
2797 		 * element ID list and element ID extension list
2798 		 * which we will use to detect the same elements
2799 		 * in the transmitted BSSID profile and choose not
2800 		 * to inherit those elements while constructing the
2801 		 * frame for nontransmitted BSSID profile.
2802 		 */
2803 		extn_rem_len = extn_elem[TAG_LEN_POS] - MIN_IE_LEN;
2804 		ninh->non_inherit = true;
2805 
2806 		if (extn_rem_len && extn_elem[ELEM_ID_LIST_LEN_POS]) {
2807 			if (extn_rem_len >= extn_elem[ELEM_ID_LIST_LEN_POS]) {
2808 				ninh->list_len =
2809 					extn_elem[ELEM_ID_LIST_LEN_POS];
2810 				*elem_list = extn_elem + ELEM_ID_LIST_POS;
2811 				extn_rem_len -= ninh->list_len;
2812 			} else {
2813 				/*
2814 				 * Corrupt frame. length subfield of
2815 				 * element ID list is greater than
2816 				 * what it should be. Go ahead with
2817 				 * frame generation but do not honour
2818 				 * the non inheritance part. Also, mark
2819 				 * the element ID in subcopy as 0, so
2820 				 * that this element info will not
2821 				 * be copied.
2822 				 */
2823 				ninh->non_inherit = false;
2824 				extn_elem[0] = 0;
2825 			}
2826 		}
2827 
2828 		extn_rem_len--;
2829 		if (extn_rem_len > 0) {
2830 			if (!ninh->list_len) {
2831 				ninh->extn_len =
2832 					extn_elem[ELEM_ID_LIST_LEN_POS + 1];
2833 			} else {
2834 				ninh->extn_len =
2835 					extn_elem[ELEM_ID_LIST_POS +
2836 					ninh->list_len];
2837 			}
2838 
2839 			if (extn_rem_len != ninh->extn_len) {
2840 				/*
2841 				 * Corrupt frame. length subfield of
2842 				 * element ID extn list is not
2843 				 * what it should be. Go ahead with
2844 				 * frame generation but do not honour
2845 				 * the non inheritance part. Also, mark
2846 				 * the element ID in subcopy as 0, so
2847 				 * that this element info will not
2848 				 * be copied.
2849 				 */
2850 				ninh->non_inherit = false;
2851 				extn_elem[0] = 0;
2852 			}
2853 
2854 			if (ninh->extn_len) {
2855 				*extn_elem_list =
2856 					(extn_elem + ninh->list_len +
2857 					 ELEM_ID_LIST_POS + 1);
2858 			}
2859 		}
2860 	}
2861 }
2862 
2863 #ifdef WLAN_FEATURE_11BE_MLO
2864 /**
2865  * util_handle_rnr_ie_for_mbssid() - parse and modify RNR IE for MBSSID feature
2866  * @rnr: The pointer to RNR IE
2867  * @bssid_index: BSSID index from MBSSID index IE
2868  * @pos: The buffer pointer to save the transformed RNR IE, caller is expected
2869  *       to supply a buffer that is at least as big as @rnr
2870  *
2871  * Per the description about Neighbor AP Information field about MLD
2872  * parameters subfield in section 9.4.2.170.2 of Draft P802.11be_D1.4.
2873  * If the reported AP is affiliated with the same MLD of the reporting AP,
2874  * the TBTT information is skipped; If the reported AP is affiliated with
2875  * the same MLD of the nontransmitted BSSID, the TBTT information is
2876  * copied and the MLD ID is changed to 0.
2877  *
2878  * Return: Length of the element written to @pos
2879  */
2880 static int util_handle_rnr_ie_for_mbssid(const uint8_t *rnr,
2881 					 uint8_t bssid_index, uint8_t *pos)
2882 {
2883 	size_t rnr_len;
2884 	const uint8_t *data, *rnr_end;
2885 	uint8_t *rnr_new;
2886 	struct neighbor_ap_info_field *neighbor_ap_info;
2887 	struct rnr_mld_info *mld_param;
2888 	uint8_t tbtt_type, tbtt_len, tbtt_count;
2889 	uint8_t mld_pos, mld_id;
2890 	int32_t i, copy_len;
2891 	/* The count of TBTT info field whose MLD ID equals to 0 in a neighbor
2892 	 * AP information field.
2893 	 */
2894 	uint32_t tbtt_info_field_count;
2895 	/* The total bytes of TBTT info fields whose MLD ID equals to 0 in
2896 	 * current RNR IE.
2897 	 */
2898 	uint32_t tbtt_info_field_len = 0;
2899 	uint8_t nbr_ap_info_len = sizeof(struct neighbor_ap_info_field);
2900 
2901 	rnr_len = rnr[TAG_LEN_POS];
2902 	rnr_end = rnr + rnr_len + MIN_IE_LEN;
2903 	rnr_new = pos;
2904 	qdf_mem_copy(pos, rnr, MIN_IE_LEN);
2905 	pos += MIN_IE_LEN;
2906 
2907 	data = rnr + PAYLOAD_START_POS;
2908 	while (data < rnr_end) {
2909 		neighbor_ap_info = (struct neighbor_ap_info_field *)data;
2910 		tbtt_count = neighbor_ap_info->tbtt_header.tbtt_info_count;
2911 		tbtt_len = neighbor_ap_info->tbtt_header.tbtt_info_length;
2912 		tbtt_type = neighbor_ap_info->tbtt_header.tbbt_info_fieldtype;
2913 		scm_debug("channel number %d, op class %d, bssid_index %d",
2914 			  neighbor_ap_info->channel_number,
2915 			  neighbor_ap_info->operting_class, bssid_index);
2916 		scm_debug("tbtt_count %d, tbtt_length %d, tbtt_type %d",
2917 			  tbtt_count, tbtt_len, tbtt_type);
2918 
2919 		copy_len = tbtt_len * (tbtt_count + 1) +
2920 			   nbr_ap_info_len;
2921 		if (data + copy_len > rnr_end)
2922 			return 0;
2923 
2924 		if (tbtt_len >=
2925 		    TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM_20MHZ_PSD_MLD_PARAM)
2926 			mld_pos =
2927 			     TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM_20MHZ_PSD;
2928 		else
2929 			mld_pos = 0;
2930 
2931 		/* If MLD params do not exist, copy this neighbor AP
2932 		 * information field.
2933 		 * Per Draft P802.11be_D1.4, tbtt_type value 1, 2 and 3
2934 		 * are reserved,
2935 		 */
2936 		if (mld_pos == 0 || tbtt_type != 0) {
2937 			scm_debug("no MLD params, tbtt_type %d", tbtt_type);
2938 			qdf_mem_copy(pos, data, copy_len);
2939 			pos += copy_len;
2940 			data += copy_len;
2941 			continue;
2942 		}
2943 
2944 		qdf_mem_copy(pos, data, nbr_ap_info_len);
2945 		neighbor_ap_info = (struct neighbor_ap_info_field *)pos;
2946 		pos += nbr_ap_info_len;
2947 		data += nbr_ap_info_len;
2948 
2949 		tbtt_info_field_count = 0;
2950 		for (i = 0; i < tbtt_count + 1; i++) {
2951 			mld_param = (struct rnr_mld_info *)&data[mld_pos];
2952 			mld_id = mld_param->mld_id;
2953 
2954 			/* Refer to Draft P802.11be_D1.4
2955 			 * 9.4.2.170.2 Neighbor AP Information field about
2956 			 * MLD parameters subfield
2957 			 */
2958 			if (mld_id == 0) {
2959 				/* Skip this TBTT information since this
2960 				 * reported AP is affiliated with the same MLD
2961 				 * of the reporting AP who sending the frame
2962 				 * carrying this element.
2963 				 */
2964 				tbtt_info_field_len += tbtt_len;
2965 				data += tbtt_len;
2966 				tbtt_info_field_count++;
2967 			} else if (mld_id == bssid_index) {
2968 				/* Copy this TBTT information and change MLD
2969 				 * to 0 as this reported AP is affiliated with
2970 				 * the same MLD of the nontransmitted BSSID.
2971 				 */
2972 				qdf_mem_copy(pos, data, tbtt_len);
2973 				mld_param =
2974 					(struct rnr_mld_info *)&pos[mld_pos];
2975 				scm_debug("change MLD ID from %d to 0",
2976 					  mld_param->mld_id);
2977 				mld_param->mld_id = 0;
2978 				data += tbtt_len;
2979 				pos += tbtt_len;
2980 			} else {
2981 				qdf_mem_copy(pos, data, tbtt_len);
2982 				data += tbtt_len;
2983 				pos += tbtt_len;
2984 			}
2985 		}
2986 
2987 		scm_debug("skip %d neighbor info", tbtt_info_field_count);
2988 		if (tbtt_info_field_count == (tbtt_count + 1)) {
2989 			/* If all the TBTT information are skipped, then also
2990 			 * revert the neighbor AP info which has been copied.
2991 			 */
2992 			pos -= nbr_ap_info_len;
2993 			tbtt_info_field_len += nbr_ap_info_len;
2994 		} else {
2995 			neighbor_ap_info->tbtt_header.tbtt_info_count -=
2996 							tbtt_info_field_count;
2997 		}
2998 	}
2999 
3000 	rnr_new[TAG_LEN_POS] = rnr_len - tbtt_info_field_len;
3001 	if (rnr_new[TAG_LEN_POS] > 0)
3002 		rnr_len = rnr_new[TAG_LEN_POS] + MIN_IE_LEN;
3003 	else
3004 		rnr_len = 0;
3005 
3006 	return rnr_len;
3007 }
3008 #else
3009 static int util_handle_rnr_ie_for_mbssid(const uint8_t *rnr,
3010 					 uint8_t bssid_index, uint8_t *pos)
3011 {
3012 	return 0;
3013 }
3014 #endif
3015 
3016 #ifdef WLAN_FEATURE_ACTION_OUI
3017 static uint8_t *util_copy_reporting_ap_vendor_ies(struct wlan_objmgr_psoc *psoc,
3018 						  const uint8_t *ie,
3019 						  uint32_t ie_len,
3020 						  uint8_t *buf_ie)
3021 {
3022 	struct action_oui_search_attr attr;
3023 	enum action_oui_id oui_id = ACTION_OUI_RESTRICT_MAX_MLO_LINKS;
3024 
3025 	attr.ie_data = (uint8_t *)ie;
3026 	attr.ie_length = ie_len;
3027 
3028 	if (wlan_action_oui_search(psoc, &attr, oui_id)) {
3029 		qdf_mem_copy(buf_ie, ie, ie_len);
3030 		buf_ie += ie_len;
3031 	}
3032 
3033 	return buf_ie;
3034 }
3035 #else
3036 static inline uint8_t *
3037 util_copy_reporting_ap_vendor_ies(struct wlan_objmgr_psoc *psoc,
3038 				  const uint8_t *ie, uint32_t ie_len,
3039 				  uint8_t *buf_ie)
3040 {
3041 	return buf_ie;
3042 }
3043 #endif
3044 
3045 static uint32_t util_gen_new_ie(struct wlan_objmgr_pdev *pdev,
3046 				uint8_t *ie, uint32_t ielen,
3047 				uint8_t *subelement,
3048 				size_t subie_len, uint8_t *new_ie,
3049 				uint8_t bssid_index)
3050 {
3051 	struct wlan_objmgr_psoc *psoc;
3052 	uint8_t *pos, *tmp;
3053 	const uint8_t *tmp_old, *tmp_new;
3054 	uint8_t *sub_copy, *extn_elem = NULL;
3055 	struct non_inheritance_ie ninh = {0};
3056 	uint8_t *elem_list = NULL, *extn_elem_list = NULL;
3057 	size_t tmp_rem_len;
3058 
3059 	psoc = wlan_pdev_get_psoc(pdev);
3060 	if (!psoc) {
3061 		scm_err("NULL PSOC");
3062 		return 0;
3063 	}
3064 
3065 	/* copy subelement as we need to change its content to
3066 	 * mark an ie after it is processed.
3067 	 */
3068 	sub_copy = qdf_mem_malloc(subie_len);
3069 	if (!sub_copy)
3070 		return 0;
3071 	qdf_mem_copy(sub_copy, subelement, subie_len);
3072 
3073 	pos = &new_ie[0];
3074 
3075 	/* new ssid */
3076 	tmp_new = util_scan_find_ie(WLAN_ELEMID_SSID, sub_copy, subie_len);
3077 	if (tmp_new) {
3078 		scm_debug(" SSID " QDF_SSID_FMT,
3079 			  QDF_SSID_REF(tmp_new[1],
3080 				       &tmp_new[PAYLOAD_START_POS]));
3081 		if ((pos + tmp_new[1] + MIN_IE_LEN) <=
3082 		    (new_ie + ielen)) {
3083 			qdf_mem_copy(pos, tmp_new,
3084 				     (tmp_new[1] + MIN_IE_LEN));
3085 			pos += (tmp_new[1] + MIN_IE_LEN);
3086 		}
3087 	}
3088 
3089 	extn_elem = util_scan_find_noninheritance_ie(WLAN_ELEMID_EXTN_ELEM,
3090 						     sub_copy, subie_len);
3091 
3092 	if (extn_elem && extn_elem[TAG_LEN_POS]) {
3093 		util_parse_noninheritance_list(extn_elem, &elem_list,
3094 					       &extn_elem_list, &ninh);
3095 	}
3096 
3097 	/* go through IEs in ie (skip SSID) and subelement,
3098 	 * merge them into new_ie
3099 	 */
3100 	tmp_old = util_scan_find_ie(WLAN_ELEMID_SSID, ie, ielen);
3101 	tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + MIN_IE_LEN : ie;
3102 
3103 	while (((tmp_old + tmp_old[1] + MIN_IE_LEN) - ie) <= ielen) {
3104 		ninh.non_inh_ie_found = 0;
3105 		if (ninh.non_inherit) {
3106 			if (ninh.list_len) {
3107 				ninh.non_inh_ie_found =
3108 					util_is_noninh_ie(tmp_old[0],
3109 							  elem_list,
3110 							  ninh.list_len);
3111 			}
3112 
3113 			if (!ninh.non_inh_ie_found &&
3114 			    ninh.extn_len &&
3115 			    (tmp_old[0] == WLAN_ELEMID_EXTN_ELEM)) {
3116 				ninh.non_inh_ie_found =
3117 					util_is_noninh_ie(tmp_old[2],
3118 							  extn_elem_list,
3119 							  ninh.extn_len);
3120 			}
3121 		}
3122 
3123 		if (ninh.non_inh_ie_found || (tmp_old[0] == 0)) {
3124 			tmp_old += tmp_old[1] + MIN_IE_LEN;
3125 			continue;
3126 		}
3127 
3128 		tmp = (uint8_t *)util_scan_find_ie(tmp_old[0], sub_copy,
3129 						   subie_len);
3130 		if (!tmp) {
3131 			/* ie in old ie but not in subelement */
3132 			if (tmp_old[0] == WLAN_ELEMID_REDUCED_NEIGHBOR_REPORT) {
3133 				/* handle rnr ie for mbssid*/
3134 				pos +=
3135 				    util_handle_rnr_ie_for_mbssid(tmp_old,
3136 								  bssid_index,
3137 								  pos);
3138 			} else if (tmp_old[0] != WLAN_ELEMID_MULTIPLE_BSSID) {
3139 				if ((pos + tmp_old[1] + MIN_IE_LEN) <=
3140 				    (new_ie + ielen)) {
3141 					qdf_mem_copy(pos, tmp_old,
3142 						     (tmp_old[1] +
3143 						      MIN_IE_LEN));
3144 					pos += tmp_old[1] + MIN_IE_LEN;
3145 				}
3146 			}
3147 		} else {
3148 			/* ie in transmitting ie also in subelement,
3149 			 * copy from subelement and flag the ie in subelement
3150 			 * as copied (by setting eid field to 0xff).
3151 			 * To determine if the vendor ies are same:
3152 			 * 1. For Cisco OUI, compare only OUI + type
3153 			 * 2. For other OUI, compare OUI + type + subType
3154 			 */
3155 			tmp_rem_len = subie_len - (tmp - sub_copy);
3156 			if (tmp_old[0] == WLAN_ELEMID_VENDOR &&
3157 			    tmp_rem_len >= MIN_VENDOR_TAG_LEN) {
3158 				/*
3159 				 * In order to identify few Vendor APs the
3160 				 * generated frame should contain the reporting
3161 				 * APs matching VSIE or else the entry generated
3162 				 * will not have this VSIE and logic kept to
3163 				 * take certain action on specific Vendor APs
3164 				 * will fail.
3165 				 */
3166 				pos = util_copy_reporting_ap_vendor_ies(psoc,
3167 									tmp_old,
3168 									tmp_old[1] + MIN_IE_LEN,
3169 									pos);
3170 				/* If Vendor IE also presents in STA profile,
3171 				 * then ignore the Vendor IE which is for
3172 				 * reporting STA. It only needs to copy Vendor
3173 				 * IE from STA profile for reported BSSID.
3174 				 * The copy happens when going through the
3175 				 * remaining IEs.
3176 				 */
3177 			} else if (tmp_old[0] == WLAN_ELEMID_EXTN_ELEM) {
3178 				if (tmp_old[PAYLOAD_START_POS] ==
3179 				    tmp[PAYLOAD_START_POS]) {
3180 					/* same ie, copy from subelement */
3181 					if ((pos + tmp[1] + MIN_IE_LEN) <=
3182 					    (new_ie + ielen)) {
3183 						qdf_mem_copy(pos, tmp,
3184 							     tmp[1] +
3185 							     MIN_IE_LEN);
3186 						pos += tmp[1] + MIN_IE_LEN;
3187 						tmp[0] = 0;
3188 					}
3189 				} else {
3190 					if ((pos + tmp_old[1] + MIN_IE_LEN) <=
3191 					    (new_ie + ielen)) {
3192 						qdf_mem_copy(pos, tmp_old,
3193 							     tmp_old[1] +
3194 							     MIN_IE_LEN);
3195 						pos += tmp_old[1] +
3196 							MIN_IE_LEN;
3197 					}
3198 				}
3199 
3200 			} else {
3201 				/* copy ie from subelement into new ie */
3202 				if ((pos + tmp[1] + MIN_IE_LEN) <=
3203 				    (new_ie + ielen)) {
3204 					qdf_mem_copy(pos, tmp,
3205 						     tmp[1] + MIN_IE_LEN);
3206 					pos += tmp[1] + MIN_IE_LEN;
3207 					tmp[0] = 0;
3208 				}
3209 			}
3210 		}
3211 
3212 		if (((tmp_old + tmp_old[1] + MIN_IE_LEN) - ie) >= ielen)
3213 			break;
3214 
3215 		tmp_old += tmp_old[1] + MIN_IE_LEN;
3216 	}
3217 
3218 	/* go through subelement again to check if there is any ie not
3219 	 * copied to new ie, skip ssid, capability, bssid-index ie
3220 	 */
3221 	tmp_new = sub_copy;
3222 	while ((subie_len > 0) &&
3223 	       (((tmp_new + tmp_new[1] + MIN_IE_LEN) - sub_copy) <=
3224 		subie_len)) {
3225 		if (!(tmp_new[0] == WLAN_ELEMID_NONTX_BSSID_CAP ||
3226 		      tmp_new[0] == WLAN_ELEMID_SSID ||
3227 		      tmp_new[0] == WLAN_ELEMID_MULTI_BSSID_IDX ||
3228 		      ((tmp_new[0] == WLAN_ELEMID_EXTN_ELEM) &&
3229 		       (tmp_new[2] == WLAN_EXTN_ELEMID_NONINHERITANCE)))) {
3230 			if ((pos + tmp_new[1] + MIN_IE_LEN) <=
3231 			    (new_ie + ielen)) {
3232 				qdf_mem_copy(pos, tmp_new,
3233 					     tmp_new[1] + MIN_IE_LEN);
3234 				pos += tmp_new[1] + MIN_IE_LEN;
3235 			}
3236 		}
3237 		if (((tmp_new + tmp_new[1] + MIN_IE_LEN) - sub_copy) >=
3238 		    (subie_len - 1))
3239 			break;
3240 		tmp_new += tmp_new[1] + MIN_IE_LEN;
3241 	}
3242 
3243 	qdf_mem_free(sub_copy);
3244 
3245 	if (pos > new_ie)
3246 		return pos - new_ie;
3247 	else
3248 		return 0;
3249 }
3250 
3251 static enum nontx_profile_reasoncode
3252 util_handle_nontx_prof(uint8_t *mbssid_elem, uint8_t *subelement,
3253 		       uint8_t *next_subelement,
3254 		       struct scan_mbssid_info *mbssid_info,
3255 		       char *bssid, char *new_bssid)
3256 {
3257 	uint8_t *mbssid_index_ie;
3258 	uint32_t prof_len;
3259 
3260 	prof_len = subelement[TAG_LEN_POS];
3261 	/*
3262 	 * If we are executing the split portion of the nontx
3263 	 * profile present in the subsequent MBSSID, then there
3264 	 * is no need of any sanity check for valid BSS profile
3265 	 */
3266 
3267 	if (mbssid_info->split_prof_continue) {
3268 		if ((subelement[ID_POS] != 0) ||
3269 		    (subelement[TAG_LEN_POS] < SPLIT_PROF_DATA_LEAST_LEN)) {
3270 			return INVALID_SPLIT_PROF;
3271 		}
3272 	} else {
3273 		if ((subelement[ID_POS] != 0) ||
3274 		    (subelement[TAG_LEN_POS] < VALID_ELEM_LEAST_LEN)) {
3275 			/* not a valid BSS profile */
3276 			return INVALID_NONTX_PROF;
3277 		}
3278 	}
3279 
3280 	if (mbssid_info->split_profile) {
3281 		if (next_subelement[PAYLOAD_START_POS] !=
3282 		    WLAN_ELEMID_NONTX_BSSID_CAP) {
3283 			mbssid_info->prof_residue = true;
3284 		}
3285 	}
3286 
3287 	if (!mbssid_info->split_prof_continue &&
3288 	    ((subelement[PAYLOAD_START_POS] != WLAN_ELEMID_NONTX_BSSID_CAP) ||
3289 	     (subelement[NONTX_BSSID_CAP_TAG_LEN_POS] != CAP_INFO_LEN))) {
3290 		/* The first element within the Nontransmitted
3291 		 * BSSID Profile is not the Nontransmitted
3292 		 * BSSID Capability element.
3293 		 */
3294 		return INVALID_NONTX_PROF;
3295 	}
3296 
3297 	/* found a Nontransmitted BSSID Profile */
3298 	mbssid_index_ie =
3299 		util_scan_find_ie(WLAN_ELEMID_MULTI_BSSID_IDX,
3300 				  (subelement + PAYLOAD_START_POS), prof_len);
3301 
3302 	if (!mbssid_index_ie) {
3303 		if (!mbssid_info->prof_residue)
3304 			return INVALID_NONTX_PROF;
3305 
3306 		mbssid_info->skip_bssid_copy = true;
3307 	} else if ((mbssid_index_ie[TAG_LEN_POS] < 1) ||
3308 		   (mbssid_index_ie[BSS_INDEX_POS] == 0)) {
3309 		/* No valid Multiple BSSID-Index element */
3310 		return INVALID_NONTX_PROF;
3311 	}
3312 
3313 	if (!mbssid_info->skip_bssid_copy) {
3314 		qdf_mem_copy(mbssid_info->trans_bssid,
3315 			     bssid, QDF_MAC_ADDR_SIZE);
3316 		mbssid_info->profile_num =
3317 			mbssid_index_ie[BSS_INDEX_POS];
3318 		util_gen_new_bssid(bssid,
3319 				   mbssid_elem[MBSSID_INDICATOR_POS],
3320 				   mbssid_index_ie[BSS_INDEX_POS],
3321 				   new_bssid);
3322 		qdf_mem_copy(mbssid_info->non_trans_bssid, new_bssid,
3323 			     QDF_MAC_ADDR_SIZE);
3324 	}
3325 	/* In single MBSS IE, there could be subelement holding
3326 	 * remaining vendor IEs of non tx profile from last MBSS IE
3327 	 * [split profile] and new non tx profile, hence reset
3328 	 * skip_bssid_copy flag after each subelement processing
3329 	 */
3330 	mbssid_info->skip_bssid_copy = false;
3331 	return VALID_NONTX_PROF;
3332 }
3333 
3334 /*
3335  * What's split profile:
3336  *  If any nontransmitted BSSID profile is fragmented across
3337  * multiple MBSSID elements, then it is called split profile.
3338  * For a split profile to exist we need to have at least two
3339  * MBSSID elements as part of the RX beacon or probe response
3340  * Hence, first we need to identify the next MBSSID element
3341  * and check for the 5th bit from the starting of the next
3342  * MBSSID IE and if it does not have Nontransmitted BSSID
3343  * capability element, then it's a split profile case.
3344  */
3345 static bool util_scan_is_split_prof_found(uint8_t *next_elem,
3346 					  uint8_t *ie, uint32_t ielen)
3347 {
3348 	uint8_t *next_mbssid_elem;
3349 
3350 	if (next_elem[0] == WLAN_ELEMID_MULTIPLE_BSSID) {
3351 		if ((next_elem[TAG_LEN_POS] >= VALID_ELEM_LEAST_LEN) &&
3352 		    (next_elem[SUBELEM_DATA_POS_FROM_MBSSID] !=
3353 		     WLAN_ELEMID_NONTX_BSSID_CAP)) {
3354 			return true;
3355 		}
3356 	} else {
3357 		next_mbssid_elem =
3358 			util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID,
3359 					  next_elem,
3360 					  ielen - (next_elem - ie));
3361 		if (!next_mbssid_elem)
3362 			return false;
3363 
3364 		if ((next_mbssid_elem[TAG_LEN_POS] >= VALID_ELEM_LEAST_LEN) &&
3365 		    (next_mbssid_elem[SUBELEM_DATA_POS_FROM_MBSSID] !=
3366 		     WLAN_ELEMID_NONTX_BSSID_CAP)) {
3367 			return true;
3368 		}
3369 	}
3370 
3371 	return false;
3372 }
3373 
3374 static QDF_STATUS util_scan_parse_mbssid(struct wlan_objmgr_pdev *pdev,
3375 					 uint8_t *frame, qdf_size_t frame_len,
3376 					 uint32_t frm_subtype,
3377 					 struct mgmt_rx_event_params *rx_param,
3378 					 qdf_list_t *scan_list)
3379 {
3380 	struct wlan_scan_obj *scan_obj;
3381 	struct wlan_bcn_frame *bcn;
3382 	struct wlan_frame_hdr *hdr;
3383 	struct scan_mbssid_info mbssid_info = {0};
3384 	QDF_STATUS status;
3385 	uint8_t *pos, *subelement, *next_elem;
3386 	uint8_t *mbssid_elem;
3387 	uint32_t subie_len, new_ie_len, ielen;
3388 	uint8_t *next_subelement = NULL;
3389 	uint8_t new_bssid[QDF_MAC_ADDR_SIZE], bssid[QDF_MAC_ADDR_SIZE];
3390 	uint8_t *new_ie, *split_prof_start = NULL, *split_prof_end = NULL;
3391 	uint8_t *ie, *new_frame = NULL;
3392 	int new_frame_len = 0, split_prof_len = 0;
3393 	enum nontx_profile_reasoncode retval;
3394 	uint8_t *nontx_profile = NULL;
3395 
3396 	scan_obj = wlan_pdev_get_scan_obj(pdev);
3397 	if (!scan_obj)
3398 		return QDF_STATUS_E_INVAL;
3399 
3400 	hdr = (struct wlan_frame_hdr *)frame;
3401 	bcn = (struct wlan_bcn_frame *)(frame + sizeof(struct wlan_frame_hdr));
3402 	ie = (uint8_t *)&bcn->ie;
3403 	ielen = (uint16_t)(frame_len -
3404 			   sizeof(struct wlan_frame_hdr) -
3405 			   offsetof(struct wlan_bcn_frame, ie));
3406 	qdf_mem_copy(bssid, hdr->i_addr3, QDF_MAC_ADDR_SIZE);
3407 
3408 	if (!util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID, ie, ielen))
3409 		return QDF_STATUS_E_FAILURE;
3410 
3411 	pos = ie;
3412 
3413 	new_ie = qdf_mem_malloc(ielen);
3414 	if (!new_ie)
3415 		return QDF_STATUS_E_NOMEM;
3416 
3417 	while (pos < (ie + ielen + MIN_IE_LEN)) {
3418 		mbssid_elem =
3419 			util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID, pos,
3420 					  ielen - (pos - ie));
3421 		if (!mbssid_elem)
3422 			break;
3423 
3424 		mbssid_info.profile_count =
3425 			(1 << mbssid_elem[MBSSID_INDICATOR_POS]);
3426 
3427 		next_elem =
3428 			mbssid_elem + mbssid_elem[TAG_LEN_POS] + MIN_IE_LEN;
3429 
3430 		/* Skip Element ID, Len, MaxBSSID Indicator */
3431 		if (!mbssid_info.split_profile &&
3432 		    (mbssid_elem[TAG_LEN_POS] < VALID_ELEM_LEAST_LEN)) {
3433 			break;
3434 		}
3435 
3436 		/*
3437 		 * Find if the next IE is MBSSID, if not, then scan through
3438 		 * the IE list and find the next MBSSID tag, if present.
3439 		 * Once we find the MBSSID tag, check if this MBSSID tag has
3440 		 * the other fragmented part of the non Tx profile.
3441 		 */
3442 
3443 		mbssid_info.split_profile =
3444 			util_scan_is_split_prof_found(next_elem, ie, ielen);
3445 
3446 		for (subelement = mbssid_elem + SUBELEMENT_START_POS;
3447 		     subelement < (next_elem - 1);
3448 		     subelement += MIN_IE_LEN + subelement[TAG_LEN_POS]) {
3449 			subie_len = subelement[TAG_LEN_POS];
3450 
3451 			/*
3452 			 * if prof_residue is true, that means we are
3453 			 * in the continuation of the fragmented profile part,
3454 			 * present in the next MBSSD IE else this profile
3455 			 * is a non fragmented non tx BSSID profile.
3456 			 */
3457 
3458 			if (mbssid_info.prof_residue)
3459 				mbssid_info.split_prof_continue = true;
3460 			else
3461 				mbssid_info.split_prof_continue = false;
3462 
3463 			if (subie_len > MAX_SUBELEM_LEN) {
3464 				scm_debug_rl("Corrupt frame with subie_len: %d "
3465 					     "split_prof_continue: %d,prof_residue: %d",
3466 					     subie_len,
3467 					     mbssid_info.split_prof_continue,
3468 					     mbssid_info.prof_residue);
3469 				if (mbssid_info.split_prof_continue) {
3470 					qdf_mem_free(split_prof_start);
3471 					split_prof_start = NULL;
3472 				}
3473 
3474 				qdf_mem_free(new_ie);
3475 				return QDF_STATUS_E_INVAL;
3476 			}
3477 
3478 			if ((next_elem - subelement) <
3479 			    (MIN_IE_LEN + subie_len))
3480 				break;
3481 
3482 			next_subelement = subelement + subie_len + MIN_IE_LEN;
3483 			retval = util_handle_nontx_prof(mbssid_elem, subelement,
3484 							next_subelement,
3485 							&mbssid_info,
3486 							bssid, new_bssid);
3487 
3488 			if (retval == INVALID_SPLIT_PROF) {
3489 				scm_debug_rl("Corrupt frame with ID_POS: %d,TAG_LEN_POS: %d",
3490 					     subelement[ID_POS],
3491 					     subelement[TAG_LEN_POS]);
3492 				qdf_mem_free(split_prof_start);
3493 				split_prof_start = NULL;
3494 				qdf_mem_free(new_ie);
3495 				return QDF_STATUS_E_INVAL;
3496 			} else if (retval == INVALID_NONTX_PROF) {
3497 				continue;
3498 			}
3499 
3500 			/*
3501 			 * Merging parts of nontx profile-
3502 			 * Just for understanding, let's make an assumption
3503 			 * that nontx profile is fragmented across MBSSIE1
3504 			 * and MBSSIE2.
3505 			 * mbssid_info.prof_residue being set indicates
3506 			 * that the ongoing nontx profile is part of split
3507 			 * profile, whose other fragmented part is present
3508 			 * in MBSSIE2.
3509 			 * So once prof_residue is set, we need to
3510 			 * identify whether we are accessing the split
3511 			 * profile in MBSSIE1 or MBSSIE2.
3512 			 * If we are in MBSSIE1, then copy the part of split
3513 			 * profile from MBSSIE1 into a new buffer and then
3514 			 * move to the next part of the split profile which
3515 			 * is present in MBSSIE2 and append that part into
3516 			 * the new buffer.
3517 			 * Once the full profile is accumulated, go ahead with
3518 			 * the ie generation and length calculation of the
3519 			 * new frame.
3520 			 */
3521 
3522 			if (mbssid_info.prof_residue) {
3523 				if (!mbssid_info.split_prof_continue) {
3524 					split_prof_start =
3525 						qdf_mem_malloc(ielen);
3526 					if (!split_prof_start) {
3527 						scm_err_rl("Malloc failed");
3528 						qdf_mem_free(new_ie);
3529 						return QDF_STATUS_E_NOMEM;
3530 					}
3531 
3532 					qdf_mem_copy(split_prof_start,
3533 						     subelement,
3534 						     (subie_len +
3535 						      MIN_IE_LEN));
3536 					split_prof_end = (split_prof_start +
3537 							  subie_len +
3538 							  MIN_IE_LEN);
3539 					break;
3540 				}
3541 
3542 				/*
3543 				 * Currently we are accessing other part of the
3544 				 * split profile present in the subsequent
3545 				 * MBSSIE. There is a possibility that one
3546 				 * non tx profile is spread across more than
3547 				 * two MBSSID tag as well. This code will
3548 				 * handle such scenario.
3549 				 */
3550 
3551 				if (split_prof_end) {
3552 					qdf_mem_copy(split_prof_end,
3553 						     (subelement + MIN_IE_LEN),
3554 						     subie_len);
3555 					split_prof_end += subie_len;
3556 				}
3557 
3558 				/*
3559 				 * When to stop the process of accumulating
3560 				 * parts of split profile, is decided by
3561 				 * mbssid_info.prof_residue. prof_residue
3562 				 * could be made false if there is not any
3563 				 * continuation of the split profile.
3564 				 * which could be identified by two factors
3565 				 * 1. By checking if the next MBSSIE's first
3566 				 * non tx profile is not a fragmented one or
3567 				 * 2. there is a probability that first
3568 				 * subelement of MBSSIE2 is end if split
3569 				 * profile and the next subelement of MBSSIE2
3570 				 * is a non split one.
3571 				 */
3572 
3573 				if (!mbssid_info.split_profile ||
3574 				    (next_subelement[PAYLOAD_START_POS] ==
3575 				     WLAN_ELEMID_NONTX_BSSID_CAP)) {
3576 					mbssid_info.prof_residue = false;
3577 				}
3578 
3579 				/*
3580 				 * Until above mentioned conditions are met,
3581 				 * we need to iterate and keep accumulating
3582 				 * the split profile contents.
3583 				 */
3584 
3585 				if (mbssid_info.prof_residue)
3586 					break;
3587 
3588 				if (split_prof_end) {
3589 					split_prof_len =
3590 						(split_prof_end -
3591 						 split_prof_start - MIN_IE_LEN);
3592 				}
3593 			}
3594 
3595 			if (mbssid_info.split_prof_continue) {
3596 				nontx_profile = split_prof_start;
3597 				subie_len = split_prof_len;
3598 			} else {
3599 				nontx_profile = subelement;
3600 			}
3601 
3602 			new_ie_len =
3603 				util_gen_new_ie(pdev, ie, ielen,
3604 						(nontx_profile +
3605 						 PAYLOAD_START_POS),
3606 						subie_len, new_ie,
3607 						mbssid_info.profile_num);
3608 
3609 			if (!new_ie_len) {
3610 				if (mbssid_info.split_prof_continue) {
3611 					qdf_mem_free(split_prof_start);
3612 					split_prof_start = NULL;
3613 					split_prof_end = NULL;
3614 				}
3615 				continue;
3616 			}
3617 
3618 			new_frame_len = frame_len - ielen + new_ie_len;
3619 
3620 			if (new_frame_len < 0 || new_frame_len > frame_len) {
3621 				if (mbssid_info.split_prof_continue) {
3622 					qdf_mem_free(split_prof_start);
3623 					split_prof_start = NULL;
3624 				}
3625 				qdf_mem_free(new_ie);
3626 				scm_debug_rl("Invalid frame:Stop MBSSIE parsing, Frame_len: %zu "
3627 					     "ielen:%u,new_ie_len:%u",
3628 					     frame_len, ielen, new_ie_len);
3629 				return QDF_STATUS_E_INVAL;
3630 			}
3631 
3632 			new_frame = qdf_mem_malloc(new_frame_len);
3633 			if (!new_frame) {
3634 				if (mbssid_info.split_prof_continue) {
3635 					qdf_mem_free(split_prof_start);
3636 					split_prof_start = NULL;
3637 				}
3638 				qdf_mem_free(new_ie);
3639 				scm_err_rl("Malloc for new_frame failed");
3640 				scm_err_rl("split_prof_continue: %d",
3641 					   mbssid_info.split_prof_continue);
3642 				return QDF_STATUS_E_NOMEM;
3643 			}
3644 
3645 			/*
3646 			 * Copy the header(24byte), timestamp(8 byte),
3647 			 * beaconinterval(2byte) and capability(2byte)
3648 			 */
3649 			qdf_mem_copy(new_frame, frame, FIXED_LENGTH);
3650 			/* Copy the new ie generated from MBSSID profile*/
3651 			hdr = (struct wlan_frame_hdr *)new_frame;
3652 			qdf_mem_copy(hdr->i_addr2, new_bssid,
3653 				     QDF_MAC_ADDR_SIZE);
3654 			qdf_mem_copy(hdr->i_addr3, new_bssid,
3655 				     QDF_MAC_ADDR_SIZE);
3656 			bcn = (struct wlan_bcn_frame *)
3657 				(new_frame + sizeof(struct wlan_frame_hdr));
3658 			/* update the non-tx capability */
3659 			qdf_mem_copy(&bcn->capability,
3660 				     nontx_profile + CAP_INFO_POS,
3661 				     CAP_INFO_LEN);
3662 
3663 			/* Copy the new ie generated from MBSSID profile*/
3664 			qdf_mem_copy(new_frame +
3665 				     offsetof(struct wlan_bcn_frame, ie) +
3666 				     sizeof(struct wlan_frame_hdr),
3667 				     new_ie, new_ie_len);
3668 			if (scan_obj->cb.inform_mbssid_bcn_prb_rsp)
3669 				scan_obj->cb.inform_mbssid_bcn_prb_rsp(
3670 						       new_frame, new_frame_len,
3671 						       frm_subtype, new_bssid);
3672 
3673 			status = util_scan_gen_scan_entry(pdev, new_frame,
3674 							  new_frame_len,
3675 							  frm_subtype,
3676 							  rx_param,
3677 							  &mbssid_info,
3678 							  scan_list);
3679 			if (QDF_IS_STATUS_ERROR(status)) {
3680 				if (mbssid_info.split_prof_continue) {
3681 					qdf_mem_free(split_prof_start);
3682 					split_prof_start = NULL;
3683 					split_prof_end = NULL;
3684 					qdf_mem_zero(&mbssid_info,
3685 						     sizeof(mbssid_info));
3686 				}
3687 				qdf_mem_free(new_frame);
3688 				scm_debug_rl("failed to generate a scan entry "
3689 					     "split_prof_continue: %d",
3690 					     mbssid_info.split_prof_continue);
3691 				break;
3692 			}
3693 			/* scan entry makes its own copy so free the frame*/
3694 			if (mbssid_info.split_prof_continue) {
3695 				qdf_mem_free(split_prof_start);
3696 				split_prof_start = NULL;
3697 				split_prof_end = NULL;
3698 			}
3699 			qdf_mem_free(new_frame);
3700 		}
3701 
3702 		pos = next_elem;
3703 	}
3704 	qdf_mem_free(new_ie);
3705 
3706 	if (split_prof_start)
3707 		qdf_mem_free(split_prof_start);
3708 
3709 	return QDF_STATUS_SUCCESS;
3710 }
3711 #else
3712 static QDF_STATUS util_scan_parse_mbssid(struct wlan_objmgr_pdev *pdev,
3713 					 uint8_t *frame, qdf_size_t frame_len,
3714 					 uint32_t frm_subtype,
3715 					 struct mgmt_rx_event_params *rx_param,
3716 					 qdf_list_t *scan_list)
3717 {
3718 	return QDF_STATUS_SUCCESS;
3719 }
3720 #endif
3721 
3722 #if defined(WLAN_FEATURE_11BE) && defined(WLAN_FEATURE_11BE_MLO_MBSSID)
3723 /*
3724  * util_scan_gen_txvap_scan_entry() - Strip out the MBSSID tag from the received
3725  * frame and update the modified frame length before generating a scan entry.
3726  * It is redundant to have MBSSID information as part of the TX vap/ profile
3727  * specific scan entry.
3728  *
3729  * @pdev: pdev context
3730  * @frame: Unsoiled frame passed from util_scan_parse_beacon_frame()
3731  * @frame_len: Length of the unsoiled frame
3732  * @ie_list: Points to the start of IE list in parent/ unsoiled frame
3733  * @ielen: Length of the complete IE list from parent/ unsoiled frame
3734  * @frm_subtype: Frame subtype
3735  * @rx_param: host mgmt header params
3736  * @scan_list: Scan entry list of bss candidates after filtering
3737  * @mbssid_info: Data structure to carry MBSSID information
3738  *
3739  * Return: False if the scan entry generation is not successful
3740  */
3741 static QDF_STATUS
3742 util_scan_gen_txvap_scan_entry(struct wlan_objmgr_pdev *pdev,
3743 			       uint8_t *frame, qdf_size_t frame_len,
3744 			       uint8_t *ie_list, uint32_t ielen,
3745 			       uint32_t frm_subtype,
3746 			       struct mgmt_rx_event_params *rx_param,
3747 			       qdf_list_t *scan_list,
3748 			       struct scan_mbssid_info *mbssid_info)
3749 {
3750 	uint8_t *src_ie, *dest_ptr, *container;
3751 	uint16_t new_frame_len, new_ie_len = 0;
3752 	uint8_t *trimmed_frame, fixed_len = 0;
3753 	QDF_STATUS status = QDF_STATUS_SUCCESS;
3754 
3755 	/*
3756 	 * Allocate a buffer to copy only the TX VAP information after
3757 	 * stripping out the MBSSID IE from the parent beacon.
3758 	 * The allocation size should be the size of a frame as at
3759 	 * this point it is unknown what would be the new frame length
3760 	 * after stripping the MBSSID IE.
3761 	 */
3762 	container = qdf_mem_malloc(frame_len);
3763 	if (!container) {
3764 		scm_err_rl("Malloc for container failed");
3765 		return QDF_STATUS_E_NOMEM;
3766 	}
3767 
3768 	dest_ptr = &container[0];
3769 	fixed_len = sizeof(struct wlan_frame_hdr) +
3770 		offsetof(struct wlan_bcn_frame, ie);
3771 
3772 	/*Copy the data till IE list before procesisng the IE one by one*/
3773 	qdf_mem_copy(dest_ptr, frame, fixed_len);
3774 
3775 	dest_ptr += fixed_len;
3776 	src_ie = ie_list;
3777 
3778 	/*
3779 	 * Go through the IE list from the parent beacon and copy one by one.
3780 	 * Skip copying it to the container if it's an MBSSID tag.
3781 	 */
3782 	while (((src_ie + src_ie[TAG_LEN_POS] + MIN_IE_LEN) -
3783 		ie_list) <= ielen) {
3784 		if (src_ie[ID_POS] == WLAN_ELEMID_MULTIPLE_BSSID) {
3785 			src_ie += src_ie[TAG_LEN_POS] + MIN_IE_LEN;
3786 			continue;
3787 		}
3788 
3789 		qdf_mem_copy(dest_ptr, src_ie,
3790 			     (src_ie[TAG_LEN_POS] + MIN_IE_LEN));
3791 
3792 		dest_ptr += src_ie[TAG_LEN_POS] + MIN_IE_LEN;
3793 		if (((src_ie + src_ie[TAG_LEN_POS] +
3794 		      MIN_IE_LEN) - ie_list) >= ielen)
3795 			break;
3796 
3797 		src_ie += src_ie[TAG_LEN_POS] + MIN_IE_LEN;
3798 	}
3799 
3800 	if (dest_ptr > container)
3801 		new_ie_len = dest_ptr - (container + fixed_len);
3802 
3803 	new_frame_len = frame_len - ielen + new_ie_len;
3804 
3805 	/*
3806 	 * At the start of this handler, we have allocated a memory block
3807 	 * of size same as a full beacon frame size, as we are not sure
3808 	 * of what would be the size of the new frame. After stripping out
3809 	 * the MBSSID tag from the parent beacon, there are some unused
3810 	 * memory. Hence do another malloc of the new frame length
3811 	 * (length of the new frame which has only TX VAP information)
3812 	 * and copy the needed data from the container, then free the
3813 	 * memory corresponds to container.
3814 	 * Post copy, use the trimmed frame and the new frame length
3815 	 * to generate scan entry for the TX profile.
3816 	 */
3817 	trimmed_frame = qdf_mem_malloc(new_frame_len);
3818 	if (!trimmed_frame) {
3819 		scm_err_rl("Malloc for trimmed frame failed");
3820 		qdf_mem_free(container);
3821 		return QDF_STATUS_E_NOMEM;
3822 	}
3823 
3824 	qdf_mem_copy(trimmed_frame, container, new_frame_len);
3825 	qdf_mem_free(container);
3826 
3827 	status = util_scan_gen_scan_entry(pdev, trimmed_frame,
3828 					  new_frame_len,
3829 					  frm_subtype,
3830 					  rx_param,
3831 					  mbssid_info,
3832 					  scan_list);
3833 
3834 	if (QDF_IS_STATUS_ERROR(status))
3835 		scm_debug_rl("Failed to create a scan entry");
3836 
3837 	qdf_mem_free(trimmed_frame);
3838 	return status;
3839 }
3840 
3841 /*
3842  * util_scan_parse_eht_beacon() : This API will be executed
3843  * only for 11BE platforms as per current design.
3844  * IF MBSSID IE is present in the beacon then
3845  * scan component will create a new entry for
3846  * each BSSID found in the MBSSID
3847  * util_scan_parse_mbssid() takes care of creating
3848  * scan entries for every non tx profile present in
3849  * the MBSSID tag.
3850  * util_scan_gen_txvap_scan_entry() helps in generating
3851  * scan entry for the tx profile.
3852  */
3853 static QDF_STATUS
3854 util_scan_parse_eht_beacon(struct wlan_objmgr_pdev *pdev,
3855 			   uint8_t *frame, qdf_size_t frame_len,
3856 			   uint8_t *ie_list, uint32_t ielen,
3857 			   uint32_t frm_subtype,
3858 			   struct mgmt_rx_event_params *rx_param,
3859 			   qdf_list_t *scan_list,
3860 			   struct scan_mbssid_info *mbssid_info,
3861 			   uint8_t *mbssid_ie)
3862 {
3863 	QDF_STATUS status = QDF_STATUS_SUCCESS;
3864 
3865 	if (mbssid_ie && ie_list) {
3866 		if (ie_list[TAG_LEN_POS] <= 0) {
3867 			scm_debug_rl("Corrupt IE");
3868 			return QDF_STATUS_E_INVAL;
3869 		}
3870 
3871 		status = util_scan_parse_mbssid(pdev, frame, frame_len,
3872 						frm_subtype, rx_param,
3873 						scan_list);
3874 
3875 		if (QDF_IS_STATUS_ERROR(status)) {
3876 			scm_debug_rl("NonTx prof: Failed to create scan entry");
3877 			return status;
3878 		}
3879 
3880 		status = util_scan_gen_txvap_scan_entry(pdev, frame,
3881 							frame_len, ie_list,
3882 							ielen, frm_subtype,
3883 							rx_param, scan_list,
3884 							mbssid_info);
3885 
3886 		if (QDF_IS_STATUS_ERROR(status))
3887 			scm_debug_rl("TX prof: Failed to create scan entry");
3888 
3889 		return status;
3890 	}
3891 
3892 	/*For Non MBSSIE case*/
3893 	status = util_scan_gen_scan_entry(pdev, frame, frame_len,
3894 					  frm_subtype, rx_param,
3895 					  mbssid_info, scan_list);
3896 
3897 	if (QDF_IS_STATUS_ERROR(status))
3898 		scm_debug_rl("Non-MBSSIE frame: Failed to create scan entry");
3899 
3900 	return status;
3901 }
3902 
3903 static bool
3904 util_scan_is_platform_eht_capable(struct wlan_objmgr_pdev *pdev)
3905 {
3906 	struct wlan_objmgr_psoc *psoc = NULL;
3907 	struct wlan_lmac_if_tx_ops *tx_ops = NULL;
3908 	struct wlan_lmac_if_scan_tx_ops *scan_ops = NULL;
3909 	uint8_t pdev_id;
3910 
3911 	psoc = wlan_pdev_get_psoc(pdev);
3912 	if (!psoc) {
3913 		scm_debug_rl("psoc is null");
3914 		return false;
3915 	}
3916 	tx_ops = wlan_psoc_get_lmac_if_txops(psoc);
3917 	if (!tx_ops) {
3918 		scm_debug_rl("tx_ops is null");
3919 		return false;
3920 	}
3921 	scan_ops = &tx_ops->scan;
3922 	if (!scan_ops) {
3923 		scm_debug_rl("scan_ops is null");
3924 		return false;
3925 	}
3926 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
3927 
3928 	if (scan_ops->is_platform_eht_capable)
3929 		return scan_ops->is_platform_eht_capable(psoc, pdev_id);
3930 
3931 	return false;
3932 }
3933 #else
3934 static QDF_STATUS
3935 util_scan_parse_eht_beacon(struct wlan_objmgr_pdev *pdev,
3936 			   uint8_t *frame, qdf_size_t frame_len,
3937 			   uint8_t *ie_list, uint32_t ielen,
3938 			   uint32_t frm_subtype,
3939 			   struct mgmt_rx_event_params *rx_param,
3940 			   qdf_list_t *scan_list,
3941 			   struct scan_mbssid_info *mbssid_info,
3942 			   uint8_t *mbssid_ie)
3943 {
3944 	return QDF_STATUS_SUCCESS;
3945 }
3946 
3947 static bool
3948 util_scan_is_platform_eht_capable(struct wlan_objmgr_pdev *pdev)
3949 {
3950 	return false;
3951 }
3952 #endif
3953 
3954 static QDF_STATUS
3955 util_scan_parse_beacon_frame(struct wlan_objmgr_pdev *pdev,
3956 			     uint8_t *frame,
3957 			     qdf_size_t frame_len,
3958 			     uint32_t frm_subtype,
3959 			     struct mgmt_rx_event_params *rx_param,
3960 			     qdf_list_t *scan_list)
3961 {
3962 	struct wlan_bcn_frame *bcn;
3963 	struct wlan_frame_hdr *hdr;
3964 	uint8_t *mbssid_ie = NULL, *extcap_ie;
3965 	uint32_t ie_len = 0;
3966 	QDF_STATUS status = QDF_STATUS_E_FAILURE;
3967 	struct scan_mbssid_info mbssid_info = { 0 };
3968 	uint8_t *ie_list;
3969 	bool eht_support = false;
3970 
3971 	hdr = (struct wlan_frame_hdr *)frame;
3972 	bcn = (struct wlan_bcn_frame *)
3973 		(frame + sizeof(struct wlan_frame_hdr));
3974 	ie_list = (uint8_t *)&bcn->ie;
3975 	ie_len = (uint16_t)(frame_len -
3976 			    sizeof(struct wlan_frame_hdr) -
3977 			    offsetof(struct wlan_bcn_frame, ie));
3978 
3979 	extcap_ie = util_scan_find_ie(WLAN_ELEMID_XCAPS,
3980 				      (uint8_t *)&bcn->ie, ie_len);
3981 	/* Process MBSSID when Multiple BSSID (Bit 22) is set in Ext Caps */
3982 	if (extcap_ie &&
3983 	    extcap_ie[1] >= 3 && extcap_ie[1] <= WLAN_EXTCAP_IE_MAX_LEN &&
3984 	    (extcap_ie[4] & 0x40)) {
3985 		mbssid_ie = util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID,
3986 					      (uint8_t *)&bcn->ie, ie_len);
3987 		if (mbssid_ie) {
3988 			/* some APs announce the MBSSID ie_len as 1 */
3989 			if (mbssid_ie[TAG_LEN_POS] < 1) {
3990 				scm_debug("MBSSID IE length is wrong %d",
3991 					  mbssid_ie[TAG_LEN_POS]);
3992 				return status;
3993 			}
3994 			qdf_mem_copy(&mbssid_info.trans_bssid,
3995 				     hdr->i_addr3, QDF_MAC_ADDR_SIZE);
3996 			mbssid_info.profile_count = 1 << mbssid_ie[2];
3997 		}
3998 	}
3999 
4000 	eht_support = util_scan_is_platform_eht_capable(pdev);
4001 
4002 	if (eht_support) {
4003 		status = util_scan_parse_eht_beacon(pdev, frame, frame_len,
4004 						    ie_list, ie_len,
4005 						    frm_subtype, rx_param,
4006 						    scan_list, &mbssid_info,
4007 						    mbssid_ie);
4008 		return status;
4009 	}
4010 
4011 	status = util_scan_gen_scan_entry(pdev, frame, frame_len,
4012 					  frm_subtype, rx_param,
4013 					  &mbssid_info,
4014 					  scan_list);
4015 
4016 	if (mbssid_ie)
4017 		status = util_scan_parse_mbssid(pdev, frame, frame_len,
4018 						frm_subtype, rx_param,
4019 						scan_list);
4020 
4021 	if (QDF_IS_STATUS_ERROR(status))
4022 		scm_debug_rl("Failed to create scan entry");
4023 
4024 	return status;
4025 }
4026 
4027 qdf_list_t *
4028 util_scan_unpack_beacon_frame(struct wlan_objmgr_pdev *pdev, uint8_t *frame,
4029 			      qdf_size_t frame_len, uint32_t frm_subtype,
4030 			      struct mgmt_rx_event_params *rx_param)
4031 {
4032 	qdf_list_t *scan_list;
4033 	QDF_STATUS status;
4034 
4035 	scan_list = qdf_mem_malloc_atomic(sizeof(*scan_list));
4036 	if (!scan_list) {
4037 		scm_err("failed to allocate scan_list");
4038 		return NULL;
4039 	}
4040 	qdf_list_create(scan_list, MAX_SCAN_CACHE_SIZE);
4041 
4042 	status = util_scan_parse_beacon_frame(pdev, frame, frame_len,
4043 					      frm_subtype, rx_param,
4044 					      scan_list);
4045 	if (QDF_IS_STATUS_ERROR(status)) {
4046 		ucfg_scan_purge_results(scan_list);
4047 		return NULL;
4048 	}
4049 
4050 	return scan_list;
4051 }
4052 
4053 QDF_STATUS
4054 util_scan_entry_update_mlme_info(struct wlan_objmgr_pdev *pdev,
4055 	struct scan_cache_entry *scan_entry)
4056 {
4057 
4058 	if (!pdev || !scan_entry) {
4059 		scm_err("pdev 0x%pK, scan_entry: 0x%pK", pdev, scan_entry);
4060 		return QDF_STATUS_E_INVAL;
4061 	}
4062 
4063 	return scm_update_scan_mlme_info(pdev, scan_entry);
4064 }
4065 
4066 bool util_is_scan_completed(struct scan_event *event, bool *success)
4067 {
4068 	if ((event->type == SCAN_EVENT_TYPE_COMPLETED) ||
4069 	    (event->type == SCAN_EVENT_TYPE_DEQUEUED) ||
4070 	    (event->type == SCAN_EVENT_TYPE_START_FAILED)) {
4071 		if ((event->type == SCAN_EVENT_TYPE_COMPLETED) &&
4072 		    (event->reason == SCAN_REASON_COMPLETED))
4073 			*success = true;
4074 		else
4075 			*success = false;
4076 
4077 		return true;
4078 	}
4079 
4080 	*success = false;
4081 	return false;
4082 }
4083 
4084 #if defined(WLAN_SAE_SINGLE_PMK) && defined(WLAN_FEATURE_ROAM_OFFLOAD)
4085 bool
4086 util_scan_entry_single_pmk(struct wlan_objmgr_psoc *psoc,
4087 			   struct scan_cache_entry *scan_entry)
4088 {
4089 	if (scan_entry->ie_list.single_pmk &&
4090 	    wlan_mlme_is_sae_single_pmk_enabled(psoc))
4091 		return true;
4092 
4093 	return false;
4094 }
4095 #endif
4096