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