• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * wpa_supplicant - SME
3  * Copyright (c) 2009-2014, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "common/ocv.h"
16 #include "eapol_supp/eapol_supp_sm.h"
17 #include "common/wpa_common.h"
18 #include "common/sae.h"
19 #include "common/dpp.h"
20 #include "rsn_supp/wpa.h"
21 #include "rsn_supp/pmksa_cache.h"
22 #include "config.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "wpas_glue.h"
26 #include "wps_supplicant.h"
27 #include "p2p_supplicant.h"
28 #include "notify.h"
29 #include "bss.h"
30 #include "scan.h"
31 #include "sme.h"
32 #include "hs20_supplicant.h"
33 
34 #define SME_AUTH_TIMEOUT 5
35 #define SME_ASSOC_TIMEOUT 5
36 
37 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
38 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
39 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
40 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
41 
42 
43 #ifdef CONFIG_SAE
44 
index_within_array(const int * array,int idx)45 static int index_within_array(const int *array, int idx)
46 {
47 	int i;
48 	for (i = 0; i < idx; i++) {
49 		if (array[i] <= 0)
50 			return 0;
51 	}
52 	return 1;
53 }
54 
55 
sme_set_sae_group(struct wpa_supplicant * wpa_s)56 static int sme_set_sae_group(struct wpa_supplicant *wpa_s)
57 {
58 	int *groups = wpa_s->conf->sae_groups;
59 	int default_groups[] = { 19, 20, 21, 0 };
60 
61 	if (!groups || groups[0] <= 0)
62 		groups = default_groups;
63 
64 	/* Configuration may have changed, so validate current index */
65 	if (!index_within_array(groups, wpa_s->sme.sae_group_index))
66 		return -1;
67 
68 	for (;;) {
69 		int group = groups[wpa_s->sme.sae_group_index];
70 		if (group <= 0)
71 			break;
72 		if (sae_set_group(&wpa_s->sme.sae, group) == 0) {
73 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
74 				wpa_s->sme.sae.group);
75 			return 0;
76 		}
77 		wpa_s->sme.sae_group_index++;
78 	}
79 
80 	return -1;
81 }
82 
83 
sme_auth_build_sae_commit(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const u8 * bssid,int external,int reuse,int * ret_use_pt,bool * ret_use_pk)84 static struct wpabuf * sme_auth_build_sae_commit(struct wpa_supplicant *wpa_s,
85 						 struct wpa_ssid *ssid,
86 						 const u8 *bssid, int external,
87 						 int reuse, int *ret_use_pt,
88 						 bool *ret_use_pk)
89 {
90 	struct wpabuf *buf;
91 	size_t len;
92 	const char *password;
93 	struct wpa_bss *bss;
94 	int use_pt = 0;
95 	bool use_pk = false;
96 	u8 rsnxe_capa = 0;
97 
98 	if (ret_use_pt)
99 		*ret_use_pt = 0;
100 	if (ret_use_pk)
101 		*ret_use_pk = false;
102 
103 #ifdef CONFIG_TESTING_OPTIONS
104 	if (wpa_s->sae_commit_override) {
105 		wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
106 		buf = wpabuf_alloc(4 + wpabuf_len(wpa_s->sae_commit_override));
107 		if (!buf)
108 			return NULL;
109 		if (!external) {
110 			wpabuf_put_le16(buf, 1); /* Transaction seq# */
111 			wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
112 		}
113 		wpabuf_put_buf(buf, wpa_s->sae_commit_override);
114 		return buf;
115 	}
116 #endif /* CONFIG_TESTING_OPTIONS */
117 
118 	password = ssid->sae_password;
119 	if (!password)
120 		password = ssid->passphrase;
121 	if (!password) {
122 		wpa_printf(MSG_DEBUG, "SAE: No password available");
123 		return NULL;
124 	}
125 
126 	if (reuse && wpa_s->sme.sae.tmp &&
127 	    os_memcmp(bssid, wpa_s->sme.sae.tmp->bssid, ETH_ALEN) == 0) {
128 		wpa_printf(MSG_DEBUG,
129 			   "SAE: Reuse previously generated PWE on a retry with the same AP");
130 		use_pt = wpa_s->sme.sae.h2e;
131 		use_pk = wpa_s->sme.sae.pk;
132 		goto reuse_data;
133 	}
134 	if (sme_set_sae_group(wpa_s) < 0) {
135 		wpa_printf(MSG_DEBUG, "SAE: Failed to select group");
136 		return NULL;
137 	}
138 
139 	bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
140 	if (!bss) {
141 		wpa_printf(MSG_DEBUG,
142 			   "SAE: BSS not available, update scan result to get BSS");
143 		wpa_supplicant_update_scan_results(wpa_s);
144 		bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
145 	}
146 	if (bss) {
147 		const u8 *rsnxe;
148 
149 		rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
150 		if (rsnxe && rsnxe[1] >= 1)
151 			rsnxe_capa = rsnxe[2];
152 	}
153 
154 	if (ssid->sae_password_id && wpa_s->conf->sae_pwe != 3)
155 		use_pt = 1;
156 #ifdef CONFIG_SAE_PK
157 	if ((rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_PK)) &&
158 	    ssid->sae_pk != SAE_PK_MODE_DISABLED &&
159 	    ((ssid->sae_password &&
160 	      sae_pk_valid_password(ssid->sae_password)) ||
161 	     (!ssid->sae_password && ssid->passphrase &&
162 	      sae_pk_valid_password(ssid->passphrase)))) {
163 		use_pt = 1;
164 		use_pk = true;
165 	}
166 
167 	if (ssid->sae_pk == SAE_PK_MODE_ONLY && !use_pk) {
168 		wpa_printf(MSG_DEBUG,
169 			   "SAE: Cannot use PK with the selected AP");
170 		return NULL;
171 	}
172 #endif /* CONFIG_SAE_PK */
173 
174 	if (use_pt || wpa_s->conf->sae_pwe == 1 || wpa_s->conf->sae_pwe == 2) {
175 		use_pt = !!(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_H2E));
176 
177 		if ((wpa_s->conf->sae_pwe == 1 || ssid->sae_password_id) &&
178 		    wpa_s->conf->sae_pwe != 3 &&
179 		    !use_pt) {
180 			wpa_printf(MSG_DEBUG,
181 				   "SAE: Cannot use H2E with the selected AP");
182 			return NULL;
183 		}
184 	}
185 
186 	if (use_pt &&
187 	    sae_prepare_commit_pt(&wpa_s->sme.sae, ssid->pt,
188 				  wpa_s->own_addr, bssid,
189 				  wpa_s->sme.sae_rejected_groups, NULL) < 0)
190 		return NULL;
191 	if (!use_pt &&
192 	    sae_prepare_commit(wpa_s->own_addr, bssid,
193 			       (u8 *) password, os_strlen(password),
194 			       &wpa_s->sme.sae) < 0) {
195 		wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
196 		return NULL;
197 	}
198 	if (wpa_s->sme.sae.tmp) {
199 		os_memcpy(wpa_s->sme.sae.tmp->bssid, bssid, ETH_ALEN);
200 		if (use_pt && use_pk)
201 			wpa_s->sme.sae.pk = 1;
202 #ifdef CONFIG_SAE_PK
203 		os_memcpy(wpa_s->sme.sae.tmp->own_addr, wpa_s->own_addr,
204 			  ETH_ALEN);
205 		os_memcpy(wpa_s->sme.sae.tmp->peer_addr, bssid, ETH_ALEN);
206 		sae_pk_set_password(&wpa_s->sme.sae, password);
207 #endif /* CONFIG_SAE_PK */
208 	}
209 
210 reuse_data:
211 	len = wpa_s->sme.sae_token ? 3 + wpabuf_len(wpa_s->sme.sae_token) : 0;
212 	if (ssid->sae_password_id)
213 		len += 4 + os_strlen(ssid->sae_password_id);
214 	buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + len);
215 	if (buf == NULL)
216 		return NULL;
217 	if (!external) {
218 		wpabuf_put_le16(buf, 1); /* Transaction seq# */
219 		if (use_pk)
220 			wpabuf_put_le16(buf, WLAN_STATUS_SAE_PK);
221 		else if (use_pt)
222 			wpabuf_put_le16(buf, WLAN_STATUS_SAE_HASH_TO_ELEMENT);
223 		else
224 			wpabuf_put_le16(buf,WLAN_STATUS_SUCCESS);
225 	}
226 	if (sae_write_commit(&wpa_s->sme.sae, buf, wpa_s->sme.sae_token,
227 			     ssid->sae_password_id) < 0) {
228 		wpabuf_free(buf);
229 		return NULL;
230 	}
231 	if (ret_use_pt)
232 		*ret_use_pt = use_pt;
233 	if (ret_use_pk)
234 		*ret_use_pk = use_pk;
235 
236 	return buf;
237 }
238 
239 
sme_auth_build_sae_confirm(struct wpa_supplicant * wpa_s,int external)240 static struct wpabuf * sme_auth_build_sae_confirm(struct wpa_supplicant *wpa_s,
241 						  int external)
242 {
243 	struct wpabuf *buf;
244 
245 	buf = wpabuf_alloc(4 + SAE_CONFIRM_MAX_LEN);
246 	if (buf == NULL)
247 		return NULL;
248 
249 	if (!external) {
250 		wpabuf_put_le16(buf, 2); /* Transaction seq# */
251 		wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
252 	}
253 	sae_write_confirm(&wpa_s->sme.sae, buf);
254 
255 	return buf;
256 }
257 
258 #endif /* CONFIG_SAE */
259 
260 
261 /**
262  * sme_auth_handle_rrm - Handle RRM aspects of current authentication attempt
263  * @wpa_s: Pointer to wpa_supplicant data
264  * @bss: Pointer to the bss which is the target of authentication attempt
265  */
sme_auth_handle_rrm(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)266 static void sme_auth_handle_rrm(struct wpa_supplicant *wpa_s,
267 				struct wpa_bss *bss)
268 {
269 	const u8 rrm_ie_len = 5;
270 	u8 *pos;
271 	const u8 *rrm_ie;
272 
273 	wpa_s->rrm.rrm_used = 0;
274 
275 	wpa_printf(MSG_DEBUG,
276 		   "RRM: Determining whether RRM can be used - device support: 0x%x",
277 		   wpa_s->drv_rrm_flags);
278 
279 	rrm_ie = wpa_bss_get_ie(bss, WLAN_EID_RRM_ENABLED_CAPABILITIES);
280 	if (!rrm_ie || !(bss->caps & IEEE80211_CAP_RRM)) {
281 		wpa_printf(MSG_DEBUG, "RRM: No RRM in network");
282 		return;
283 	}
284 
285 	if (!((wpa_s->drv_rrm_flags &
286 	       WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
287 	      (wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
288 	    !(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) {
289 		wpa_printf(MSG_DEBUG,
290 			   "RRM: Insufficient RRM support in driver - do not use RRM");
291 		return;
292 	}
293 
294 	if (sizeof(wpa_s->sme.assoc_req_ie) <
295 	    wpa_s->sme.assoc_req_ie_len + rrm_ie_len + 2) {
296 		wpa_printf(MSG_INFO,
297 			   "RRM: Unable to use RRM, no room for RRM IE");
298 		return;
299 	}
300 
301 	wpa_printf(MSG_DEBUG, "RRM: Adding RRM IE to Association Request");
302 	pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
303 	os_memset(pos, 0, 2 + rrm_ie_len);
304 	*pos++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
305 	*pos++ = rrm_ie_len;
306 
307 	/* Set supported capabilities flags */
308 	if (wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)
309 		*pos |= WLAN_RRM_CAPS_LINK_MEASUREMENT;
310 
311 	*pos |= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
312 		WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
313 		WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
314 
315 	if (wpa_s->lci)
316 		pos[1] |= WLAN_RRM_CAPS_LCI_MEASUREMENT;
317 
318 	wpa_s->sme.assoc_req_ie_len += rrm_ie_len + 2;
319 	wpa_s->rrm.rrm_used = 1;
320 }
321 
322 
sme_send_authentication(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,int start)323 static void sme_send_authentication(struct wpa_supplicant *wpa_s,
324 				    struct wpa_bss *bss, struct wpa_ssid *ssid,
325 				    int start)
326 {
327 	struct wpa_driver_auth_params params;
328 	struct wpa_ssid *old_ssid;
329 #ifdef CONFIG_IEEE80211R
330 	const u8 *ie;
331 #endif /* CONFIG_IEEE80211R */
332 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_FILS)
333 	const u8 *md = NULL;
334 #endif /* CONFIG_IEEE80211R || CONFIG_FILS */
335 	int bssid_changed;
336 	struct wpabuf *resp = NULL;
337 	u8 ext_capab[18];
338 	int ext_capab_len;
339 	int skip_auth;
340 	u8 *wpa_ie;
341 	size_t wpa_ie_len;
342 #ifdef CONFIG_MBO
343 	const u8 *mbo_ie;
344 #endif /* CONFIG_MBO */
345 	int omit_rsnxe = 0;
346 
347 	if (bss == NULL) {
348 		wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
349 			"the network");
350 		wpas_connect_work_done(wpa_s);
351 		return;
352 	}
353 
354 	skip_auth = wpa_s->conf->reassoc_same_bss_optim &&
355 		wpa_s->reassoc_same_bss;
356 	wpa_s->current_bss = bss;
357 
358 	os_memset(&params, 0, sizeof(params));
359 	wpa_s->reassociate = 0;
360 
361 	params.freq = bss->freq;
362 	params.bssid = bss->bssid;
363 	params.ssid = bss->ssid;
364 	params.ssid_len = bss->ssid_len;
365 	params.p2p = ssid->p2p_group;
366 
367 	if (wpa_s->sme.ssid_len != params.ssid_len ||
368 	    os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
369 		wpa_s->sme.prev_bssid_set = 0;
370 
371 	wpa_s->sme.freq = params.freq;
372 	os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
373 	wpa_s->sme.ssid_len = params.ssid_len;
374 
375 	params.auth_alg = WPA_AUTH_ALG_OPEN;
376 #ifdef IEEE8021X_EAPOL
377 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
378 		if (ssid->leap) {
379 			if (ssid->non_leap == 0)
380 				params.auth_alg = WPA_AUTH_ALG_LEAP;
381 			else
382 				params.auth_alg |= WPA_AUTH_ALG_LEAP;
383 		}
384 	}
385 #endif /* IEEE8021X_EAPOL */
386 	wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
387 		params.auth_alg);
388 	if (ssid->auth_alg) {
389 		params.auth_alg = ssid->auth_alg;
390 		wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
391 			"0x%x", params.auth_alg);
392 	}
393 #ifdef CONFIG_SAE
394 	wpa_s->sme.sae_pmksa_caching = 0;
395 	if (wpa_key_mgmt_sae(ssid->key_mgmt)) {
396 		const u8 *rsn;
397 		struct wpa_ie_data ied;
398 
399 		rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
400 		if (!rsn) {
401 			wpa_dbg(wpa_s, MSG_DEBUG,
402 				"SAE enabled, but target BSS does not advertise RSN");
403 #ifdef CONFIG_DPP
404 		} else if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
405 			   (ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
406 			   (ied.key_mgmt & WPA_KEY_MGMT_DPP)) {
407 			wpa_dbg(wpa_s, MSG_DEBUG, "Prefer DPP over SAE when both are enabled");
408 #endif /* CONFIG_DPP */
409 		} else if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
410 			   wpa_key_mgmt_sae(ied.key_mgmt)) {
411 			wpa_dbg(wpa_s, MSG_DEBUG, "Using SAE auth_alg");
412 			params.auth_alg = WPA_AUTH_ALG_SAE;
413 		} else {
414 			wpa_dbg(wpa_s, MSG_DEBUG,
415 				"SAE enabled, but target BSS does not advertise SAE AKM for RSN");
416 		}
417 	}
418 #endif /* CONFIG_SAE */
419 
420 #ifdef CONFIG_WEP
421 	{
422 		int i;
423 
424 		for (i = 0; i < NUM_WEP_KEYS; i++) {
425 			if (ssid->wep_key_len[i])
426 				params.wep_key[i] = ssid->wep_key[i];
427 			params.wep_key_len[i] = ssid->wep_key_len[i];
428 		}
429 		params.wep_tx_keyidx = ssid->wep_tx_keyidx;
430 	}
431 #endif /* CONFIG_WEP */
432 
433 	if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
434 	     wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
435 	    wpa_key_mgmt_wpa(ssid->key_mgmt)) {
436 		int try_opportunistic;
437 		const u8 *cache_id = NULL;
438 
439 		try_opportunistic = (ssid->proactive_key_caching < 0 ?
440 				     wpa_s->conf->okc :
441 				     ssid->proactive_key_caching) &&
442 			(ssid->proto & WPA_PROTO_RSN);
443 #ifdef CONFIG_FILS
444 		if (wpa_key_mgmt_fils(ssid->key_mgmt))
445 			cache_id = wpa_bss_get_fils_cache_id(bss);
446 #endif /* CONFIG_FILS */
447 		if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
448 					    wpa_s->current_ssid,
449 					    try_opportunistic, cache_id,
450 					    0) == 0)
451 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
452 		wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
453 		if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
454 					      wpa_s->sme.assoc_req_ie,
455 					      &wpa_s->sme.assoc_req_ie_len)) {
456 			wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
457 				"key management and encryption suites");
458 			wpas_connect_work_done(wpa_s);
459 			return;
460 		}
461 #ifdef CONFIG_HS20
462 	} else if (wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE) &&
463 		   (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)) {
464 		/* No PMKSA caching, but otherwise similar to RSN/WPA */
465 		wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
466 		if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
467 					      wpa_s->sme.assoc_req_ie,
468 					      &wpa_s->sme.assoc_req_ie_len)) {
469 			wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
470 				"key management and encryption suites");
471 			wpas_connect_work_done(wpa_s);
472 			return;
473 		}
474 #endif /* CONFIG_HS20 */
475 	} else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
476 		   wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
477 		/*
478 		 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
479 		 * use non-WPA since the scan results did not indicate that the
480 		 * AP is using WPA or WPA2.
481 		 */
482 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
483 		wpa_s->sme.assoc_req_ie_len = 0;
484 	} else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
485 		wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
486 		if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
487 					      wpa_s->sme.assoc_req_ie,
488 					      &wpa_s->sme.assoc_req_ie_len)) {
489 			wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
490 				"key management and encryption suites (no "
491 				"scan results)");
492 			wpas_connect_work_done(wpa_s);
493 			return;
494 		}
495 #ifdef CONFIG_WPS
496 	} else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
497 		struct wpabuf *wps_ie;
498 		wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
499 		if (wps_ie && wpabuf_len(wps_ie) <=
500 		    sizeof(wpa_s->sme.assoc_req_ie)) {
501 			wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
502 			os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
503 				  wpa_s->sme.assoc_req_ie_len);
504 		} else
505 			wpa_s->sme.assoc_req_ie_len = 0;
506 		wpabuf_free(wps_ie);
507 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
508 #endif /* CONFIG_WPS */
509 	} else {
510 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
511 		wpa_s->sme.assoc_req_ie_len = 0;
512 	}
513 
514 	/* In case the WPA vendor IE is used, it should be placed after all the
515 	 * non-vendor IEs, as the lower layer expects the IEs to be ordered as
516 	 * defined in the standard. Store the WPA IE so it can later be
517 	 * inserted at the correct location.
518 	 */
519 	wpa_ie = NULL;
520 	wpa_ie_len = 0;
521 	if (wpa_s->wpa_proto == WPA_PROTO_WPA) {
522 		wpa_ie = os_memdup(wpa_s->sme.assoc_req_ie,
523 				   wpa_s->sme.assoc_req_ie_len);
524 		if (wpa_ie) {
525 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Storing WPA IE");
526 
527 			wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
528 			wpa_s->sme.assoc_req_ie_len = 0;
529 		} else {
530 			wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed copy WPA IE");
531 			wpas_connect_work_done(wpa_s);
532 			return;
533 		}
534 	}
535 
536 #ifdef CONFIG_IEEE80211R
537 	ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
538 	if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
539 		md = ie + 2;
540 	wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
541 	if (md && (!wpa_key_mgmt_ft(ssid->key_mgmt) ||
542 		   !wpa_key_mgmt_ft(wpa_s->key_mgmt)))
543 		md = NULL;
544 	if (md) {
545 		/* Prepare for the next transition */
546 		wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
547 	}
548 
549 	if (md) {
550 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: FT mobility domain %02x%02x",
551 			md[0], md[1]);
552 
553 		omit_rsnxe = !wpa_bss_get_ie(bss, WLAN_EID_RSNX);
554 		if (wpa_s->sme.assoc_req_ie_len + 5 <
555 		    sizeof(wpa_s->sme.assoc_req_ie)) {
556 			struct rsn_mdie *mdie;
557 			u8 *pos = wpa_s->sme.assoc_req_ie +
558 				wpa_s->sme.assoc_req_ie_len;
559 			*pos++ = WLAN_EID_MOBILITY_DOMAIN;
560 			*pos++ = sizeof(*mdie);
561 			mdie = (struct rsn_mdie *) pos;
562 			os_memcpy(mdie->mobility_domain, md,
563 				  MOBILITY_DOMAIN_ID_LEN);
564 			mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
565 			wpa_s->sme.assoc_req_ie_len += 5;
566 		}
567 
568 		if (wpa_s->sme.prev_bssid_set && wpa_s->sme.ft_used &&
569 		    os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
570 		    wpa_sm_has_ptk(wpa_s->wpa)) {
571 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
572 				"over-the-air");
573 			params.auth_alg = WPA_AUTH_ALG_FT;
574 			params.ie = wpa_s->sme.ft_ies;
575 			params.ie_len = wpa_s->sme.ft_ies_len;
576 		}
577 	}
578 #endif /* CONFIG_IEEE80211R */
579 
580 	wpa_s->sme.mfp = wpas_get_ssid_pmf(wpa_s, ssid);
581 	if (wpa_s->sme.mfp != NO_MGMT_FRAME_PROTECTION) {
582 		const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
583 		struct wpa_ie_data _ie;
584 		if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
585 		    _ie.capabilities &
586 		    (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
587 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
588 				"MFP: require MFP");
589 			wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
590 		}
591 	}
592 
593 #ifdef CONFIG_P2P
594 	if (wpa_s->global->p2p) {
595 		u8 *pos;
596 		size_t len;
597 		int res;
598 		pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
599 		len = sizeof(wpa_s->sme.assoc_req_ie) -
600 			wpa_s->sme.assoc_req_ie_len;
601 		res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
602 					    ssid->p2p_group);
603 		if (res >= 0)
604 			wpa_s->sme.assoc_req_ie_len += res;
605 	}
606 #endif /* CONFIG_P2P */
607 
608 #ifdef CONFIG_FST
609 	if (wpa_s->fst_ies) {
610 		int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
611 
612 		if (wpa_s->sme.assoc_req_ie_len + fst_ies_len <=
613 		    sizeof(wpa_s->sme.assoc_req_ie)) {
614 			os_memcpy(wpa_s->sme.assoc_req_ie +
615 				  wpa_s->sme.assoc_req_ie_len,
616 				  wpabuf_head(wpa_s->fst_ies),
617 				  fst_ies_len);
618 			wpa_s->sme.assoc_req_ie_len += fst_ies_len;
619 		}
620 	}
621 #endif /* CONFIG_FST */
622 
623 	sme_auth_handle_rrm(wpa_s, bss);
624 
625 	wpa_s->sme.assoc_req_ie_len += wpas_supp_op_class_ie(
626 		wpa_s, ssid, bss,
627 		wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
628 		sizeof(wpa_s->sme.assoc_req_ie) - wpa_s->sme.assoc_req_ie_len);
629 
630 	if (params.p2p)
631 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
632 	else
633 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
634 
635 	ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
636 					     sizeof(ext_capab));
637 	if (ext_capab_len > 0) {
638 		u8 *pos = wpa_s->sme.assoc_req_ie;
639 		if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
640 			pos += 2 + pos[1];
641 		os_memmove(pos + ext_capab_len, pos,
642 			   wpa_s->sme.assoc_req_ie_len -
643 			   (pos - wpa_s->sme.assoc_req_ie));
644 		wpa_s->sme.assoc_req_ie_len += ext_capab_len;
645 		os_memcpy(pos, ext_capab, ext_capab_len);
646 	}
647 
648 #ifdef CONFIG_TESTING_OPTIONS
649 	if (wpa_s->rsnxe_override_assoc &&
650 	    wpabuf_len(wpa_s->rsnxe_override_assoc) <=
651 	    sizeof(wpa_s->sme.assoc_req_ie) - wpa_s->sme.assoc_req_ie_len) {
652 		wpa_printf(MSG_DEBUG, "TESTING: RSNXE AssocReq override");
653 		os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
654 			  wpabuf_head(wpa_s->rsnxe_override_assoc),
655 			  wpabuf_len(wpa_s->rsnxe_override_assoc));
656 		wpa_s->sme.assoc_req_ie_len +=
657 			wpabuf_len(wpa_s->rsnxe_override_assoc);
658 	} else
659 #endif /* CONFIG_TESTING_OPTIONS */
660 	if (wpa_s->rsnxe_len > 0 &&
661 	    wpa_s->rsnxe_len <=
662 	    sizeof(wpa_s->sme.assoc_req_ie) - wpa_s->sme.assoc_req_ie_len &&
663 	    !omit_rsnxe) {
664 		os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
665 			  wpa_s->rsnxe, wpa_s->rsnxe_len);
666 		wpa_s->sme.assoc_req_ie_len += wpa_s->rsnxe_len;
667 	}
668 
669 #ifdef CONFIG_HS20
670 	if (is_hs20_network(wpa_s, ssid, bss)
671 #ifndef ANDROID /* Android does not use the native HS 2.0 config */
672 			&& is_hs20_config(wpa_s)
673 #endif /* ANDROID */
674 	) {
675 		struct wpabuf *hs20;
676 
677 		hs20 = wpabuf_alloc(20 + MAX_ROAMING_CONS_OI_LEN);
678 		if (hs20) {
679 			int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
680 			size_t len;
681 
682 			wpas_hs20_add_indication(hs20, pps_mo_id,
683 						 get_hs20_version(bss));
684 			wpas_hs20_add_roam_cons_sel(hs20, ssid);
685 			len = sizeof(wpa_s->sme.assoc_req_ie) -
686 				wpa_s->sme.assoc_req_ie_len;
687 			if (wpabuf_len(hs20) <= len) {
688 				os_memcpy(wpa_s->sme.assoc_req_ie +
689 					  wpa_s->sme.assoc_req_ie_len,
690 					  wpabuf_head(hs20), wpabuf_len(hs20));
691 				wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
692 			}
693 			wpabuf_free(hs20);
694 		}
695 	}
696 #endif /* CONFIG_HS20 */
697 
698 	if (wpa_ie) {
699 		size_t len;
700 
701 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Reinsert WPA IE");
702 
703 		len = sizeof(wpa_s->sme.assoc_req_ie) -
704 			wpa_s->sme.assoc_req_ie_len;
705 
706 		if (len > wpa_ie_len) {
707 			os_memcpy(wpa_s->sme.assoc_req_ie +
708 				  wpa_s->sme.assoc_req_ie_len,
709 				  wpa_ie, wpa_ie_len);
710 			wpa_s->sme.assoc_req_ie_len += wpa_ie_len;
711 		} else {
712 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Failed to add WPA IE");
713 		}
714 
715 		os_free(wpa_ie);
716 	}
717 
718 	if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
719 		struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
720 		size_t len;
721 
722 		len = sizeof(wpa_s->sme.assoc_req_ie) -
723 			wpa_s->sme.assoc_req_ie_len;
724 		if (wpabuf_len(buf) <= len) {
725 			os_memcpy(wpa_s->sme.assoc_req_ie +
726 				  wpa_s->sme.assoc_req_ie_len,
727 				  wpabuf_head(buf), wpabuf_len(buf));
728 			wpa_s->sme.assoc_req_ie_len += wpabuf_len(buf);
729 		}
730 	}
731 
732 #ifdef CONFIG_MBO
733 	mbo_ie = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
734 	if (!wpa_s->disable_mbo_oce && mbo_ie) {
735 		int len;
736 
737 		len = wpas_mbo_ie(wpa_s, wpa_s->sme.assoc_req_ie +
738 				  wpa_s->sme.assoc_req_ie_len,
739 				  sizeof(wpa_s->sme.assoc_req_ie) -
740 				  wpa_s->sme.assoc_req_ie_len,
741 				  !!mbo_attr_from_mbo_ie(mbo_ie,
742 							 OCE_ATTR_ID_CAPA_IND));
743 		if (len >= 0)
744 			wpa_s->sme.assoc_req_ie_len += len;
745 	}
746 #endif /* CONFIG_MBO */
747 
748 #ifdef CONFIG_SAE
749 	if (!skip_auth && params.auth_alg == WPA_AUTH_ALG_SAE &&
750 	    pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid, ssid, 0,
751 				    NULL,
752 				    wpa_s->key_mgmt == WPA_KEY_MGMT_FT_SAE ?
753 				    WPA_KEY_MGMT_FT_SAE :
754 				    WPA_KEY_MGMT_SAE) == 0) {
755 		wpa_dbg(wpa_s, MSG_DEBUG,
756 			"PMKSA cache entry found - try to use PMKSA caching instead of new SAE authentication");
757 		wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
758 		params.auth_alg = WPA_AUTH_ALG_OPEN;
759 		wpa_s->sme.sae_pmksa_caching = 1;
760 	}
761 
762 	if (!skip_auth && params.auth_alg == WPA_AUTH_ALG_SAE) {
763 		if (start)
764 			resp = sme_auth_build_sae_commit(wpa_s, ssid,
765 							 bss->bssid, 0,
766 							 start == 2, NULL,
767 							 NULL);
768 		else
769 			resp = sme_auth_build_sae_confirm(wpa_s, 0);
770 		if (resp == NULL) {
771 			wpas_connection_failed(wpa_s, bss->bssid);
772 			return;
773 		}
774 		params.auth_data = wpabuf_head(resp);
775 		params.auth_data_len = wpabuf_len(resp);
776 		wpa_s->sme.sae.state = start ? SAE_COMMITTED : SAE_CONFIRMED;
777 	}
778 #endif /* CONFIG_SAE */
779 
780 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
781 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
782 	os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
783 	if (bssid_changed)
784 		wpas_notify_bssid_changed(wpa_s);
785 
786 	old_ssid = wpa_s->current_ssid;
787 	wpa_s->current_ssid = ssid;
788 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
789 	wpa_supplicant_initiate_eapol(wpa_s);
790 
791 #ifdef CONFIG_FILS
792 	/* TODO: FILS operations can in some cases be done between different
793 	 * network_ctx (i.e., same credentials can be used with multiple
794 	 * networks). */
795 	if (params.auth_alg == WPA_AUTH_ALG_OPEN &&
796 	    wpa_key_mgmt_fils(ssid->key_mgmt)) {
797 		const u8 *indic;
798 		u16 fils_info;
799 		const u8 *realm, *username, *rrk;
800 		size_t realm_len, username_len, rrk_len;
801 		u16 next_seq_num;
802 
803 		/*
804 		 * Check FILS Indication element (FILS Information field) bits
805 		 * indicating supported authentication algorithms against local
806 		 * configuration (ssid->fils_dh_group). Try to use FILS
807 		 * authentication only if the AP supports the combination in the
808 		 * network profile. */
809 		indic = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
810 		if (!indic || indic[1] < 2) {
811 			wpa_printf(MSG_DEBUG, "SME: " MACSTR
812 				   " does not include FILS Indication element - cannot use FILS authentication with it",
813 				   MAC2STR(bss->bssid));
814 			goto no_fils;
815 		}
816 
817 		fils_info = WPA_GET_LE16(indic + 2);
818 		if (ssid->fils_dh_group == 0 && !(fils_info & BIT(9))) {
819 			wpa_printf(MSG_DEBUG, "SME: " MACSTR
820 				   " does not support FILS SK without PFS - cannot use FILS authentication with it",
821 				   MAC2STR(bss->bssid));
822 			goto no_fils;
823 		}
824 		if (ssid->fils_dh_group != 0 && !(fils_info & BIT(10))) {
825 			wpa_printf(MSG_DEBUG, "SME: " MACSTR
826 				   " does not support FILS SK with PFS - cannot use FILS authentication with it",
827 				   MAC2STR(bss->bssid));
828 			goto no_fils;
829 		}
830 
831 		if (wpa_s->last_con_fail_realm &&
832 		    eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap,
833 					  &username, &username_len,
834 					  &realm, &realm_len, &next_seq_num,
835 					  &rrk, &rrk_len) == 0 &&
836 		    realm && realm_len == wpa_s->last_con_fail_realm_len &&
837 		    os_memcmp(realm, wpa_s->last_con_fail_realm,
838 			      realm_len) == 0) {
839 			wpa_printf(MSG_DEBUG,
840 				   "SME: FILS authentication for this realm failed last time - try to regenerate ERP key hierarchy");
841 			goto no_fils;
842 		}
843 
844 		if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
845 					    ssid, 0,
846 					    wpa_bss_get_fils_cache_id(bss),
847 					    0) == 0)
848 			wpa_printf(MSG_DEBUG,
849 				   "SME: Try to use FILS with PMKSA caching");
850 		resp = fils_build_auth(wpa_s->wpa, ssid->fils_dh_group, md);
851 		if (resp) {
852 			int auth_alg;
853 
854 			if (ssid->fils_dh_group)
855 				wpa_printf(MSG_DEBUG,
856 					   "SME: Try to use FILS SK authentication with PFS (DH Group %u)",
857 					   ssid->fils_dh_group);
858 			else
859 				wpa_printf(MSG_DEBUG,
860 					   "SME: Try to use FILS SK authentication without PFS");
861 			auth_alg = ssid->fils_dh_group ?
862 				WPA_AUTH_ALG_FILS_SK_PFS : WPA_AUTH_ALG_FILS;
863 			params.auth_alg = auth_alg;
864 			params.auth_data = wpabuf_head(resp);
865 			params.auth_data_len = wpabuf_len(resp);
866 			wpa_s->sme.auth_alg = auth_alg;
867 		}
868 	}
869 no_fils:
870 #endif /* CONFIG_FILS */
871 
872 	wpa_supplicant_cancel_sched_scan(wpa_s);
873 	wpa_supplicant_cancel_scan(wpa_s);
874 
875 	wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
876 		" (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
877 		wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
878 
879 	eapol_sm_notify_portValid(wpa_s->eapol, false);
880 	wpa_clear_keys(wpa_s, bss->bssid);
881 	wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
882 	if (old_ssid != wpa_s->current_ssid)
883 		wpas_notify_network_changed(wpa_s);
884 
885 #ifdef CONFIG_HS20
886 	hs20_configure_frame_filters(wpa_s);
887 #endif /* CONFIG_HS20 */
888 
889 #ifdef CONFIG_P2P
890 	/*
891 	 * If multi-channel concurrency is not supported, check for any
892 	 * frequency conflict. In case of any frequency conflict, remove the
893 	 * least prioritized connection.
894 	 */
895 	if (wpa_s->num_multichan_concurrent < 2) {
896 		int freq, num;
897 		num = get_shared_radio_freqs(wpa_s, &freq, 1);
898 		if (num > 0 && freq > 0 && freq != params.freq) {
899 			wpa_printf(MSG_DEBUG,
900 				   "Conflicting frequency found (%d != %d)",
901 				   freq, params.freq);
902 			if (wpas_p2p_handle_frequency_conflicts(wpa_s,
903 								params.freq,
904 								ssid) < 0) {
905 				wpas_connection_failed(wpa_s, bss->bssid);
906 				wpa_supplicant_mark_disassoc(wpa_s);
907 				wpabuf_free(resp);
908 				wpas_connect_work_done(wpa_s);
909 				return;
910 			}
911 		}
912 	}
913 #endif /* CONFIG_P2P */
914 
915 	if (skip_auth) {
916 		wpa_msg(wpa_s, MSG_DEBUG,
917 			"SME: Skip authentication step on reassoc-to-same-BSS");
918 		wpabuf_free(resp);
919 		sme_associate(wpa_s, ssid->mode, bss->bssid, WLAN_AUTH_OPEN);
920 		return;
921 	}
922 
923 
924 	wpa_s->sme.auth_alg = params.auth_alg;
925 	if (wpa_drv_authenticate(wpa_s, &params) < 0) {
926 		wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
927 			"driver failed");
928 		wpas_connection_failed(wpa_s, bss->bssid);
929 		wpa_supplicant_mark_disassoc(wpa_s);
930 		wpabuf_free(resp);
931 		wpas_connect_work_done(wpa_s);
932 		return;
933 	}
934 
935 	eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
936 			       NULL);
937 
938 	/*
939 	 * Association will be started based on the authentication event from
940 	 * the driver.
941 	 */
942 
943 	wpabuf_free(resp);
944 }
945 
946 
sme_auth_start_cb(struct wpa_radio_work * work,int deinit)947 static void sme_auth_start_cb(struct wpa_radio_work *work, int deinit)
948 {
949 	struct wpa_connect_work *cwork = work->ctx;
950 	struct wpa_supplicant *wpa_s = work->wpa_s;
951 
952 	wpa_s->roam_in_progress = false;
953 #ifdef CONFIG_WNM
954 	wpa_s->bss_trans_mgmt_in_progress = false;
955 #endif /* CONFIG_WNM */
956 
957 	if (deinit) {
958 		if (work->started)
959 			wpa_s->connect_work = NULL;
960 
961 		wpas_connect_work_free(cwork);
962 		return;
963 	}
964 
965 	wpa_s->connect_work = work;
966 
967 	if (cwork->bss_removed ||
968 	    !wpas_valid_bss_ssid(wpa_s, cwork->bss, cwork->ssid) ||
969 	    wpas_network_disabled(wpa_s, cwork->ssid)) {
970 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: BSS/SSID entry for authentication not valid anymore - drop connection attempt");
971 		wpas_connect_work_done(wpa_s);
972 		return;
973 	}
974 
975 	/* Starting new connection, so clear the possibly used WPA IE from the
976 	 * previous association. */
977 	wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
978 	wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
979 	wpa_s->rsnxe_len = 0;
980 
981 	sme_send_authentication(wpa_s, cwork->bss, cwork->ssid, 1);
982 }
983 
984 
sme_authenticate(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid)985 void sme_authenticate(struct wpa_supplicant *wpa_s,
986 		      struct wpa_bss *bss, struct wpa_ssid *ssid)
987 {
988 	struct wpa_connect_work *cwork;
989 
990 	if (bss == NULL || ssid == NULL)
991 		return;
992 	if (wpa_s->connect_work) {
993 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reject sme_authenticate() call since connect_work exist");
994 		return;
995 	}
996 
997 	if (wpa_s->roam_in_progress) {
998 		wpa_dbg(wpa_s, MSG_DEBUG,
999 			"SME: Reject sme_authenticate() in favor of explicit roam request");
1000 		return;
1001 	}
1002 #ifdef CONFIG_WNM
1003 	if (wpa_s->bss_trans_mgmt_in_progress) {
1004 		wpa_dbg(wpa_s, MSG_DEBUG,
1005 			"SME: Reject sme_authenticate() in favor of BSS transition management request");
1006 		return;
1007 	}
1008 #endif /* CONFIG_WNM */
1009 	if (radio_work_pending(wpa_s, "sme-connect")) {
1010 		/*
1011 		 * The previous sme-connect work might no longer be valid due to
1012 		 * the fact that the BSS list was updated. In addition, it makes
1013 		 * sense to adhere to the 'newer' decision.
1014 		 */
1015 		wpa_dbg(wpa_s, MSG_DEBUG,
1016 			"SME: Remove previous pending sme-connect");
1017 		radio_remove_works(wpa_s, "sme-connect", 0);
1018 	}
1019 
1020 	wpas_abort_ongoing_scan(wpa_s);
1021 
1022 	cwork = os_zalloc(sizeof(*cwork));
1023 	if (cwork == NULL)
1024 		return;
1025 	cwork->bss = bss;
1026 	cwork->ssid = ssid;
1027 	cwork->sme = 1;
1028 
1029 #ifdef CONFIG_SAE
1030 	wpa_s->sme.sae.state = SAE_NOTHING;
1031 	wpa_s->sme.sae.send_confirm = 0;
1032 	wpa_s->sme.sae_group_index = 0;
1033 #endif /* CONFIG_SAE */
1034 
1035 	if (radio_add_work(wpa_s, bss->freq, "sme-connect", 1,
1036 			   sme_auth_start_cb, cwork) < 0)
1037 		wpas_connect_work_free(cwork);
1038 }
1039 
1040 
1041 #ifdef CONFIG_SAE
1042 
sme_external_auth_build_buf(struct wpabuf * buf,struct wpabuf * params,const u8 * sa,const u8 * da,u16 auth_transaction,u16 seq_num,u16 status_code)1043 static int sme_external_auth_build_buf(struct wpabuf *buf,
1044 				       struct wpabuf *params,
1045 				       const u8 *sa, const u8 *da,
1046 				       u16 auth_transaction, u16 seq_num,
1047 				       u16 status_code)
1048 {
1049 	struct ieee80211_mgmt *resp;
1050 
1051 	resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
1052 					u.auth.variable));
1053 
1054 	resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1055 					   (WLAN_FC_STYPE_AUTH << 4));
1056 	os_memcpy(resp->da, da, ETH_ALEN);
1057 	os_memcpy(resp->sa, sa, ETH_ALEN);
1058 	os_memcpy(resp->bssid, da, ETH_ALEN);
1059 	resp->u.auth.auth_alg = host_to_le16(WLAN_AUTH_SAE);
1060 	resp->seq_ctrl = host_to_le16(seq_num << 4);
1061 	resp->u.auth.auth_transaction = host_to_le16(auth_transaction);
1062 	resp->u.auth.status_code = host_to_le16(status_code);
1063 	if (params)
1064 		wpabuf_put_buf(buf, params);
1065 
1066 	return 0;
1067 }
1068 
1069 
sme_external_auth_send_sae_commit(struct wpa_supplicant * wpa_s,const u8 * bssid,struct wpa_ssid * ssid)1070 static int sme_external_auth_send_sae_commit(struct wpa_supplicant *wpa_s,
1071 					     const u8 *bssid,
1072 					     struct wpa_ssid *ssid)
1073 {
1074 	struct wpabuf *resp, *buf;
1075 	int use_pt;
1076 	bool use_pk;
1077 	u16 status;
1078 
1079 	resp = sme_auth_build_sae_commit(wpa_s, ssid, bssid, 1, 0, &use_pt,
1080 					 &use_pk);
1081 	if (!resp) {
1082 		wpa_printf(MSG_DEBUG, "SAE: Failed to build SAE commit");
1083 		return -1;
1084 	}
1085 
1086 	wpa_s->sme.sae.state = SAE_COMMITTED;
1087 	buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + wpabuf_len(resp));
1088 	if (!buf) {
1089 		wpabuf_free(resp);
1090 		return -1;
1091 	}
1092 
1093 	wpa_s->sme.seq_num++;
1094 	if (use_pk)
1095 		status = WLAN_STATUS_SAE_PK;
1096 	else if (use_pt)
1097 		status = WLAN_STATUS_SAE_HASH_TO_ELEMENT;
1098 	else
1099 		status = WLAN_STATUS_SUCCESS;
1100 	sme_external_auth_build_buf(buf, resp, wpa_s->own_addr,
1101 				    bssid, 1, wpa_s->sme.seq_num, status);
1102 	wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1, 0, 0);
1103 	wpabuf_free(resp);
1104 	wpabuf_free(buf);
1105 
1106 	return 0;
1107 }
1108 
1109 
sme_send_external_auth_status(struct wpa_supplicant * wpa_s,u16 status)1110 static void sme_send_external_auth_status(struct wpa_supplicant *wpa_s,
1111 					  u16 status)
1112 {
1113 	struct external_auth params;
1114 
1115 	os_memset(&params, 0, sizeof(params));
1116 	params.status = status;
1117 	params.ssid = wpa_s->sme.ext_auth_ssid;
1118 	params.ssid_len = wpa_s->sme.ext_auth_ssid_len;
1119 	params.bssid = wpa_s->sme.ext_auth_bssid;
1120 	if (wpa_s->conf->sae_pmkid_in_assoc && status == WLAN_STATUS_SUCCESS)
1121 		params.pmkid = wpa_s->sme.sae.pmkid;
1122 	wpa_drv_send_external_auth_status(wpa_s, &params);
1123 }
1124 
1125 
sme_handle_external_auth_start(struct wpa_supplicant * wpa_s,union wpa_event_data * data)1126 static int sme_handle_external_auth_start(struct wpa_supplicant *wpa_s,
1127 					  union wpa_event_data *data)
1128 {
1129 	struct wpa_ssid *ssid;
1130 	size_t ssid_str_len = data->external_auth.ssid_len;
1131 	const u8 *ssid_str = data->external_auth.ssid;
1132 
1133 	/* Get the SSID conf from the ssid string obtained */
1134 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1135 		if (!wpas_network_disabled(wpa_s, ssid) &&
1136 		    ssid_str_len == ssid->ssid_len &&
1137 		    os_memcmp(ssid_str, ssid->ssid, ssid_str_len) == 0 &&
1138 		    (ssid->key_mgmt & (WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE)))
1139 			break;
1140 	}
1141 	if (!ssid ||
1142 	    sme_external_auth_send_sae_commit(wpa_s, data->external_auth.bssid,
1143 					      ssid) < 0)
1144 		return -1;
1145 
1146 	return 0;
1147 }
1148 
1149 
sme_external_auth_send_sae_confirm(struct wpa_supplicant * wpa_s,const u8 * da)1150 static void sme_external_auth_send_sae_confirm(struct wpa_supplicant *wpa_s,
1151 					       const u8 *da)
1152 {
1153 	struct wpabuf *resp, *buf;
1154 
1155 	resp = sme_auth_build_sae_confirm(wpa_s, 1);
1156 	if (!resp) {
1157 		wpa_printf(MSG_DEBUG, "SAE: Confirm message buf alloc failure");
1158 		return;
1159 	}
1160 
1161 	wpa_s->sme.sae.state = SAE_CONFIRMED;
1162 	buf = wpabuf_alloc(4 + SAE_CONFIRM_MAX_LEN + wpabuf_len(resp));
1163 	if (!buf) {
1164 		wpa_printf(MSG_DEBUG, "SAE: Auth Confirm buf alloc failure");
1165 		wpabuf_free(resp);
1166 		return;
1167 	}
1168 	wpa_s->sme.seq_num++;
1169 	sme_external_auth_build_buf(buf, resp, wpa_s->own_addr,
1170 				    da, 2, wpa_s->sme.seq_num,
1171 				    WLAN_STATUS_SUCCESS);
1172 	wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1, 0, 0);
1173 	wpabuf_free(resp);
1174 	wpabuf_free(buf);
1175 }
1176 
1177 
sme_external_auth_trigger(struct wpa_supplicant * wpa_s,union wpa_event_data * data)1178 void sme_external_auth_trigger(struct wpa_supplicant *wpa_s,
1179 			       union wpa_event_data *data)
1180 {
1181 	if (RSN_SELECTOR_GET(&data->external_auth.key_mgmt_suite) !=
1182 	    RSN_AUTH_KEY_MGMT_SAE)
1183 		return;
1184 
1185 	if (data->external_auth.action == EXT_AUTH_START) {
1186 		if (!data->external_auth.bssid || !data->external_auth.ssid)
1187 			return;
1188 		os_memcpy(wpa_s->sme.ext_auth_bssid, data->external_auth.bssid,
1189 			  ETH_ALEN);
1190 		os_memcpy(wpa_s->sme.ext_auth_ssid, data->external_auth.ssid,
1191 			  data->external_auth.ssid_len);
1192 		wpa_s->sme.ext_auth_ssid_len = data->external_auth.ssid_len;
1193 		wpa_s->sme.seq_num = 0;
1194 		wpa_s->sme.sae.state = SAE_NOTHING;
1195 		wpa_s->sme.sae.send_confirm = 0;
1196 		wpa_s->sme.sae_group_index = 0;
1197 		if (sme_handle_external_auth_start(wpa_s, data) < 0)
1198 			sme_send_external_auth_status(wpa_s,
1199 					      WLAN_STATUS_UNSPECIFIED_FAILURE);
1200 	} else if (data->external_auth.action == EXT_AUTH_ABORT) {
1201 		/* Report failure to driver for the wrong trigger */
1202 		sme_send_external_auth_status(wpa_s,
1203 					      WLAN_STATUS_UNSPECIFIED_FAILURE);
1204 	}
1205 }
1206 
1207 
sme_sae_is_group_enabled(struct wpa_supplicant * wpa_s,int group)1208 static int sme_sae_is_group_enabled(struct wpa_supplicant *wpa_s, int group)
1209 {
1210 	int *groups = wpa_s->conf->sae_groups;
1211 	int default_groups[] = { 19, 20, 21, 0 };
1212 	int i;
1213 
1214 	if (!groups)
1215 		groups = default_groups;
1216 
1217 	for (i = 0; groups[i] > 0; i++) {
1218 		if (groups[i] == group)
1219 			return 1;
1220 	}
1221 
1222 	return 0;
1223 }
1224 
1225 
sme_check_sae_rejected_groups(struct wpa_supplicant * wpa_s,const struct wpabuf * groups)1226 static int sme_check_sae_rejected_groups(struct wpa_supplicant *wpa_s,
1227 					 const struct wpabuf *groups)
1228 {
1229 	size_t i, count;
1230 	const u8 *pos;
1231 
1232 	if (!groups)
1233 		return 0;
1234 
1235 	pos = wpabuf_head(groups);
1236 	count = wpabuf_len(groups) / 2;
1237 	for (i = 0; i < count; i++) {
1238 		int enabled;
1239 		u16 group;
1240 
1241 		group = WPA_GET_LE16(pos);
1242 		pos += 2;
1243 		enabled = sme_sae_is_group_enabled(wpa_s, group);
1244 		wpa_printf(MSG_DEBUG, "SAE: Rejected group %u is %s",
1245 			   group, enabled ? "enabled" : "disabled");
1246 		if (enabled)
1247 			return 1;
1248 	}
1249 
1250 	return 0;
1251 }
1252 
1253 
sme_sae_auth(struct wpa_supplicant * wpa_s,u16 auth_transaction,u16 status_code,const u8 * data,size_t len,int external,const u8 * sa)1254 static int sme_sae_auth(struct wpa_supplicant *wpa_s, u16 auth_transaction,
1255 			u16 status_code, const u8 *data, size_t len,
1256 			int external, const u8 *sa)
1257 {
1258 	int *groups;
1259 
1260 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE authentication transaction %u "
1261 		"status code %u", auth_transaction, status_code);
1262 
1263 	if (auth_transaction == 1 &&
1264 	    status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
1265 	    wpa_s->sme.sae.state == SAE_COMMITTED &&
1266 	    (external || wpa_s->current_bss) && wpa_s->current_ssid) {
1267 		int default_groups[] = { 19, 20, 21, 0 };
1268 		u16 group;
1269 		const u8 *token_pos;
1270 		size_t token_len;
1271 		int h2e = 0;
1272 
1273 		groups = wpa_s->conf->sae_groups;
1274 		if (!groups || groups[0] <= 0)
1275 			groups = default_groups;
1276 
1277 		wpa_hexdump(MSG_DEBUG, "SME: SAE anti-clogging token request",
1278 			    data, len);
1279 		if (len < sizeof(le16)) {
1280 			wpa_dbg(wpa_s, MSG_DEBUG,
1281 				"SME: Too short SAE anti-clogging token request");
1282 			return -1;
1283 		}
1284 		group = WPA_GET_LE16(data);
1285 		wpa_dbg(wpa_s, MSG_DEBUG,
1286 			"SME: SAE anti-clogging token requested (group %u)",
1287 			group);
1288 		if (sae_group_allowed(&wpa_s->sme.sae, groups, group) !=
1289 		    WLAN_STATUS_SUCCESS) {
1290 			wpa_dbg(wpa_s, MSG_ERROR,
1291 				"SME: SAE group %u of anti-clogging request is invalid",
1292 				group);
1293 			return -1;
1294 		}
1295 		wpabuf_free(wpa_s->sme.sae_token);
1296 		token_pos = data + sizeof(le16);
1297 		token_len = len - sizeof(le16);
1298 		h2e = wpa_s->sme.sae.h2e;
1299 		if (h2e) {
1300 			if (token_len < 3) {
1301 				wpa_dbg(wpa_s, MSG_DEBUG,
1302 					"SME: Too short SAE anti-clogging token container");
1303 				return -1;
1304 			}
1305 			if (token_pos[0] != WLAN_EID_EXTENSION ||
1306 			    token_pos[1] == 0 ||
1307 			    token_pos[1] > token_len - 2 ||
1308 			    token_pos[2] != WLAN_EID_EXT_ANTI_CLOGGING_TOKEN) {
1309 				wpa_dbg(wpa_s, MSG_DEBUG,
1310 					"SME: Invalid SAE anti-clogging token container header");
1311 				return -1;
1312 			}
1313 			token_len = token_pos[1] - 1;
1314 			token_pos += 3;
1315 		}
1316 		wpa_s->sme.sae_token = wpabuf_alloc_copy(token_pos, token_len);
1317 		wpa_hexdump_buf(MSG_DEBUG, "SME: Requested anti-clogging token",
1318 				wpa_s->sme.sae_token);
1319 		if (!external)
1320 			sme_send_authentication(wpa_s, wpa_s->current_bss,
1321 						wpa_s->current_ssid, 2);
1322 		else
1323 			sme_external_auth_send_sae_commit(
1324 				wpa_s, wpa_s->sme.ext_auth_bssid,
1325 				wpa_s->current_ssid);
1326 		return 0;
1327 	}
1328 
1329 	if (auth_transaction == 1 &&
1330 	    status_code == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1331 	    wpa_s->sme.sae.state == SAE_COMMITTED &&
1332 	    (external || wpa_s->current_bss) && wpa_s->current_ssid) {
1333 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE group not supported");
1334 		int_array_add_unique(&wpa_s->sme.sae_rejected_groups,
1335 				     wpa_s->sme.sae.group);
1336 		wpa_s->sme.sae_group_index++;
1337 		if (sme_set_sae_group(wpa_s) < 0)
1338 			return -1; /* no other groups enabled */
1339 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Try next enabled SAE group");
1340 		if (!external)
1341 			sme_send_authentication(wpa_s, wpa_s->current_bss,
1342 						wpa_s->current_ssid, 1);
1343 		else
1344 			sme_external_auth_send_sae_commit(
1345 				wpa_s, wpa_s->sme.ext_auth_bssid,
1346 				wpa_s->current_ssid);
1347 		return 0;
1348 	}
1349 
1350 	if (auth_transaction == 1 &&
1351 	    status_code == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER) {
1352 		const u8 *bssid = sa ? sa : wpa_s->pending_bssid;
1353 
1354 		wpa_msg(wpa_s, MSG_INFO,
1355 			WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER MACSTR,
1356 			MAC2STR(bssid));
1357 		return -1;
1358 	}
1359 
1360 	if (status_code != WLAN_STATUS_SUCCESS &&
1361 	    status_code != WLAN_STATUS_SAE_HASH_TO_ELEMENT &&
1362 	    status_code != WLAN_STATUS_SAE_PK) {
1363 		const u8 *bssid = sa ? sa : wpa_s->pending_bssid;
1364 
1365 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AUTH_REJECT MACSTR
1366 			" auth_type=%u auth_transaction=%u status_code=%u",
1367 			MAC2STR(bssid), WLAN_AUTH_SAE,
1368 			auth_transaction, status_code);
1369 		return -2;
1370 	}
1371 
1372 	if (auth_transaction == 1) {
1373 		u16 res;
1374 
1375 		groups = wpa_s->conf->sae_groups;
1376 
1377 		wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE commit");
1378 		if ((!external && wpa_s->current_bss == NULL) ||
1379 		    wpa_s->current_ssid == NULL)
1380 			return -1;
1381 		if (wpa_s->sme.sae.state != SAE_COMMITTED) {
1382 			wpa_printf(MSG_DEBUG,
1383 				   "SAE: Ignore commit message while waiting for confirm");
1384 			return 0;
1385 		}
1386 		if (wpa_s->sme.sae.h2e && status_code == WLAN_STATUS_SUCCESS) {
1387 			wpa_printf(MSG_DEBUG,
1388 				   "SAE: Unexpected use of status code 0 in SAE commit when H2E was expected");
1389 			return -1;
1390 		}
1391 		if ((!wpa_s->sme.sae.h2e || wpa_s->sme.sae.pk) &&
1392 		    status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT) {
1393 			wpa_printf(MSG_DEBUG,
1394 				   "SAE: Unexpected use of status code for H2E in SAE commit when H2E was not expected");
1395 			return -1;
1396 		}
1397 		if (!wpa_s->sme.sae.pk &&
1398 		    status_code == WLAN_STATUS_SAE_PK) {
1399 			wpa_printf(MSG_DEBUG,
1400 				   "SAE: Unexpected use of status code for PK in SAE commit when PK was not expected");
1401 			return -1;
1402 		}
1403 
1404 		if (groups && groups[0] <= 0)
1405 			groups = NULL;
1406 		res = sae_parse_commit(&wpa_s->sme.sae, data, len, NULL, NULL,
1407 				       groups, status_code ==
1408 				       WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1409 				       status_code == WLAN_STATUS_SAE_PK);
1410 		if (res == SAE_SILENTLY_DISCARD) {
1411 			wpa_printf(MSG_DEBUG,
1412 				   "SAE: Drop commit message due to reflection attack");
1413 			return 0;
1414 		}
1415 		if (res != WLAN_STATUS_SUCCESS)
1416 			return -1;
1417 
1418 		if (wpa_s->sme.sae.tmp &&
1419 		    sme_check_sae_rejected_groups(
1420 			    wpa_s,
1421 			    wpa_s->sme.sae.tmp->peer_rejected_groups))
1422 			return -1;
1423 
1424 		if (sae_process_commit(&wpa_s->sme.sae) < 0) {
1425 			wpa_printf(MSG_DEBUG, "SAE: Failed to process peer "
1426 				   "commit");
1427 			return -1;
1428 		}
1429 
1430 		wpabuf_free(wpa_s->sme.sae_token);
1431 		wpa_s->sme.sae_token = NULL;
1432 		if (!external)
1433 			sme_send_authentication(wpa_s, wpa_s->current_bss,
1434 						wpa_s->current_ssid, 0);
1435 		else
1436 			sme_external_auth_send_sae_confirm(wpa_s, sa);
1437 		return 0;
1438 	} else if (auth_transaction == 2) {
1439 		if (status_code != WLAN_STATUS_SUCCESS)
1440 			return -1;
1441 		wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE confirm");
1442 		if (wpa_s->sme.sae.state != SAE_CONFIRMED)
1443 			return -1;
1444 		if (sae_check_confirm(&wpa_s->sme.sae, data, len) < 0)
1445 			return -1;
1446 		wpa_s->sme.sae.state = SAE_ACCEPTED;
1447 		sae_clear_temp_data(&wpa_s->sme.sae);
1448 
1449 		if (external) {
1450 			/* Report success to driver */
1451 			sme_send_external_auth_status(wpa_s,
1452 						      WLAN_STATUS_SUCCESS);
1453 		}
1454 
1455 		return 1;
1456 	}
1457 
1458 	return -1;
1459 }
1460 
1461 
sme_sae_set_pmk(struct wpa_supplicant * wpa_s,const u8 * bssid)1462 static int sme_sae_set_pmk(struct wpa_supplicant *wpa_s, const u8 *bssid)
1463 {
1464 	wpa_printf(MSG_DEBUG,
1465 		   "SME: SAE completed - setting PMK for 4-way handshake");
1466 	wpa_sm_set_pmk(wpa_s->wpa, wpa_s->sme.sae.pmk, PMK_LEN,
1467 		       wpa_s->sme.sae.pmkid, bssid);
1468 	if (wpa_s->conf->sae_pmkid_in_assoc) {
1469 		/* Update the own RSNE contents now that we have set the PMK
1470 		 * and added a PMKSA cache entry based on the successfully
1471 		 * completed SAE exchange. In practice, this will add the PMKID
1472 		 * into RSNE. */
1473 		if (wpa_s->sme.assoc_req_ie_len + 2 + PMKID_LEN >
1474 		    sizeof(wpa_s->sme.assoc_req_ie)) {
1475 			wpa_msg(wpa_s, MSG_WARNING,
1476 				"RSN: Not enough room for inserting own PMKID into RSNE");
1477 			return -1;
1478 		}
1479 		if (wpa_insert_pmkid(wpa_s->sme.assoc_req_ie,
1480 				     &wpa_s->sme.assoc_req_ie_len,
1481 				     wpa_s->sme.sae.pmkid) < 0)
1482 			return -1;
1483 		wpa_hexdump(MSG_DEBUG,
1484 			    "SME: Updated Association Request IEs",
1485 			    wpa_s->sme.assoc_req_ie,
1486 			    wpa_s->sme.assoc_req_ie_len);
1487 	}
1488 
1489 	return 0;
1490 }
1491 
1492 
sme_external_auth_mgmt_rx(struct wpa_supplicant * wpa_s,const u8 * auth_frame,size_t len)1493 void sme_external_auth_mgmt_rx(struct wpa_supplicant *wpa_s,
1494 			       const u8 *auth_frame, size_t len)
1495 {
1496 	const struct ieee80211_mgmt *header;
1497 	size_t auth_length;
1498 
1499 	header = (const struct ieee80211_mgmt *) auth_frame;
1500 	auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
1501 
1502 	if (len < auth_length) {
1503 		/* Notify failure to the driver */
1504 		sme_send_external_auth_status(wpa_s,
1505 					      WLAN_STATUS_UNSPECIFIED_FAILURE);
1506 		return;
1507 	}
1508 
1509 	if (le_to_host16(header->u.auth.auth_alg) == WLAN_AUTH_SAE) {
1510 		int res;
1511 
1512 		res = sme_sae_auth(
1513 			wpa_s, le_to_host16(header->u.auth.auth_transaction),
1514 			le_to_host16(header->u.auth.status_code),
1515 			header->u.auth.variable,
1516 			len - auth_length, 1, header->sa);
1517 		if (res < 0) {
1518 			/* Notify failure to the driver */
1519 			sme_send_external_auth_status(
1520 				wpa_s,
1521 				res == -2 ?
1522 				le_to_host16(header->u.auth.status_code) :
1523 				WLAN_STATUS_UNSPECIFIED_FAILURE);
1524 			return;
1525 		}
1526 		if (res != 1)
1527 			return;
1528 
1529 		if (sme_sae_set_pmk(wpa_s, wpa_s->sme.ext_auth_bssid) < 0)
1530 			return;
1531 	}
1532 }
1533 
1534 #endif /* CONFIG_SAE */
1535 
1536 
sme_event_auth(struct wpa_supplicant * wpa_s,union wpa_event_data * data)1537 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
1538 {
1539 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1540 
1541 	if (ssid == NULL) {
1542 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
1543 			"when network is not selected");
1544 		return;
1545 	}
1546 
1547 	if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
1548 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
1549 			"when not in authenticating state");
1550 		return;
1551 	}
1552 
1553 	if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
1554 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
1555 			"unexpected peer " MACSTR,
1556 			MAC2STR(data->auth.peer));
1557 		return;
1558 	}
1559 
1560 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
1561 		" auth_type=%d auth_transaction=%d status_code=%d",
1562 		MAC2STR(data->auth.peer), data->auth.auth_type,
1563 		data->auth.auth_transaction, data->auth.status_code);
1564 	wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
1565 		    data->auth.ies, data->auth.ies_len);
1566 
1567 	eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1568 
1569 #ifdef CONFIG_SAE
1570 	if (data->auth.auth_type == WLAN_AUTH_SAE) {
1571 		int res;
1572 		res = sme_sae_auth(wpa_s, data->auth.auth_transaction,
1573 				   data->auth.status_code, data->auth.ies,
1574 				   data->auth.ies_len, 0, data->auth.peer);
1575 		if (res < 0) {
1576 			wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1577 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1578 
1579 		}
1580 		if (res != 1)
1581 			return;
1582 
1583 		if (sme_sae_set_pmk(wpa_s, wpa_s->pending_bssid) < 0)
1584 			return;
1585 	}
1586 #endif /* CONFIG_SAE */
1587 
1588 	if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
1589 		char *ie_txt = NULL;
1590 
1591 		if (data->auth.ies && data->auth.ies_len) {
1592 			size_t buflen = 2 * data->auth.ies_len + 1;
1593 			ie_txt = os_malloc(buflen);
1594 			if (ie_txt) {
1595 				wpa_snprintf_hex(ie_txt, buflen, data->auth.ies,
1596 						 data->auth.ies_len);
1597 			}
1598 		}
1599 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AUTH_REJECT MACSTR
1600 			" auth_type=%u auth_transaction=%u status_code=%u%s%s",
1601 			MAC2STR(data->auth.peer), data->auth.auth_type,
1602 			data->auth.auth_transaction, data->auth.status_code,
1603 			ie_txt ? " ie=" : "",
1604 			ie_txt ? ie_txt : "");
1605 		os_free(ie_txt);
1606 
1607 #ifdef CONFIG_FILS
1608 		if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS ||
1609 		    wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS_SK_PFS)
1610 			fils_connection_failure(wpa_s);
1611 #endif /* CONFIG_FILS */
1612 
1613 		if (data->auth.status_code !=
1614 		    WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
1615 		    wpa_s->sme.auth_alg == data->auth.auth_type ||
1616 		    wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
1617 			wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1618 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1619 			return;
1620 		}
1621 
1622 		wpas_connect_work_done(wpa_s);
1623 
1624 		switch (data->auth.auth_type) {
1625 		case WLAN_AUTH_OPEN:
1626 			wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
1627 
1628 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
1629 			wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
1630 						 wpa_s->current_ssid);
1631 			return;
1632 
1633 		case WLAN_AUTH_SHARED_KEY:
1634 			wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
1635 
1636 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
1637 			wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
1638 						 wpa_s->current_ssid);
1639 			return;
1640 
1641 		default:
1642 			return;
1643 		}
1644 	}
1645 
1646 #ifdef CONFIG_IEEE80211R
1647 	if (data->auth.auth_type == WLAN_AUTH_FT) {
1648 		const u8 *ric_ies = NULL;
1649 		size_t ric_ies_len = 0;
1650 
1651 		if (wpa_s->ric_ies) {
1652 			ric_ies = wpabuf_head(wpa_s->ric_ies);
1653 			ric_ies_len = wpabuf_len(wpa_s->ric_ies);
1654 		}
1655 		if (wpa_ft_process_response(wpa_s->wpa, data->auth.ies,
1656 					    data->auth.ies_len, 0,
1657 					    data->auth.peer,
1658 					    ric_ies, ric_ies_len) < 0) {
1659 			wpa_dbg(wpa_s, MSG_DEBUG,
1660 				"SME: FT Authentication response processing failed");
1661 			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
1662 				MACSTR
1663 				" reason=%d locally_generated=1",
1664 				MAC2STR(wpa_s->pending_bssid),
1665 				WLAN_REASON_DEAUTH_LEAVING);
1666 			wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1667 			wpa_supplicant_mark_disassoc(wpa_s);
1668 			return;
1669 		}
1670 	}
1671 #endif /* CONFIG_IEEE80211R */
1672 
1673 #ifdef CONFIG_FILS
1674 	if (data->auth.auth_type == WLAN_AUTH_FILS_SK ||
1675 	    data->auth.auth_type == WLAN_AUTH_FILS_SK_PFS) {
1676 		u16 expect_auth_type;
1677 
1678 		expect_auth_type = wpa_s->sme.auth_alg ==
1679 			WPA_AUTH_ALG_FILS_SK_PFS ? WLAN_AUTH_FILS_SK_PFS :
1680 			WLAN_AUTH_FILS_SK;
1681 		if (data->auth.auth_type != expect_auth_type) {
1682 			wpa_dbg(wpa_s, MSG_DEBUG,
1683 				"SME: FILS Authentication response used different auth alg (%u; expected %u)",
1684 				data->auth.auth_type, expect_auth_type);
1685 			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
1686 				MACSTR
1687 				" reason=%d locally_generated=1",
1688 				MAC2STR(wpa_s->pending_bssid),
1689 				WLAN_REASON_DEAUTH_LEAVING);
1690 			wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1691 			wpa_supplicant_mark_disassoc(wpa_s);
1692 			return;
1693 		}
1694 
1695 		if (fils_process_auth(wpa_s->wpa, wpa_s->pending_bssid,
1696 				      data->auth.ies, data->auth.ies_len) < 0) {
1697 			wpa_dbg(wpa_s, MSG_DEBUG,
1698 				"SME: FILS Authentication response processing failed");
1699 			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
1700 				MACSTR
1701 				" reason=%d locally_generated=1",
1702 				MAC2STR(wpa_s->pending_bssid),
1703 				WLAN_REASON_DEAUTH_LEAVING);
1704 			wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1705 			wpa_supplicant_mark_disassoc(wpa_s);
1706 			return;
1707 		}
1708 	}
1709 #endif /* CONFIG_FILS */
1710 
1711 	sme_associate(wpa_s, ssid->mode, data->auth.peer,
1712 		      data->auth.auth_type);
1713 }
1714 
1715 
1716 #ifdef CONFIG_IEEE80211R
remove_ie(u8 * buf,size_t * len,u8 eid)1717 static void remove_ie(u8 *buf, size_t *len, u8 eid)
1718 {
1719 	u8 *pos, *next, *end;
1720 
1721 	pos = (u8 *) get_ie(buf, *len, eid);
1722 	if (pos) {
1723 		next = pos + 2 + pos[1];
1724 		end = buf + *len;
1725 		*len -= 2 + pos[1];
1726 		os_memmove(pos, next, end - next);
1727 	}
1728 }
1729 #endif /* CONFIG_IEEE80211R */
1730 
1731 
sme_associate(struct wpa_supplicant * wpa_s,enum wpas_mode mode,const u8 * bssid,u16 auth_type)1732 void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
1733 		   const u8 *bssid, u16 auth_type)
1734 {
1735 	struct wpa_driver_associate_params params;
1736 	struct ieee802_11_elems elems;
1737 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1738 #ifdef CONFIG_FILS
1739 	u8 nonces[2 * FILS_NONCE_LEN];
1740 #endif /* CONFIG_FILS */
1741 #ifdef CONFIG_HT_OVERRIDES
1742 	struct ieee80211_ht_capabilities htcaps;
1743 	struct ieee80211_ht_capabilities htcaps_mask;
1744 #endif /* CONFIG_HT_OVERRIDES */
1745 #ifdef CONFIG_VHT_OVERRIDES
1746 	struct ieee80211_vht_capabilities vhtcaps;
1747 	struct ieee80211_vht_capabilities vhtcaps_mask;
1748 #endif /* CONFIG_VHT_OVERRIDES */
1749 
1750 	os_memset(&params, 0, sizeof(params));
1751 
1752 #ifdef CONFIG_FILS
1753 	if (auth_type == WLAN_AUTH_FILS_SK ||
1754 	    auth_type == WLAN_AUTH_FILS_SK_PFS) {
1755 		struct wpabuf *buf;
1756 		const u8 *snonce, *anonce;
1757 		const unsigned int max_hlp = 20;
1758 		struct wpabuf *hlp[max_hlp];
1759 		unsigned int i, num_hlp = 0;
1760 		struct fils_hlp_req *req;
1761 
1762 		dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
1763 				 list) {
1764 			hlp[num_hlp] = wpabuf_alloc(2 * ETH_ALEN + 6 +
1765 					      wpabuf_len(req->pkt));
1766 			if (!hlp[num_hlp])
1767 				break;
1768 			wpabuf_put_data(hlp[num_hlp], req->dst, ETH_ALEN);
1769 			wpabuf_put_data(hlp[num_hlp], wpa_s->own_addr,
1770 					ETH_ALEN);
1771 			wpabuf_put_data(hlp[num_hlp],
1772 					"\xaa\xaa\x03\x00\x00\x00", 6);
1773 			wpabuf_put_buf(hlp[num_hlp], req->pkt);
1774 			num_hlp++;
1775 			if (num_hlp >= max_hlp)
1776 				break;
1777 		}
1778 
1779 		buf = fils_build_assoc_req(wpa_s->wpa, &params.fils_kek,
1780 					   &params.fils_kek_len, &snonce,
1781 					   &anonce,
1782 					   (const struct wpabuf **) hlp,
1783 					   num_hlp);
1784 		for (i = 0; i < num_hlp; i++)
1785 			wpabuf_free(hlp[i]);
1786 		if (!buf)
1787 			return;
1788 		wpa_hexdump(MSG_DEBUG, "FILS: assoc_req before FILS elements",
1789 			    wpa_s->sme.assoc_req_ie,
1790 			    wpa_s->sme.assoc_req_ie_len);
1791 #ifdef CONFIG_IEEE80211R
1792 		if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
1793 			/* Remove RSNE and MDE to allow them to be overridden
1794 			 * with FILS+FT specific values from
1795 			 * fils_build_assoc_req(). */
1796 			remove_ie(wpa_s->sme.assoc_req_ie,
1797 				  &wpa_s->sme.assoc_req_ie_len,
1798 				  WLAN_EID_RSN);
1799 			wpa_hexdump(MSG_DEBUG,
1800 				    "FILS: assoc_req after RSNE removal",
1801 				    wpa_s->sme.assoc_req_ie,
1802 				    wpa_s->sme.assoc_req_ie_len);
1803 			remove_ie(wpa_s->sme.assoc_req_ie,
1804 				  &wpa_s->sme.assoc_req_ie_len,
1805 				  WLAN_EID_MOBILITY_DOMAIN);
1806 			wpa_hexdump(MSG_DEBUG,
1807 				    "FILS: assoc_req after MDE removal",
1808 				    wpa_s->sme.assoc_req_ie,
1809 				    wpa_s->sme.assoc_req_ie_len);
1810 		}
1811 #endif /* CONFIG_IEEE80211R */
1812 		/* TODO: Make wpa_s->sme.assoc_req_ie use dynamic allocation */
1813 		if (wpa_s->sme.assoc_req_ie_len + wpabuf_len(buf) >
1814 		    sizeof(wpa_s->sme.assoc_req_ie)) {
1815 			wpa_printf(MSG_ERROR,
1816 				   "FILS: Not enough buffer room for own AssocReq elements");
1817 			wpabuf_free(buf);
1818 			return;
1819 		}
1820 		os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
1821 			  wpabuf_head(buf), wpabuf_len(buf));
1822 		wpa_s->sme.assoc_req_ie_len += wpabuf_len(buf);
1823 		wpabuf_free(buf);
1824 		wpa_hexdump(MSG_DEBUG, "FILS: assoc_req after FILS elements",
1825 			    wpa_s->sme.assoc_req_ie,
1826 			    wpa_s->sme.assoc_req_ie_len);
1827 
1828 		os_memcpy(nonces, snonce, FILS_NONCE_LEN);
1829 		os_memcpy(nonces + FILS_NONCE_LEN, anonce, FILS_NONCE_LEN);
1830 		params.fils_nonces = nonces;
1831 		params.fils_nonces_len = sizeof(nonces);
1832 	}
1833 #endif /* CONFIG_FILS */
1834 
1835 #ifdef CONFIG_OWE
1836 #ifdef CONFIG_TESTING_OPTIONS
1837 	if (get_ie_ext(wpa_s->sme.assoc_req_ie, wpa_s->sme.assoc_req_ie_len,
1838 		       WLAN_EID_EXT_OWE_DH_PARAM)) {
1839 		wpa_printf(MSG_INFO, "TESTING: Override OWE DH element");
1840 	} else
1841 #endif /* CONFIG_TESTING_OPTIONS */
1842 	if (auth_type == WLAN_AUTH_OPEN &&
1843 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE) {
1844 		struct wpabuf *owe_ie;
1845 		u16 group;
1846 
1847 		if (ssid && ssid->owe_group) {
1848 			group = ssid->owe_group;
1849 		} else if (wpa_s->assoc_status_code ==
1850 			   WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
1851 			if (wpa_s->last_owe_group == 19)
1852 				group = 20;
1853 			else if (wpa_s->last_owe_group == 20)
1854 				group = 21;
1855 			else
1856 				group = OWE_DH_GROUP;
1857 		} else {
1858 			group = OWE_DH_GROUP;
1859 		}
1860 
1861 		wpa_s->last_owe_group = group;
1862 		wpa_printf(MSG_DEBUG, "OWE: Try to use group %u", group);
1863 		owe_ie = owe_build_assoc_req(wpa_s->wpa, group);
1864 		if (!owe_ie) {
1865 			wpa_printf(MSG_ERROR,
1866 				   "OWE: Failed to build IE for Association Request frame");
1867 			return;
1868 		}
1869 		if (wpa_s->sme.assoc_req_ie_len + wpabuf_len(owe_ie) >
1870 		    sizeof(wpa_s->sme.assoc_req_ie)) {
1871 			wpa_printf(MSG_ERROR,
1872 				   "OWE: Not enough buffer room for own Association Request frame elements");
1873 			wpabuf_free(owe_ie);
1874 			return;
1875 		}
1876 		os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
1877 			  wpabuf_head(owe_ie), wpabuf_len(owe_ie));
1878 		wpa_s->sme.assoc_req_ie_len += wpabuf_len(owe_ie);
1879 		wpabuf_free(owe_ie);
1880 	}
1881 #endif /* CONFIG_OWE */
1882 
1883 #ifdef CONFIG_DPP2
1884 	if (DPP_VERSION > 1 && wpa_s->key_mgmt == WPA_KEY_MGMT_DPP && ssid &&
1885 	    ssid->dpp_netaccesskey && ssid->dpp_pfs != 2 &&
1886 	    !ssid->dpp_pfs_fallback) {
1887 		struct rsn_pmksa_cache_entry *pmksa;
1888 
1889 		pmksa = pmksa_cache_get_current(wpa_s->wpa);
1890 		if (!pmksa || !pmksa->dpp_pfs)
1891 			goto pfs_fail;
1892 
1893 		dpp_pfs_free(wpa_s->dpp_pfs);
1894 		wpa_s->dpp_pfs = dpp_pfs_init(ssid->dpp_netaccesskey,
1895 					      ssid->dpp_netaccesskey_len);
1896 		if (!wpa_s->dpp_pfs) {
1897 			wpa_printf(MSG_DEBUG, "DPP: Could not initialize PFS");
1898 			/* Try to continue without PFS */
1899 			goto pfs_fail;
1900 		}
1901 		if (wpa_s->sme.assoc_req_ie_len +
1902 		    wpabuf_len(wpa_s->dpp_pfs->ie) >
1903 		    sizeof(wpa_s->sme.assoc_req_ie)) {
1904 			wpa_printf(MSG_ERROR,
1905 				   "DPP: Not enough buffer room for own Association Request frame elements");
1906 			dpp_pfs_free(wpa_s->dpp_pfs);
1907 			wpa_s->dpp_pfs = NULL;
1908 			goto pfs_fail;
1909 		}
1910 		os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
1911 			  wpabuf_head(wpa_s->dpp_pfs->ie),
1912 			  wpabuf_len(wpa_s->dpp_pfs->ie));
1913 		wpa_s->sme.assoc_req_ie_len += wpabuf_len(wpa_s->dpp_pfs->ie);
1914 	}
1915 pfs_fail:
1916 #endif /* CONFIG_DPP2 */
1917 
1918 	wpa_s->mscs_setup_done = false;
1919 	if (wpa_bss_ext_capab(wpa_s->current_bss, WLAN_EXT_CAPAB_MSCS) &&
1920 	    wpa_s->robust_av.valid_config) {
1921 		struct wpabuf *mscs_ie;
1922 		size_t mscs_ie_len, buf_len, *wpa_ie_len, max_ie_len;
1923 
1924 		buf_len = 3 +	/* MSCS descriptor IE header */
1925 			  1 +	/* Request type */
1926 			  2 +	/* User priority control */
1927 			  4 +	/* Stream timeout */
1928 			  3 +	/* TCLAS Mask IE header */
1929 			  wpa_s->robust_av.frame_classifier_len;
1930 		mscs_ie = wpabuf_alloc(buf_len);
1931 		if (!mscs_ie) {
1932 			wpa_printf(MSG_INFO,
1933 				   "MSCS: Failed to allocate MSCS IE");
1934 			goto mscs_fail;
1935 		}
1936 
1937 		wpa_ie_len = &wpa_s->sme.assoc_req_ie_len;
1938 		max_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
1939 		wpas_populate_mscs_descriptor_ie(&wpa_s->robust_av, mscs_ie);
1940 		if ((*wpa_ie_len + wpabuf_len(mscs_ie)) <= max_ie_len) {
1941 			wpa_hexdump_buf(MSG_MSGDUMP, "MSCS IE", mscs_ie);
1942 			mscs_ie_len = wpabuf_len(mscs_ie);
1943 			os_memcpy(wpa_s->sme.assoc_req_ie + *wpa_ie_len,
1944 				  wpabuf_head(mscs_ie), mscs_ie_len);
1945 			*wpa_ie_len += mscs_ie_len;
1946 		}
1947 
1948 		wpabuf_free(mscs_ie);
1949 	}
1950 mscs_fail:
1951 
1952 	if (ssid && ssid->multi_ap_backhaul_sta) {
1953 		size_t multi_ap_ie_len;
1954 
1955 		multi_ap_ie_len = add_multi_ap_ie(
1956 			wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
1957 			sizeof(wpa_s->sme.assoc_req_ie) -
1958 			wpa_s->sme.assoc_req_ie_len,
1959 			MULTI_AP_BACKHAUL_STA);
1960 		if (multi_ap_ie_len == 0) {
1961 			wpa_printf(MSG_ERROR,
1962 				   "Multi-AP: Failed to build Multi-AP IE");
1963 			return;
1964 		}
1965 		wpa_s->sme.assoc_req_ie_len += multi_ap_ie_len;
1966 	}
1967 
1968 	params.bssid = bssid;
1969 	params.ssid = wpa_s->sme.ssid;
1970 	params.ssid_len = wpa_s->sme.ssid_len;
1971 	params.freq.freq = wpa_s->sme.freq;
1972 	params.bg_scan_period = ssid ? ssid->bg_scan_period : -1;
1973 	params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
1974 		wpa_s->sme.assoc_req_ie : NULL;
1975 	params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
1976 	wpa_hexdump(MSG_DEBUG, "SME: Association Request IEs",
1977 		    params.wpa_ie, params.wpa_ie_len);
1978 	params.pairwise_suite = wpa_s->pairwise_cipher;
1979 	params.group_suite = wpa_s->group_cipher;
1980 	params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
1981 	params.key_mgmt_suite = wpa_s->key_mgmt;
1982 	params.wpa_proto = wpa_s->wpa_proto;
1983 #ifdef CONFIG_HT_OVERRIDES
1984 	os_memset(&htcaps, 0, sizeof(htcaps));
1985 	os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1986 	params.htcaps = (u8 *) &htcaps;
1987 	params.htcaps_mask = (u8 *) &htcaps_mask;
1988 	wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1989 #endif /* CONFIG_HT_OVERRIDES */
1990 #ifdef CONFIG_VHT_OVERRIDES
1991 	os_memset(&vhtcaps, 0, sizeof(vhtcaps));
1992 	os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
1993 	params.vhtcaps = &vhtcaps;
1994 	params.vhtcaps_mask = &vhtcaps_mask;
1995 	wpa_supplicant_apply_vht_overrides(wpa_s, ssid, &params);
1996 #endif /* CONFIG_VHT_OVERRIDES */
1997 #ifdef CONFIG_HE_OVERRIDES
1998 	wpa_supplicant_apply_he_overrides(wpa_s, ssid, &params);
1999 #endif /* CONFIG_HE_OVERRIDES */
2000 #ifdef CONFIG_IEEE80211R
2001 	if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies &&
2002 	    get_ie(wpa_s->sme.ft_ies, wpa_s->sme.ft_ies_len,
2003 		   WLAN_EID_RIC_DATA)) {
2004 		/* There seems to be a pretty inconvenient bug in the Linux
2005 		 * kernel IE splitting functionality when RIC is used. For now,
2006 		 * skip correct behavior in IE construction here (i.e., drop the
2007 		 * additional non-FT-specific IEs) to avoid kernel issues. This
2008 		 * is fine since RIC is used only for testing purposes in the
2009 		 * current implementation. */
2010 		wpa_printf(MSG_INFO,
2011 			   "SME: Linux kernel workaround - do not try to include additional IEs with RIC");
2012 		params.wpa_ie = wpa_s->sme.ft_ies;
2013 		params.wpa_ie_len = wpa_s->sme.ft_ies_len;
2014 	} else if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
2015 		const u8 *rm_en, *pos, *end;
2016 		size_t rm_en_len = 0;
2017 		u8 *rm_en_dup = NULL, *wpos;
2018 
2019 		/* Remove RSNE, MDE, FTE to allow them to be overridden with
2020 		 * FT specific values */
2021 		remove_ie(wpa_s->sme.assoc_req_ie,
2022 			  &wpa_s->sme.assoc_req_ie_len,
2023 			  WLAN_EID_RSN);
2024 		remove_ie(wpa_s->sme.assoc_req_ie,
2025 			  &wpa_s->sme.assoc_req_ie_len,
2026 			  WLAN_EID_MOBILITY_DOMAIN);
2027 		remove_ie(wpa_s->sme.assoc_req_ie,
2028 			  &wpa_s->sme.assoc_req_ie_len,
2029 			  WLAN_EID_FAST_BSS_TRANSITION);
2030 		rm_en = get_ie(wpa_s->sme.assoc_req_ie,
2031 			       wpa_s->sme.assoc_req_ie_len,
2032 			       WLAN_EID_RRM_ENABLED_CAPABILITIES);
2033 		if (rm_en) {
2034 			/* Need to remove RM Enabled Capabilities element as
2035 			 * well temporarily, so that it can be placed between
2036 			 * RSNE and MDE. */
2037 			rm_en_len = 2 + rm_en[1];
2038 			rm_en_dup = os_memdup(rm_en, rm_en_len);
2039 			remove_ie(wpa_s->sme.assoc_req_ie,
2040 				  &wpa_s->sme.assoc_req_ie_len,
2041 				  WLAN_EID_RRM_ENABLED_CAPABILITIES);
2042 		}
2043 		wpa_hexdump(MSG_DEBUG,
2044 			    "SME: Association Request IEs after FT IE removal",
2045 			    wpa_s->sme.assoc_req_ie,
2046 			    wpa_s->sme.assoc_req_ie_len);
2047 		if (wpa_s->sme.assoc_req_ie_len + wpa_s->sme.ft_ies_len +
2048 		    rm_en_len > sizeof(wpa_s->sme.assoc_req_ie)) {
2049 			wpa_printf(MSG_ERROR,
2050 				   "SME: Not enough buffer room for FT IEs in Association Request frame");
2051 			os_free(rm_en_dup);
2052 			return;
2053 		}
2054 
2055 		os_memmove(wpa_s->sme.assoc_req_ie + wpa_s->sme.ft_ies_len +
2056 			   rm_en_len,
2057 			   wpa_s->sme.assoc_req_ie,
2058 			   wpa_s->sme.assoc_req_ie_len);
2059 		pos = wpa_s->sme.ft_ies;
2060 		end = pos + wpa_s->sme.ft_ies_len;
2061 		wpos = wpa_s->sme.assoc_req_ie;
2062 		if (*pos == WLAN_EID_RSN) {
2063 			os_memcpy(wpos, pos, 2 + pos[1]);
2064 			wpos += 2 + pos[1];
2065 			pos += 2 + pos[1];
2066 		}
2067 		if (rm_en_dup) {
2068 			os_memcpy(wpos, rm_en_dup, rm_en_len);
2069 			wpos += rm_en_len;
2070 			os_free(rm_en_dup);
2071 		}
2072 		os_memcpy(wpos, pos, end - pos);
2073 		wpa_s->sme.assoc_req_ie_len += wpa_s->sme.ft_ies_len +
2074 			rm_en_len;
2075 		params.wpa_ie = wpa_s->sme.assoc_req_ie;
2076 		params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
2077 		wpa_hexdump(MSG_DEBUG,
2078 			    "SME: Association Request IEs after FT override",
2079 			    params.wpa_ie, params.wpa_ie_len);
2080 	}
2081 #endif /* CONFIG_IEEE80211R */
2082 	params.mode = mode;
2083 	params.mgmt_frame_protection = wpa_s->sme.mfp;
2084 	params.rrm_used = wpa_s->rrm.rrm_used;
2085 	if (wpa_s->sme.prev_bssid_set)
2086 		params.prev_bssid = wpa_s->sme.prev_bssid;
2087 
2088 	wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
2089 		" (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
2090 		params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
2091 		params.freq.freq);
2092 
2093 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
2094 
2095 	if (params.wpa_ie == NULL ||
2096 	    ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
2097 	    < 0) {
2098 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
2099 		os_memset(&elems, 0, sizeof(elems));
2100 	}
2101 	if (elems.rsn_ie) {
2102 		params.wpa_proto = WPA_PROTO_RSN;
2103 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
2104 					elems.rsn_ie_len + 2);
2105 	} else if (elems.wpa_ie) {
2106 		params.wpa_proto = WPA_PROTO_WPA;
2107 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
2108 					elems.wpa_ie_len + 2);
2109 	} else if (elems.osen) {
2110 		params.wpa_proto = WPA_PROTO_OSEN;
2111 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.osen - 2,
2112 					elems.osen_len + 2);
2113 	} else
2114 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
2115 	if (elems.rsnxe)
2116 		wpa_sm_set_assoc_rsnxe(wpa_s->wpa, elems.rsnxe - 2,
2117 				       elems.rsnxe_len + 2);
2118 	else
2119 		wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
2120 	if (ssid && ssid->p2p_group)
2121 		params.p2p = 1;
2122 
2123 	if (wpa_s->p2pdev->set_sta_uapsd)
2124 		params.uapsd = wpa_s->p2pdev->sta_uapsd;
2125 	else
2126 		params.uapsd = -1;
2127 
2128 	if (wpa_drv_associate(wpa_s, &params) < 0) {
2129 		wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
2130 			"driver failed");
2131 		wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
2132 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2133 		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
2134 		return;
2135 	}
2136 
2137 	eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
2138 			       NULL);
2139 
2140 #ifdef CONFIG_TESTING_OPTIONS
2141 	wpabuf_free(wpa_s->last_assoc_req_wpa_ie);
2142 	wpa_s->last_assoc_req_wpa_ie = NULL;
2143 	if (params.wpa_ie)
2144 		wpa_s->last_assoc_req_wpa_ie =
2145 			wpabuf_alloc_copy(params.wpa_ie, params.wpa_ie_len);
2146 #endif /* CONFIG_TESTING_OPTIONS */
2147 }
2148 
2149 
sme_update_ft_ies(struct wpa_supplicant * wpa_s,const u8 * md,const u8 * ies,size_t ies_len)2150 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
2151 		      const u8 *ies, size_t ies_len)
2152 {
2153 	if (md == NULL || ies == NULL) {
2154 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
2155 		os_free(wpa_s->sme.ft_ies);
2156 		wpa_s->sme.ft_ies = NULL;
2157 		wpa_s->sme.ft_ies_len = 0;
2158 		wpa_s->sme.ft_used = 0;
2159 		return 0;
2160 	}
2161 
2162 	os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
2163 	wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
2164 	os_free(wpa_s->sme.ft_ies);
2165 	wpa_s->sme.ft_ies = os_memdup(ies, ies_len);
2166 	if (wpa_s->sme.ft_ies == NULL)
2167 		return -1;
2168 	wpa_s->sme.ft_ies_len = ies_len;
2169 	return 0;
2170 }
2171 
2172 
sme_deauth(struct wpa_supplicant * wpa_s)2173 static void sme_deauth(struct wpa_supplicant *wpa_s)
2174 {
2175 	int bssid_changed;
2176 
2177 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
2178 
2179 	if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
2180 				   WLAN_REASON_DEAUTH_LEAVING) < 0) {
2181 		wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
2182 			"failed");
2183 	}
2184 	wpa_s->sme.prev_bssid_set = 0;
2185 
2186 	wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
2187 	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2188 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
2189 	os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
2190 	if (bssid_changed)
2191 		wpas_notify_bssid_changed(wpa_s);
2192 }
2193 
2194 
sme_event_assoc_reject(struct wpa_supplicant * wpa_s,union wpa_event_data * data)2195 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
2196 			    union wpa_event_data *data)
2197 {
2198 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
2199 		"status code %d", MAC2STR(wpa_s->pending_bssid),
2200 		data->assoc_reject.status_code);
2201 
2202 	eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
2203 
2204 #ifdef CONFIG_SAE
2205 	if (wpa_s->sme.sae_pmksa_caching && wpa_s->current_ssid &&
2206 	    wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt)) {
2207 		wpa_dbg(wpa_s, MSG_DEBUG,
2208 			"PMKSA caching attempt rejected - drop PMKSA cache entry and fall back to SAE authentication");
2209 		wpa_sm_aborted_cached(wpa_s->wpa);
2210 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
2211 		if (wpa_s->current_bss) {
2212 			struct wpa_bss *bss = wpa_s->current_bss;
2213 			struct wpa_ssid *ssid = wpa_s->current_ssid;
2214 
2215 			wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
2216 					       WLAN_REASON_DEAUTH_LEAVING);
2217 			wpas_connect_work_done(wpa_s);
2218 			wpa_supplicant_mark_disassoc(wpa_s);
2219 			wpa_supplicant_connect(wpa_s, bss, ssid);
2220 			return;
2221 		}
2222 	}
2223 #endif /* CONFIG_SAE */
2224 
2225 	/*
2226 	 * For now, unconditionally terminate the previous authentication. In
2227 	 * theory, this should not be needed, but mac80211 gets quite confused
2228 	 * if the authentication is left pending.. Some roaming cases might
2229 	 * benefit from using the previous authentication, so this could be
2230 	 * optimized in the future.
2231 	 */
2232 	sme_deauth(wpa_s);
2233 }
2234 
2235 
sme_event_auth_timed_out(struct wpa_supplicant * wpa_s,union wpa_event_data * data)2236 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
2237 			      union wpa_event_data *data)
2238 {
2239 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
2240 	wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
2241 	wpa_supplicant_mark_disassoc(wpa_s);
2242 }
2243 
2244 
sme_event_assoc_timed_out(struct wpa_supplicant * wpa_s,union wpa_event_data * data)2245 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
2246 			       union wpa_event_data *data)
2247 {
2248 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
2249 	wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
2250 	wpa_supplicant_mark_disassoc(wpa_s);
2251 }
2252 
2253 
sme_event_disassoc(struct wpa_supplicant * wpa_s,struct disassoc_info * info)2254 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
2255 			struct disassoc_info *info)
2256 {
2257 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
2258 	if (wpa_s->sme.prev_bssid_set) {
2259 		/*
2260 		 * cfg80211/mac80211 can get into somewhat confused state if
2261 		 * the AP only disassociates us and leaves us in authenticated
2262 		 * state. For now, force the state to be cleared to avoid
2263 		 * confusing errors if we try to associate with the AP again.
2264 		 */
2265 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
2266 			"driver state");
2267 		wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
2268 				       WLAN_REASON_DEAUTH_LEAVING);
2269 	}
2270 }
2271 
2272 
sme_auth_timer(void * eloop_ctx,void * timeout_ctx)2273 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
2274 {
2275 	struct wpa_supplicant *wpa_s = eloop_ctx;
2276 	if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
2277 		wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
2278 		sme_deauth(wpa_s);
2279 	}
2280 }
2281 
2282 
sme_assoc_timer(void * eloop_ctx,void * timeout_ctx)2283 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
2284 {
2285 	struct wpa_supplicant *wpa_s = eloop_ctx;
2286 	if (wpa_s->wpa_state == WPA_ASSOCIATING) {
2287 		wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
2288 		sme_deauth(wpa_s);
2289 	}
2290 }
2291 
2292 
sme_state_changed(struct wpa_supplicant * wpa_s)2293 void sme_state_changed(struct wpa_supplicant *wpa_s)
2294 {
2295 	/* Make sure timers are cleaned up appropriately. */
2296 	if (wpa_s->wpa_state != WPA_ASSOCIATING)
2297 		eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
2298 	if (wpa_s->wpa_state != WPA_AUTHENTICATING)
2299 		eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
2300 }
2301 
2302 
sme_disassoc_while_authenticating(struct wpa_supplicant * wpa_s,const u8 * prev_pending_bssid)2303 void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
2304 				       const u8 *prev_pending_bssid)
2305 {
2306 	/*
2307 	 * mac80211-workaround to force deauth on failed auth cmd,
2308 	 * requires us to remain in authenticating state to allow the
2309 	 * second authentication attempt to be continued properly.
2310 	 */
2311 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
2312 		"to proceed after disconnection event");
2313 	wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
2314 	os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
2315 
2316 	/*
2317 	 * Re-arm authentication timer in case auth fails for whatever reason.
2318 	 */
2319 	eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
2320 	eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
2321 			       NULL);
2322 }
2323 
2324 
sme_clear_on_disassoc(struct wpa_supplicant * wpa_s)2325 void sme_clear_on_disassoc(struct wpa_supplicant *wpa_s)
2326 {
2327 	wpa_s->sme.prev_bssid_set = 0;
2328 #ifdef CONFIG_SAE
2329 	wpabuf_free(wpa_s->sme.sae_token);
2330 	wpa_s->sme.sae_token = NULL;
2331 	sae_clear_data(&wpa_s->sme.sae);
2332 #endif /* CONFIG_SAE */
2333 #ifdef CONFIG_IEEE80211R
2334 	if (wpa_s->sme.ft_ies || wpa_s->sme.ft_used)
2335 		sme_update_ft_ies(wpa_s, NULL, NULL, 0);
2336 #endif /* CONFIG_IEEE80211R */
2337 	sme_stop_sa_query(wpa_s);
2338 }
2339 
2340 
sme_deinit(struct wpa_supplicant * wpa_s)2341 void sme_deinit(struct wpa_supplicant *wpa_s)
2342 {
2343 	sme_clear_on_disassoc(wpa_s);
2344 #ifdef CONFIG_SAE
2345 	os_free(wpa_s->sme.sae_rejected_groups);
2346 	wpa_s->sme.sae_rejected_groups = NULL;
2347 #endif /* CONFIG_SAE */
2348 
2349 	eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
2350 	eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
2351 	eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
2352 }
2353 
2354 
sme_send_2040_bss_coex(struct wpa_supplicant * wpa_s,const u8 * chan_list,u8 num_channels,u8 num_intol)2355 static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
2356 				   const u8 *chan_list, u8 num_channels,
2357 				   u8 num_intol)
2358 {
2359 	struct ieee80211_2040_bss_coex_ie *bc_ie;
2360 	struct ieee80211_2040_intol_chan_report *ic_report;
2361 	struct wpabuf *buf;
2362 
2363 	wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR
2364 		   " (num_channels=%u num_intol=%u)",
2365 		   MAC2STR(wpa_s->bssid), num_channels, num_intol);
2366 	wpa_hexdump(MSG_DEBUG, "SME: 20/40 BSS Intolerant Channels",
2367 		    chan_list, num_channels);
2368 
2369 	buf = wpabuf_alloc(2 + /* action.category + action_code */
2370 			   sizeof(struct ieee80211_2040_bss_coex_ie) +
2371 			   sizeof(struct ieee80211_2040_intol_chan_report) +
2372 			   num_channels);
2373 	if (buf == NULL)
2374 		return;
2375 
2376 	wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
2377 	wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
2378 
2379 	bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
2380 	bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
2381 	bc_ie->length = 1;
2382 	if (num_intol)
2383 		bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
2384 
2385 	if (num_channels > 0) {
2386 		ic_report = wpabuf_put(buf, sizeof(*ic_report));
2387 		ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
2388 		ic_report->length = num_channels + 1;
2389 		ic_report->op_class = 0;
2390 		os_memcpy(wpabuf_put(buf, num_channels), chan_list,
2391 			  num_channels);
2392 	}
2393 
2394 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
2395 				wpa_s->own_addr, wpa_s->bssid,
2396 				wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
2397 		wpa_msg(wpa_s, MSG_INFO,
2398 			"SME: Failed to send 20/40 BSS Coexistence frame");
2399 	}
2400 
2401 	wpabuf_free(buf);
2402 }
2403 
2404 
sme_proc_obss_scan(struct wpa_supplicant * wpa_s)2405 int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
2406 {
2407 	struct wpa_bss *bss;
2408 	const u8 *ie;
2409 	u16 ht_cap;
2410 	u8 chan_list[P2P_MAX_CHANNELS], channel;
2411 	u8 num_channels = 0, num_intol = 0, i;
2412 
2413 	if (!wpa_s->sme.sched_obss_scan)
2414 		return 0;
2415 
2416 	wpa_s->sme.sched_obss_scan = 0;
2417 	if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
2418 		return 1;
2419 
2420 	/*
2421 	 * Check whether AP uses regulatory triplet or channel triplet in
2422 	 * country info. Right now the operating class of the BSS channel
2423 	 * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
2424 	 * based on the assumption that operating class triplet is not used in
2425 	 * beacon frame. If the First Channel Number/Operating Extension
2426 	 * Identifier octet has a positive integer value of 201 or greater,
2427 	 * then its operating class triplet.
2428 	 *
2429 	 * TODO: If Supported Operating Classes element is present in beacon
2430 	 * frame, have to lookup operating class in Annex E and fill them in
2431 	 * 2040 coex frame.
2432 	 */
2433 	ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
2434 	if (ie && (ie[1] >= 6) && (ie[5] >= 201))
2435 		return 1;
2436 
2437 	os_memset(chan_list, 0, sizeof(chan_list));
2438 
2439 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2440 		/* Skip other band bss */
2441 		enum hostapd_hw_mode mode;
2442 		mode = ieee80211_freq_to_chan(bss->freq, &channel);
2443 		if (mode != HOSTAPD_MODE_IEEE80211G &&
2444 		    mode != HOSTAPD_MODE_IEEE80211B)
2445 			continue;
2446 
2447 		ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
2448 		ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
2449 		wpa_printf(MSG_DEBUG, "SME OBSS scan BSS " MACSTR
2450 			   " freq=%u chan=%u ht_cap=0x%x",
2451 			   MAC2STR(bss->bssid), bss->freq, channel, ht_cap);
2452 
2453 		if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
2454 			if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
2455 				num_intol++;
2456 
2457 			/* Check whether the channel is already considered */
2458 			for (i = 0; i < num_channels; i++) {
2459 				if (channel == chan_list[i])
2460 					break;
2461 			}
2462 			if (i != num_channels)
2463 				continue;
2464 
2465 			chan_list[num_channels++] = channel;
2466 		}
2467 	}
2468 
2469 	sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
2470 	return 1;
2471 }
2472 
2473 
wpa_obss_scan_freqs_list(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)2474 static void wpa_obss_scan_freqs_list(struct wpa_supplicant *wpa_s,
2475 				     struct wpa_driver_scan_params *params)
2476 {
2477 	/* Include only affected channels */
2478 	struct hostapd_hw_modes *mode;
2479 	int count, i;
2480 	int start, end;
2481 
2482 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
2483 			HOSTAPD_MODE_IEEE80211G, false);
2484 	if (mode == NULL) {
2485 		/* No channels supported in this band - use empty list */
2486 		params->freqs = os_zalloc(sizeof(int));
2487 		return;
2488 	}
2489 
2490 	if (wpa_s->sme.ht_sec_chan == HT_SEC_CHAN_UNKNOWN &&
2491 	    wpa_s->current_bss) {
2492 		const u8 *ie;
2493 
2494 		ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_OPERATION);
2495 		if (ie && ie[1] >= 2) {
2496 			u8 o;
2497 
2498 			o = ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
2499 			if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
2500 				wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_ABOVE;
2501 			else if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
2502 				wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_BELOW;
2503 		}
2504 	}
2505 
2506 	start = wpa_s->assoc_freq - 10;
2507 	end = wpa_s->assoc_freq + 10;
2508 	switch (wpa_s->sme.ht_sec_chan) {
2509 	case HT_SEC_CHAN_UNKNOWN:
2510 		/* HT40+ possible on channels 1..9 */
2511 		if (wpa_s->assoc_freq <= 2452)
2512 			start -= 20;
2513 		/* HT40- possible on channels 5-13 */
2514 		if (wpa_s->assoc_freq >= 2432)
2515 			end += 20;
2516 		break;
2517 	case HT_SEC_CHAN_ABOVE:
2518 		end += 20;
2519 		break;
2520 	case HT_SEC_CHAN_BELOW:
2521 		start -= 20;
2522 		break;
2523 	}
2524 	wpa_printf(MSG_DEBUG,
2525 		   "OBSS: assoc_freq %d possible affected range %d-%d",
2526 		   wpa_s->assoc_freq, start, end);
2527 
2528 	params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
2529 	if (params->freqs == NULL)
2530 		return;
2531 	for (count = 0, i = 0; i < mode->num_channels; i++) {
2532 		int freq;
2533 
2534 		if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
2535 			continue;
2536 		freq = mode->channels[i].freq;
2537 		if (freq - 10 >= end || freq + 10 <= start)
2538 			continue; /* not affected */
2539 		params->freqs[count++] = freq;
2540 	}
2541 }
2542 
2543 
sme_obss_scan_timeout(void * eloop_ctx,void * timeout_ctx)2544 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
2545 {
2546 	struct wpa_supplicant *wpa_s = eloop_ctx;
2547 	struct wpa_driver_scan_params params;
2548 
2549 	if (!wpa_s->current_bss) {
2550 		wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
2551 		return;
2552 	}
2553 
2554 	os_memset(&params, 0, sizeof(params));
2555 	wpa_obss_scan_freqs_list(wpa_s, &params);
2556 	params.low_priority = 1;
2557 	wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
2558 
2559 	if (wpa_supplicant_trigger_scan(wpa_s, &params))
2560 		wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
2561 	else
2562 		wpa_s->sme.sched_obss_scan = 1;
2563 	os_free(params.freqs);
2564 
2565 	eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
2566 			       sme_obss_scan_timeout, wpa_s, NULL);
2567 }
2568 
2569 
sme_sched_obss_scan(struct wpa_supplicant * wpa_s,int enable)2570 void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
2571 {
2572 	const u8 *ie;
2573 	struct wpa_bss *bss = wpa_s->current_bss;
2574 	struct wpa_ssid *ssid = wpa_s->current_ssid;
2575 	struct hostapd_hw_modes *hw_mode = NULL;
2576 	int i;
2577 
2578 	eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
2579 	wpa_s->sme.sched_obss_scan = 0;
2580 	wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_UNKNOWN;
2581 	if (!enable)
2582 		return;
2583 
2584 	/*
2585 	 * Schedule OBSS scan if driver is using station SME in wpa_supplicant
2586 	 * or it expects OBSS scan to be performed by wpa_supplicant.
2587 	 */
2588 	if (!((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
2589 	      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OBSS_SCAN)) ||
2590 	    ssid == NULL || ssid->mode != WPAS_MODE_INFRA)
2591 		return;
2592 
2593 #ifdef CONFIG_HT_OVERRIDES
2594 	/* No need for OBSS scan if HT40 is explicitly disabled */
2595 	if (ssid->disable_ht40)
2596 		return;
2597 #endif /* CONFIG_HT_OVERRIDES */
2598 
2599 	if (!wpa_s->hw.modes)
2600 		return;
2601 
2602 	/* only HT caps in 11g mode are relevant */
2603 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
2604 		hw_mode = &wpa_s->hw.modes[i];
2605 		if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
2606 			break;
2607 	}
2608 
2609 	/* Driver does not support HT40 for 11g or doesn't have 11g. */
2610 	if (i == wpa_s->hw.num_modes || !hw_mode ||
2611 	    !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
2612 		return;
2613 
2614 	if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
2615 		return; /* Not associated on 2.4 GHz band */
2616 
2617 	/* Check whether AP supports HT40 */
2618 	ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
2619 	if (!ie || ie[1] < 2 ||
2620 	    !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
2621 		return; /* AP does not support HT40 */
2622 
2623 	ie = wpa_bss_get_ie(wpa_s->current_bss,
2624 			    WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
2625 	if (!ie || ie[1] < 14)
2626 		return; /* AP does not request OBSS scans */
2627 
2628 	wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
2629 	if (wpa_s->sme.obss_scan_int < 10) {
2630 		wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
2631 			   "replaced with the minimum 10 sec",
2632 			   wpa_s->sme.obss_scan_int);
2633 		wpa_s->sme.obss_scan_int = 10;
2634 	}
2635 	wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
2636 		   wpa_s->sme.obss_scan_int);
2637 	eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
2638 			       sme_obss_scan_timeout, wpa_s, NULL);
2639 }
2640 
2641 
2642 static const unsigned int sa_query_max_timeout = 1000;
2643 static const unsigned int sa_query_retry_timeout = 201;
2644 static const unsigned int sa_query_ch_switch_max_delay = 5000; /* in usec */
2645 
sme_check_sa_query_timeout(struct wpa_supplicant * wpa_s)2646 static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
2647 {
2648 	u32 tu;
2649 	struct os_reltime now, passed;
2650 	os_get_reltime(&now);
2651 	os_reltime_sub(&now, &wpa_s->sme.sa_query_start, &passed);
2652 	tu = (passed.sec * 1000000 + passed.usec) / 1024;
2653 	if (sa_query_max_timeout < tu) {
2654 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
2655 		sme_stop_sa_query(wpa_s);
2656 		wpa_supplicant_deauthenticate(
2657 			wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
2658 		return 1;
2659 	}
2660 
2661 	return 0;
2662 }
2663 
2664 
sme_send_sa_query_req(struct wpa_supplicant * wpa_s,const u8 * trans_id)2665 static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
2666 				  const u8 *trans_id)
2667 {
2668 	u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN + OCV_OCI_EXTENDED_LEN];
2669 	u8 req_len = 2 + WLAN_SA_QUERY_TR_ID_LEN;
2670 
2671 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
2672 		MACSTR, MAC2STR(wpa_s->bssid));
2673 	wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
2674 		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
2675 	req[0] = WLAN_ACTION_SA_QUERY;
2676 	req[1] = WLAN_SA_QUERY_REQUEST;
2677 	os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
2678 
2679 #ifdef CONFIG_OCV
2680 	if (wpa_sm_ocv_enabled(wpa_s->wpa)) {
2681 		struct wpa_channel_info ci;
2682 
2683 		if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
2684 			wpa_printf(MSG_WARNING,
2685 				   "Failed to get channel info for OCI element in SA Query Request frame");
2686 			return;
2687 		}
2688 
2689 #ifdef CONFIG_TESTING_OPTIONS
2690 		if (wpa_s->oci_freq_override_saquery_req) {
2691 			wpa_printf(MSG_INFO,
2692 				   "TEST: Override SA Query Request OCI frequency %d -> %d MHz",
2693 				   ci.frequency,
2694 				   wpa_s->oci_freq_override_saquery_req);
2695 			ci.frequency = wpa_s->oci_freq_override_saquery_req;
2696 		}
2697 #endif /* CONFIG_TESTING_OPTIONS */
2698 
2699 		if (ocv_insert_extended_oci(&ci, req + req_len) < 0)
2700 			return;
2701 
2702 		req_len += OCV_OCI_EXTENDED_LEN;
2703 	}
2704 #endif /* CONFIG_OCV */
2705 
2706 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
2707 				wpa_s->own_addr, wpa_s->bssid,
2708 				req, req_len, 0) < 0)
2709 		wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
2710 			"Request");
2711 }
2712 
2713 
sme_sa_query_timer(void * eloop_ctx,void * timeout_ctx)2714 static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
2715 {
2716 	struct wpa_supplicant *wpa_s = eloop_ctx;
2717 	unsigned int timeout, sec, usec;
2718 	u8 *trans_id, *nbuf;
2719 
2720 	if (wpa_s->sme.sa_query_count > 0 &&
2721 	    sme_check_sa_query_timeout(wpa_s))
2722 		return;
2723 
2724 	nbuf = os_realloc_array(wpa_s->sme.sa_query_trans_id,
2725 				wpa_s->sme.sa_query_count + 1,
2726 				WLAN_SA_QUERY_TR_ID_LEN);
2727 	if (nbuf == NULL) {
2728 		sme_stop_sa_query(wpa_s);
2729 		return;
2730 	}
2731 	if (wpa_s->sme.sa_query_count == 0) {
2732 		/* Starting a new SA Query procedure */
2733 		os_get_reltime(&wpa_s->sme.sa_query_start);
2734 	}
2735 	trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
2736 	wpa_s->sme.sa_query_trans_id = nbuf;
2737 	wpa_s->sme.sa_query_count++;
2738 
2739 	if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
2740 		wpa_printf(MSG_DEBUG, "Could not generate SA Query ID");
2741 		sme_stop_sa_query(wpa_s);
2742 		return;
2743 	}
2744 
2745 	timeout = sa_query_retry_timeout;
2746 	sec = ((timeout / 1000) * 1024) / 1000;
2747 	usec = (timeout % 1000) * 1024;
2748 	eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
2749 
2750 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
2751 		wpa_s->sme.sa_query_count);
2752 
2753 	sme_send_sa_query_req(wpa_s, trans_id);
2754 }
2755 
2756 
sme_start_sa_query(struct wpa_supplicant * wpa_s)2757 static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
2758 {
2759 	sme_sa_query_timer(wpa_s, NULL);
2760 }
2761 
2762 
sme_stop_sa_query(struct wpa_supplicant * wpa_s)2763 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
2764 {
2765 	if (wpa_s->sme.sa_query_trans_id)
2766 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Stop SA Query");
2767 	eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
2768 	os_free(wpa_s->sme.sa_query_trans_id);
2769 	wpa_s->sme.sa_query_trans_id = NULL;
2770 	wpa_s->sme.sa_query_count = 0;
2771 }
2772 
2773 
sme_event_unprot_disconnect(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * da,u16 reason_code)2774 void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
2775 				 const u8 *da, u16 reason_code)
2776 {
2777 	struct wpa_ssid *ssid;
2778 	struct os_reltime now;
2779 
2780 	if (wpa_s->wpa_state != WPA_COMPLETED)
2781 		return;
2782 	ssid = wpa_s->current_ssid;
2783 	if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION)
2784 		return;
2785 	if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
2786 		return;
2787 	if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
2788 	    reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
2789 		return;
2790 	if (wpa_s->sme.sa_query_count > 0)
2791 		return;
2792 #ifdef CONFIG_TESTING_OPTIONS
2793 	if (wpa_s->disable_sa_query)
2794 		return;
2795 #endif /* CONFIG_TESTING_OPTIONS */
2796 
2797 	os_get_reltime(&now);
2798 	if (wpa_s->sme.last_unprot_disconnect.sec &&
2799 	    !os_reltime_expired(&now, &wpa_s->sme.last_unprot_disconnect, 10))
2800 		return; /* limit SA Query procedure frequency */
2801 	wpa_s->sme.last_unprot_disconnect = now;
2802 
2803 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
2804 		"possible AP/STA state mismatch - trigger SA Query");
2805 	sme_start_sa_query(wpa_s);
2806 }
2807 
2808 
sme_event_ch_switch(struct wpa_supplicant * wpa_s)2809 void sme_event_ch_switch(struct wpa_supplicant *wpa_s)
2810 {
2811 	unsigned int usec;
2812 	u32 _rand;
2813 
2814 	if (wpa_s->wpa_state != WPA_COMPLETED ||
2815 	    !wpa_sm_ocv_enabled(wpa_s->wpa))
2816 		return;
2817 
2818 	wpa_dbg(wpa_s, MSG_DEBUG,
2819 		"SME: Channel switch completed - trigger new SA Query to verify new operating channel");
2820 	sme_stop_sa_query(wpa_s);
2821 
2822 	if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
2823 		_rand = os_random();
2824 	usec = _rand % (sa_query_ch_switch_max_delay + 1);
2825 	eloop_register_timeout(0, usec, sme_sa_query_timer, wpa_s, NULL);
2826 }
2827 
2828 
sme_process_sa_query_request(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * data,size_t len)2829 static void sme_process_sa_query_request(struct wpa_supplicant *wpa_s,
2830 					 const u8 *sa, const u8 *data,
2831 					 size_t len)
2832 {
2833 	u8 resp[2 + WLAN_SA_QUERY_TR_ID_LEN + OCV_OCI_EXTENDED_LEN];
2834 	u8 resp_len = 2 + WLAN_SA_QUERY_TR_ID_LEN;
2835 
2836 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Response to "
2837 		MACSTR, MAC2STR(wpa_s->bssid));
2838 
2839 	resp[0] = WLAN_ACTION_SA_QUERY;
2840 	resp[1] = WLAN_SA_QUERY_RESPONSE;
2841 	os_memcpy(resp + 2, data + 1, WLAN_SA_QUERY_TR_ID_LEN);
2842 
2843 #ifdef CONFIG_OCV
2844 	if (wpa_sm_ocv_enabled(wpa_s->wpa)) {
2845 		struct wpa_channel_info ci;
2846 
2847 		if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
2848 			wpa_printf(MSG_WARNING,
2849 				   "Failed to get channel info for OCI element in SA Query Response frame");
2850 			return;
2851 		}
2852 
2853 #ifdef CONFIG_TESTING_OPTIONS
2854 		if (wpa_s->oci_freq_override_saquery_resp) {
2855 			wpa_printf(MSG_INFO,
2856 				   "TEST: Override SA Query Response OCI frequency %d -> %d MHz",
2857 				   ci.frequency,
2858 				   wpa_s->oci_freq_override_saquery_resp);
2859 			ci.frequency = wpa_s->oci_freq_override_saquery_resp;
2860 		}
2861 #endif /* CONFIG_TESTING_OPTIONS */
2862 
2863 		if (ocv_insert_extended_oci(&ci, resp + resp_len) < 0)
2864 			return;
2865 
2866 		resp_len += OCV_OCI_EXTENDED_LEN;
2867 	}
2868 #endif /* CONFIG_OCV */
2869 
2870 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
2871 				wpa_s->own_addr, wpa_s->bssid,
2872 				resp, resp_len, 0) < 0)
2873 		wpa_msg(wpa_s, MSG_INFO,
2874 			"SME: Failed to send SA Query Response");
2875 }
2876 
2877 
sme_process_sa_query_response(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 * data,size_t len)2878 static void sme_process_sa_query_response(struct wpa_supplicant *wpa_s,
2879 					  const u8 *sa, const u8 *data,
2880 					  size_t len)
2881 {
2882 	int i;
2883 
2884 	if (!wpa_s->sme.sa_query_trans_id)
2885 		return;
2886 
2887 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
2888 		MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
2889 
2890 	if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
2891 		return;
2892 
2893 	for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
2894 		if (os_memcmp(wpa_s->sme.sa_query_trans_id +
2895 			      i * WLAN_SA_QUERY_TR_ID_LEN,
2896 			      data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
2897 			break;
2898 	}
2899 
2900 	if (i >= wpa_s->sme.sa_query_count) {
2901 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
2902 			"transaction identifier found");
2903 		return;
2904 	}
2905 
2906 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
2907 		"from " MACSTR, MAC2STR(sa));
2908 	sme_stop_sa_query(wpa_s);
2909 }
2910 
2911 
sme_sa_query_rx(struct wpa_supplicant * wpa_s,const u8 * da,const u8 * sa,const u8 * data,size_t len)2912 void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *da, const u8 *sa,
2913 		     const u8 *data, size_t len)
2914 {
2915 	if (len < 1 + WLAN_SA_QUERY_TR_ID_LEN)
2916 		return;
2917 	if (is_multicast_ether_addr(da)) {
2918 		wpa_printf(MSG_DEBUG,
2919 			   "IEEE 802.11: Ignore group-addressed SA Query frame (A1=" MACSTR " A2=" MACSTR ")",
2920 			   MAC2STR(da), MAC2STR(sa));
2921 		return;
2922 	}
2923 
2924 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query frame from "
2925 		MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
2926 
2927 #ifdef CONFIG_OCV
2928 	if (wpa_sm_ocv_enabled(wpa_s->wpa)) {
2929 		struct ieee802_11_elems elems;
2930 		struct wpa_channel_info ci;
2931 
2932 		if (ieee802_11_parse_elems(data + 1 + WLAN_SA_QUERY_TR_ID_LEN,
2933 					   len - 1 - WLAN_SA_QUERY_TR_ID_LEN,
2934 					   &elems, 1) == ParseFailed) {
2935 			wpa_printf(MSG_DEBUG,
2936 				   "SA Query: Failed to parse elements");
2937 			return;
2938 		}
2939 
2940 		if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
2941 			wpa_printf(MSG_WARNING,
2942 				   "Failed to get channel info to validate received OCI in SA Query Action frame");
2943 			return;
2944 		}
2945 
2946 		if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
2947 					 channel_width_to_int(ci.chanwidth),
2948 					 ci.seg1_idx) != OCI_SUCCESS) {
2949 			wpa_msg(wpa_s, MSG_INFO, OCV_FAILURE "addr=" MACSTR
2950 				" frame=saquery%s error=%s",
2951 				MAC2STR(sa), data[0] == WLAN_SA_QUERY_REQUEST ?
2952 				"req" : "resp", ocv_errorstr);
2953 			return;
2954 		}
2955 	}
2956 #endif /* CONFIG_OCV */
2957 
2958 	if (data[0] == WLAN_SA_QUERY_REQUEST)
2959 		sme_process_sa_query_request(wpa_s, sa, data, len);
2960 	else if (data[0] == WLAN_SA_QUERY_RESPONSE)
2961 		sme_process_sa_query_response(wpa_s, sa, data, len);
2962 }
2963