xref: /freebsd/contrib/wpa/wpa_supplicant/sme.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /*
2  * wpa_supplicant - SME
3  * Copyright (c) 2009-2010, 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 "eapol_supp/eapol_supp_sm.h"
16 #include "common/wpa_common.h"
17 #include "rsn_supp/wpa.h"
18 #include "rsn_supp/pmksa_cache.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "driver_i.h"
22 #include "wpas_glue.h"
23 #include "wps_supplicant.h"
24 #include "p2p_supplicant.h"
25 #include "notify.h"
26 #include "bss.h"
27 #include "scan.h"
28 #include "sme.h"
29 #include "hs20_supplicant.h"
30 
31 #define SME_AUTH_TIMEOUT 5
32 #define SME_ASSOC_TIMEOUT 5
33 
34 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
35 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
36 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
37 #ifdef CONFIG_IEEE80211W
38 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
39 #endif /* CONFIG_IEEE80211W */
40 
41 
42 #ifdef CONFIG_SAE
43 
44 static struct wpabuf * sme_auth_build_sae_commit(struct wpa_supplicant *wpa_s)
45 {
46 	struct wpabuf *buf;
47 
48 	buf = wpabuf_alloc(4 + 2);
49 	if (buf == NULL)
50 		return NULL;
51 
52 	wpabuf_put_le16(buf, 1); /* Transaction seq# */
53 	wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
54 	wpabuf_put_le16(buf, 19); /* Finite Cyclic Group */
55 	/* TODO: Anti-Clogging Token (if requested) */
56 	/* TODO: Scalar */
57 	/* TODO: Element */
58 
59 	return buf;
60 }
61 
62 
63 static struct wpabuf * sme_auth_build_sae_confirm(struct wpa_supplicant *wpa_s)
64 {
65 	struct wpabuf *buf;
66 
67 	buf = wpabuf_alloc(4 + 2);
68 	if (buf == NULL)
69 		return NULL;
70 
71 	wpabuf_put_le16(buf, 2); /* Transaction seq# */
72 	wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
73 	wpabuf_put_le16(buf, wpa_s->sme.sae_send_confirm);
74 	wpa_s->sme.sae_send_confirm++;
75 	/* TODO: Confirm */
76 
77 	return buf;
78 }
79 
80 #endif /* CONFIG_SAE */
81 
82 
83 static void sme_send_authentication(struct wpa_supplicant *wpa_s,
84 				    struct wpa_bss *bss, struct wpa_ssid *ssid,
85 				    int start)
86 {
87 	struct wpa_driver_auth_params params;
88 	struct wpa_ssid *old_ssid;
89 #ifdef CONFIG_IEEE80211R
90 	const u8 *ie;
91 #endif /* CONFIG_IEEE80211R */
92 #ifdef CONFIG_IEEE80211R
93 	const u8 *md = NULL;
94 #endif /* CONFIG_IEEE80211R */
95 	int i, bssid_changed;
96 	struct wpabuf *resp = NULL;
97 	u8 ext_capab[10];
98 	int ext_capab_len;
99 
100 	if (bss == NULL) {
101 		wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
102 			"the network");
103 		return;
104 	}
105 
106 	wpa_s->current_bss = bss;
107 
108 	os_memset(&params, 0, sizeof(params));
109 	wpa_s->reassociate = 0;
110 
111 	params.freq = bss->freq;
112 	params.bssid = bss->bssid;
113 	params.ssid = bss->ssid;
114 	params.ssid_len = bss->ssid_len;
115 	params.p2p = ssid->p2p_group;
116 
117 	if (wpa_s->sme.ssid_len != params.ssid_len ||
118 	    os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
119 		wpa_s->sme.prev_bssid_set = 0;
120 
121 	wpa_s->sme.freq = params.freq;
122 	os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
123 	wpa_s->sme.ssid_len = params.ssid_len;
124 
125 	params.auth_alg = WPA_AUTH_ALG_OPEN;
126 #ifdef IEEE8021X_EAPOL
127 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
128 		if (ssid->leap) {
129 			if (ssid->non_leap == 0)
130 				params.auth_alg = WPA_AUTH_ALG_LEAP;
131 			else
132 				params.auth_alg |= WPA_AUTH_ALG_LEAP;
133 		}
134 	}
135 #endif /* IEEE8021X_EAPOL */
136 	wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
137 		params.auth_alg);
138 	if (ssid->auth_alg) {
139 		params.auth_alg = ssid->auth_alg;
140 		wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
141 			"0x%x", params.auth_alg);
142 	}
143 #ifdef CONFIG_SAE
144 	if (wpa_key_mgmt_sae(ssid->key_mgmt)) {
145 		const u8 *rsn;
146 		struct wpa_ie_data ied;
147 
148 		rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
149 		if (rsn &&
150 		    wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0) {
151 			if (wpa_key_mgmt_sae(ied.key_mgmt)) {
152 				wpa_dbg(wpa_s, MSG_DEBUG, "Using SAE auth_alg");
153 				params.auth_alg = WPA_AUTH_ALG_SAE;
154 			}
155 		}
156 	}
157 #endif /* CONFIG_SAE */
158 
159 	for (i = 0; i < NUM_WEP_KEYS; i++) {
160 		if (ssid->wep_key_len[i])
161 			params.wep_key[i] = ssid->wep_key[i];
162 		params.wep_key_len[i] = ssid->wep_key_len[i];
163 	}
164 	params.wep_tx_keyidx = ssid->wep_tx_keyidx;
165 
166 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
167 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
168 	os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
169 	if (bssid_changed)
170 		wpas_notify_bssid_changed(wpa_s);
171 
172 	if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
173 	     wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
174 	    wpa_key_mgmt_wpa(ssid->key_mgmt)) {
175 		int try_opportunistic;
176 		try_opportunistic = (ssid->proactive_key_caching < 0 ?
177 				     wpa_s->conf->okc :
178 				     ssid->proactive_key_caching) &&
179 			(ssid->proto & WPA_PROTO_RSN);
180 		if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
181 					    wpa_s->current_ssid,
182 					    try_opportunistic) == 0)
183 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
184 		wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
185 		if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
186 					      wpa_s->sme.assoc_req_ie,
187 					      &wpa_s->sme.assoc_req_ie_len)) {
188 			wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
189 				"key management and encryption suites");
190 			return;
191 		}
192 	} else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
193 		   wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
194 		/*
195 		 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
196 		 * use non-WPA since the scan results did not indicate that the
197 		 * AP is using WPA or WPA2.
198 		 */
199 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
200 		wpa_s->sme.assoc_req_ie_len = 0;
201 	} else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
202 		wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
203 		if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
204 					      wpa_s->sme.assoc_req_ie,
205 					      &wpa_s->sme.assoc_req_ie_len)) {
206 			wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
207 				"key management and encryption suites (no "
208 				"scan results)");
209 			return;
210 		}
211 #ifdef CONFIG_WPS
212 	} else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
213 		struct wpabuf *wps_ie;
214 		wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
215 		if (wps_ie && wpabuf_len(wps_ie) <=
216 		    sizeof(wpa_s->sme.assoc_req_ie)) {
217 			wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
218 			os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
219 				  wpa_s->sme.assoc_req_ie_len);
220 		} else
221 			wpa_s->sme.assoc_req_ie_len = 0;
222 		wpabuf_free(wps_ie);
223 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
224 #endif /* CONFIG_WPS */
225 	} else {
226 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
227 		wpa_s->sme.assoc_req_ie_len = 0;
228 	}
229 
230 #ifdef CONFIG_IEEE80211R
231 	ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
232 	if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
233 		md = ie + 2;
234 	wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
235 	if (md) {
236 		/* Prepare for the next transition */
237 		wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
238 	}
239 
240 	if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
241 		if (wpa_s->sme.assoc_req_ie_len + 5 <
242 		    sizeof(wpa_s->sme.assoc_req_ie)) {
243 			struct rsn_mdie *mdie;
244 			u8 *pos = wpa_s->sme.assoc_req_ie +
245 				wpa_s->sme.assoc_req_ie_len;
246 			*pos++ = WLAN_EID_MOBILITY_DOMAIN;
247 			*pos++ = sizeof(*mdie);
248 			mdie = (struct rsn_mdie *) pos;
249 			os_memcpy(mdie->mobility_domain, md,
250 				  MOBILITY_DOMAIN_ID_LEN);
251 			mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
252 			wpa_s->sme.assoc_req_ie_len += 5;
253 		}
254 
255 		if (wpa_s->sme.ft_used &&
256 		    os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
257 		    wpa_sm_has_ptk(wpa_s->wpa)) {
258 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
259 				"over-the-air");
260 			params.auth_alg = WPA_AUTH_ALG_FT;
261 			params.ie = wpa_s->sme.ft_ies;
262 			params.ie_len = wpa_s->sme.ft_ies_len;
263 		}
264 	}
265 #endif /* CONFIG_IEEE80211R */
266 
267 #ifdef CONFIG_IEEE80211W
268 	wpa_s->sme.mfp = ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
269 		wpa_s->conf->pmf : ssid->ieee80211w;
270 	if (wpa_s->sme.mfp != NO_MGMT_FRAME_PROTECTION) {
271 		const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
272 		struct wpa_ie_data _ie;
273 		if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
274 		    _ie.capabilities &
275 		    (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
276 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
277 				"MFP: require MFP");
278 			wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
279 		}
280 	}
281 #endif /* CONFIG_IEEE80211W */
282 
283 #ifdef CONFIG_P2P
284 	if (wpa_s->global->p2p) {
285 		u8 *pos;
286 		size_t len;
287 		int res;
288 		pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
289 		len = sizeof(wpa_s->sme.assoc_req_ie) -
290 			wpa_s->sme.assoc_req_ie_len;
291 		res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
292 					    ssid->p2p_group);
293 		if (res >= 0)
294 			wpa_s->sme.assoc_req_ie_len += res;
295 	}
296 #endif /* CONFIG_P2P */
297 
298 #ifdef CONFIG_HS20
299 	if (wpa_s->conf->hs20) {
300 		struct wpabuf *hs20;
301 		hs20 = wpabuf_alloc(20);
302 		if (hs20) {
303 			wpas_hs20_add_indication(hs20);
304 			os_memcpy(wpa_s->sme.assoc_req_ie +
305 				  wpa_s->sme.assoc_req_ie_len,
306 				  wpabuf_head(hs20), wpabuf_len(hs20));
307 			wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
308 			wpabuf_free(hs20);
309 		}
310 	}
311 #endif /* CONFIG_HS20 */
312 
313 	ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab);
314 	if (ext_capab_len > 0) {
315 		u8 *pos = wpa_s->sme.assoc_req_ie;
316 		if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
317 			pos += 2 + pos[1];
318 		os_memmove(pos + ext_capab_len, pos,
319 			   wpa_s->sme.assoc_req_ie_len -
320 			   (pos - wpa_s->sme.assoc_req_ie));
321 		wpa_s->sme.assoc_req_ie_len += ext_capab_len;
322 		os_memcpy(pos, ext_capab, ext_capab_len);
323 	}
324 
325 #ifdef CONFIG_SAE
326 	if (params.auth_alg == WPA_AUTH_ALG_SAE) {
327 		if (start)
328 			resp = sme_auth_build_sae_commit(wpa_s);
329 		else
330 			resp = sme_auth_build_sae_confirm(wpa_s);
331 		if (resp == NULL)
332 			return;
333 		params.sae_data = wpabuf_head(resp);
334 		params.sae_data_len = wpabuf_len(resp);
335 		wpa_s->sme.sae_state = start ? SME_SAE_COMMIT : SME_SAE_CONFIRM;
336 	}
337 #endif /* CONFIG_SAE */
338 
339 	wpa_supplicant_cancel_sched_scan(wpa_s);
340 	wpa_supplicant_cancel_scan(wpa_s);
341 
342 	wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
343 		" (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
344 		wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
345 
346 	wpa_clear_keys(wpa_s, bss->bssid);
347 	wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
348 	old_ssid = wpa_s->current_ssid;
349 	wpa_s->current_ssid = ssid;
350 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
351 	wpa_supplicant_initiate_eapol(wpa_s);
352 	if (old_ssid != wpa_s->current_ssid)
353 		wpas_notify_network_changed(wpa_s);
354 
355 	wpa_s->sme.auth_alg = params.auth_alg;
356 	if (wpa_drv_authenticate(wpa_s, &params) < 0) {
357 		wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
358 			"driver failed");
359 		wpas_connection_failed(wpa_s, bss->bssid);
360 		wpa_supplicant_mark_disassoc(wpa_s);
361 		wpabuf_free(resp);
362 		return;
363 	}
364 
365 	eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
366 			       NULL);
367 
368 	/*
369 	 * Association will be started based on the authentication event from
370 	 * the driver.
371 	 */
372 
373 	wpabuf_free(resp);
374 }
375 
376 
377 void sme_authenticate(struct wpa_supplicant *wpa_s,
378 		      struct wpa_bss *bss, struct wpa_ssid *ssid)
379 {
380 	wpa_s->sme.sae_state = SME_SAE_INIT;
381 	wpa_s->sme.sae_send_confirm = 0;
382 	sme_send_authentication(wpa_s, bss, ssid, 1);
383 }
384 
385 
386 #ifdef CONFIG_SAE
387 
388 static int sme_sae_process_commit(struct wpa_supplicant *wpa_s, const u8 *data,
389 				  size_t len)
390 {
391 	/* Check Finite Cyclic Group */
392 	if (len < 2)
393 		return -1;
394 	if (WPA_GET_LE16(data) != 19) {
395 		wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
396 			   WPA_GET_LE16(data));
397 		return -1;
398 	}
399 
400 	/* TODO */
401 
402 	return 0;
403 }
404 
405 
406 static int sme_sae_process_confirm(struct wpa_supplicant *wpa_s, const u8 *data,
407 				   size_t len)
408 {
409 	u16 rc;
410 
411 	if (len < 2)
412 		return -1;
413 	rc = WPA_GET_LE16(data);
414 	wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", rc);
415 
416 	/* TODO */
417 	return 0;
418 }
419 
420 
421 static int sme_sae_auth(struct wpa_supplicant *wpa_s, u16 auth_transaction,
422 			u16 status_code, const u8 *data, size_t len)
423 {
424 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE authentication transaction %u "
425 		"status code %u", auth_transaction, status_code);
426 	wpa_hexdump(MSG_DEBUG, "SME: SAE fields", data, len);
427 
428 	if (status_code != WLAN_STATUS_SUCCESS)
429 		return -1;
430 
431 	if (auth_transaction == 1) {
432 		wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE commit");
433 		if (wpa_s->current_bss == NULL ||
434 		    wpa_s->current_ssid == NULL)
435 			return -1;
436 		if (wpa_s->sme.sae_state != SME_SAE_COMMIT)
437 			return -1;
438 		if (sme_sae_process_commit(wpa_s, data, len) < 0)
439 			return -1;
440 		sme_send_authentication(wpa_s, wpa_s->current_bss,
441 					wpa_s->current_ssid, 0);
442 		return 0;
443 	} else if (auth_transaction == 2) {
444 		wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE confirm");
445 		if (wpa_s->sme.sae_state != SME_SAE_CONFIRM)
446 			return -1;
447 		if (sme_sae_process_confirm(wpa_s, data, len) < 0)
448 			return -1;
449 		return 1;
450 	}
451 
452 	return -1;
453 }
454 #endif /* CONFIG_SAE */
455 
456 
457 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
458 {
459 	struct wpa_ssid *ssid = wpa_s->current_ssid;
460 
461 	if (ssid == NULL) {
462 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
463 			"when network is not selected");
464 		return;
465 	}
466 
467 	if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
468 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
469 			"when not in authenticating state");
470 		return;
471 	}
472 
473 	if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
474 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
475 			"unexpected peer " MACSTR,
476 			MAC2STR(data->auth.peer));
477 		return;
478 	}
479 
480 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
481 		" auth_type=%d auth_transaction=%d status_code=%d",
482 		MAC2STR(data->auth.peer), data->auth.auth_type,
483 		data->auth.auth_transaction, data->auth.status_code);
484 	wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
485 		    data->auth.ies, data->auth.ies_len);
486 
487 	eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
488 
489 #ifdef CONFIG_SAE
490 	if (data->auth.auth_type == WLAN_AUTH_SAE) {
491 		int res;
492 		res = sme_sae_auth(wpa_s, data->auth.auth_transaction,
493 				   data->auth.status_code, data->auth.ies,
494 				   data->auth.ies_len);
495 		if (res < 0) {
496 			wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
497 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
498 
499 		}
500 		if (res != 1)
501 			return;
502 	}
503 #endif /* CONFIG_SAE */
504 
505 	if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
506 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
507 			"code %d)", data->auth.status_code);
508 
509 		if (data->auth.status_code !=
510 		    WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
511 		    wpa_s->sme.auth_alg == data->auth.auth_type ||
512 		    wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
513 			wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
514 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
515 			return;
516 		}
517 
518 		switch (data->auth.auth_type) {
519 		case WLAN_AUTH_OPEN:
520 			wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
521 
522 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
523 			wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
524 						 wpa_s->current_ssid);
525 			return;
526 
527 		case WLAN_AUTH_SHARED_KEY:
528 			wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
529 
530 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
531 			wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
532 						 wpa_s->current_ssid);
533 			return;
534 
535 		default:
536 			return;
537 		}
538 	}
539 
540 #ifdef CONFIG_IEEE80211R
541 	if (data->auth.auth_type == WLAN_AUTH_FT) {
542 		union wpa_event_data edata;
543 		os_memset(&edata, 0, sizeof(edata));
544 		edata.ft_ies.ies = data->auth.ies;
545 		edata.ft_ies.ies_len = data->auth.ies_len;
546 		os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
547 		wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
548 	}
549 #endif /* CONFIG_IEEE80211R */
550 
551 	sme_associate(wpa_s, ssid->mode, data->auth.peer,
552 		      data->auth.auth_type);
553 }
554 
555 
556 void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
557 		   const u8 *bssid, u16 auth_type)
558 {
559 	struct wpa_driver_associate_params params;
560 	struct ieee802_11_elems elems;
561 #ifdef CONFIG_HT_OVERRIDES
562 	struct ieee80211_ht_capabilities htcaps;
563 	struct ieee80211_ht_capabilities htcaps_mask;
564 #endif /* CONFIG_HT_OVERRIDES */
565 
566 	os_memset(&params, 0, sizeof(params));
567 	params.bssid = bssid;
568 	params.ssid = wpa_s->sme.ssid;
569 	params.ssid_len = wpa_s->sme.ssid_len;
570 	params.freq = wpa_s->sme.freq;
571 	params.bg_scan_period = wpa_s->current_ssid ?
572 		wpa_s->current_ssid->bg_scan_period : -1;
573 	params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
574 		wpa_s->sme.assoc_req_ie : NULL;
575 	params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
576 	params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
577 	params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
578 #ifdef CONFIG_HT_OVERRIDES
579 	os_memset(&htcaps, 0, sizeof(htcaps));
580 	os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
581 	params.htcaps = (u8 *) &htcaps;
582 	params.htcaps_mask = (u8 *) &htcaps_mask;
583 	wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
584 #endif /* CONFIG_HT_OVERRIDES */
585 #ifdef CONFIG_IEEE80211R
586 	if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
587 		params.wpa_ie = wpa_s->sme.ft_ies;
588 		params.wpa_ie_len = wpa_s->sme.ft_ies_len;
589 	}
590 #endif /* CONFIG_IEEE80211R */
591 	params.mode = mode;
592 	params.mgmt_frame_protection = wpa_s->sme.mfp;
593 	if (wpa_s->sme.prev_bssid_set)
594 		params.prev_bssid = wpa_s->sme.prev_bssid;
595 
596 	wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
597 		" (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
598 		params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
599 		params.freq);
600 
601 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
602 
603 	if (params.wpa_ie == NULL ||
604 	    ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
605 	    < 0) {
606 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
607 		os_memset(&elems, 0, sizeof(elems));
608 	}
609 	if (elems.rsn_ie) {
610 		params.wpa_proto = WPA_PROTO_RSN;
611 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
612 					elems.rsn_ie_len + 2);
613 	} else if (elems.wpa_ie) {
614 		params.wpa_proto = WPA_PROTO_WPA;
615 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
616 					elems.wpa_ie_len + 2);
617 	} else
618 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
619 	if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
620 		params.p2p = 1;
621 
622 	if (wpa_s->parent->set_sta_uapsd)
623 		params.uapsd = wpa_s->parent->sta_uapsd;
624 	else
625 		params.uapsd = -1;
626 
627 	if (wpa_drv_associate(wpa_s, &params) < 0) {
628 		wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
629 			"driver failed");
630 		wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
631 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
632 		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
633 		return;
634 	}
635 
636 	eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
637 			       NULL);
638 }
639 
640 
641 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
642 		      const u8 *ies, size_t ies_len)
643 {
644 	if (md == NULL || ies == NULL) {
645 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
646 		os_free(wpa_s->sme.ft_ies);
647 		wpa_s->sme.ft_ies = NULL;
648 		wpa_s->sme.ft_ies_len = 0;
649 		wpa_s->sme.ft_used = 0;
650 		return 0;
651 	}
652 
653 	os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
654 	wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
655 	os_free(wpa_s->sme.ft_ies);
656 	wpa_s->sme.ft_ies = os_malloc(ies_len);
657 	if (wpa_s->sme.ft_ies == NULL)
658 		return -1;
659 	os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
660 	wpa_s->sme.ft_ies_len = ies_len;
661 	return 0;
662 }
663 
664 
665 static void sme_deauth(struct wpa_supplicant *wpa_s)
666 {
667 	int bssid_changed;
668 
669 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
670 
671 	if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
672 				   WLAN_REASON_DEAUTH_LEAVING) < 0) {
673 		wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
674 			"failed");
675 	}
676 	wpa_s->sme.prev_bssid_set = 0;
677 
678 	wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
679 	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
680 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
681 	os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
682 	if (bssid_changed)
683 		wpas_notify_bssid_changed(wpa_s);
684 }
685 
686 
687 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
688 			    union wpa_event_data *data)
689 {
690 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
691 		"status code %d", MAC2STR(wpa_s->pending_bssid),
692 		data->assoc_reject.status_code);
693 
694 	eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
695 
696 	/*
697 	 * For now, unconditionally terminate the previous authentication. In
698 	 * theory, this should not be needed, but mac80211 gets quite confused
699 	 * if the authentication is left pending.. Some roaming cases might
700 	 * benefit from using the previous authentication, so this could be
701 	 * optimized in the future.
702 	 */
703 	sme_deauth(wpa_s);
704 }
705 
706 
707 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
708 			      union wpa_event_data *data)
709 {
710 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
711 	wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
712 	wpa_supplicant_mark_disassoc(wpa_s);
713 }
714 
715 
716 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
717 			       union wpa_event_data *data)
718 {
719 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
720 	wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
721 	wpa_supplicant_mark_disassoc(wpa_s);
722 }
723 
724 
725 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
726 			union wpa_event_data *data)
727 {
728 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
729 	if (wpa_s->sme.prev_bssid_set) {
730 		/*
731 		 * cfg80211/mac80211 can get into somewhat confused state if
732 		 * the AP only disassociates us and leaves us in authenticated
733 		 * state. For now, force the state to be cleared to avoid
734 		 * confusing errors if we try to associate with the AP again.
735 		 */
736 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
737 			"driver state");
738 		wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
739 				       WLAN_REASON_DEAUTH_LEAVING);
740 	}
741 }
742 
743 
744 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
745 {
746 	struct wpa_supplicant *wpa_s = eloop_ctx;
747 	if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
748 		wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
749 		sme_deauth(wpa_s);
750 	}
751 }
752 
753 
754 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
755 {
756 	struct wpa_supplicant *wpa_s = eloop_ctx;
757 	if (wpa_s->wpa_state == WPA_ASSOCIATING) {
758 		wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
759 		sme_deauth(wpa_s);
760 	}
761 }
762 
763 
764 void sme_state_changed(struct wpa_supplicant *wpa_s)
765 {
766 	/* Make sure timers are cleaned up appropriately. */
767 	if (wpa_s->wpa_state != WPA_ASSOCIATING)
768 		eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
769 	if (wpa_s->wpa_state != WPA_AUTHENTICATING)
770 		eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
771 }
772 
773 
774 void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
775 				       const u8 *prev_pending_bssid)
776 {
777 	/*
778 	 * mac80211-workaround to force deauth on failed auth cmd,
779 	 * requires us to remain in authenticating state to allow the
780 	 * second authentication attempt to be continued properly.
781 	 */
782 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
783 		"to proceed after disconnection event");
784 	wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
785 	os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
786 
787 	/*
788 	 * Re-arm authentication timer in case auth fails for whatever reason.
789 	 */
790 	eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
791 	eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
792 			       NULL);
793 }
794 
795 
796 void sme_deinit(struct wpa_supplicant *wpa_s)
797 {
798 	os_free(wpa_s->sme.ft_ies);
799 	wpa_s->sme.ft_ies = NULL;
800 	wpa_s->sme.ft_ies_len = 0;
801 #ifdef CONFIG_IEEE80211W
802 	sme_stop_sa_query(wpa_s);
803 #endif /* CONFIG_IEEE80211W */
804 
805 	eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
806 	eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
807 	eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
808 }
809 
810 
811 static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
812 				   const u8 *chan_list, u8 num_channels,
813 				   u8 num_intol)
814 {
815 	struct ieee80211_2040_bss_coex_ie *bc_ie;
816 	struct ieee80211_2040_intol_chan_report *ic_report;
817 	struct wpabuf *buf;
818 
819 	wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR,
820 		   MAC2STR(wpa_s->bssid));
821 
822 	buf = wpabuf_alloc(2 + /* action.category + action_code */
823 			   sizeof(struct ieee80211_2040_bss_coex_ie) +
824 			   sizeof(struct ieee80211_2040_intol_chan_report) +
825 			   num_channels);
826 	if (buf == NULL)
827 		return;
828 
829 	wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
830 	wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
831 
832 	bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
833 	bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
834 	bc_ie->length = 1;
835 	if (num_intol)
836 		bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
837 
838 	if (num_channels > 0) {
839 		ic_report = wpabuf_put(buf, sizeof(*ic_report));
840 		ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
841 		ic_report->length = num_channels + 1;
842 		ic_report->op_class = 0;
843 		os_memcpy(wpabuf_put(buf, num_channels), chan_list,
844 			  num_channels);
845 	}
846 
847 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
848 				wpa_s->own_addr, wpa_s->bssid,
849 				wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
850 		wpa_msg(wpa_s, MSG_INFO,
851 			"SME: Failed to send 20/40 BSS Coexistence frame");
852 	}
853 
854 	wpabuf_free(buf);
855 }
856 
857 
858 /**
859  * enum wpas_band - Frequency band
860  * @WPAS_BAND_2GHZ: 2.4 GHz ISM band
861  * @WPAS_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
862  */
863 enum wpas_band {
864 	WPAS_BAND_2GHZ,
865 	WPAS_BAND_5GHZ,
866 	WPAS_BAND_INVALID
867 };
868 
869 /**
870  * freq_to_channel - Convert frequency into channel info
871  * @channel: Buffer for returning channel number
872  * Returns: Band (2 or 5 GHz)
873  */
874 static enum wpas_band freq_to_channel(int freq, u8 *channel)
875 {
876 	enum wpas_band band = (freq <= 2484) ? WPAS_BAND_2GHZ : WPAS_BAND_5GHZ;
877 	u8 chan = 0;
878 
879 	if (freq >= 2412 && freq <= 2472)
880 		chan = (freq - 2407) / 5;
881 	else if (freq == 2484)
882 		chan = 14;
883 	else if (freq >= 5180 && freq <= 5805)
884 		chan = (freq - 5000) / 5;
885 
886 	*channel = chan;
887 	return band;
888 }
889 
890 
891 int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
892 {
893 	struct wpa_bss *bss;
894 	const u8 *ie;
895 	u16 ht_cap;
896 	u8 chan_list[P2P_MAX_CHANNELS], channel;
897 	u8 num_channels = 0, num_intol = 0, i;
898 
899 	if (!wpa_s->sme.sched_obss_scan)
900 		return 0;
901 
902 	wpa_s->sme.sched_obss_scan = 0;
903 	if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
904 		return 1;
905 
906 	/*
907 	 * Check whether AP uses regulatory triplet or channel triplet in
908 	 * country info. Right now the operating class of the BSS channel
909 	 * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
910 	 * based on the assumption that operating class triplet is not used in
911 	 * beacon frame. If the First Channel Number/Operating Extension
912 	 * Identifier octet has a positive integer value of 201 or greater,
913 	 * then its operating class triplet.
914 	 *
915 	 * TODO: If Supported Operating Classes element is present in beacon
916 	 * frame, have to lookup operating class in Annex E and fill them in
917 	 * 2040 coex frame.
918 	 */
919 	ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
920 	if (ie && (ie[1] >= 6) && (ie[5] >= 201))
921 		return 1;
922 
923 	os_memset(chan_list, 0, sizeof(chan_list));
924 
925 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
926 		/* Skip other band bss */
927 		if (freq_to_channel(bss->freq, &channel) != WPAS_BAND_2GHZ)
928 			continue;
929 
930 		ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
931 		ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
932 
933 		if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
934 			/* Check whether the channel is already considered */
935 			for (i = 0; i < num_channels; i++) {
936 				if (channel == chan_list[i])
937 					break;
938 			}
939 			if (i != num_channels)
940 				continue;
941 
942 			if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
943 				num_intol++;
944 
945 			chan_list[num_channels++] = channel;
946 		}
947 	}
948 
949 	sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
950 	return 1;
951 }
952 
953 
954 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
955 					  u16 num_modes,
956 					  enum hostapd_hw_mode mode)
957 {
958 	u16 i;
959 
960 	for (i = 0; i < num_modes; i++) {
961 		if (modes[i].mode == mode)
962 			return &modes[i];
963 	}
964 
965 	return NULL;
966 }
967 
968 
969 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
970 					enum hostapd_hw_mode band,
971 					struct wpa_driver_scan_params *params)
972 {
973 	/* Include only supported channels for the specified band */
974 	struct hostapd_hw_modes *mode;
975 	int count, i;
976 
977 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
978 	if (mode == NULL) {
979 		/* No channels supported in this band - use empty list */
980 		params->freqs = os_zalloc(sizeof(int));
981 		return;
982 	}
983 
984 	params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
985 	if (params->freqs == NULL)
986 		return;
987 	for (count = 0, i = 0; i < mode->num_channels; i++) {
988 		if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
989 			continue;
990 		params->freqs[count++] = mode->channels[i].freq;
991 	}
992 }
993 
994 
995 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
996 {
997 	struct wpa_supplicant *wpa_s = eloop_ctx;
998 	struct wpa_driver_scan_params params;
999 
1000 	if (!wpa_s->current_bss) {
1001 		wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
1002 		return;
1003 	}
1004 
1005 	os_memset(&params, 0, sizeof(params));
1006 	wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params);
1007 	wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
1008 
1009 	if (wpa_supplicant_trigger_scan(wpa_s, &params))
1010 		wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
1011 	else
1012 		wpa_s->sme.sched_obss_scan = 1;
1013 	os_free(params.freqs);
1014 
1015 	eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1016 			       sme_obss_scan_timeout, wpa_s, NULL);
1017 }
1018 
1019 
1020 void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
1021 {
1022 	const u8 *ie;
1023 	struct wpa_bss *bss = wpa_s->current_bss;
1024 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1025 	struct hostapd_hw_modes *hw_mode = NULL;
1026 	int i;
1027 
1028 	eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
1029 	wpa_s->sme.sched_obss_scan = 0;
1030 	if (!enable)
1031 		return;
1032 
1033 	/*
1034 	 * Schedule OBSS scan if driver is using station SME in wpa_supplicant
1035 	 * or it expects OBSS scan to be performed by wpa_supplicant.
1036 	 */
1037 	if (!((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
1038 	      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OBSS_SCAN)) ||
1039 	    ssid == NULL || ssid->mode != IEEE80211_MODE_INFRA)
1040 		return;
1041 
1042 	if (!wpa_s->hw.modes)
1043 		return;
1044 
1045 	/* only HT caps in 11g mode are relevant */
1046 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
1047 		hw_mode = &wpa_s->hw.modes[i];
1048 		if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
1049 			break;
1050 	}
1051 
1052 	/* Driver does not support HT40 for 11g or doesn't have 11g. */
1053 	if (i == wpa_s->hw.num_modes || !hw_mode ||
1054 	    !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1055 		return;
1056 
1057 	if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
1058 		return; /* Not associated on 2.4 GHz band */
1059 
1060 	/* Check whether AP supports HT40 */
1061 	ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
1062 	if (!ie || ie[1] < 2 ||
1063 	    !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1064 		return; /* AP does not support HT40 */
1065 
1066 	ie = wpa_bss_get_ie(wpa_s->current_bss,
1067 			    WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
1068 	if (!ie || ie[1] < 14)
1069 		return; /* AP does not request OBSS scans */
1070 
1071 	wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
1072 	if (wpa_s->sme.obss_scan_int < 10) {
1073 		wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
1074 			   "replaced with the minimum 10 sec",
1075 			   wpa_s->sme.obss_scan_int);
1076 		wpa_s->sme.obss_scan_int = 10;
1077 	}
1078 	wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
1079 		   wpa_s->sme.obss_scan_int);
1080 	eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1081 			       sme_obss_scan_timeout, wpa_s, NULL);
1082 }
1083 
1084 
1085 #ifdef CONFIG_IEEE80211W
1086 
1087 static const unsigned int sa_query_max_timeout = 1000;
1088 static const unsigned int sa_query_retry_timeout = 201;
1089 
1090 static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
1091 {
1092 	u32 tu;
1093 	struct os_time now, passed;
1094 	os_get_time(&now);
1095 	os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
1096 	tu = (passed.sec * 1000000 + passed.usec) / 1024;
1097 	if (sa_query_max_timeout < tu) {
1098 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
1099 		sme_stop_sa_query(wpa_s);
1100 		wpa_supplicant_deauthenticate(
1101 			wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
1102 		return 1;
1103 	}
1104 
1105 	return 0;
1106 }
1107 
1108 
1109 static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
1110 				  const u8 *trans_id)
1111 {
1112 	u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
1113 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
1114 		MACSTR, MAC2STR(wpa_s->bssid));
1115 	wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
1116 		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1117 	req[0] = WLAN_ACTION_SA_QUERY;
1118 	req[1] = WLAN_SA_QUERY_REQUEST;
1119 	os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1120 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1121 				wpa_s->own_addr, wpa_s->bssid,
1122 				req, sizeof(req), 0) < 0)
1123 		wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
1124 			"Request");
1125 }
1126 
1127 
1128 static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1129 {
1130 	struct wpa_supplicant *wpa_s = eloop_ctx;
1131 	unsigned int timeout, sec, usec;
1132 	u8 *trans_id, *nbuf;
1133 
1134 	if (wpa_s->sme.sa_query_count > 0 &&
1135 	    sme_check_sa_query_timeout(wpa_s))
1136 		return;
1137 
1138 	nbuf = os_realloc_array(wpa_s->sme.sa_query_trans_id,
1139 				wpa_s->sme.sa_query_count + 1,
1140 				WLAN_SA_QUERY_TR_ID_LEN);
1141 	if (nbuf == NULL)
1142 		return;
1143 	if (wpa_s->sme.sa_query_count == 0) {
1144 		/* Starting a new SA Query procedure */
1145 		os_get_time(&wpa_s->sme.sa_query_start);
1146 	}
1147 	trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1148 	wpa_s->sme.sa_query_trans_id = nbuf;
1149 	wpa_s->sme.sa_query_count++;
1150 
1151 	os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1152 
1153 	timeout = sa_query_retry_timeout;
1154 	sec = ((timeout / 1000) * 1024) / 1000;
1155 	usec = (timeout % 1000) * 1024;
1156 	eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
1157 
1158 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
1159 		wpa_s->sme.sa_query_count);
1160 
1161 	sme_send_sa_query_req(wpa_s, trans_id);
1162 }
1163 
1164 
1165 static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
1166 {
1167 	sme_sa_query_timer(wpa_s, NULL);
1168 }
1169 
1170 
1171 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
1172 {
1173 	eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
1174 	os_free(wpa_s->sme.sa_query_trans_id);
1175 	wpa_s->sme.sa_query_trans_id = NULL;
1176 	wpa_s->sme.sa_query_count = 0;
1177 }
1178 
1179 
1180 void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
1181 				 const u8 *da, u16 reason_code)
1182 {
1183 	struct wpa_ssid *ssid;
1184 
1185 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1186 		return;
1187 	if (wpa_s->wpa_state != WPA_COMPLETED)
1188 		return;
1189 	ssid = wpa_s->current_ssid;
1190 	if (ssid == NULL ||
1191 	    (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1192 	     wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION)
1193 		return;
1194 	if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1195 		return;
1196 	if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
1197 	    reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
1198 		return;
1199 	if (wpa_s->sme.sa_query_count > 0)
1200 		return;
1201 
1202 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
1203 		"possible AP/STA state mismatch - trigger SA Query");
1204 	sme_start_sa_query(wpa_s);
1205 }
1206 
1207 
1208 void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
1209 		     const u8 *data, size_t len)
1210 {
1211 	int i;
1212 
1213 	if (wpa_s->sme.sa_query_trans_id == NULL ||
1214 	    len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
1215 	    data[0] != WLAN_SA_QUERY_RESPONSE)
1216 		return;
1217 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
1218 		MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
1219 
1220 	if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1221 		return;
1222 
1223 	for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
1224 		if (os_memcmp(wpa_s->sme.sa_query_trans_id +
1225 			      i * WLAN_SA_QUERY_TR_ID_LEN,
1226 			      data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
1227 			break;
1228 	}
1229 
1230 	if (i >= wpa_s->sme.sa_query_count) {
1231 		wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
1232 			"transaction identifier found");
1233 		return;
1234 	}
1235 
1236 	wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
1237 		"from " MACSTR, MAC2STR(sa));
1238 	sme_stop_sa_query(wpa_s);
1239 }
1240 
1241 #endif /* CONFIG_IEEE80211W */
1242