xref: /wlan-dirver/qca-wifi-host-cmn/umac/scan/core/src/wlan_scan_filter.c (revision 1397a33f48ea6455be40871470b286e535820eb8)
1 /*
2  * Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 /*
19  * DOC: contains scan cache filter logic
20  */
21 
22 #include <wlan_scan_utils_api.h>
23 #include "wlan_scan_main.h"
24 #include "wlan_scan_cache_db_i.h"
25 
26 /**
27  * scm_is_open_security() - Check if scan entry support open security
28  * @filter: scan filter
29  * @db_entry: db entry
30  * @security: matched security.
31  *
32  * Return: true if open security else false
33  */
34 static bool scm_is_open_security(struct scan_filter *filter,
35 	struct scan_cache_entry *db_entry,
36 	struct security_info *security)
37 {
38 	bool match = false;
39 	int i;
40 
41 	if (db_entry->cap_info.wlan_caps.privacy)
42 		return false;
43 
44 	/* Check MC cipher and Auth type requested. */
45 	for (i = 0; i < filter->num_of_mc_enc_type; i++) {
46 		if (WLAN_ENCRYPT_TYPE_NONE ==
47 			filter->mc_enc_type[i]) {
48 			security->mc_enc =
49 				filter->mc_enc_type[i];
50 			match = true;
51 			break;
52 		}
53 	}
54 	if (!match && filter->num_of_mc_enc_type)
55 		return match;
56 
57 	match = false;
58 	/* Check Auth list. It should contain AuthOpen. */
59 	for (i = 0; i < filter->num_of_auth; i++) {
60 		if ((WLAN_AUTH_TYPE_OPEN_SYSTEM ==
61 			filter->auth_type[i]) ||
62 			(WLAN_AUTH_TYPE_AUTOSWITCH ==
63 			filter->auth_type[i])) {
64 			security->auth_type =
65 				WLAN_AUTH_TYPE_OPEN_SYSTEM;
66 			match = true;
67 			break;
68 		}
69 	}
70 
71 	return match;
72 }
73 
74 /**
75  * scm_is_cipher_match() - Check if cipher match the cipher list
76  * @cipher_list: cipher list to match
77  * @num_cipher: number of cipher in cipher list
78  * @cipher_to_match: cipher to found in cipher list
79  *
80  * Return: true if open security else false
81  */
82 static bool scm_is_cipher_match(
83 	uint32_t *cipher_list,
84 	uint16_t num_cipher, uint32_t cipher_to_match)
85 {
86 	int i;
87 	bool match = false;
88 
89 	for (i = 0; i < num_cipher ; i++) {
90 		match = (cipher_list[i] == cipher_to_match);
91 		if (match)
92 			break;
93 	}
94 
95 	return match;
96 }
97 
98 /**
99  * scm_get_cipher_suite_type() - get cypher suite type from enc type
100  * @enc: enc type
101  *
102  * Return: cypher suite type
103  */
104 static uint8_t scm_get_cipher_suite_type(enum wlan_enc_type enc)
105 {
106 	uint8_t cipher_type;
107 
108 	switch (enc) {
109 	case WLAN_ENCRYPT_TYPE_WEP40:
110 	case WLAN_ENCRYPT_TYPE_WEP40_STATICKEY:
111 		cipher_type = WLAN_CSE_WEP40;
112 		break;
113 	case WLAN_ENCRYPT_TYPE_WEP104:
114 	case WLAN_ENCRYPT_TYPE_WEP104_STATICKEY:
115 		cipher_type = WLAN_CSE_WEP104;
116 		break;
117 	case WLAN_ENCRYPT_TYPE_TKIP:
118 		cipher_type = WLAN_CSE_TKIP;
119 		break;
120 	case WLAN_ENCRYPT_TYPE_AES:
121 		cipher_type = WLAN_CSE_CCMP;
122 		break;
123 	case WLAN_ENCRYPT_TYPE_AES_GCMP:
124 		cipher_type = WLAN_CSE_GCMP_128;
125 		break;
126 	case WLAN_ENCRYPT_TYPE_AES_GCMP_256:
127 		cipher_type = WLAN_CSE_GCMP_256;
128 		break;
129 	case WLAN_ENCRYPT_TYPE_NONE:
130 		cipher_type = WLAN_CSE_NONE;
131 		break;
132 	case WLAN_ENCRYPT_TYPE_WPI:
133 		cipher_type = WLAN_WAI_CERT_OR_SMS4;
134 		break;
135 	default:
136 		cipher_type = WLAN_CSE_RESERVED;
137 		break;
138 	}
139 
140 	return cipher_type;
141 }
142 
143 /**
144  * scm_is_wep_security() - Check if scan entry support WEP security
145  * @filter: scan filter
146  * @db_entry: db entry
147  * @security: matched security.
148  *
149  * Return: true if WEP security else false
150  */
151 static bool scm_is_wep_security(struct scan_filter *filter,
152 	struct scan_cache_entry *db_entry,
153 	struct security_info *security)
154 {
155 	int i;
156 	QDF_STATUS status;
157 	bool match = false;
158 	enum wlan_auth_type neg_auth = WLAN_AUTH_TYPE_OPEN_SYSTEM;
159 	enum wlan_enc_type neg_mccipher = WLAN_ENCRYPT_TYPE_NONE;
160 
161 	if (!security)
162 		return false;
163 
164 	/* If privacy bit is not set, consider no match */
165 	if (!db_entry->cap_info.wlan_caps.privacy)
166 		return false;
167 
168 	for (i = 0; i < filter->num_of_mc_enc_type; i++) {
169 		switch (filter->mc_enc_type[i]) {
170 		case WLAN_ENCRYPT_TYPE_WEP40_STATICKEY:
171 		case WLAN_ENCRYPT_TYPE_WEP104_STATICKEY:
172 		case WLAN_ENCRYPT_TYPE_WEP40:
173 		case WLAN_ENCRYPT_TYPE_WEP104:
174 			/*
175 			 * Multicast list may contain WEP40/WEP104.
176 			 * Check whether it matches UC.
177 			 */
178 			if (security->uc_enc ==
179 			   filter->mc_enc_type[i]) {
180 				match = true;
181 				neg_mccipher =
182 				   filter->mc_enc_type[i];
183 			}
184 			break;
185 		default:
186 			match = false;
187 			break;
188 		}
189 		if (match)
190 			break;
191 	}
192 
193 	if (!match)
194 		return match;
195 
196 	for (i = 0; i < filter->num_of_auth; i++) {
197 		switch (filter->auth_type[i]) {
198 		case WLAN_AUTH_TYPE_OPEN_SYSTEM:
199 		case WLAN_AUTH_TYPE_SHARED_KEY:
200 		case WLAN_AUTH_TYPE_AUTOSWITCH:
201 			match = true;
202 			neg_auth = filter->auth_type[i];
203 			break;
204 		default:
205 			match = false;
206 		}
207 		if (match)
208 			break;
209 	}
210 
211 	if (!match)
212 		return match;
213 
214 	/*
215 	 * In case of WPA / WPA2, check whether it supports WEP as well.
216 	 * Prepare the encryption type for WPA/WPA2 functions
217 	 */
218 	if (security->uc_enc == WLAN_ENCRYPT_TYPE_WEP40_STATICKEY)
219 		security->uc_enc = WLAN_ENCRYPT_TYPE_WEP40;
220 	else if (security->uc_enc == WLAN_ENCRYPT_TYPE_WEP104)
221 		security->uc_enc = WLAN_ENCRYPT_TYPE_WEP104;
222 
223 	/* else we can use the encryption type directly */
224 	if (util_scan_entry_wpa(db_entry)) {
225 		struct wlan_wpa_ie wpa = {0};
226 		uint8_t cipher_type;
227 
228 		cipher_type =
229 			scm_get_cipher_suite_type(security->uc_enc);
230 		status = wlan_parse_wpa_ie(util_scan_entry_wpa(db_entry), &wpa);
231 		if (QDF_IS_STATUS_ERROR(status)) {
232 			scm_err("failed to parse WPA IE, status %d", status);
233 			scm_hex_dump(QDF_TRACE_LEVEL_DEBUG,
234 				     util_scan_entry_wpa(db_entry),
235 				     util_scan_get_wpa_len(db_entry));
236 			return false;
237 		}
238 
239 		match = scm_is_cipher_match(&wpa.mc_cipher,
240 				  1, WLAN_WPA_SEL(cipher_type));
241 	}
242 	if (!match && util_scan_entry_rsn(db_entry)) {
243 		struct wlan_rsn_ie rsn = {0};
244 		uint8_t cipher_type;
245 
246 		cipher_type =
247 			scm_get_cipher_suite_type(security->uc_enc);
248 		status = wlan_parse_rsn_ie(util_scan_entry_rsn(db_entry), &rsn);
249 		if (QDF_IS_STATUS_ERROR(status)) {
250 			scm_err("failed to parse RSN IE, status %d", status);
251 			scm_hex_dump(QDF_TRACE_LEVEL_DEBUG,
252 				     util_scan_entry_rsn(db_entry),
253 				     util_scan_get_rsn_len(db_entry));
254 			return false;
255 		}
256 		match = scm_is_cipher_match(&rsn.gp_cipher_suite,
257 				  1, WLAN_RSN_SEL(cipher_type));
258 	}
259 
260 
261 	if (match) {
262 		security->auth_type = neg_auth;
263 		security->mc_enc = neg_mccipher;
264 	}
265 
266 	return match;
267 }
268 
269 /**
270  * scm_check_pmf_match() - Check PMF security of entry match filter
271  * @filter: scan filter
272  * @rsn: rsn IE of the scan entry
273  *
274  * Return: true if PMF security match else false
275  */
276 static bool
277 scm_check_pmf_match(struct scan_filter *filter,
278 	struct wlan_rsn_ie *rsn)
279 {
280 	enum wlan_pmf_cap ap_pmf_cap = WLAN_PMF_DISABLED;
281 
282 	if (rsn->cap & RSN_CAP_MFP_CAPABLE)
283 		ap_pmf_cap = WLAN_PMF_CAPABLE;
284 	if (rsn->cap & RSN_CAP_MFP_REQUIRED)
285 		ap_pmf_cap = WLAN_PMF_REQUIRED;
286 
287 	if ((filter->pmf_cap == WLAN_PMF_REQUIRED) &&
288 		(ap_pmf_cap == WLAN_PMF_DISABLED))
289 		return false;
290 	else if ((filter->pmf_cap == WLAN_PMF_DISABLED) &&
291 		(ap_pmf_cap == WLAN_PMF_REQUIRED))
292 		return false;
293 
294 	return true;
295 }
296 
297 /**
298  * scm_is_rsn_mcast_cipher_match() - match the rsn mcast cipher type with AP's
299  * mcast cipher
300  * @rsn: AP's RSNE
301  * @filter: scan filter
302  * @neg_mccipher: negotiated mc cipher if matched.
303  *
304  * Return: true if mc cipher is negotiated
305  */
306 static bool
307 scm_is_rsn_mcast_cipher_match(struct wlan_rsn_ie *rsn,
308 	struct scan_filter *filter, enum wlan_enc_type *neg_mccipher)
309 {
310 	int i;
311 	bool match;
312 	uint8_t cipher_type;
313 
314 	if (!rsn || !neg_mccipher || !filter)
315 		return false;
316 
317 	for (i = 0; i < filter->num_of_mc_enc_type; i++) {
318 
319 		if (filter->mc_enc_type[i] == WLAN_ENCRYPT_TYPE_ANY) {
320 			/* Try the more secured ones first. */
321 			/* Check GCMP_256 first */
322 			cipher_type = WLAN_CSE_GCMP_256;
323 			match = scm_is_cipher_match(&rsn->gp_cipher_suite, 1,
324 						    WLAN_RSN_SEL(cipher_type));
325 			if (match) {
326 				*neg_mccipher = WLAN_ENCRYPT_TYPE_AES_GCMP_256;
327 				return true;
328 			}
329 			/* Check GCMP */
330 			cipher_type = WLAN_CSE_GCMP_128;
331 			match = scm_is_cipher_match(&rsn->gp_cipher_suite, 1,
332 						    WLAN_RSN_SEL(cipher_type));
333 			if (match) {
334 				*neg_mccipher = WLAN_ENCRYPT_TYPE_AES_GCMP;
335 				return true;
336 			}
337 			/* Check AES */
338 			cipher_type = WLAN_CSE_CCMP;
339 			match = scm_is_cipher_match(&rsn->gp_cipher_suite, 1,
340 						    WLAN_RSN_SEL(cipher_type));
341 			if (match) {
342 				*neg_mccipher = WLAN_ENCRYPT_TYPE_AES;
343 				return true;
344 			}
345 			/* Check TKIP */
346 			cipher_type = WLAN_CSE_TKIP;
347 			match = scm_is_cipher_match(&rsn->gp_cipher_suite, 1,
348 						    WLAN_RSN_SEL(cipher_type));
349 			if (match) {
350 				*neg_mccipher = WLAN_ENCRYPT_TYPE_TKIP;
351 				return true;
352 			}
353 		} else {
354 			cipher_type =
355 			     scm_get_cipher_suite_type(filter->mc_enc_type[i]);
356 			match = scm_is_cipher_match(&rsn->gp_cipher_suite, 1,
357 						    WLAN_RSN_SEL(cipher_type));
358 			if (match) {
359 				*neg_mccipher = filter->mc_enc_type[i];
360 				return true;
361 			}
362 		}
363 	}
364 
365 	return false;
366 }
367 
368 /**
369  * scm_is_rsn_security() - Check if scan entry support RSN security
370  * @filter: scan filter
371  * @db_entry: db entry
372  * @security: matched security.
373  *
374  * Return: true if RSN security else false
375  */
376 static bool scm_is_rsn_security(struct scan_filter *filter,
377 	struct scan_cache_entry *db_entry,
378 	struct security_info *security)
379 {
380 	int i;
381 	uint8_t cipher_type;
382 	bool match_any_akm, match = false;
383 	enum wlan_auth_type neg_auth = WLAN_NUM_OF_SUPPORT_AUTH_TYPE;
384 	enum wlan_enc_type neg_mccipher = WLAN_ENCRYPT_TYPE_NONE;
385 	struct wlan_rsn_ie rsn = {0};
386 	QDF_STATUS status;
387 
388 	if (!security)
389 		return false;
390 	if (!util_scan_entry_rsn(db_entry))
391 		return false;
392 	status = wlan_parse_rsn_ie(util_scan_entry_rsn(db_entry), &rsn);
393 	if (QDF_IS_STATUS_ERROR(status)) {
394 		scm_err("failed to parse RSN IE, status %d", status);
395 		scm_hex_dump(QDF_TRACE_LEVEL_DEBUG,
396 			     util_scan_entry_rsn(db_entry),
397 			     util_scan_get_rsn_len(db_entry));
398 		return false;
399 	}
400 
401 	cipher_type =
402 		scm_get_cipher_suite_type(security->uc_enc);
403 	match = scm_is_cipher_match(rsn.pwise_cipher_suites,
404 		rsn.pwise_cipher_count, WLAN_RSN_SEL(cipher_type));
405 	if (!match)
406 		return false;
407 
408 	match = scm_is_rsn_mcast_cipher_match(&rsn, filter, &neg_mccipher);
409 	if (!match)
410 		return false;
411 
412 	/* Initializing with false as it has true value already */
413 	match = false;
414 	for (i = 0; i < filter->num_of_auth; i++) {
415 
416 		if (filter->auth_type[i] == WLAN_AUTH_TYPE_ANY)
417 			match_any_akm = true;
418 		else
419 			match_any_akm = false;
420 		/*
421 		 * Ciphers are supported, Match authentication algorithm and
422 		 * pick first matching authtype.
423 		 */
424 		if (scm_is_cipher_match(rsn.akm_suites,
425 		   rsn.akm_suite_count,
426 		   WLAN_RSN_SEL(WLAN_AKM_FILS_FT_SHA384))) {
427 			if (match_any_akm || (WLAN_AUTH_TYPE_FT_FILS_SHA384 ==
428 			    filter->auth_type[i])) {
429 				neg_auth = WLAN_AUTH_TYPE_FT_FILS_SHA384;
430 				match = true;
431 				break;
432 			}
433 		}
434 		if (scm_is_cipher_match(rsn.akm_suites,
435 		   rsn.akm_suite_count,
436 		   WLAN_RSN_SEL(WLAN_AKM_FILS_FT_SHA256))) {
437 			if (match_any_akm || (WLAN_AUTH_TYPE_FT_FILS_SHA256 ==
438 			    filter->auth_type[i])) {
439 				neg_auth = WLAN_AUTH_TYPE_FT_FILS_SHA256;
440 				match = true;
441 				break;
442 			}
443 		}
444 		if (scm_is_cipher_match(rsn.akm_suites,
445 		   rsn.akm_suite_count,
446 		   WLAN_RSN_SEL(WLAN_AKM_FILS_SHA384))) {
447 			if (match_any_akm || (WLAN_AUTH_TYPE_FILS_SHA384 ==
448 			    filter->auth_type[i])) {
449 				neg_auth = WLAN_AUTH_TYPE_FILS_SHA384;
450 				match = true;
451 				break;
452 			}
453 		}
454 		if (scm_is_cipher_match(rsn.akm_suites,
455 		   rsn.akm_suite_count,
456 		   WLAN_RSN_SEL(WLAN_AKM_FILS_SHA256))) {
457 			if (match_any_akm || (WLAN_AUTH_TYPE_FILS_SHA256 ==
458 			    filter->auth_type[i])) {
459 				neg_auth = WLAN_AUTH_TYPE_FILS_SHA256;
460 				match = true;
461 				break;
462 			}
463 		}
464 
465 		if (scm_is_cipher_match(rsn.akm_suites,
466 		    rsn.akm_suite_count,
467 		   WLAN_RSN_SEL(WLAN_AKM_SAE))) {
468 			if (match_any_akm || (WLAN_AUTH_TYPE_SAE ==
469 			    filter->auth_type[i])) {
470 				neg_auth = WLAN_AUTH_TYPE_SAE;
471 				match = true;
472 				break;
473 			}
474 		}
475 
476 		if (scm_is_cipher_match(rsn.akm_suites,
477 		   rsn.akm_suite_count, WLAN_RSN_DPP_AKM)) {
478 			if (match_any_akm || (WLAN_AUTH_TYPE_DPP_RSN ==
479 			    filter->auth_type[i])) {
480 				neg_auth = WLAN_AUTH_TYPE_DPP_RSN;
481 				match = true;
482 				break;
483 			}
484 		}
485 		if (scm_is_cipher_match(rsn.akm_suites,
486 					rsn.akm_suite_count,
487 					WLAN_RSN_OSEN_AKM)) {
488 			if (match_any_akm ||
489 			    WLAN_AUTH_TYPE_OSEN == filter->auth_type[i]) {
490 				neg_auth = WLAN_AUTH_TYPE_OSEN;
491 				match = true;
492 				break;
493 			}
494 		}
495 		if (scm_is_cipher_match(rsn.akm_suites,
496 		   rsn.akm_suite_count,
497 		   WLAN_RSN_SEL(WLAN_AKM_OWE))) {
498 			if (match_any_akm || (WLAN_AUTH_TYPE_OWE ==
499 			    filter->auth_type[i])) {
500 				neg_auth = WLAN_AUTH_TYPE_OWE;
501 				match = true;
502 				break;
503 			}
504 		}
505 		if (scm_is_cipher_match(rsn.akm_suites,
506 		   rsn.akm_suite_count,
507 		   WLAN_RSN_SEL(WLAN_AKM_FT_IEEE8021X))) {
508 			if (match_any_akm || (WLAN_AUTH_TYPE_FT_RSN ==
509 			    filter->auth_type[i])) {
510 				neg_auth = WLAN_AUTH_TYPE_FT_RSN;
511 				match = true;
512 				break;
513 			}
514 		}
515 
516 		if (scm_is_cipher_match(rsn.akm_suites,
517 		   rsn.akm_suite_count,
518 		   WLAN_RSN_SEL(WLAN_AKM_FT_PSK))) {
519 			if (match_any_akm || (WLAN_AUTH_TYPE_FT_RSN_PSK ==
520 			   filter->auth_type[i])) {
521 				neg_auth = WLAN_AUTH_TYPE_FT_RSN_PSK;
522 				match = true;
523 				break;
524 			}
525 		}
526 		/* ESE only supports 802.1X.  No PSK. */
527 		if (scm_is_cipher_match(rsn.akm_suites,
528 		   rsn.akm_suite_count,
529 		   WLAN_RSN_CCKM_AKM)) {
530 			if (match_any_akm || (WLAN_AUTH_TYPE_CCKM_RSN ==
531 			   filter->auth_type[i])) {
532 				neg_auth = WLAN_AUTH_TYPE_CCKM_RSN;
533 				match = true;
534 				break;
535 			}
536 		}
537 		/* RSN */
538 		if (scm_is_cipher_match(rsn.akm_suites,
539 		   rsn.akm_suite_count,
540 		   WLAN_RSN_SEL(WLAN_AKM_IEEE8021X))) {
541 			if (match_any_akm || (WLAN_AUTH_TYPE_RSN ==
542 			   filter->auth_type[i])) {
543 				neg_auth = WLAN_AUTH_TYPE_RSN;
544 				match = true;
545 				break;
546 			}
547 		}
548 		/* TKIP */
549 		if (scm_is_cipher_match(rsn.akm_suites,
550 		   rsn.akm_suite_count,
551 		   WLAN_RSN_SEL(WLAN_AKM_PSK))) {
552 			if (match_any_akm || (WLAN_AUTH_TYPE_RSN_PSK ==
553 			   filter->auth_type[i])) {
554 				neg_auth = WLAN_AUTH_TYPE_RSN_PSK;
555 				match = true;
556 				break;
557 			}
558 		}
559 		/* SHA256 */
560 		if (scm_is_cipher_match(rsn.akm_suites,
561 		   rsn.akm_suite_count,
562 		   WLAN_RSN_SEL(WLAN_AKM_SHA256_PSK))) {
563 			if (match_any_akm || (WLAN_AUTH_TYPE_RSN_PSK_SHA256 ==
564 			   filter->auth_type[i])) {
565 				neg_auth =
566 					WLAN_AUTH_TYPE_RSN_PSK_SHA256;
567 				match = true;
568 				break;
569 			}
570 		}
571 		/* 8021X SHA256 */
572 		if (scm_is_cipher_match(rsn.akm_suites,
573 		   rsn.akm_suite_count,
574 		   WLAN_RSN_SEL(WLAN_AKM_SHA256_IEEE8021X))) {
575 			if (match_any_akm || (WLAN_AUTH_TYPE_RSN_8021X_SHA256 ==
576 			   filter->auth_type[i])) {
577 				neg_auth =
578 					WLAN_AUTH_TYPE_RSN_8021X_SHA256;
579 				match = true;
580 				break;
581 			}
582 		}
583 		if (scm_is_cipher_match(rsn.akm_suites,
584 		   rsn.akm_suite_count,
585 		   WLAN_RSN_SEL(WLAN_AKM_SUITEB_EAP_SHA256))) {
586 			if (match_any_akm ||
587 			    (WLAN_AUTH_TYPE_SUITEB_EAP_SHA256 ==
588 			     filter->auth_type[i])) {
589 				neg_auth = WLAN_AUTH_TYPE_SUITEB_EAP_SHA256;
590 				match = true;
591 				break;
592 			}
593 		}
594 		if (scm_is_cipher_match(rsn.akm_suites,
595 		   rsn.akm_suite_count,
596 		   WLAN_RSN_SEL(WLAN_AKM_SUITEB_EAP_SHA384))) {
597 			if (match_any_akm ||
598 			    (WLAN_AUTH_TYPE_SUITEB_EAP_SHA384 ==
599 			     filter->auth_type[i])) {
600 				neg_auth = WLAN_AUTH_TYPE_SUITEB_EAP_SHA384;
601 				match = true;
602 				break;
603 			}
604 		}
605 	}
606 
607 	if (!match)
608 		return false;
609 
610 	if (!filter->ignore_pmf_cap)
611 		match = scm_check_pmf_match(filter, &rsn);
612 
613 	if (match) {
614 		security->auth_type = neg_auth;
615 		security->mc_enc = neg_mccipher;
616 	}
617 
618 	return match;
619 }
620 
621 /**
622  * scm_is_wpa_mcast_cipher_match() - match the wpa mcast cipher type with AP's
623  * mcast cipher
624  * @wpa: AP's WPA IE
625  * @filter: scan filter
626  * @neg_mccipher: negotiated mc cipher if matched.
627  *
628  * Return: true if mc cipher is negotiated
629  */
630 static bool
631 scm_is_wpa_mcast_cipher_match(struct wlan_wpa_ie *wpa,
632 	struct scan_filter *filter, enum wlan_enc_type *neg_mccipher)
633 {
634 	int i;
635 	bool match;
636 	uint8_t cipher_type;
637 
638 	if (!wpa || !neg_mccipher || !filter)
639 		return false;
640 
641 	for (i = 0; i < filter->num_of_mc_enc_type; i++) {
642 
643 		if (filter->mc_enc_type[i] == WLAN_ENCRYPT_TYPE_ANY) {
644 			/* Try the more secured ones first. */
645 
646 			/* Check AES */
647 			cipher_type = WLAN_CSE_CCMP;
648 			match = scm_is_cipher_match(&wpa->mc_cipher, 1,
649 						    WLAN_WPA_SEL(cipher_type));
650 			if (match) {
651 				*neg_mccipher = WLAN_ENCRYPT_TYPE_AES;
652 				return true;
653 			}
654 			/* Check TKIP */
655 			cipher_type = WLAN_CSE_TKIP;
656 			match = scm_is_cipher_match(&wpa->mc_cipher, 1,
657 						    WLAN_WPA_SEL(cipher_type));
658 			if (match) {
659 				*neg_mccipher = WLAN_ENCRYPT_TYPE_TKIP;
660 				return true;
661 			}
662 		} else {
663 			cipher_type =
664 			     scm_get_cipher_suite_type(filter->mc_enc_type[i]);
665 			match = scm_is_cipher_match(&wpa->mc_cipher, 1,
666 						    WLAN_WPA_SEL(cipher_type));
667 			if (match) {
668 				*neg_mccipher = filter->mc_enc_type[i];
669 				return true;
670 			}
671 		}
672 	}
673 
674 	return false;
675 }
676 
677 /**
678  * scm_is_wpa_security() - Check if scan entry support WPA security
679  * @filter: scan filter
680  * @db_entry: db entry
681  * @security: matched security.
682  *
683  * Return: true if WPA security else false
684  */
685 static bool scm_is_wpa_security(struct scan_filter *filter,
686 	struct scan_cache_entry *db_entry,
687 	struct security_info *security)
688 {
689 	int i;
690 	QDF_STATUS status;
691 	uint8_t cipher_type;
692 	bool match_any_akm, match = false;
693 	enum wlan_auth_type neg_auth = WLAN_NUM_OF_SUPPORT_AUTH_TYPE;
694 	enum wlan_enc_type neg_mccipher = WLAN_ENCRYPT_TYPE_NONE;
695 	struct wlan_wpa_ie wpa = {0};
696 
697 	if (!security)
698 		return false;
699 	if (!util_scan_entry_wpa(db_entry))
700 		return false;
701 
702 	status = wlan_parse_wpa_ie(util_scan_entry_wpa(db_entry), &wpa);
703 	if (QDF_IS_STATUS_ERROR(status)) {
704 		scm_err("failed to parse WPA IE, status %d", status);
705 		scm_hex_dump(QDF_TRACE_LEVEL_DEBUG,
706 			     util_scan_entry_wpa(db_entry),
707 			     util_scan_get_wpa_len(db_entry));
708 		return false;
709 	}
710 
711 	cipher_type =
712 		scm_get_cipher_suite_type(security->uc_enc);
713 	match = scm_is_cipher_match(wpa.uc_ciphers,
714 		wpa.uc_cipher_count, WLAN_WPA_SEL(cipher_type));
715 	if (!match)
716 		return false;
717 
718 	match = scm_is_wpa_mcast_cipher_match(&wpa, filter, &neg_mccipher);
719 	if (!match)
720 		return false;
721 
722 	/* Initializing with false as it has true value already */
723 	match = false;
724 	for (i = 0; i < filter->num_of_auth; i++) {
725 
726 		if (filter->auth_type[i] == WLAN_AUTH_TYPE_ANY)
727 			match_any_akm = true;
728 		else
729 			match_any_akm = false;
730 		/*
731 		 * Ciphers are supported, Match authentication algorithm and
732 		 * pick first matching authtype.
733 		 */
734 		/**/
735 		if (scm_is_cipher_match(wpa.auth_suites,
736 		   wpa.auth_suite_count,
737 		   WLAN_WPA_SEL(WLAN_AKM_IEEE8021X))) {
738 			if (match_any_akm || (WLAN_AUTH_TYPE_WPA ==
739 			    filter->auth_type[i])) {
740 				neg_auth = WLAN_AUTH_TYPE_WPA;
741 				match = true;
742 				break;
743 			}
744 		}
745 		if (scm_is_cipher_match(wpa.auth_suites,
746 		   wpa.auth_suite_count,
747 		   WLAN_WPA_SEL(WLAN_AKM_PSK))) {
748 			if (match_any_akm || (WLAN_AUTH_TYPE_WPA_PSK ==
749 			    filter->auth_type[i])) {
750 				neg_auth = WLAN_AUTH_TYPE_WPA_PSK;
751 				match = true;
752 				break;
753 			}
754 		}
755 		if (scm_is_cipher_match(wpa.auth_suites,
756 		   wpa.auth_suite_count,
757 		   WLAN_WPA_CCKM_AKM)) {
758 			if (match_any_akm || (WLAN_AUTH_TYPE_CCKM_WPA ==
759 			    filter->auth_type[i])) {
760 				neg_auth = WLAN_AUTH_TYPE_CCKM_WPA;
761 				match = true;
762 				break;
763 			}
764 		}
765 	}
766 
767 	if (match) {
768 		security->auth_type = neg_auth;
769 		security->mc_enc = neg_mccipher;
770 	}
771 
772 	return match;
773 }
774 
775 /**
776  * scm_is_wapi_security() - Check if scan entry support WAPI security
777  * @filter: scan filter
778  * @db_entry: db entry
779  * @security: matched security.
780  *
781  * Return: true if WAPI security else false
782  */
783 static bool scm_is_wapi_security(struct scan_filter *filter,
784 	struct scan_cache_entry *db_entry,
785 	struct security_info *security)
786 {
787 	int i;
788 	uint8_t cipher_type;
789 	bool match = false;
790 	enum wlan_auth_type neg_auth = WLAN_NUM_OF_SUPPORT_AUTH_TYPE;
791 	enum wlan_enc_type neg_mccipher = WLAN_ENCRYPT_TYPE_NONE;
792 	struct wlan_wapi_ie wapi = {0};
793 
794 	if (!security)
795 		return false;
796 	if (!util_scan_entry_wapi(db_entry))
797 		return false;
798 
799 	wlan_parse_wapi_ie(
800 		   util_scan_entry_wapi(db_entry), &wapi);
801 
802 	cipher_type =
803 		scm_get_cipher_suite_type(security->uc_enc);
804 	match = scm_is_cipher_match(wapi.uc_cipher_suites,
805 		wapi.uc_cipher_count, WLAN_WAPI_SEL(cipher_type));
806 	if (!match)
807 		return false;
808 
809 	for (i = 0; i < filter->num_of_mc_enc_type; i++) {
810 		cipher_type =
811 		  scm_get_cipher_suite_type(
812 		  filter->mc_enc_type[i]);
813 		match = scm_is_cipher_match(&wapi.mc_cipher_suite,
814 				  1, WLAN_WAPI_SEL(cipher_type));
815 		if (match)
816 			break;
817 	}
818 	if (!match)
819 		return false;
820 	neg_mccipher = filter->mc_enc_type[i];
821 
822 	if (scm_is_cipher_match(wapi.akm_suites,
823 	   wapi.akm_suite_count,
824 	   WLAN_WAPI_SEL(WLAN_WAI_CERT_OR_SMS4)))
825 		neg_auth =
826 			WLAN_AUTH_TYPE_WAPI_WAI_CERTIFICATE;
827 	else if (scm_is_cipher_match(wapi.akm_suites,
828 	   wapi.akm_suite_count, WLAN_WAPI_SEL(WLAN_WAI_PSK)))
829 		neg_auth = WLAN_AUTH_TYPE_WAPI_WAI_PSK;
830 	else
831 		return false;
832 
833 	match = false;
834 	for (i = 0; i < filter->num_of_auth; i++) {
835 		if (filter->auth_type[i] == neg_auth) {
836 			match = true;
837 			break;
838 		}
839 	}
840 
841 	if (match) {
842 		security->auth_type = neg_auth;
843 		security->mc_enc = neg_mccipher;
844 	}
845 
846 	return match;
847 }
848 
849 /**
850  * scm_is_def_security() - Check if any security in filter match
851  * @filter: scan filter
852  * @db_entry: db entry
853  * @security: matched security.
854  *
855  * Return: true if any security else false
856  */
857 static bool scm_is_def_security(struct scan_filter *filter,
858 	struct scan_cache_entry *db_entry,
859 	struct security_info *security)
860 {
861 
862 	/* It is allowed to match anything. Try the more secured ones first. */
863 	/* Check GCMP_256 first */
864 	security->uc_enc = WLAN_ENCRYPT_TYPE_AES_GCMP_256;
865 	if (scm_is_rsn_security(filter, db_entry, security))
866 		return true;
867 
868 	/* Check GCMP */
869 	security->uc_enc = WLAN_ENCRYPT_TYPE_AES_GCMP;
870 	if (scm_is_rsn_security(filter, db_entry, security))
871 		return true;
872 
873 	/* Check AES */
874 	security->uc_enc = WLAN_ENCRYPT_TYPE_AES;
875 	if (scm_is_rsn_security(filter, db_entry, security))
876 		return true;
877 	if (scm_is_wpa_security(filter, db_entry, security))
878 		return true;
879 
880 	/* Check TKIP */
881 	security->uc_enc = WLAN_ENCRYPT_TYPE_TKIP;
882 	if (scm_is_rsn_security(filter, db_entry, security))
883 		return true;
884 	if (scm_is_wpa_security(filter, db_entry, security))
885 		return true;
886 
887 	/* Check AES */
888 	security->uc_enc = WLAN_ENCRYPT_TYPE_AES;
889 	if (scm_is_wpa_security(filter, db_entry, security))
890 		return true;
891 
892 	/* Check TKIP */
893 	security->uc_enc = WLAN_ENCRYPT_TYPE_TKIP;
894 	if (scm_is_wpa_security(filter, db_entry, security))
895 		return true;
896 
897 	/* Check WAPI */
898 	security->uc_enc = WLAN_ENCRYPT_TYPE_WPI;
899 	if (scm_is_wapi_security(filter, db_entry, security))
900 		return true;
901 
902 	security->uc_enc = WLAN_ENCRYPT_TYPE_WEP104;
903 	if (scm_is_wep_security(filter, db_entry, security))
904 		return true;
905 	security->uc_enc = WLAN_ENCRYPT_TYPE_WEP40;
906 	if (scm_is_wep_security(filter, db_entry, security))
907 		return true;
908 	security->uc_enc = WLAN_ENCRYPT_TYPE_WEP104_STATICKEY;
909 	if (scm_is_wep_security(filter, db_entry, security))
910 		return true;
911 	security->uc_enc = WLAN_ENCRYPT_TYPE_WEP40_STATICKEY;
912 	if (scm_is_wep_security(filter, db_entry, security))
913 		return true;
914 
915 	/* It must be open and no enc */
916 	if (db_entry->cap_info.wlan_caps.privacy)
917 		return false;
918 
919 	security->auth_type = WLAN_AUTH_TYPE_OPEN_SYSTEM;
920 	security->mc_enc = WLAN_ENCRYPT_TYPE_NONE;
921 	security->uc_enc = WLAN_ENCRYPT_TYPE_NONE;
922 
923 	return true;
924 }
925 
926 /**
927  * scm_is_fils_config_match() - Check if FILS config matches
928  * @filter: scan filter
929  * @db_entry: db entry
930  *
931  * Return: true if FILS config matches else false
932  */
933 static bool scm_is_fils_config_match(struct scan_filter *filter,
934 	struct scan_cache_entry *db_entry)
935 {
936 	int i;
937 	struct fils_indication_ie *indication_ie;
938 	uint8_t *data;
939 
940 	if (!filter->fils_scan_filter.realm_check)
941 		return true;
942 
943 	if (!db_entry->ie_list.fils_indication)
944 		return false;
945 
946 
947 	indication_ie =
948 		(struct fils_indication_ie *) db_entry->ie_list.fils_indication;
949 
950 	data = indication_ie->variable_data;
951 	if (indication_ie->is_cache_id_present)
952 		data += CACHE_IDENTIFIER_LEN;
953 
954 	if (indication_ie->is_hessid_present)
955 		data += HESSID_LEN;
956 
957 	for (i = 1; i <= indication_ie->realm_identifiers_cnt; i++) {
958 		if (!qdf_mem_cmp(filter->fils_scan_filter.fils_realm,
959 				 data, REAM_HASH_LEN))
960 			return true;
961 		/* Max realm count reached */
962 		if (indication_ie->realm_identifiers_cnt == i)
963 			break;
964 		else
965 			data = data + REAM_HASH_LEN;
966 	}
967 
968 	return false;
969 }
970 
971 /**
972  * scm_is_security_match() - Check if security in filter match
973  * @filter: scan filter
974  * @db_entry: db entry
975  * @security: matched security.
976  *
977  * Return: true if security match else false
978  */
979 static bool scm_is_security_match(struct scan_filter *filter,
980 	struct scan_cache_entry *db_entry,
981 	struct security_info *security)
982 {
983 	int i;
984 	bool match = false;
985 	struct security_info local_security = {0};
986 
987 	if (!filter->num_of_enc_type)
988 		return true;
989 
990 	for (i = 0; (i < filter->num_of_enc_type) &&
991 	    !match; i++) {
992 
993 		local_security.uc_enc =
994 			filter->enc_type[i];
995 
996 		switch (filter->enc_type[i]) {
997 		case WLAN_ENCRYPT_TYPE_NONE:
998 			match = scm_is_open_security(filter,
999 				    db_entry, &local_security);
1000 			break;
1001 		case WLAN_ENCRYPT_TYPE_WEP40_STATICKEY:
1002 		case WLAN_ENCRYPT_TYPE_WEP104_STATICKEY:
1003 		case WLAN_ENCRYPT_TYPE_WEP40:
1004 		case WLAN_ENCRYPT_TYPE_WEP104:
1005 			match = scm_is_wep_security(filter,
1006 				    db_entry, &local_security);
1007 			break;
1008 		case WLAN_ENCRYPT_TYPE_TKIP:
1009 		case WLAN_ENCRYPT_TYPE_AES:
1010 		case WLAN_ENCRYPT_TYPE_AES_GCMP:
1011 		case WLAN_ENCRYPT_TYPE_AES_GCMP_256:
1012 			/* First check if there is a RSN match */
1013 			match = scm_is_rsn_security(filter,
1014 				    db_entry, &local_security);
1015 			/* If not RSN, then check WPA match */
1016 			if (!match)
1017 				match = scm_is_wpa_security(filter,
1018 				    db_entry, &local_security);
1019 			break;
1020 		case WLAN_ENCRYPT_TYPE_WPI:/* WAPI */
1021 			match = scm_is_wapi_security(filter,
1022 				    db_entry, &local_security);
1023 			break;
1024 		case WLAN_ENCRYPT_TYPE_ANY:
1025 		default:
1026 			match  = scm_is_def_security(filter,
1027 				    db_entry, &local_security);
1028 			break;
1029 		}
1030 	}
1031 
1032 	if (match && security)
1033 		qdf_mem_copy(security,
1034 			&local_security, sizeof(*security));
1035 
1036 	return match;
1037 }
1038 
1039 bool scm_filter_match(struct wlan_objmgr_psoc *psoc,
1040 	struct scan_cache_entry *db_entry,
1041 	struct scan_filter *filter,
1042 	struct security_info *security)
1043 {
1044 	int i;
1045 	bool match = false;
1046 	struct roam_filter_params *roam_params;
1047 	struct scan_default_params *def_param;
1048 	struct wlan_country_ie *cc_ie;
1049 
1050 	def_param = wlan_scan_psoc_get_def_params(psoc);
1051 	if (!def_param)
1052 		return false;
1053 
1054 	roam_params = &def_param->roam_params;
1055 
1056 	if (filter->p2p_results && !db_entry->is_p2p)
1057 		return false;
1058 
1059 	for (i = 0; i < roam_params->num_bssid_avoid_list; i++)
1060 		if (qdf_is_macaddr_equal(&roam_params->bssid_avoid_list[i],
1061 		   &db_entry->bssid))
1062 			return false;
1063 
1064 	match = false;
1065 	if (db_entry->ssid.length) {
1066 		for (i = 0; i < filter->num_of_ssid; i++) {
1067 			if (util_is_ssid_match(&filter->ssid_list[i],
1068 			   &db_entry->ssid)) {
1069 				match = true;
1070 				break;
1071 			}
1072 		}
1073 	}
1074 	/*
1075 	 * In OWE transition mode, ssid is hidden. And supplicant does not issue
1076 	 * scan with specific ssid prior to connect as in other hidden ssid
1077 	 * cases. Add explicit check to allow OWE when ssid is hidden.
1078 	 */
1079 	if (!match && util_scan_entry_is_hidden_ap(db_entry)) {
1080 		for (i = 0; i < filter->num_of_auth; i++) {
1081 			if (filter->auth_type[i] == WLAN_AUTH_TYPE_OWE) {
1082 				match = true;
1083 				break;
1084 			}
1085 		}
1086 	}
1087 	if (!match && filter->num_of_ssid)
1088 		return false;
1089 
1090 	match = false;
1091 	/* TO do Fill p2p MAC*/
1092 	for (i = 0; i < filter->num_of_bssid; i++) {
1093 		if (util_is_bssid_match(&filter->bssid_list[i],
1094 		   &db_entry->bssid)) {
1095 			match = true;
1096 			break;
1097 		}
1098 		/* TODO match p2p mac */
1099 	}
1100 	if (!match && filter->num_of_bssid)
1101 		return false;
1102 
1103 	match = false;
1104 	for (i = 0; i < filter->num_of_channels; i++) {
1105 		if (!filter->channel_list[i] || (
1106 		   (filter->channel_list[i] ==
1107 		   db_entry->channel.chan_idx))) {
1108 			match = true;
1109 			break;
1110 		}
1111 	}
1112 
1113 	if (!match && filter->num_of_channels)
1114 		return false;
1115 
1116 	if (filter->rrm_measurement_filter)
1117 		return true;
1118 
1119 	/* TODO match phyMode */
1120 
1121 	if (!filter->ignore_auth_enc_type &&
1122 	   !scm_is_security_match(filter,
1123 	   db_entry, security))
1124 		return false;
1125 
1126 	if (!util_is_bss_type_match(filter->bss_type,
1127 	   db_entry->cap_info))
1128 		return false;
1129 
1130 	/* TODO match rate set */
1131 
1132 	if (filter->only_wmm_ap &&
1133 	   !db_entry->ie_list.wmeinfo &&
1134 	   !db_entry->ie_list.wmeparam)
1135 		return false;
1136 
1137 	/* Match realm */
1138 	if (!scm_is_fils_config_match(filter, db_entry))
1139 		return false;
1140 
1141 	cc_ie = util_scan_entry_country(db_entry);
1142 	if (!util_country_code_match(filter->country, cc_ie))
1143 		return false;
1144 
1145 	if (!util_mdie_match(filter->mobility_domain,
1146 	   (struct rsn_mdie *)db_entry->ie_list.mdie))
1147 		return false;
1148 
1149 	return true;
1150 }
1151