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