xref: /freebsd/contrib/wpa/wpa_supplicant/events.c (revision 11d38a5764295585a2472d5e861fa8abe1a11eb2)
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2015, 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 "eapol_supp/eapol_supp_sm.h"
13 #include "rsn_supp/wpa.h"
14 #include "eloop.h"
15 #include "config.h"
16 #include "l2_packet/l2_packet.h"
17 #include "wpa_supplicant_i.h"
18 #include "driver_i.h"
19 #include "pcsc_funcs.h"
20 #include "rsn_supp/preauth.h"
21 #include "rsn_supp/pmksa_cache.h"
22 #include "common/wpa_ctrl.h"
23 #include "eap_peer/eap.h"
24 #include "ap/hostapd.h"
25 #include "p2p/p2p.h"
26 #include "fst/fst.h"
27 #include "wnm_sta.h"
28 #include "notify.h"
29 #include "common/ieee802_11_defs.h"
30 #include "common/ieee802_11_common.h"
31 #include "crypto/random.h"
32 #include "blacklist.h"
33 #include "wpas_glue.h"
34 #include "wps_supplicant.h"
35 #include "ibss_rsn.h"
36 #include "sme.h"
37 #include "gas_query.h"
38 #include "p2p_supplicant.h"
39 #include "bgscan.h"
40 #include "autoscan.h"
41 #include "ap.h"
42 #include "bss.h"
43 #include "scan.h"
44 #include "offchannel.h"
45 #include "interworking.h"
46 #include "mesh.h"
47 #include "mesh_mpm.h"
48 #include "wmm_ac.h"
49 
50 
51 #ifndef CONFIG_NO_SCAN_PROCESSING
52 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
53 					      int new_scan, int own_request);
54 #endif /* CONFIG_NO_SCAN_PROCESSING */
55 
56 
57 static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
58 			      struct wpa_ssid *ssid)
59 {
60 	struct os_reltime now;
61 
62 	if (ssid == NULL || ssid->disabled_until.sec == 0)
63 		return 0;
64 
65 	os_get_reltime(&now);
66 	if (ssid->disabled_until.sec > now.sec)
67 		return ssid->disabled_until.sec - now.sec;
68 
69 	wpas_clear_temp_disabled(wpa_s, ssid, 0);
70 
71 	return 0;
72 }
73 
74 
75 /**
76  * wpas_reenabled_network_time - Time until first network is re-enabled
77  * @wpa_s: Pointer to wpa_supplicant data
78  * Returns: If all enabled networks are temporarily disabled, returns the time
79  *	(in sec) until the first network is re-enabled. Otherwise returns 0.
80  *
81  * This function is used in case all enabled networks are temporarily disabled,
82  * in which case it returns the time (in sec) that the first network will be
83  * re-enabled. The function assumes that at least one network is enabled.
84  */
85 static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
86 {
87 	struct wpa_ssid *ssid;
88 	int disabled_for, res = 0;
89 
90 #ifdef CONFIG_INTERWORKING
91 	if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
92 	    wpa_s->conf->cred)
93 		return 0;
94 #endif /* CONFIG_INTERWORKING */
95 
96 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
97 		if (ssid->disabled)
98 			continue;
99 
100 		disabled_for = wpas_temp_disabled(wpa_s, ssid);
101 		if (!disabled_for)
102 			return 0;
103 
104 		if (!res || disabled_for < res)
105 			res = disabled_for;
106 	}
107 
108 	return res;
109 }
110 
111 
112 void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
113 {
114 	struct wpa_supplicant *wpa_s = eloop_ctx;
115 
116 	if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
117 		return;
118 
119 	wpa_dbg(wpa_s, MSG_DEBUG,
120 		"Try to associate due to network getting re-enabled");
121 	if (wpa_supplicant_fast_associate(wpa_s) != 1) {
122 		wpa_supplicant_cancel_sched_scan(wpa_s);
123 		wpa_supplicant_req_scan(wpa_s, 0, 0);
124 	}
125 }
126 
127 
128 static struct wpa_bss * wpa_supplicant_get_new_bss(
129 	struct wpa_supplicant *wpa_s, const u8 *bssid)
130 {
131 	struct wpa_bss *bss = NULL;
132 	struct wpa_ssid *ssid = wpa_s->current_ssid;
133 
134 	if (ssid->ssid_len > 0)
135 		bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
136 	if (!bss)
137 		bss = wpa_bss_get_bssid(wpa_s, bssid);
138 
139 	return bss;
140 }
141 
142 
143 static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
144 {
145 	struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
146 
147 	if (!bss) {
148 		wpa_supplicant_update_scan_results(wpa_s);
149 
150 		/* Get the BSS from the new scan results */
151 		bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
152 	}
153 
154 	if (bss)
155 		wpa_s->current_bss = bss;
156 }
157 
158 
159 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
160 {
161 	struct wpa_ssid *ssid, *old_ssid;
162 	u8 drv_ssid[SSID_MAX_LEN];
163 	size_t drv_ssid_len;
164 	int res;
165 
166 	if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
167 		wpa_supplicant_update_current_bss(wpa_s);
168 
169 		if (wpa_s->current_ssid->ssid_len == 0)
170 			return 0; /* current profile still in use */
171 		res = wpa_drv_get_ssid(wpa_s, drv_ssid);
172 		if (res < 0) {
173 			wpa_msg(wpa_s, MSG_INFO,
174 				"Failed to read SSID from driver");
175 			return 0; /* try to use current profile */
176 		}
177 		drv_ssid_len = res;
178 
179 		if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
180 		    os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
181 			      drv_ssid_len) == 0)
182 			return 0; /* current profile still in use */
183 
184 		wpa_msg(wpa_s, MSG_DEBUG,
185 			"Driver-initiated BSS selection changed the SSID to %s",
186 			wpa_ssid_txt(drv_ssid, drv_ssid_len));
187 		/* continue selecting a new network profile */
188 	}
189 
190 	wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
191 		"information");
192 	ssid = wpa_supplicant_get_ssid(wpa_s);
193 	if (ssid == NULL) {
194 		wpa_msg(wpa_s, MSG_INFO,
195 			"No network configuration found for the current AP");
196 		return -1;
197 	}
198 
199 	if (wpas_network_disabled(wpa_s, ssid)) {
200 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
201 		return -1;
202 	}
203 
204 	if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
205 	    disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
206 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
207 		return -1;
208 	}
209 
210 	res = wpas_temp_disabled(wpa_s, ssid);
211 	if (res > 0) {
212 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
213 			"disabled for %d second(s)", res);
214 		return -1;
215 	}
216 
217 	wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
218 		"current AP");
219 	if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
220 		u8 wpa_ie[80];
221 		size_t wpa_ie_len = sizeof(wpa_ie);
222 		if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
223 					      wpa_ie, &wpa_ie_len) < 0)
224 			wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
225 	} else {
226 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
227 	}
228 
229 	if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
230 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
231 	old_ssid = wpa_s->current_ssid;
232 	wpa_s->current_ssid = ssid;
233 
234 	wpa_supplicant_update_current_bss(wpa_s);
235 
236 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
237 	wpa_supplicant_initiate_eapol(wpa_s);
238 	if (old_ssid != wpa_s->current_ssid)
239 		wpas_notify_network_changed(wpa_s);
240 
241 	return 0;
242 }
243 
244 
245 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
246 {
247 	struct wpa_supplicant *wpa_s = eloop_ctx;
248 
249 	if (wpa_s->countermeasures) {
250 		wpa_s->countermeasures = 0;
251 		wpa_drv_set_countermeasures(wpa_s, 0);
252 		wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
253 
254 		/*
255 		 * It is possible that the device is sched scanning, which means
256 		 * that a connection attempt will be done only when we receive
257 		 * scan results. However, in this case, it would be preferable
258 		 * to scan and connect immediately, so cancel the sched_scan and
259 		 * issue a regular scan flow.
260 		 */
261 		wpa_supplicant_cancel_sched_scan(wpa_s);
262 		wpa_supplicant_req_scan(wpa_s, 0, 0);
263 	}
264 }
265 
266 
267 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
268 {
269 	int bssid_changed;
270 
271 	wnm_bss_keep_alive_deinit(wpa_s);
272 
273 #ifdef CONFIG_IBSS_RSN
274 	ibss_rsn_deinit(wpa_s->ibss_rsn);
275 	wpa_s->ibss_rsn = NULL;
276 #endif /* CONFIG_IBSS_RSN */
277 
278 #ifdef CONFIG_AP
279 	wpa_supplicant_ap_deinit(wpa_s);
280 #endif /* CONFIG_AP */
281 
282 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
283 		return;
284 
285 	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
286 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
287 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
288 	os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
289 	sme_clear_on_disassoc(wpa_s);
290 	wpa_s->current_bss = NULL;
291 	wpa_s->assoc_freq = 0;
292 
293 	if (bssid_changed)
294 		wpas_notify_bssid_changed(wpa_s);
295 
296 	eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
297 	eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
298 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
299 		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
300 	wpa_s->ap_ies_from_associnfo = 0;
301 	wpa_s->current_ssid = NULL;
302 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
303 	wpa_s->key_mgmt = 0;
304 
305 	wpas_rrm_reset(wpa_s);
306 }
307 
308 
309 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
310 {
311 	struct wpa_ie_data ie;
312 	int pmksa_set = -1;
313 	size_t i;
314 
315 	if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
316 	    ie.pmkid == NULL)
317 		return;
318 
319 	for (i = 0; i < ie.num_pmkid; i++) {
320 		pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
321 						    ie.pmkid + i * PMKID_LEN,
322 						    NULL, NULL, 0);
323 		if (pmksa_set == 0) {
324 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
325 			break;
326 		}
327 	}
328 
329 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
330 		"PMKSA cache", pmksa_set == 0 ? "" : "not ");
331 }
332 
333 
334 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
335 						 union wpa_event_data *data)
336 {
337 	if (data == NULL) {
338 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
339 			"event");
340 		return;
341 	}
342 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
343 		" index=%d preauth=%d",
344 		MAC2STR(data->pmkid_candidate.bssid),
345 		data->pmkid_candidate.index,
346 		data->pmkid_candidate.preauth);
347 
348 	pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
349 			    data->pmkid_candidate.index,
350 			    data->pmkid_candidate.preauth);
351 }
352 
353 
354 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
355 {
356 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
357 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
358 		return 0;
359 
360 #ifdef IEEE8021X_EAPOL
361 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
362 	    wpa_s->current_ssid &&
363 	    !(wpa_s->current_ssid->eapol_flags &
364 	      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
365 	       EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
366 		/* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
367 		 * plaintext or static WEP keys). */
368 		return 0;
369 	}
370 #endif /* IEEE8021X_EAPOL */
371 
372 	return 1;
373 }
374 
375 
376 /**
377  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
378  * @wpa_s: pointer to wpa_supplicant data
379  * @ssid: Configuration data for the network
380  * Returns: 0 on success, -1 on failure
381  *
382  * This function is called when starting authentication with a network that is
383  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
384  */
385 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
386 			      struct wpa_ssid *ssid)
387 {
388 #ifdef IEEE8021X_EAPOL
389 #ifdef PCSC_FUNCS
390 	int aka = 0, sim = 0;
391 
392 	if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
393 	    wpa_s->scard != NULL || wpa_s->conf->external_sim)
394 		return 0;
395 
396 	if (ssid == NULL || ssid->eap.eap_methods == NULL) {
397 		sim = 1;
398 		aka = 1;
399 	} else {
400 		struct eap_method_type *eap = ssid->eap.eap_methods;
401 		while (eap->vendor != EAP_VENDOR_IETF ||
402 		       eap->method != EAP_TYPE_NONE) {
403 			if (eap->vendor == EAP_VENDOR_IETF) {
404 				if (eap->method == EAP_TYPE_SIM)
405 					sim = 1;
406 				else if (eap->method == EAP_TYPE_AKA ||
407 					 eap->method == EAP_TYPE_AKA_PRIME)
408 					aka = 1;
409 			}
410 			eap++;
411 		}
412 	}
413 
414 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
415 		sim = 0;
416 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
417 	    eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
418 	    NULL)
419 		aka = 0;
420 
421 	if (!sim && !aka) {
422 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
423 			"use SIM, but neither EAP-SIM nor EAP-AKA are "
424 			"enabled");
425 		return 0;
426 	}
427 
428 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
429 		"(sim=%d aka=%d) - initialize PCSC", sim, aka);
430 
431 	wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
432 	if (wpa_s->scard == NULL) {
433 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
434 			"(pcsc-lite)");
435 		return -1;
436 	}
437 	wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
438 	eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
439 #endif /* PCSC_FUNCS */
440 #endif /* IEEE8021X_EAPOL */
441 
442 	return 0;
443 }
444 
445 
446 #ifndef CONFIG_NO_SCAN_PROCESSING
447 
448 static int has_wep_key(struct wpa_ssid *ssid)
449 {
450 	int i;
451 
452 	for (i = 0; i < NUM_WEP_KEYS; i++) {
453 		if (ssid->wep_key_len[i])
454 			return 1;
455 	}
456 
457 	return 0;
458 }
459 
460 
461 static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
462 					struct wpa_ssid *ssid)
463 {
464 	int privacy = 0;
465 
466 	if (ssid->mixed_cell)
467 		return 1;
468 
469 #ifdef CONFIG_WPS
470 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
471 		return 1;
472 #endif /* CONFIG_WPS */
473 
474 	if (has_wep_key(ssid))
475 		privacy = 1;
476 
477 #ifdef IEEE8021X_EAPOL
478 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
479 	    ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
480 				 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
481 		privacy = 1;
482 #endif /* IEEE8021X_EAPOL */
483 
484 	if (wpa_key_mgmt_wpa(ssid->key_mgmt))
485 		privacy = 1;
486 
487 	if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
488 		privacy = 1;
489 
490 	if (bss->caps & IEEE80211_CAP_PRIVACY)
491 		return privacy;
492 	return !privacy;
493 }
494 
495 
496 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
497 					 struct wpa_ssid *ssid,
498 					 struct wpa_bss *bss)
499 {
500 	struct wpa_ie_data ie;
501 	int proto_match = 0;
502 	const u8 *rsn_ie, *wpa_ie;
503 	int ret;
504 	int wep_ok;
505 
506 	ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
507 	if (ret >= 0)
508 		return ret;
509 
510 	/* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
511 	wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
512 		(((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
513 		  ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
514 		 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
515 
516 	rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
517 	while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
518 		proto_match++;
519 
520 		if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
521 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - parse "
522 				"failed");
523 			break;
524 		}
525 
526 		if (wep_ok &&
527 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
528 		{
529 			wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
530 				"in RSN IE");
531 			return 1;
532 		}
533 
534 		if (!(ie.proto & ssid->proto)) {
535 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - proto "
536 				"mismatch");
537 			break;
538 		}
539 
540 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
541 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - PTK "
542 				"cipher mismatch");
543 			break;
544 		}
545 
546 		if (!(ie.group_cipher & ssid->group_cipher)) {
547 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - GTK "
548 				"cipher mismatch");
549 			break;
550 		}
551 
552 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
553 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - key mgmt "
554 				"mismatch");
555 			break;
556 		}
557 
558 #ifdef CONFIG_IEEE80211W
559 		if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
560 		    wpas_get_ssid_pmf(wpa_s, ssid) ==
561 		    MGMT_FRAME_PROTECTION_REQUIRED) {
562 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - no mgmt "
563 				"frame protection");
564 			break;
565 		}
566 #endif /* CONFIG_IEEE80211W */
567 
568 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on RSN IE");
569 		return 1;
570 	}
571 
572 	wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
573 	while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
574 		proto_match++;
575 
576 		if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
577 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - parse "
578 				"failed");
579 			break;
580 		}
581 
582 		if (wep_ok &&
583 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
584 		{
585 			wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
586 				"in WPA IE");
587 			return 1;
588 		}
589 
590 		if (!(ie.proto & ssid->proto)) {
591 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - proto "
592 				"mismatch");
593 			break;
594 		}
595 
596 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
597 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - PTK "
598 				"cipher mismatch");
599 			break;
600 		}
601 
602 		if (!(ie.group_cipher & ssid->group_cipher)) {
603 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - GTK "
604 				"cipher mismatch");
605 			break;
606 		}
607 
608 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
609 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - key mgmt "
610 				"mismatch");
611 			break;
612 		}
613 
614 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on WPA IE");
615 		return 1;
616 	}
617 
618 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
619 	    !rsn_ie) {
620 		wpa_dbg(wpa_s, MSG_DEBUG, "   allow for non-WPA IEEE 802.1X");
621 		return 1;
622 	}
623 
624 	if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
625 	    wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
626 		wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no WPA/RSN proto match");
627 		return 0;
628 	}
629 
630 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
631 	    wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
632 		wpa_dbg(wpa_s, MSG_DEBUG, "   allow in OSEN");
633 		return 1;
634 	}
635 
636 	if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
637 		wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
638 		return 1;
639 	}
640 
641 	wpa_dbg(wpa_s, MSG_DEBUG, "   reject due to mismatch with "
642 		"WPA/WPA2");
643 
644 	return 0;
645 }
646 
647 
648 static int freq_allowed(int *freqs, int freq)
649 {
650 	int i;
651 
652 	if (freqs == NULL)
653 		return 1;
654 
655 	for (i = 0; freqs[i]; i++)
656 		if (freqs[i] == freq)
657 			return 1;
658 	return 0;
659 }
660 
661 
662 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
663 {
664 	const struct hostapd_hw_modes *mode = NULL, *modes;
665 	const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
666 	const u8 *rate_ie;
667 	int i, j, k;
668 
669 	if (bss->freq == 0)
670 		return 1; /* Cannot do matching without knowing band */
671 
672 	modes = wpa_s->hw.modes;
673 	if (modes == NULL) {
674 		/*
675 		 * The driver does not provide any additional information
676 		 * about the utilized hardware, so allow the connection attempt
677 		 * to continue.
678 		 */
679 		return 1;
680 	}
681 
682 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
683 		for (j = 0; j < modes[i].num_channels; j++) {
684 			int freq = modes[i].channels[j].freq;
685 			if (freq == bss->freq) {
686 				if (mode &&
687 				    mode->mode == HOSTAPD_MODE_IEEE80211G)
688 					break; /* do not allow 802.11b replace
689 						* 802.11g */
690 				mode = &modes[i];
691 				break;
692 			}
693 		}
694 	}
695 
696 	if (mode == NULL)
697 		return 0;
698 
699 	for (i = 0; i < (int) sizeof(scan_ie); i++) {
700 		rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
701 		if (rate_ie == NULL)
702 			continue;
703 
704 		for (j = 2; j < rate_ie[1] + 2; j++) {
705 			int flagged = !!(rate_ie[j] & 0x80);
706 			int r = (rate_ie[j] & 0x7f) * 5;
707 
708 			/*
709 			 * IEEE Std 802.11n-2009 7.3.2.2:
710 			 * The new BSS Membership selector value is encoded
711 			 * like a legacy basic rate, but it is not a rate and
712 			 * only indicates if the BSS members are required to
713 			 * support the mandatory features of Clause 20 [HT PHY]
714 			 * in order to join the BSS.
715 			 */
716 			if (flagged && ((rate_ie[j] & 0x7f) ==
717 					BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
718 				if (!ht_supported(mode)) {
719 					wpa_dbg(wpa_s, MSG_DEBUG,
720 						"   hardware does not support "
721 						"HT PHY");
722 					return 0;
723 				}
724 				continue;
725 			}
726 
727 			/* There's also a VHT selector for 802.11ac */
728 			if (flagged && ((rate_ie[j] & 0x7f) ==
729 					BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
730 				if (!vht_supported(mode)) {
731 					wpa_dbg(wpa_s, MSG_DEBUG,
732 						"   hardware does not support "
733 						"VHT PHY");
734 					return 0;
735 				}
736 				continue;
737 			}
738 
739 			if (!flagged)
740 				continue;
741 
742 			/* check for legacy basic rates */
743 			for (k = 0; k < mode->num_rates; k++) {
744 				if (mode->rates[k] == r)
745 					break;
746 			}
747 			if (k == mode->num_rates) {
748 				/*
749 				 * IEEE Std 802.11-2007 7.3.2.2 demands that in
750 				 * order to join a BSS all required rates
751 				 * have to be supported by the hardware.
752 				 */
753 				wpa_dbg(wpa_s, MSG_DEBUG,
754 					"   hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
755 					r / 10, r % 10,
756 					bss->freq, mode->mode, mode->num_rates);
757 				return 0;
758 			}
759 		}
760 	}
761 
762 	return 1;
763 }
764 
765 
766 /*
767  * Test whether BSS is in an ESS.
768  * This is done differently in DMG (60 GHz) and non-DMG bands
769  */
770 static int bss_is_ess(struct wpa_bss *bss)
771 {
772 	if (bss_is_dmg(bss)) {
773 		return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
774 			IEEE80211_CAP_DMG_AP;
775 	}
776 
777 	return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
778 		IEEE80211_CAP_ESS);
779 }
780 
781 
782 static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
783 {
784 	size_t i;
785 
786 	for (i = 0; i < ETH_ALEN; i++) {
787 		if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
788 			return 0;
789 	}
790 	return 1;
791 }
792 
793 
794 static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
795 {
796 	size_t i;
797 
798 	for (i = 0; i < num; i++) {
799 		const u8 *a = list + i * ETH_ALEN * 2;
800 		const u8 *m = a + ETH_ALEN;
801 
802 		if (match_mac_mask(a, addr, m))
803 			return 1;
804 	}
805 	return 0;
806 }
807 
808 
809 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
810 					    int i, struct wpa_bss *bss,
811 					    struct wpa_ssid *group,
812 					    int only_first_ssid)
813 {
814 	u8 wpa_ie_len, rsn_ie_len;
815 	int wpa;
816 	struct wpa_blacklist *e;
817 	const u8 *ie;
818 	struct wpa_ssid *ssid;
819 	int osen;
820 
821 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
822 	wpa_ie_len = ie ? ie[1] : 0;
823 
824 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
825 	rsn_ie_len = ie ? ie[1] : 0;
826 
827 	ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
828 	osen = ie != NULL;
829 
830 	wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
831 		"wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
832 		i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
833 		wpa_ie_len, rsn_ie_len, bss->caps, bss->level, bss->freq,
834 		wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
835 		(wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
836 		 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
837 		" p2p" : "",
838 		osen ? " osen=1" : "");
839 
840 	e = wpa_blacklist_get(wpa_s, bss->bssid);
841 	if (e) {
842 		int limit = 1;
843 		if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
844 			/*
845 			 * When only a single network is enabled, we can
846 			 * trigger blacklisting on the first failure. This
847 			 * should not be done with multiple enabled networks to
848 			 * avoid getting forced to move into a worse ESS on
849 			 * single error if there are no other BSSes of the
850 			 * current ESS.
851 			 */
852 			limit = 0;
853 		}
854 		if (e->count > limit) {
855 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
856 				"(count=%d limit=%d)", e->count, limit);
857 			return NULL;
858 		}
859 	}
860 
861 	if (bss->ssid_len == 0) {
862 		wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
863 		return NULL;
864 	}
865 
866 	if (disallowed_bssid(wpa_s, bss->bssid)) {
867 		wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID disallowed");
868 		return NULL;
869 	}
870 
871 	if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
872 		wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID disallowed");
873 		return NULL;
874 	}
875 
876 	wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
877 
878 	for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
879 		int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
880 		int res;
881 
882 		if (wpas_network_disabled(wpa_s, ssid)) {
883 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
884 			continue;
885 		}
886 
887 		res = wpas_temp_disabled(wpa_s, ssid);
888 		if (res > 0) {
889 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled "
890 				"temporarily for %d second(s)", res);
891 			continue;
892 		}
893 
894 #ifdef CONFIG_WPS
895 		if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
896 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
897 				"(WPS)");
898 			continue;
899 		}
900 
901 		if (wpa && ssid->ssid_len == 0 &&
902 		    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
903 			check_ssid = 0;
904 
905 		if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
906 			/* Only allow wildcard SSID match if an AP
907 			 * advertises active WPS operation that matches
908 			 * with our mode. */
909 			check_ssid = 1;
910 			if (ssid->ssid_len == 0 &&
911 			    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
912 				check_ssid = 0;
913 		}
914 #endif /* CONFIG_WPS */
915 
916 		if (ssid->bssid_set && ssid->ssid_len == 0 &&
917 		    os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
918 			check_ssid = 0;
919 
920 		if (check_ssid &&
921 		    (bss->ssid_len != ssid->ssid_len ||
922 		     os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
923 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
924 			continue;
925 		}
926 
927 		if (ssid->bssid_set &&
928 		    os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
929 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
930 			continue;
931 		}
932 
933 		/* check blacklist */
934 		if (ssid->num_bssid_blacklist &&
935 		    addr_in_list(bss->bssid, ssid->bssid_blacklist,
936 				 ssid->num_bssid_blacklist)) {
937 			wpa_dbg(wpa_s, MSG_DEBUG,
938 				"   skip - BSSID blacklisted");
939 			continue;
940 		}
941 
942 		/* if there is a whitelist, only accept those APs */
943 		if (ssid->num_bssid_whitelist &&
944 		    !addr_in_list(bss->bssid, ssid->bssid_whitelist,
945 				  ssid->num_bssid_whitelist)) {
946 			wpa_dbg(wpa_s, MSG_DEBUG,
947 				"   skip - BSSID not in whitelist");
948 			continue;
949 		}
950 
951 		if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
952 			continue;
953 
954 		if (!osen && !wpa &&
955 		    !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
956 		    !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
957 		    !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
958 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-WPA network "
959 				"not allowed");
960 			continue;
961 		}
962 
963 		if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
964 		    has_wep_key(ssid)) {
965 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - ignore WPA/WPA2 AP for WEP network block");
966 			continue;
967 		}
968 
969 		if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
970 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-OSEN network "
971 				"not allowed");
972 			continue;
973 		}
974 
975 		if (!wpa_supplicant_match_privacy(bss, ssid)) {
976 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy "
977 				"mismatch");
978 			continue;
979 		}
980 
981 		if (!bss_is_ess(bss)) {
982 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - not ESS network");
983 			continue;
984 		}
985 
986 		if (!freq_allowed(ssid->freq_list, bss->freq)) {
987 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - frequency not "
988 				"allowed");
989 			continue;
990 		}
991 
992 		if (!rate_match(wpa_s, bss)) {
993 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - rate sets do "
994 				"not match");
995 			continue;
996 		}
997 
998 #ifdef CONFIG_P2P
999 		if (ssid->p2p_group &&
1000 		    !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1001 		    !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
1002 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no P2P IE seen");
1003 			continue;
1004 		}
1005 
1006 		if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1007 			struct wpabuf *p2p_ie;
1008 			u8 dev_addr[ETH_ALEN];
1009 
1010 			ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1011 			if (ie == NULL) {
1012 				wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no P2P element");
1013 				continue;
1014 			}
1015 			p2p_ie = wpa_bss_get_vendor_ie_multi(
1016 				bss, P2P_IE_VENDOR_TYPE);
1017 			if (p2p_ie == NULL) {
1018 				wpa_dbg(wpa_s, MSG_DEBUG, "   skip - could not fetch P2P element");
1019 				continue;
1020 			}
1021 
1022 			if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
1023 			    || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
1024 					 ETH_ALEN) != 0) {
1025 				wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no matching GO P2P Device Address in P2P element");
1026 				wpabuf_free(p2p_ie);
1027 				continue;
1028 			}
1029 			wpabuf_free(p2p_ie);
1030 		}
1031 
1032 		/*
1033 		 * TODO: skip the AP if its P2P IE has Group Formation
1034 		 * bit set in the P2P Group Capability Bitmap and we
1035 		 * are not in Group Formation with that device.
1036 		 */
1037 #endif /* CONFIG_P2P */
1038 
1039 		if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
1040 		{
1041 			struct os_reltime diff;
1042 
1043 			os_reltime_sub(&wpa_s->scan_min_time,
1044 				       &bss->last_update, &diff);
1045 			wpa_dbg(wpa_s, MSG_DEBUG,
1046 				"   skip - scan result not recent enough (%u.%06u seconds too old)",
1047 				(unsigned int) diff.sec,
1048 				(unsigned int) diff.usec);
1049 			continue;
1050 		}
1051 
1052 		/* Matching configuration found */
1053 		return ssid;
1054 	}
1055 
1056 	/* No matching configuration found */
1057 	return NULL;
1058 }
1059 
1060 
1061 static struct wpa_bss *
1062 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
1063 			  struct wpa_ssid *group,
1064 			  struct wpa_ssid **selected_ssid,
1065 			  int only_first_ssid)
1066 {
1067 	unsigned int i;
1068 
1069 	if (only_first_ssid)
1070 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1071 			group->id);
1072 	else
1073 		wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1074 			group->priority);
1075 
1076 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1077 		struct wpa_bss *bss = wpa_s->last_scan_res[i];
1078 		*selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1079 						    only_first_ssid);
1080 		if (!*selected_ssid)
1081 			continue;
1082 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
1083 			" ssid='%s'",
1084 			MAC2STR(bss->bssid),
1085 			wpa_ssid_txt(bss->ssid, bss->ssid_len));
1086 		return bss;
1087 	}
1088 
1089 	return NULL;
1090 }
1091 
1092 
1093 struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1094 					     struct wpa_ssid **selected_ssid)
1095 {
1096 	struct wpa_bss *selected = NULL;
1097 	int prio;
1098 	struct wpa_ssid *next_ssid = NULL;
1099 	struct wpa_ssid *ssid;
1100 
1101 	if (wpa_s->last_scan_res == NULL ||
1102 	    wpa_s->last_scan_res_used == 0)
1103 		return NULL; /* no scan results from last update */
1104 
1105 	if (wpa_s->next_ssid) {
1106 		/* check that next_ssid is still valid */
1107 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1108 			if (ssid == wpa_s->next_ssid)
1109 				break;
1110 		}
1111 		next_ssid = ssid;
1112 		wpa_s->next_ssid = NULL;
1113 	}
1114 
1115 	while (selected == NULL) {
1116 		for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1117 			if (next_ssid && next_ssid->priority ==
1118 			    wpa_s->conf->pssid[prio]->priority) {
1119 				selected = wpa_supplicant_select_bss(
1120 					wpa_s, next_ssid, selected_ssid, 1);
1121 				if (selected)
1122 					break;
1123 			}
1124 			selected = wpa_supplicant_select_bss(
1125 				wpa_s, wpa_s->conf->pssid[prio],
1126 				selected_ssid, 0);
1127 			if (selected)
1128 				break;
1129 		}
1130 
1131 		if (selected == NULL && wpa_s->blacklist &&
1132 		    !wpa_s->countermeasures) {
1133 			wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1134 				"blacklist and try again");
1135 			wpa_blacklist_clear(wpa_s);
1136 			wpa_s->blacklist_cleared++;
1137 		} else if (selected == NULL)
1138 			break;
1139 	}
1140 
1141 	ssid = *selected_ssid;
1142 	if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1143 	    !ssid->passphrase && !ssid->ext_psk) {
1144 		const char *field_name, *txt = NULL;
1145 
1146 		wpa_dbg(wpa_s, MSG_DEBUG,
1147 			"PSK/passphrase not yet available for the selected network");
1148 
1149 		wpas_notify_network_request(wpa_s, ssid,
1150 					    WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1151 
1152 		field_name = wpa_supplicant_ctrl_req_to_string(
1153 			WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1154 		if (field_name == NULL)
1155 			return NULL;
1156 
1157 		wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1158 
1159 		selected = NULL;
1160 	}
1161 
1162 	return selected;
1163 }
1164 
1165 
1166 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1167 					int timeout_sec, int timeout_usec)
1168 {
1169 	if (!wpa_supplicant_enabled_networks(wpa_s)) {
1170 		/*
1171 		 * No networks are enabled; short-circuit request so
1172 		 * we don't wait timeout seconds before transitioning
1173 		 * to INACTIVE state.
1174 		 */
1175 		wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1176 			"since there are no enabled networks");
1177 		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1178 		return;
1179 	}
1180 
1181 	wpa_s->scan_for_connection = 1;
1182 	wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1183 }
1184 
1185 
1186 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
1187 			   struct wpa_bss *selected,
1188 			   struct wpa_ssid *ssid)
1189 {
1190 	if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1191 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1192 			"PBC session overlap");
1193 		wpas_notify_wps_event_pbc_overlap(wpa_s);
1194 #ifdef CONFIG_P2P
1195 		if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1196 		    wpa_s->p2p_in_provisioning) {
1197 			eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1198 					       wpa_s, NULL);
1199 			return -1;
1200 		}
1201 #endif /* CONFIG_P2P */
1202 
1203 #ifdef CONFIG_WPS
1204 		wpas_wps_pbc_overlap(wpa_s);
1205 		wpas_wps_cancel(wpa_s);
1206 #endif /* CONFIG_WPS */
1207 		return -1;
1208 	}
1209 
1210 	wpa_msg(wpa_s, MSG_DEBUG,
1211 		"Considering connect request: reassociate: %d  selected: "
1212 		MACSTR "  bssid: " MACSTR "  pending: " MACSTR
1213 		"  wpa_state: %s  ssid=%p  current_ssid=%p",
1214 		wpa_s->reassociate, MAC2STR(selected->bssid),
1215 		MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1216 		wpa_supplicant_state_txt(wpa_s->wpa_state),
1217 		ssid, wpa_s->current_ssid);
1218 
1219 	/*
1220 	 * Do not trigger new association unless the BSSID has changed or if
1221 	 * reassociation is requested. If we are in process of associating with
1222 	 * the selected BSSID, do not trigger new attempt.
1223 	 */
1224 	if (wpa_s->reassociate ||
1225 	    (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1226 	     ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1227 	       wpa_s->wpa_state != WPA_AUTHENTICATING) ||
1228 	      (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1229 	       os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1230 	       0) ||
1231 	      (is_zero_ether_addr(wpa_s->pending_bssid) &&
1232 	       ssid != wpa_s->current_ssid)))) {
1233 		if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1234 			wpa_supplicant_req_new_scan(wpa_s, 10, 0);
1235 			return 0;
1236 		}
1237 		wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1238 			MAC2STR(selected->bssid));
1239 		wpa_supplicant_associate(wpa_s, selected, ssid);
1240 	} else {
1241 		wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1242 			"connect with the selected AP");
1243 	}
1244 
1245 	return 0;
1246 }
1247 
1248 
1249 static struct wpa_ssid *
1250 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1251 {
1252 	int prio;
1253 	struct wpa_ssid *ssid;
1254 
1255 	for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1256 		for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1257 		{
1258 			if (wpas_network_disabled(wpa_s, ssid))
1259 				continue;
1260 			if (ssid->mode == IEEE80211_MODE_IBSS ||
1261 			    ssid->mode == IEEE80211_MODE_AP ||
1262 			    ssid->mode == IEEE80211_MODE_MESH)
1263 				return ssid;
1264 		}
1265 	}
1266 	return NULL;
1267 }
1268 
1269 
1270 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1271  * on BSS added and BSS changed events */
1272 static void wpa_supplicant_rsn_preauth_scan_results(
1273 	struct wpa_supplicant *wpa_s)
1274 {
1275 	struct wpa_bss *bss;
1276 
1277 	if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1278 		return;
1279 
1280 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1281 		const u8 *ssid, *rsn;
1282 
1283 		ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1284 		if (ssid == NULL)
1285 			continue;
1286 
1287 		rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1288 		if (rsn == NULL)
1289 			continue;
1290 
1291 		rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
1292 	}
1293 
1294 }
1295 
1296 
1297 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1298 				       struct wpa_bss *selected,
1299 				       struct wpa_ssid *ssid)
1300 {
1301 	struct wpa_bss *current_bss = NULL;
1302 #ifndef CONFIG_NO_ROAMING
1303 	int min_diff;
1304 #endif /* CONFIG_NO_ROAMING */
1305 
1306 	if (wpa_s->reassociate)
1307 		return 1; /* explicit request to reassociate */
1308 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
1309 		return 1; /* we are not associated; continue */
1310 	if (wpa_s->current_ssid == NULL)
1311 		return 1; /* unknown current SSID */
1312 	if (wpa_s->current_ssid != ssid)
1313 		return 1; /* different network block */
1314 
1315 	if (wpas_driver_bss_selection(wpa_s))
1316 		return 0; /* Driver-based roaming */
1317 
1318 	if (wpa_s->current_ssid->ssid)
1319 		current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1320 					  wpa_s->current_ssid->ssid,
1321 					  wpa_s->current_ssid->ssid_len);
1322 	if (!current_bss)
1323 		current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
1324 
1325 	if (!current_bss)
1326 		return 1; /* current BSS not seen in scan results */
1327 
1328 	if (current_bss == selected)
1329 		return 0;
1330 
1331 	if (selected->last_update_idx > current_bss->last_update_idx)
1332 		return 1; /* current BSS not seen in the last scan */
1333 
1334 #ifndef CONFIG_NO_ROAMING
1335 	wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1336 	wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
1337 		" level=%d snr=%d est_throughput=%u",
1338 		MAC2STR(current_bss->bssid), current_bss->level,
1339 		current_bss->snr, current_bss->est_throughput);
1340 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
1341 		" level=%d snr=%d est_throughput=%u",
1342 		MAC2STR(selected->bssid), selected->level,
1343 		selected->snr, selected->est_throughput);
1344 
1345 	if (wpa_s->current_ssid->bssid_set &&
1346 	    os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1347 	    0) {
1348 		wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1349 			"has preferred BSSID");
1350 		return 1;
1351 	}
1352 
1353 	if (selected->est_throughput > current_bss->est_throughput + 5000) {
1354 		wpa_dbg(wpa_s, MSG_DEBUG,
1355 			"Allow reassociation - selected BSS has better estimated throughput");
1356 		return 1;
1357 	}
1358 
1359 	if (current_bss->level < 0 && current_bss->level > selected->level) {
1360 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1361 			"signal level");
1362 		return 0;
1363 	}
1364 
1365 	min_diff = 2;
1366 	if (current_bss->level < 0) {
1367 		if (current_bss->level < -85)
1368 			min_diff = 1;
1369 		else if (current_bss->level < -80)
1370 			min_diff = 2;
1371 		else if (current_bss->level < -75)
1372 			min_diff = 3;
1373 		else if (current_bss->level < -70)
1374 			min_diff = 4;
1375 		else
1376 			min_diff = 5;
1377 	}
1378 	if (abs(current_bss->level - selected->level) < min_diff) {
1379 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1380 			"in signal level");
1381 		return 0;
1382 	}
1383 
1384 	return 1;
1385 #else /* CONFIG_NO_ROAMING */
1386 	return 0;
1387 #endif /* CONFIG_NO_ROAMING */
1388 }
1389 
1390 
1391 /* Return != 0 if no scan results could be fetched or if scan results should not
1392  * be shared with other virtual interfaces. */
1393 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1394 					      union wpa_event_data *data,
1395 					      int own_request)
1396 {
1397 	struct wpa_scan_results *scan_res = NULL;
1398 	int ret = 0;
1399 	int ap = 0;
1400 #ifndef CONFIG_NO_RANDOM_POOL
1401 	size_t i, num;
1402 #endif /* CONFIG_NO_RANDOM_POOL */
1403 
1404 #ifdef CONFIG_AP
1405 	if (wpa_s->ap_iface)
1406 		ap = 1;
1407 #endif /* CONFIG_AP */
1408 
1409 	wpa_supplicant_notify_scanning(wpa_s, 0);
1410 
1411 	scan_res = wpa_supplicant_get_scan_results(wpa_s,
1412 						   data ? &data->scan_info :
1413 						   NULL, 1);
1414 	if (scan_res == NULL) {
1415 		if (wpa_s->conf->ap_scan == 2 || ap ||
1416 		    wpa_s->scan_res_handler == scan_only_handler)
1417 			return -1;
1418 		if (!own_request)
1419 			return -1;
1420 		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1421 			"scanning again");
1422 		wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1423 		ret = -1;
1424 		goto scan_work_done;
1425 	}
1426 
1427 #ifndef CONFIG_NO_RANDOM_POOL
1428 	num = scan_res->num;
1429 	if (num > 10)
1430 		num = 10;
1431 	for (i = 0; i < num; i++) {
1432 		u8 buf[5];
1433 		struct wpa_scan_res *res = scan_res->res[i];
1434 		buf[0] = res->bssid[5];
1435 		buf[1] = res->qual & 0xff;
1436 		buf[2] = res->noise & 0xff;
1437 		buf[3] = res->level & 0xff;
1438 		buf[4] = res->tsf & 0xff;
1439 		random_add_randomness(buf, sizeof(buf));
1440 	}
1441 #endif /* CONFIG_NO_RANDOM_POOL */
1442 
1443 	if (own_request && wpa_s->scan_res_handler &&
1444 	    (wpa_s->own_scan_running || !wpa_s->radio->external_scan_running)) {
1445 		void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1446 					 struct wpa_scan_results *scan_res);
1447 
1448 		scan_res_handler = wpa_s->scan_res_handler;
1449 		wpa_s->scan_res_handler = NULL;
1450 		scan_res_handler(wpa_s, scan_res);
1451 		ret = -2;
1452 		goto scan_work_done;
1453 	}
1454 
1455 	if (ap) {
1456 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1457 #ifdef CONFIG_AP
1458 		if (wpa_s->ap_iface->scan_cb)
1459 			wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1460 #endif /* CONFIG_AP */
1461 		goto scan_work_done;
1462 	}
1463 
1464 	wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1465 		wpa_s->own_scan_running, wpa_s->radio->external_scan_running);
1466 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1467 	    wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1468 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1469 			     wpa_s->manual_scan_id);
1470 		wpa_s->manual_scan_use_id = 0;
1471 	} else {
1472 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1473 	}
1474 	wpas_notify_scan_results(wpa_s);
1475 
1476 	wpas_notify_scan_done(wpa_s, 1);
1477 
1478 	if (!wpa_s->own_scan_running && wpa_s->radio->external_scan_running) {
1479 		wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
1480 		wpa_scan_results_free(scan_res);
1481 		return 0;
1482 	}
1483 
1484 	if (wnm_scan_process(wpa_s, 1) > 0)
1485 		goto scan_work_done;
1486 
1487 	if (sme_proc_obss_scan(wpa_s) > 0)
1488 		goto scan_work_done;
1489 
1490 	if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1491 		goto scan_work_done;
1492 
1493 	if (autoscan_notify_scan(wpa_s, scan_res))
1494 		goto scan_work_done;
1495 
1496 	if (wpa_s->disconnected) {
1497 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1498 		goto scan_work_done;
1499 	}
1500 
1501 	if (!wpas_driver_bss_selection(wpa_s) &&
1502 	    bgscan_notify_scan(wpa_s, scan_res) == 1)
1503 		goto scan_work_done;
1504 
1505 	wpas_wps_update_ap_info(wpa_s, scan_res);
1506 
1507 	wpa_scan_results_free(scan_res);
1508 
1509 	if (wpa_s->scan_work) {
1510 		struct wpa_radio_work *work = wpa_s->scan_work;
1511 		wpa_s->scan_work = NULL;
1512 		radio_work_done(work);
1513 	}
1514 
1515 	return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
1516 
1517 scan_work_done:
1518 	wpa_scan_results_free(scan_res);
1519 	if (wpa_s->scan_work) {
1520 		struct wpa_radio_work *work = wpa_s->scan_work;
1521 		wpa_s->scan_work = NULL;
1522 		radio_work_done(work);
1523 	}
1524 	return ret;
1525 }
1526 
1527 
1528 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1529 					      int new_scan, int own_request)
1530 {
1531 	struct wpa_bss *selected;
1532 	struct wpa_ssid *ssid = NULL;
1533 	int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1534 
1535 	if (time_to_reenable > 0) {
1536 		wpa_dbg(wpa_s, MSG_DEBUG,
1537 			"Postpone network selection by %d seconds since all networks are disabled",
1538 			time_to_reenable);
1539 		eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1540 		eloop_register_timeout(time_to_reenable, 0,
1541 				       wpas_network_reenabled, wpa_s, NULL);
1542 		return 0;
1543 	}
1544 
1545 	if (wpa_s->p2p_mgmt)
1546 		return 0; /* no normal connection on p2p_mgmt interface */
1547 
1548 	selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1549 
1550 	if (selected) {
1551 		int skip;
1552 		skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1553 		if (skip) {
1554 			if (new_scan)
1555 				wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1556 			return 0;
1557 		}
1558 
1559 		if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1560 			wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1561 			return -1;
1562 		}
1563 		if (new_scan)
1564 			wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1565 		/*
1566 		 * Do not notify other virtual radios of scan results since we do not
1567 		 * want them to start other associations at the same time.
1568 		 */
1569 		return 1;
1570 	} else {
1571 #ifdef CONFIG_MESH
1572 		if (wpa_s->ifmsh) {
1573 			wpa_msg(wpa_s, MSG_INFO,
1574 				"Avoiding join because we already joined a mesh group");
1575 			return 0;
1576 		}
1577 #endif /* CONFIG_MESH */
1578 		wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1579 		ssid = wpa_supplicant_pick_new_network(wpa_s);
1580 		if (ssid) {
1581 			wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1582 			wpa_supplicant_associate(wpa_s, NULL, ssid);
1583 			if (new_scan)
1584 				wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1585 		} else if (own_request) {
1586 			/*
1587 			 * No SSID found. If SCAN results are as a result of
1588 			 * own scan request and not due to a scan request on
1589 			 * another shared interface, try another scan.
1590 			 */
1591 			int timeout_sec = wpa_s->scan_interval;
1592 			int timeout_usec = 0;
1593 #ifdef CONFIG_P2P
1594 			int res;
1595 
1596 			res = wpas_p2p_scan_no_go_seen(wpa_s);
1597 			if (res == 2)
1598 				return 2;
1599 			if (res == 1)
1600 				return 0;
1601 
1602 			if (wpa_s->p2p_in_provisioning ||
1603 			    wpa_s->show_group_started ||
1604 			    wpa_s->p2p_in_invitation) {
1605 				/*
1606 				 * Use shorter wait during P2P Provisioning
1607 				 * state and during P2P join-a-group operation
1608 				 * to speed up group formation.
1609 				 */
1610 				timeout_sec = 0;
1611 				timeout_usec = 250000;
1612 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1613 							    timeout_usec);
1614 				return 0;
1615 			}
1616 #endif /* CONFIG_P2P */
1617 #ifdef CONFIG_INTERWORKING
1618 			if (wpa_s->conf->auto_interworking &&
1619 			    wpa_s->conf->interworking &&
1620 			    wpa_s->conf->cred) {
1621 				wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1622 					"start ANQP fetch since no matching "
1623 					"networks found");
1624 				wpa_s->network_select = 1;
1625 				wpa_s->auto_network_select = 1;
1626 				interworking_start_fetch_anqp(wpa_s);
1627 				return 1;
1628 			}
1629 #endif /* CONFIG_INTERWORKING */
1630 #ifdef CONFIG_WPS
1631 			if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1632 				wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1633 				timeout_sec = 0;
1634 				timeout_usec = 500000;
1635 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1636 							    timeout_usec);
1637 				return 0;
1638 			}
1639 #endif /* CONFIG_WPS */
1640 			if (wpa_supplicant_req_sched_scan(wpa_s))
1641 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1642 							    timeout_usec);
1643 
1644 			wpa_msg_ctrl(wpa_s, MSG_INFO,
1645 				     WPA_EVENT_NETWORK_NOT_FOUND);
1646 		}
1647 	}
1648 	return 0;
1649 }
1650 
1651 
1652 static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1653 					     union wpa_event_data *data)
1654 {
1655 	struct wpa_supplicant *ifs;
1656 	int res;
1657 
1658 	res = _wpa_supplicant_event_scan_results(wpa_s, data, 1);
1659 	if (res == 2) {
1660 		/*
1661 		 * Interface may have been removed, so must not dereference
1662 		 * wpa_s after this.
1663 		 */
1664 		return 1;
1665 	}
1666 	if (res != 0) {
1667 		/*
1668 		 * If no scan results could be fetched, then no need to
1669 		 * notify those interfaces that did not actually request
1670 		 * this scan. Similarly, if scan results started a new operation on this
1671 		 * interface, do not notify other interfaces to avoid concurrent
1672 		 * operations during a connection attempt.
1673 		 */
1674 		return 0;
1675 	}
1676 
1677 	/*
1678 	 * Check other interfaces to see if they share the same radio. If
1679 	 * so, they get updated with this same scan info.
1680 	 */
1681 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1682 			 radio_list) {
1683 		if (ifs != wpa_s) {
1684 			wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1685 				   "sibling", ifs->ifname);
1686 			_wpa_supplicant_event_scan_results(ifs, data, 0);
1687 		}
1688 	}
1689 
1690 	return 0;
1691 }
1692 
1693 #endif /* CONFIG_NO_SCAN_PROCESSING */
1694 
1695 
1696 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1697 {
1698 #ifdef CONFIG_NO_SCAN_PROCESSING
1699 	return -1;
1700 #else /* CONFIG_NO_SCAN_PROCESSING */
1701 	struct os_reltime now;
1702 
1703 	if (wpa_s->last_scan_res_used == 0)
1704 		return -1;
1705 
1706 	os_get_reltime(&now);
1707 	if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
1708 		wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1709 		return -1;
1710 	}
1711 
1712 	return wpas_select_network_from_last_scan(wpa_s, 0, 1);
1713 #endif /* CONFIG_NO_SCAN_PROCESSING */
1714 }
1715 
1716 #ifdef CONFIG_WNM
1717 
1718 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1719 {
1720 	struct wpa_supplicant *wpa_s = eloop_ctx;
1721 
1722 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
1723 		return;
1724 
1725 	if (!wpa_s->no_keep_alive) {
1726 		wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1727 			   MAC2STR(wpa_s->bssid));
1728 		/* TODO: could skip this if normal data traffic has been sent */
1729 		/* TODO: Consider using some more appropriate data frame for
1730 		 * this */
1731 		if (wpa_s->l2)
1732 			l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1733 				       (u8 *) "", 0);
1734 	}
1735 
1736 #ifdef CONFIG_SME
1737 	if (wpa_s->sme.bss_max_idle_period) {
1738 		unsigned int msec;
1739 		msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1740 		if (msec > 100)
1741 			msec -= 100;
1742 		eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1743 				       wnm_bss_keep_alive, wpa_s, NULL);
1744 	}
1745 #endif /* CONFIG_SME */
1746 }
1747 
1748 
1749 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1750 				   const u8 *ies, size_t ies_len)
1751 {
1752 	struct ieee802_11_elems elems;
1753 
1754 	if (ies == NULL)
1755 		return;
1756 
1757 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1758 		return;
1759 
1760 #ifdef CONFIG_SME
1761 	if (elems.bss_max_idle_period) {
1762 		unsigned int msec;
1763 		wpa_s->sme.bss_max_idle_period =
1764 			WPA_GET_LE16(elems.bss_max_idle_period);
1765 		wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1766 			   "TU)%s", wpa_s->sme.bss_max_idle_period,
1767 			   (elems.bss_max_idle_period[2] & 0x01) ?
1768 			   " (protected keep-live required)" : "");
1769 		if (wpa_s->sme.bss_max_idle_period == 0)
1770 			wpa_s->sme.bss_max_idle_period = 1;
1771 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1772 			eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1773 			 /* msec times 1000 */
1774 			msec = wpa_s->sme.bss_max_idle_period * 1024;
1775 			if (msec > 100)
1776 				msec -= 100;
1777 			eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1778 					       wnm_bss_keep_alive, wpa_s,
1779 					       NULL);
1780 		}
1781 	}
1782 #endif /* CONFIG_SME */
1783 }
1784 
1785 #endif /* CONFIG_WNM */
1786 
1787 
1788 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1789 {
1790 #ifdef CONFIG_WNM
1791 	eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1792 #endif /* CONFIG_WNM */
1793 }
1794 
1795 
1796 #ifdef CONFIG_INTERWORKING
1797 
1798 static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1799 			    size_t len)
1800 {
1801 	int res;
1802 
1803 	wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1804 	res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1805 	if (res) {
1806 		wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1807 	}
1808 
1809 	return res;
1810 }
1811 
1812 
1813 static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1814 					    const u8 *ies, size_t ies_len)
1815 {
1816 	struct ieee802_11_elems elems;
1817 
1818 	if (ies == NULL)
1819 		return;
1820 
1821 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1822 		return;
1823 
1824 	if (elems.qos_map_set) {
1825 		wpas_qos_map_set(wpa_s, elems.qos_map_set,
1826 				 elems.qos_map_set_len);
1827 	}
1828 }
1829 
1830 #endif /* CONFIG_INTERWORKING */
1831 
1832 
1833 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1834 					  union wpa_event_data *data)
1835 {
1836 	int l, len, found = 0, wpa_found, rsn_found;
1837 	const u8 *p;
1838 #ifdef CONFIG_IEEE80211R
1839 	u8 bssid[ETH_ALEN];
1840 #endif /* CONFIG_IEEE80211R */
1841 
1842 	wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1843 	if (data->assoc_info.req_ies)
1844 		wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1845 			    data->assoc_info.req_ies_len);
1846 	if (data->assoc_info.resp_ies) {
1847 		wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1848 			    data->assoc_info.resp_ies_len);
1849 #ifdef CONFIG_TDLS
1850 		wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1851 					data->assoc_info.resp_ies_len);
1852 #endif /* CONFIG_TDLS */
1853 #ifdef CONFIG_WNM
1854 		wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1855 				       data->assoc_info.resp_ies_len);
1856 #endif /* CONFIG_WNM */
1857 #ifdef CONFIG_INTERWORKING
1858 		interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1859 						data->assoc_info.resp_ies_len);
1860 #endif /* CONFIG_INTERWORKING */
1861 	}
1862 	if (data->assoc_info.beacon_ies)
1863 		wpa_hexdump(MSG_DEBUG, "beacon_ies",
1864 			    data->assoc_info.beacon_ies,
1865 			    data->assoc_info.beacon_ies_len);
1866 	if (data->assoc_info.freq)
1867 		wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1868 			data->assoc_info.freq);
1869 
1870 	p = data->assoc_info.req_ies;
1871 	l = data->assoc_info.req_ies_len;
1872 
1873 	/* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1874 	while (p && l >= 2) {
1875 		len = p[1] + 2;
1876 		if (len > l) {
1877 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1878 				    p, l);
1879 			break;
1880 		}
1881 		if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1882 		     (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1883 		    (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1884 			if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1885 				break;
1886 			found = 1;
1887 			wpa_find_assoc_pmkid(wpa_s);
1888 			break;
1889 		}
1890 		l -= len;
1891 		p += len;
1892 	}
1893 	if (!found && data->assoc_info.req_ies)
1894 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1895 
1896 #ifdef CONFIG_IEEE80211R
1897 #ifdef CONFIG_SME
1898 	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1899 		if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1900 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1901 						 data->assoc_info.resp_ies,
1902 						 data->assoc_info.resp_ies_len,
1903 						 bssid) < 0) {
1904 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1905 				"Reassociation Response failed");
1906 			wpa_supplicant_deauthenticate(
1907 				wpa_s, WLAN_REASON_INVALID_IE);
1908 			return -1;
1909 		}
1910 	}
1911 
1912 	p = data->assoc_info.resp_ies;
1913 	l = data->assoc_info.resp_ies_len;
1914 
1915 #ifdef CONFIG_WPS_STRICT
1916 	if (p && wpa_s->current_ssid &&
1917 	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1918 		struct wpabuf *wps;
1919 		wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1920 		if (wps == NULL) {
1921 			wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1922 				"include WPS IE in (Re)Association Response");
1923 			return -1;
1924 		}
1925 
1926 		if (wps_validate_assoc_resp(wps) < 0) {
1927 			wpabuf_free(wps);
1928 			wpa_supplicant_deauthenticate(
1929 				wpa_s, WLAN_REASON_INVALID_IE);
1930 			return -1;
1931 		}
1932 		wpabuf_free(wps);
1933 	}
1934 #endif /* CONFIG_WPS_STRICT */
1935 
1936 	/* Go through the IEs and make a copy of the MDIE, if present. */
1937 	while (p && l >= 2) {
1938 		len = p[1] + 2;
1939 		if (len > l) {
1940 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1941 				    p, l);
1942 			break;
1943 		}
1944 		if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1945 		    p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1946 			wpa_s->sme.ft_used = 1;
1947 			os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1948 				  MOBILITY_DOMAIN_ID_LEN);
1949 			break;
1950 		}
1951 		l -= len;
1952 		p += len;
1953 	}
1954 #endif /* CONFIG_SME */
1955 
1956 	/* Process FT when SME is in the driver */
1957 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1958 	    wpa_ft_is_completed(wpa_s->wpa)) {
1959 		if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1960 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1961 						 data->assoc_info.resp_ies,
1962 						 data->assoc_info.resp_ies_len,
1963 						 bssid) < 0) {
1964 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1965 				"Reassociation Response failed");
1966 			wpa_supplicant_deauthenticate(
1967 				wpa_s, WLAN_REASON_INVALID_IE);
1968 			return -1;
1969 		}
1970 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1971 	}
1972 
1973 	wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1974 			     data->assoc_info.resp_ies_len);
1975 #endif /* CONFIG_IEEE80211R */
1976 
1977 	/* WPA/RSN IE from Beacon/ProbeResp */
1978 	p = data->assoc_info.beacon_ies;
1979 	l = data->assoc_info.beacon_ies_len;
1980 
1981 	/* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1982 	 */
1983 	wpa_found = rsn_found = 0;
1984 	while (p && l >= 2) {
1985 		len = p[1] + 2;
1986 		if (len > l) {
1987 			wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1988 				    p, l);
1989 			break;
1990 		}
1991 		if (!wpa_found &&
1992 		    p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1993 		    os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1994 			wpa_found = 1;
1995 			wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1996 		}
1997 
1998 		if (!rsn_found &&
1999 		    p[0] == WLAN_EID_RSN && p[1] >= 2) {
2000 			rsn_found = 1;
2001 			wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
2002 		}
2003 
2004 		l -= len;
2005 		p += len;
2006 	}
2007 
2008 	if (!wpa_found && data->assoc_info.beacon_ies)
2009 		wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
2010 	if (!rsn_found && data->assoc_info.beacon_ies)
2011 		wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
2012 	if (wpa_found || rsn_found)
2013 		wpa_s->ap_ies_from_associnfo = 1;
2014 
2015 #ifdef CONFIG_FST
2016 	wpabuf_free(wpa_s->received_mb_ies);
2017 	wpa_s->received_mb_ies = NULL;
2018 	if (wpa_s->fst) {
2019 		struct mb_ies_info mb_ies;
2020 
2021 		wpa_printf(MSG_DEBUG, "Looking for MB IE");
2022 		if (!mb_ies_info_by_ies(&mb_ies, data->assoc_info.resp_ies,
2023 					data->assoc_info.resp_ies_len))
2024 			wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
2025 	}
2026 #endif /* CONFIG_FST */
2027 
2028 	if (wpa_s->assoc_freq && data->assoc_info.freq &&
2029 	    wpa_s->assoc_freq != data->assoc_info.freq) {
2030 		wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2031 			   "%u to %u MHz",
2032 			   wpa_s->assoc_freq, data->assoc_info.freq);
2033 		wpa_supplicant_update_scan_results(wpa_s);
2034 	}
2035 
2036 	wpa_s->assoc_freq = data->assoc_info.freq;
2037 
2038 	return 0;
2039 }
2040 
2041 
2042 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2043 {
2044 	const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2045 
2046 	if (!wpa_s->current_bss || !wpa_s->current_ssid)
2047 		return -1;
2048 
2049 	if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2050 		return 0;
2051 
2052 	bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2053 					WPA_IE_VENDOR_TYPE);
2054 	bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2055 
2056 	if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2057 				 bss_wpa ? 2 + bss_wpa[1] : 0) ||
2058 	    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2059 				 bss_rsn ? 2 + bss_rsn[1] : 0))
2060 		return -1;
2061 
2062 	return 0;
2063 }
2064 
2065 
2066 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2067 				       union wpa_event_data *data)
2068 {
2069 	u8 bssid[ETH_ALEN];
2070 	int ft_completed;
2071 	int new_bss = 0;
2072 
2073 #ifdef CONFIG_AP
2074 	if (wpa_s->ap_iface) {
2075 		if (!data)
2076 			return;
2077 		hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2078 				    data->assoc_info.addr,
2079 				    data->assoc_info.req_ies,
2080 				    data->assoc_info.req_ies_len,
2081 				    data->assoc_info.reassoc);
2082 		return;
2083 	}
2084 #endif /* CONFIG_AP */
2085 
2086 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2087 
2088 	ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2089 	if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2090 		return;
2091 
2092 	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2093 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
2094 		wpa_supplicant_deauthenticate(
2095 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2096 		return;
2097 	}
2098 
2099 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
2100 	if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
2101 		wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2102 			MACSTR, MAC2STR(bssid));
2103 		new_bss = 1;
2104 		random_add_randomness(bssid, ETH_ALEN);
2105 		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2106 		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
2107 		wpas_notify_bssid_changed(wpa_s);
2108 
2109 		if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2110 			wpa_clear_keys(wpa_s, bssid);
2111 		}
2112 		if (wpa_supplicant_select_config(wpa_s) < 0) {
2113 			wpa_supplicant_deauthenticate(
2114 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2115 			return;
2116 		}
2117 	}
2118 
2119 	if (wpa_s->conf->ap_scan == 1 &&
2120 	    wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
2121 		if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
2122 			wpa_msg(wpa_s, MSG_WARNING,
2123 				"WPA/RSN IEs not updated");
2124 	}
2125 
2126 #ifdef CONFIG_SME
2127 	os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2128 	wpa_s->sme.prev_bssid_set = 1;
2129 	wpa_s->sme.last_unprot_disconnect.sec = 0;
2130 #endif /* CONFIG_SME */
2131 
2132 	wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2133 	if (wpa_s->current_ssid) {
2134 		/* When using scanning (ap_scan=1), SIM PC/SC interface can be
2135 		 * initialized before association, but for other modes,
2136 		 * initialize PC/SC here, if the current configuration needs
2137 		 * smartcard or SIM/USIM. */
2138 		wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2139 	}
2140 	wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2141 	if (wpa_s->l2)
2142 		l2_packet_notify_auth_start(wpa_s->l2);
2143 
2144 	/*
2145 	 * Set portEnabled first to FALSE in order to get EAP state machine out
2146 	 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2147 	 * state machine may transit to AUTHENTICATING state based on obsolete
2148 	 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2149 	 * AUTHENTICATED without ever giving chance to EAP state machine to
2150 	 * reset the state.
2151 	 */
2152 	if (!ft_completed) {
2153 		eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2154 		eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2155 	}
2156 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
2157 		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2158 	/* 802.1X::portControl = Auto */
2159 	eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2160 	wpa_s->eapol_received = 0;
2161 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2162 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2163 	    (wpa_s->current_ssid &&
2164 	     wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
2165 		if (wpa_s->current_ssid &&
2166 		    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
2167 		    (wpa_s->drv_flags &
2168 		     WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2169 			/*
2170 			 * Set the key after having received joined-IBSS event
2171 			 * from the driver.
2172 			 */
2173 			wpa_supplicant_set_wpa_none_key(wpa_s,
2174 							wpa_s->current_ssid);
2175 		}
2176 		wpa_supplicant_cancel_auth_timeout(wpa_s);
2177 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2178 	} else if (!ft_completed) {
2179 		/* Timeout for receiving the first EAPOL packet */
2180 		wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2181 	}
2182 	wpa_supplicant_cancel_scan(wpa_s);
2183 
2184 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2185 	    wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2186 		/*
2187 		 * We are done; the driver will take care of RSN 4-way
2188 		 * handshake.
2189 		 */
2190 		wpa_supplicant_cancel_auth_timeout(wpa_s);
2191 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2192 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2193 		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2194 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2195 		   wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2196 		/*
2197 		 * The driver will take care of RSN 4-way handshake, so we need
2198 		 * to allow EAPOL supplicant to complete its work without
2199 		 * waiting for WPA supplicant.
2200 		 */
2201 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2202 	} else if (ft_completed) {
2203 		/*
2204 		 * FT protocol completed - make sure EAPOL state machine ends
2205 		 * up in authenticated.
2206 		 */
2207 		wpa_supplicant_cancel_auth_timeout(wpa_s);
2208 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2209 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2210 		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2211 	}
2212 
2213 	wpa_s->last_eapol_matches_bssid = 0;
2214 
2215 	if (wpa_s->pending_eapol_rx) {
2216 		struct os_reltime now, age;
2217 		os_get_reltime(&now);
2218 		os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
2219 		if (age.sec == 0 && age.usec < 100000 &&
2220 		    os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2221 		    0) {
2222 			wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2223 				"frame that was received just before "
2224 				"association notification");
2225 			wpa_supplicant_rx_eapol(
2226 				wpa_s, wpa_s->pending_eapol_rx_src,
2227 				wpabuf_head(wpa_s->pending_eapol_rx),
2228 				wpabuf_len(wpa_s->pending_eapol_rx));
2229 		}
2230 		wpabuf_free(wpa_s->pending_eapol_rx);
2231 		wpa_s->pending_eapol_rx = NULL;
2232 	}
2233 
2234 	if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2235 	     wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
2236 	    wpa_s->current_ssid &&
2237 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2238 		/* Set static WEP keys again */
2239 		wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2240 	}
2241 
2242 #ifdef CONFIG_IBSS_RSN
2243 	if (wpa_s->current_ssid &&
2244 	    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2245 	    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2246 	    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2247 	    wpa_s->ibss_rsn == NULL) {
2248 		wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2249 		if (!wpa_s->ibss_rsn) {
2250 			wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2251 			wpa_supplicant_deauthenticate(
2252 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2253 			return;
2254 		}
2255 
2256 		ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2257 	}
2258 #endif /* CONFIG_IBSS_RSN */
2259 
2260 	wpas_wps_notify_assoc(wpa_s, bssid);
2261 
2262 	if (data) {
2263 		wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2264 				    data->assoc_info.resp_ies_len,
2265 				    &data->assoc_info.wmm_params);
2266 
2267 		if (wpa_s->reassoc_same_bss)
2268 			wmm_ac_restore_tspecs(wpa_s);
2269 	}
2270 }
2271 
2272 
2273 static int disconnect_reason_recoverable(u16 reason_code)
2274 {
2275 	return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2276 		reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2277 		reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2278 }
2279 
2280 
2281 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
2282 					  u16 reason_code,
2283 					  int locally_generated)
2284 {
2285 	const u8 *bssid;
2286 
2287 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2288 		/*
2289 		 * At least Host AP driver and a Prism3 card seemed to be
2290 		 * generating streams of disconnected events when configuring
2291 		 * IBSS for WPA-None. Ignore them for now.
2292 		 */
2293 		return;
2294 	}
2295 
2296 	bssid = wpa_s->bssid;
2297 	if (is_zero_ether_addr(bssid))
2298 		bssid = wpa_s->pending_bssid;
2299 
2300 	if (!is_zero_ether_addr(bssid) ||
2301 	    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2302 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2303 			" reason=%d%s",
2304 			MAC2STR(bssid), reason_code,
2305 			locally_generated ? " locally_generated=1" : "");
2306 	}
2307 }
2308 
2309 
2310 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2311 				 int locally_generated)
2312 {
2313 	if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2314 	    !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2315 		return 0; /* Not in 4-way handshake with PSK */
2316 
2317 	/*
2318 	 * It looks like connection was lost while trying to go through PSK
2319 	 * 4-way handshake. Filter out known disconnection cases that are caused
2320 	 * by something else than PSK mismatch to avoid confusing reports.
2321 	 */
2322 
2323 	if (locally_generated) {
2324 		if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2325 			return 0;
2326 	}
2327 
2328 	return 1;
2329 }
2330 
2331 
2332 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2333 						 u16 reason_code,
2334 						 int locally_generated)
2335 {
2336 	const u8 *bssid;
2337 	int authenticating;
2338 	u8 prev_pending_bssid[ETH_ALEN];
2339 	struct wpa_bss *fast_reconnect = NULL;
2340 	struct wpa_ssid *fast_reconnect_ssid = NULL;
2341 	struct wpa_ssid *last_ssid;
2342 
2343 	authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2344 	os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2345 
2346 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2347 		/*
2348 		 * At least Host AP driver and a Prism3 card seemed to be
2349 		 * generating streams of disconnected events when configuring
2350 		 * IBSS for WPA-None. Ignore them for now.
2351 		 */
2352 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2353 			"IBSS/WPA-None mode");
2354 		return;
2355 	}
2356 
2357 	if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
2358 		wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2359 			"pre-shared key may be incorrect");
2360 		if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2361 			return; /* P2P group removed */
2362 		wpas_auth_failed(wpa_s, "WRONG_KEY");
2363 	}
2364 	if (!wpa_s->disconnected &&
2365 	    (!wpa_s->auto_reconnect_disabled ||
2366 	     wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2367 	     wpas_wps_searching(wpa_s))) {
2368 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2369 			"reconnect (wps=%d/%d wpa_state=%d)",
2370 			wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2371 			wpas_wps_searching(wpa_s),
2372 			wpa_s->wpa_state);
2373 		if (wpa_s->wpa_state == WPA_COMPLETED &&
2374 		    wpa_s->current_ssid &&
2375 		    wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
2376 		    !locally_generated &&
2377 		    disconnect_reason_recoverable(reason_code)) {
2378 			/*
2379 			 * It looks like the AP has dropped association with
2380 			 * us, but could allow us to get back in. Try to
2381 			 * reconnect to the same BSS without full scan to save
2382 			 * time for some common cases.
2383 			 */
2384 			fast_reconnect = wpa_s->current_bss;
2385 			fast_reconnect_ssid = wpa_s->current_ssid;
2386 		} else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
2387 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
2388 		else
2389 			wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2390 				"immediate scan");
2391 	} else {
2392 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
2393 			"try to re-connect");
2394 		wpa_s->reassociate = 0;
2395 		wpa_s->disconnected = 1;
2396 		if (!wpa_s->pno)
2397 			wpa_supplicant_cancel_sched_scan(wpa_s);
2398 	}
2399 	bssid = wpa_s->bssid;
2400 	if (is_zero_ether_addr(bssid))
2401 		bssid = wpa_s->pending_bssid;
2402 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2403 		wpas_connection_failed(wpa_s, bssid);
2404 	wpa_sm_notify_disassoc(wpa_s->wpa);
2405 	if (locally_generated)
2406 		wpa_s->disconnect_reason = -reason_code;
2407 	else
2408 		wpa_s->disconnect_reason = reason_code;
2409 	wpas_notify_disconnect_reason(wpa_s);
2410 	if (wpa_supplicant_dynamic_keys(wpa_s)) {
2411 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2412 		wpa_clear_keys(wpa_s, wpa_s->bssid);
2413 	}
2414 	last_ssid = wpa_s->current_ssid;
2415 	wpa_supplicant_mark_disassoc(wpa_s);
2416 
2417 	if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
2418 		sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
2419 		wpa_s->current_ssid = last_ssid;
2420 	}
2421 
2422 	if (fast_reconnect &&
2423 	    !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2424 	    !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2425 	    !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2426 			     fast_reconnect->ssid_len) &&
2427 	    !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
2428 #ifndef CONFIG_NO_SCAN_PROCESSING
2429 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2430 		if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2431 					   fast_reconnect_ssid) < 0) {
2432 			/* Recover through full scan */
2433 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
2434 		}
2435 #endif /* CONFIG_NO_SCAN_PROCESSING */
2436 	} else if (fast_reconnect) {
2437 		/*
2438 		 * Could not reconnect to the same BSS due to network being
2439 		 * disabled. Use a new scan to match the alternative behavior
2440 		 * above, i.e., to continue automatic reconnection attempt in a
2441 		 * way that enforces disabled network rules.
2442 		 */
2443 		wpa_supplicant_req_scan(wpa_s, 0, 100000);
2444 	}
2445 }
2446 
2447 
2448 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2449 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
2450 {
2451 	struct wpa_supplicant *wpa_s = eloop_ctx;
2452 
2453 	if (!wpa_s->pending_mic_error_report)
2454 		return;
2455 
2456 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2457 	wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2458 	wpa_s->pending_mic_error_report = 0;
2459 }
2460 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2461 
2462 
2463 static void
2464 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2465 					 union wpa_event_data *data)
2466 {
2467 	int pairwise;
2468 	struct os_reltime t;
2469 
2470 	wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2471 	pairwise = (data && data->michael_mic_failure.unicast);
2472 	os_get_reltime(&t);
2473 	if ((wpa_s->last_michael_mic_error.sec &&
2474 	     !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
2475 	    wpa_s->pending_mic_error_report) {
2476 		if (wpa_s->pending_mic_error_report) {
2477 			/*
2478 			 * Send the pending MIC error report immediately since
2479 			 * we are going to start countermeasures and AP better
2480 			 * do the same.
2481 			 */
2482 			wpa_sm_key_request(wpa_s->wpa, 1,
2483 					   wpa_s->pending_mic_error_pairwise);
2484 		}
2485 
2486 		/* Send the new MIC error report immediately since we are going
2487 		 * to start countermeasures and AP better do the same.
2488 		 */
2489 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2490 
2491 		/* initialize countermeasures */
2492 		wpa_s->countermeasures = 1;
2493 
2494 		wpa_blacklist_add(wpa_s, wpa_s->bssid);
2495 
2496 		wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2497 
2498 		/*
2499 		 * Need to wait for completion of request frame. We do not get
2500 		 * any callback for the message completion, so just wait a
2501 		 * short while and hope for the best. */
2502 		os_sleep(0, 10000);
2503 
2504 		wpa_drv_set_countermeasures(wpa_s, 1);
2505 		wpa_supplicant_deauthenticate(wpa_s,
2506 					      WLAN_REASON_MICHAEL_MIC_FAILURE);
2507 		eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2508 				     wpa_s, NULL);
2509 		eloop_register_timeout(60, 0,
2510 				       wpa_supplicant_stop_countermeasures,
2511 				       wpa_s, NULL);
2512 		/* TODO: mark the AP rejected for 60 second. STA is
2513 		 * allowed to associate with another AP.. */
2514 	} else {
2515 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2516 		if (wpa_s->mic_errors_seen) {
2517 			/*
2518 			 * Reduce the effectiveness of Michael MIC error
2519 			 * reports as a means for attacking against TKIP if
2520 			 * more than one MIC failure is noticed with the same
2521 			 * PTK. We delay the transmission of the reports by a
2522 			 * random time between 0 and 60 seconds in order to
2523 			 * force the attacker wait 60 seconds before getting
2524 			 * the information on whether a frame resulted in a MIC
2525 			 * failure.
2526 			 */
2527 			u8 rval[4];
2528 			int sec;
2529 
2530 			if (os_get_random(rval, sizeof(rval)) < 0)
2531 				sec = os_random() % 60;
2532 			else
2533 				sec = WPA_GET_BE32(rval) % 60;
2534 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2535 				"report %d seconds", sec);
2536 			wpa_s->pending_mic_error_report = 1;
2537 			wpa_s->pending_mic_error_pairwise = pairwise;
2538 			eloop_cancel_timeout(
2539 				wpa_supplicant_delayed_mic_error_report,
2540 				wpa_s, NULL);
2541 			eloop_register_timeout(
2542 				sec, os_random() % 1000000,
2543 				wpa_supplicant_delayed_mic_error_report,
2544 				wpa_s, NULL);
2545 		} else {
2546 			wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2547 		}
2548 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2549 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2550 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2551 	}
2552 	wpa_s->last_michael_mic_error = t;
2553 	wpa_s->mic_errors_seen++;
2554 }
2555 
2556 
2557 #ifdef CONFIG_TERMINATE_ONLASTIF
2558 static int any_interfaces(struct wpa_supplicant *head)
2559 {
2560 	struct wpa_supplicant *wpa_s;
2561 
2562 	for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2563 		if (!wpa_s->interface_removed)
2564 			return 1;
2565 	return 0;
2566 }
2567 #endif /* CONFIG_TERMINATE_ONLASTIF */
2568 
2569 
2570 static void
2571 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2572 				      union wpa_event_data *data)
2573 {
2574 	if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2575 		return;
2576 
2577 	switch (data->interface_status.ievent) {
2578 	case EVENT_INTERFACE_ADDED:
2579 		if (!wpa_s->interface_removed)
2580 			break;
2581 		wpa_s->interface_removed = 0;
2582 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2583 		if (wpa_supplicant_driver_init(wpa_s) < 0) {
2584 			wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2585 				"driver after interface was added");
2586 		}
2587 
2588 #ifdef CONFIG_P2P
2589 		if (!wpa_s->global->p2p &&
2590 		    !wpa_s->global->p2p_disabled &&
2591 		    !wpa_s->conf->p2p_disabled &&
2592 		    (wpa_s->drv_flags &
2593 		     WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
2594 		    wpas_p2p_add_p2pdev_interface(
2595 			    wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
2596 			wpa_printf(MSG_INFO,
2597 				   "P2P: Failed to enable P2P Device interface");
2598 			/* Try to continue without. P2P will be disabled. */
2599 		}
2600 #endif /* CONFIG_P2P */
2601 
2602 		break;
2603 	case EVENT_INTERFACE_REMOVED:
2604 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2605 		wpa_s->interface_removed = 1;
2606 		wpa_supplicant_mark_disassoc(wpa_s);
2607 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2608 		l2_packet_deinit(wpa_s->l2);
2609 		wpa_s->l2 = NULL;
2610 
2611 #ifdef CONFIG_P2P
2612 		if (wpa_s->global->p2p &&
2613 		    wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
2614 		    (wpa_s->drv_flags &
2615 		     WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
2616 			wpa_dbg(wpa_s, MSG_DEBUG,
2617 				"Removing P2P Device interface");
2618 			wpa_supplicant_remove_iface(
2619 				wpa_s->global, wpa_s->global->p2p_init_wpa_s,
2620 				0);
2621 			wpa_s->global->p2p_init_wpa_s = NULL;
2622 		}
2623 #endif /* CONFIG_P2P */
2624 
2625 #ifdef CONFIG_TERMINATE_ONLASTIF
2626 		/* check if last interface */
2627 		if (!any_interfaces(wpa_s->global->ifaces))
2628 			eloop_terminate();
2629 #endif /* CONFIG_TERMINATE_ONLASTIF */
2630 		break;
2631 	}
2632 }
2633 
2634 
2635 #ifdef CONFIG_PEERKEY
2636 static void
2637 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2638 			      union wpa_event_data *data)
2639 {
2640 	if (data == NULL)
2641 		return;
2642 	wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2643 }
2644 #endif /* CONFIG_PEERKEY */
2645 
2646 
2647 #ifdef CONFIG_TDLS
2648 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2649 				      union wpa_event_data *data)
2650 {
2651 	if (data == NULL)
2652 		return;
2653 	switch (data->tdls.oper) {
2654 	case TDLS_REQUEST_SETUP:
2655 		wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2656 		if (wpa_tdls_is_external_setup(wpa_s->wpa))
2657 			wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2658 		else
2659 			wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
2660 		break;
2661 	case TDLS_REQUEST_TEARDOWN:
2662 		if (wpa_tdls_is_external_setup(wpa_s->wpa))
2663 			wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2664 					       data->tdls.reason_code);
2665 		else
2666 			wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2667 					  data->tdls.peer);
2668 		break;
2669 	case TDLS_REQUEST_DISCOVER:
2670 			wpa_tdls_send_discovery_request(wpa_s->wpa,
2671 							data->tdls.peer);
2672 		break;
2673 	}
2674 }
2675 #endif /* CONFIG_TDLS */
2676 
2677 
2678 #ifdef CONFIG_WNM
2679 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2680 				     union wpa_event_data *data)
2681 {
2682 	if (data == NULL)
2683 		return;
2684 	switch (data->wnm.oper) {
2685 	case WNM_OPER_SLEEP:
2686 		wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2687 			   "(action=%d, intval=%d)",
2688 			   data->wnm.sleep_action, data->wnm.sleep_intval);
2689 		ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2690 					     data->wnm.sleep_intval, NULL);
2691 		break;
2692 	}
2693 }
2694 #endif /* CONFIG_WNM */
2695 
2696 
2697 #ifdef CONFIG_IEEE80211R
2698 static void
2699 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2700 				 union wpa_event_data *data)
2701 {
2702 	if (data == NULL)
2703 		return;
2704 
2705 	if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2706 				    data->ft_ies.ies_len,
2707 				    data->ft_ies.ft_action,
2708 				    data->ft_ies.target_ap,
2709 				    data->ft_ies.ric_ies,
2710 				    data->ft_ies.ric_ies_len) < 0) {
2711 		/* TODO: prevent MLME/driver from trying to associate? */
2712 	}
2713 }
2714 #endif /* CONFIG_IEEE80211R */
2715 
2716 
2717 #ifdef CONFIG_IBSS_RSN
2718 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2719 						union wpa_event_data *data)
2720 {
2721 	struct wpa_ssid *ssid;
2722 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
2723 		return;
2724 	if (data == NULL)
2725 		return;
2726 	ssid = wpa_s->current_ssid;
2727 	if (ssid == NULL)
2728 		return;
2729 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2730 		return;
2731 
2732 	ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2733 }
2734 
2735 
2736 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2737 					   union wpa_event_data *data)
2738 {
2739 	struct wpa_ssid *ssid = wpa_s->current_ssid;
2740 
2741 	if (ssid == NULL)
2742 		return;
2743 
2744 	/* check if the ssid is correctly configured as IBSS/RSN */
2745 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2746 		return;
2747 
2748 	ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2749 			     data->rx_mgmt.frame_len);
2750 }
2751 #endif /* CONFIG_IBSS_RSN */
2752 
2753 
2754 #ifdef CONFIG_IEEE80211R
2755 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2756 			 size_t len)
2757 {
2758 	const u8 *sta_addr, *target_ap_addr;
2759 	u16 status;
2760 
2761 	wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2762 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2763 		return; /* only SME case supported for now */
2764 	if (len < 1 + 2 * ETH_ALEN + 2)
2765 		return;
2766 	if (data[0] != 2)
2767 		return; /* Only FT Action Response is supported for now */
2768 	sta_addr = data + 1;
2769 	target_ap_addr = data + 1 + ETH_ALEN;
2770 	status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2771 	wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2772 		MACSTR " TargetAP " MACSTR " status %u",
2773 		MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2774 
2775 	if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2776 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2777 			" in FT Action Response", MAC2STR(sta_addr));
2778 		return;
2779 	}
2780 
2781 	if (status) {
2782 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2783 			"failure (status code %d)", status);
2784 		/* TODO: report error to FT code(?) */
2785 		return;
2786 	}
2787 
2788 	if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2789 				    len - (1 + 2 * ETH_ALEN + 2), 1,
2790 				    target_ap_addr, NULL, 0) < 0)
2791 		return;
2792 
2793 #ifdef CONFIG_SME
2794 	{
2795 		struct wpa_bss *bss;
2796 		bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2797 		if (bss)
2798 			wpa_s->sme.freq = bss->freq;
2799 		wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2800 		sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2801 			      WLAN_AUTH_FT);
2802 	}
2803 #endif /* CONFIG_SME */
2804 }
2805 #endif /* CONFIG_IEEE80211R */
2806 
2807 
2808 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2809 					       struct unprot_deauth *e)
2810 {
2811 #ifdef CONFIG_IEEE80211W
2812 	wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2813 		   "dropped: " MACSTR " -> " MACSTR
2814 		   " (reason code %u)",
2815 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2816 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2817 #endif /* CONFIG_IEEE80211W */
2818 }
2819 
2820 
2821 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2822 						 struct unprot_disassoc *e)
2823 {
2824 #ifdef CONFIG_IEEE80211W
2825 	wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2826 		   "dropped: " MACSTR " -> " MACSTR
2827 		   " (reason code %u)",
2828 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2829 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2830 #endif /* CONFIG_IEEE80211W */
2831 }
2832 
2833 
2834 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2835 				  u16 reason_code, int locally_generated,
2836 				  const u8 *ie, size_t ie_len, int deauth)
2837 {
2838 #ifdef CONFIG_AP
2839 	if (wpa_s->ap_iface && addr) {
2840 		hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2841 		return;
2842 	}
2843 
2844 	if (wpa_s->ap_iface) {
2845 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2846 		return;
2847 	}
2848 #endif /* CONFIG_AP */
2849 
2850 	if (!locally_generated)
2851 		wpa_s->own_disconnect_req = 0;
2852 
2853 	wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2854 
2855 	if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2856 	      ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2857 		(wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2858 	       eapol_sm_failed(wpa_s->eapol))) &&
2859 	     !wpa_s->eap_expected_failure))
2860 		wpas_auth_failed(wpa_s, "AUTH_FAILED");
2861 
2862 #ifdef CONFIG_P2P
2863 	if (deauth && reason_code > 0) {
2864 		if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2865 					  locally_generated) > 0) {
2866 			/*
2867 			 * The interface was removed, so cannot continue
2868 			 * processing any additional operations after this.
2869 			 */
2870 			return;
2871 		}
2872 	}
2873 #endif /* CONFIG_P2P */
2874 
2875 	wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2876 					     locally_generated);
2877 }
2878 
2879 
2880 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2881 				struct disassoc_info *info)
2882 {
2883 	u16 reason_code = 0;
2884 	int locally_generated = 0;
2885 	const u8 *addr = NULL;
2886 	const u8 *ie = NULL;
2887 	size_t ie_len = 0;
2888 
2889 	wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2890 
2891 	if (info) {
2892 		addr = info->addr;
2893 		ie = info->ie;
2894 		ie_len = info->ie_len;
2895 		reason_code = info->reason_code;
2896 		locally_generated = info->locally_generated;
2897 		wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2898 			locally_generated ? " (locally generated)" : "");
2899 		if (addr)
2900 			wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2901 				MAC2STR(addr));
2902 		wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2903 			    ie, ie_len);
2904 	}
2905 
2906 #ifdef CONFIG_AP
2907 	if (wpa_s->ap_iface && info && info->addr) {
2908 		hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2909 		return;
2910 	}
2911 
2912 	if (wpa_s->ap_iface) {
2913 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2914 		return;
2915 	}
2916 #endif /* CONFIG_AP */
2917 
2918 #ifdef CONFIG_P2P
2919 	if (info) {
2920 		wpas_p2p_disassoc_notif(
2921 			wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2922 			locally_generated);
2923 	}
2924 #endif /* CONFIG_P2P */
2925 
2926 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2927 		sme_event_disassoc(wpa_s, info);
2928 
2929 	wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2930 			      ie, ie_len, 0);
2931 }
2932 
2933 
2934 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2935 			      struct deauth_info *info)
2936 {
2937 	u16 reason_code = 0;
2938 	int locally_generated = 0;
2939 	const u8 *addr = NULL;
2940 	const u8 *ie = NULL;
2941 	size_t ie_len = 0;
2942 
2943 	wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2944 
2945 	if (info) {
2946 		addr = info->addr;
2947 		ie = info->ie;
2948 		ie_len = info->ie_len;
2949 		reason_code = info->reason_code;
2950 		locally_generated = info->locally_generated;
2951 		wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2952 			reason_code,
2953 			locally_generated ? " (locally generated)" : "");
2954 		if (addr) {
2955 			wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2956 				MAC2STR(addr));
2957 		}
2958 		wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2959 			    ie, ie_len);
2960 	}
2961 
2962 	wpa_reset_ft_completed(wpa_s->wpa);
2963 
2964 	wpas_event_disconnect(wpa_s, addr, reason_code,
2965 			      locally_generated, ie, ie_len, 1);
2966 }
2967 
2968 
2969 static const char * reg_init_str(enum reg_change_initiator init)
2970 {
2971 	switch (init) {
2972 	case REGDOM_SET_BY_CORE:
2973 		return "CORE";
2974 	case REGDOM_SET_BY_USER:
2975 		return "USER";
2976 	case REGDOM_SET_BY_DRIVER:
2977 		return "DRIVER";
2978 	case REGDOM_SET_BY_COUNTRY_IE:
2979 		return "COUNTRY_IE";
2980 	case REGDOM_BEACON_HINT:
2981 		return "BEACON_HINT";
2982 	}
2983 	return "?";
2984 }
2985 
2986 
2987 static const char * reg_type_str(enum reg_type type)
2988 {
2989 	switch (type) {
2990 	case REGDOM_TYPE_UNKNOWN:
2991 		return "UNKNOWN";
2992 	case REGDOM_TYPE_COUNTRY:
2993 		return "COUNTRY";
2994 	case REGDOM_TYPE_WORLD:
2995 		return "WORLD";
2996 	case REGDOM_TYPE_CUSTOM_WORLD:
2997 		return "CUSTOM_WORLD";
2998 	case REGDOM_TYPE_INTERSECTION:
2999 		return "INTERSECTION";
3000 	}
3001 	return "?";
3002 }
3003 
3004 
3005 static void wpa_supplicant_update_channel_list(
3006 	struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
3007 {
3008 	struct wpa_supplicant *ifs;
3009 
3010 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
3011 		reg_init_str(info->initiator), reg_type_str(info->type),
3012 		info->alpha2[0] ? " alpha2=" : "",
3013 		info->alpha2[0] ? info->alpha2 : "");
3014 
3015 	if (wpa_s->drv_priv == NULL)
3016 		return; /* Ignore event during drv initialization */
3017 
3018 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3019 			 radio_list) {
3020 		wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3021 			   ifs->ifname);
3022 		free_hw_features(ifs);
3023 		ifs->hw.modes = wpa_drv_get_hw_feature_data(
3024 			ifs, &ifs->hw.num_modes, &ifs->hw.flags);
3025 	}
3026 
3027 	/* Restart sched_scan with updated channel list */
3028 	if (wpa_s->sched_scanning) {
3029 		wpa_dbg(wpa_s, MSG_DEBUG,
3030 			"Channel list changed restart sched scan.");
3031 		wpa_supplicant_cancel_sched_scan(wpa_s);
3032 		wpa_supplicant_req_scan(wpa_s, 0, 0);
3033 	}
3034 
3035 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
3036 }
3037 
3038 
3039 static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
3040 				      const u8 *frame, size_t len, int freq,
3041 				      int rssi)
3042 {
3043 	const struct ieee80211_mgmt *mgmt;
3044 	const u8 *payload;
3045 	size_t plen;
3046 	u8 category;
3047 
3048 	if (len < IEEE80211_HDRLEN + 2)
3049 		return;
3050 
3051 	mgmt = (const struct ieee80211_mgmt *) frame;
3052 	payload = frame + IEEE80211_HDRLEN;
3053 	category = *payload++;
3054 	plen = len - IEEE80211_HDRLEN - 1;
3055 
3056 	wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3057 		" Category=%u DataLen=%d freq=%d MHz",
3058 		MAC2STR(mgmt->sa), category, (int) plen, freq);
3059 
3060 	if (category == WLAN_ACTION_WMM) {
3061 		wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3062 		return;
3063 	}
3064 
3065 #ifdef CONFIG_IEEE80211R
3066 	if (category == WLAN_ACTION_FT) {
3067 		ft_rx_action(wpa_s, payload, plen);
3068 		return;
3069 	}
3070 #endif /* CONFIG_IEEE80211R */
3071 
3072 #ifdef CONFIG_IEEE80211W
3073 #ifdef CONFIG_SME
3074 	if (category == WLAN_ACTION_SA_QUERY) {
3075 		sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3076 		return;
3077 	}
3078 #endif /* CONFIG_SME */
3079 #endif /* CONFIG_IEEE80211W */
3080 
3081 #ifdef CONFIG_WNM
3082 	if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3083 		ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3084 		return;
3085 	}
3086 #endif /* CONFIG_WNM */
3087 
3088 #ifdef CONFIG_GAS
3089 	if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3090 	     mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
3091 	    gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
3092 			 mgmt->u.action.category,
3093 			 payload, plen, freq) == 0)
3094 		return;
3095 #endif /* CONFIG_GAS */
3096 
3097 #ifdef CONFIG_TDLS
3098 	if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3099 	    payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3100 		wpa_dbg(wpa_s, MSG_DEBUG,
3101 			"TDLS: Received Discovery Response from " MACSTR,
3102 			MAC2STR(mgmt->sa));
3103 		return;
3104 	}
3105 #endif /* CONFIG_TDLS */
3106 
3107 #ifdef CONFIG_INTERWORKING
3108 	if (category == WLAN_ACTION_QOS && plen >= 1 &&
3109 	    payload[0] == QOS_QOS_MAP_CONFIG) {
3110 		const u8 *pos = payload + 1;
3111 		size_t qlen = plen - 1;
3112 		wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3113 			MACSTR, MAC2STR(mgmt->sa));
3114 		if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3115 		    qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3116 		    pos[1] <= qlen - 2 && pos[1] >= 16)
3117 			wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3118 		return;
3119 	}
3120 #endif /* CONFIG_INTERWORKING */
3121 
3122 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3123 	    payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3124 		wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3125 		return;
3126 	}
3127 
3128 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3129 	    payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3130 		wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3131 							 payload + 1, plen - 1,
3132 							 rssi);
3133 		return;
3134 	}
3135 
3136 #ifdef CONFIG_FST
3137 	if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
3138 		fst_rx_action(wpa_s->fst, mgmt, len);
3139 		return;
3140 	}
3141 #endif /* CONFIG_FST */
3142 
3143 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3144 			   category, payload, plen, freq);
3145 	if (wpa_s->ifmsh)
3146 		mesh_mpm_action_rx(wpa_s, mgmt, len);
3147 }
3148 
3149 
3150 static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3151 					     union wpa_event_data *event)
3152 {
3153 	struct wpa_freq_range_list *list;
3154 	char *str = NULL;
3155 
3156 	list = &event->freq_range;
3157 
3158 	if (list->num)
3159 		str = freq_range_list_str(list);
3160 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3161 		str ? str : "");
3162 
3163 #ifdef CONFIG_P2P
3164 	if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3165 		wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3166 			__func__);
3167 	} else {
3168 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
3169 
3170 		/*
3171 		 * The update channel flow will also take care of moving a GO
3172 		 * from the unsafe frequency if needed.
3173 		 */
3174 		wpas_p2p_update_channel_list(wpa_s,
3175 					     WPAS_P2P_CHANNEL_UPDATE_AVOID);
3176 	}
3177 #endif /* CONFIG_P2P */
3178 
3179 	os_free(str);
3180 }
3181 
3182 
3183 static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3184 					    union wpa_event_data *data)
3185 {
3186 	wpa_dbg(wpa_s, MSG_DEBUG,
3187 		"Connection authorized by device, previous state %d",
3188 		wpa_s->wpa_state);
3189 	if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3190 		wpa_supplicant_cancel_auth_timeout(wpa_s);
3191 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3192 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3193 		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3194 	}
3195 	wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3196 	wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
3197 			       data->assoc_info.ptk_kck_len,
3198 			       data->assoc_info.ptk_kek,
3199 			       data->assoc_info.ptk_kek_len);
3200 }
3201 
3202 
3203 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3204 			  union wpa_event_data *data)
3205 {
3206 	struct wpa_supplicant *wpa_s = ctx;
3207 	int resched;
3208 
3209 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3210 	    event != EVENT_INTERFACE_ENABLED &&
3211 	    event != EVENT_INTERFACE_STATUS &&
3212 	    event != EVENT_SCHED_SCAN_STOPPED) {
3213 		wpa_dbg(wpa_s, MSG_DEBUG,
3214 			"Ignore event %s (%d) while interface is disabled",
3215 			event_to_string(event), event);
3216 		return;
3217 	}
3218 
3219 #ifndef CONFIG_NO_STDOUT_DEBUG
3220 {
3221 	int level = MSG_DEBUG;
3222 
3223 	if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
3224 		const struct ieee80211_hdr *hdr;
3225 		u16 fc;
3226 		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3227 		fc = le_to_host16(hdr->frame_control);
3228 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3229 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3230 			level = MSG_EXCESSIVE;
3231 	}
3232 
3233 	wpa_dbg(wpa_s, level, "Event %s (%d) received",
3234 		event_to_string(event), event);
3235 }
3236 #endif /* CONFIG_NO_STDOUT_DEBUG */
3237 
3238 	switch (event) {
3239 	case EVENT_AUTH:
3240 		sme_event_auth(wpa_s, data);
3241 		break;
3242 	case EVENT_ASSOC:
3243 		wpa_supplicant_event_assoc(wpa_s, data);
3244 		if (data && data->assoc_info.authorized)
3245 			wpa_supplicant_event_assoc_auth(wpa_s, data);
3246 		break;
3247 	case EVENT_DISASSOC:
3248 		wpas_event_disassoc(wpa_s,
3249 				    data ? &data->disassoc_info : NULL);
3250 		break;
3251 	case EVENT_DEAUTH:
3252 		wpas_event_deauth(wpa_s,
3253 				  data ? &data->deauth_info : NULL);
3254 		break;
3255 	case EVENT_MICHAEL_MIC_FAILURE:
3256 		wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3257 		break;
3258 #ifndef CONFIG_NO_SCAN_PROCESSING
3259 	case EVENT_SCAN_STARTED:
3260 		os_get_reltime(&wpa_s->scan_start_time);
3261 		if (wpa_s->own_scan_requested) {
3262 			struct os_reltime diff;
3263 
3264 			os_reltime_sub(&wpa_s->scan_start_time,
3265 				       &wpa_s->scan_trigger_time, &diff);
3266 			wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3267 				diff.sec, diff.usec);
3268 			wpa_s->own_scan_requested = 0;
3269 			wpa_s->own_scan_running = 1;
3270 			if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3271 			    wpa_s->manual_scan_use_id) {
3272 				wpa_msg_ctrl(wpa_s, MSG_INFO,
3273 					     WPA_EVENT_SCAN_STARTED "id=%u",
3274 					     wpa_s->manual_scan_id);
3275 			} else {
3276 				wpa_msg_ctrl(wpa_s, MSG_INFO,
3277 					     WPA_EVENT_SCAN_STARTED);
3278 			}
3279 		} else {
3280 			wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
3281 			wpa_s->radio->external_scan_running = 1;
3282 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
3283 		}
3284 		break;
3285 	case EVENT_SCAN_RESULTS:
3286 		if (os_reltime_initialized(&wpa_s->scan_start_time)) {
3287 			struct os_reltime now, diff;
3288 			os_get_reltime(&now);
3289 			os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3290 			wpa_s->scan_start_time.sec = 0;
3291 			wpa_s->scan_start_time.usec = 0;
3292 			wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3293 				diff.sec, diff.usec);
3294 		}
3295 		if (wpa_supplicant_event_scan_results(wpa_s, data))
3296 			break; /* interface may have been removed */
3297 		wpa_s->own_scan_running = 0;
3298 		wpa_s->radio->external_scan_running = 0;
3299 		radio_work_check_next(wpa_s);
3300 		break;
3301 #endif /* CONFIG_NO_SCAN_PROCESSING */
3302 	case EVENT_ASSOCINFO:
3303 		wpa_supplicant_event_associnfo(wpa_s, data);
3304 		break;
3305 	case EVENT_INTERFACE_STATUS:
3306 		wpa_supplicant_event_interface_status(wpa_s, data);
3307 		break;
3308 	case EVENT_PMKID_CANDIDATE:
3309 		wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3310 		break;
3311 #ifdef CONFIG_PEERKEY
3312 	case EVENT_STKSTART:
3313 		wpa_supplicant_event_stkstart(wpa_s, data);
3314 		break;
3315 #endif /* CONFIG_PEERKEY */
3316 #ifdef CONFIG_TDLS
3317 	case EVENT_TDLS:
3318 		wpa_supplicant_event_tdls(wpa_s, data);
3319 		break;
3320 #endif /* CONFIG_TDLS */
3321 #ifdef CONFIG_WNM
3322 	case EVENT_WNM:
3323 		wpa_supplicant_event_wnm(wpa_s, data);
3324 		break;
3325 #endif /* CONFIG_WNM */
3326 #ifdef CONFIG_IEEE80211R
3327 	case EVENT_FT_RESPONSE:
3328 		wpa_supplicant_event_ft_response(wpa_s, data);
3329 		break;
3330 #endif /* CONFIG_IEEE80211R */
3331 #ifdef CONFIG_IBSS_RSN
3332 	case EVENT_IBSS_RSN_START:
3333 		wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3334 		break;
3335 #endif /* CONFIG_IBSS_RSN */
3336 	case EVENT_ASSOC_REJECT:
3337 		if (data->assoc_reject.bssid)
3338 			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3339 				"bssid=" MACSTR	" status_code=%u",
3340 				MAC2STR(data->assoc_reject.bssid),
3341 				data->assoc_reject.status_code);
3342 		else
3343 			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3344 				"status_code=%u",
3345 				data->assoc_reject.status_code);
3346 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3347 			sme_event_assoc_reject(wpa_s, data);
3348 		else {
3349 			const u8 *bssid = data->assoc_reject.bssid;
3350 			if (bssid == NULL || is_zero_ether_addr(bssid))
3351 				bssid = wpa_s->pending_bssid;
3352 			wpas_connection_failed(wpa_s, bssid);
3353 			wpa_supplicant_mark_disassoc(wpa_s);
3354 		}
3355 		break;
3356 	case EVENT_AUTH_TIMED_OUT:
3357 		/* It is possible to get this event from earlier connection */
3358 		if (wpa_s->current_ssid &&
3359 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3360 			wpa_dbg(wpa_s, MSG_DEBUG,
3361 				"Ignore AUTH_TIMED_OUT in mesh configuration");
3362 			break;
3363 		}
3364 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3365 			sme_event_auth_timed_out(wpa_s, data);
3366 		break;
3367 	case EVENT_ASSOC_TIMED_OUT:
3368 		/* It is possible to get this event from earlier connection */
3369 		if (wpa_s->current_ssid &&
3370 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3371 			wpa_dbg(wpa_s, MSG_DEBUG,
3372 				"Ignore ASSOC_TIMED_OUT in mesh configuration");
3373 			break;
3374 		}
3375 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3376 			sme_event_assoc_timed_out(wpa_s, data);
3377 		break;
3378 	case EVENT_TX_STATUS:
3379 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3380 			" type=%d stype=%d",
3381 			MAC2STR(data->tx_status.dst),
3382 			data->tx_status.type, data->tx_status.stype);
3383 #ifdef CONFIG_AP
3384 		if (wpa_s->ap_iface == NULL) {
3385 #ifdef CONFIG_OFFCHANNEL
3386 			if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3387 			    data->tx_status.stype == WLAN_FC_STYPE_ACTION)
3388 				offchannel_send_action_tx_status(
3389 					wpa_s, data->tx_status.dst,
3390 					data->tx_status.data,
3391 					data->tx_status.data_len,
3392 					data->tx_status.ack ?
3393 					OFFCHANNEL_SEND_ACTION_SUCCESS :
3394 					OFFCHANNEL_SEND_ACTION_NO_ACK);
3395 #endif /* CONFIG_OFFCHANNEL */
3396 			break;
3397 		}
3398 #endif /* CONFIG_AP */
3399 #ifdef CONFIG_OFFCHANNEL
3400 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3401 			MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3402 		/*
3403 		 * Catch TX status events for Action frames we sent via group
3404 		 * interface in GO mode.
3405 		 */
3406 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3407 		    data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3408 		    os_memcmp(wpa_s->parent->pending_action_dst,
3409 			      data->tx_status.dst, ETH_ALEN) == 0) {
3410 			offchannel_send_action_tx_status(
3411 				wpa_s->parent, data->tx_status.dst,
3412 				data->tx_status.data,
3413 				data->tx_status.data_len,
3414 				data->tx_status.ack ?
3415 				OFFCHANNEL_SEND_ACTION_SUCCESS :
3416 				OFFCHANNEL_SEND_ACTION_NO_ACK);
3417 			break;
3418 		}
3419 #endif /* CONFIG_OFFCHANNEL */
3420 #ifdef CONFIG_AP
3421 		switch (data->tx_status.type) {
3422 		case WLAN_FC_TYPE_MGMT:
3423 			ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3424 				      data->tx_status.data_len,
3425 				      data->tx_status.stype,
3426 				      data->tx_status.ack);
3427 			break;
3428 		case WLAN_FC_TYPE_DATA:
3429 			ap_tx_status(wpa_s, data->tx_status.dst,
3430 				     data->tx_status.data,
3431 				     data->tx_status.data_len,
3432 				     data->tx_status.ack);
3433 			break;
3434 		}
3435 #endif /* CONFIG_AP */
3436 		break;
3437 #ifdef CONFIG_AP
3438 	case EVENT_EAPOL_TX_STATUS:
3439 		ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3440 				   data->eapol_tx_status.data,
3441 				   data->eapol_tx_status.data_len,
3442 				   data->eapol_tx_status.ack);
3443 		break;
3444 	case EVENT_DRIVER_CLIENT_POLL_OK:
3445 		ap_client_poll_ok(wpa_s, data->client_poll.addr);
3446 		break;
3447 	case EVENT_RX_FROM_UNKNOWN:
3448 		if (wpa_s->ap_iface == NULL)
3449 			break;
3450 		ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3451 				       data->rx_from_unknown.wds);
3452 		break;
3453 	case EVENT_CH_SWITCH:
3454 		if (!data)
3455 			break;
3456 		if (!wpa_s->ap_iface) {
3457 			wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3458 				"event in non-AP mode");
3459 			break;
3460 		}
3461 
3462 		wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3463 				  data->ch_switch.ht_enabled,
3464 				  data->ch_switch.ch_offset,
3465 				  data->ch_switch.ch_width,
3466 				  data->ch_switch.cf1,
3467 				  data->ch_switch.cf2);
3468 		break;
3469 #ifdef NEED_AP_MLME
3470 	case EVENT_DFS_RADAR_DETECTED:
3471 		if (data)
3472 			wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
3473 		break;
3474 	case EVENT_DFS_CAC_STARTED:
3475 		if (data)
3476 			wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
3477 		break;
3478 	case EVENT_DFS_CAC_FINISHED:
3479 		if (data)
3480 			wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
3481 		break;
3482 	case EVENT_DFS_CAC_ABORTED:
3483 		if (data)
3484 			wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
3485 		break;
3486 	case EVENT_DFS_NOP_FINISHED:
3487 		if (data)
3488 			wpas_event_dfs_cac_nop_finished(wpa_s,
3489 							&data->dfs_event);
3490 		break;
3491 #endif /* NEED_AP_MLME */
3492 #endif /* CONFIG_AP */
3493 	case EVENT_RX_MGMT: {
3494 		u16 fc, stype;
3495 		const struct ieee80211_mgmt *mgmt;
3496 
3497 #ifdef CONFIG_TESTING_OPTIONS
3498 		if (wpa_s->ext_mgmt_frame_handling) {
3499 			struct rx_mgmt *rx = &data->rx_mgmt;
3500 			size_t hex_len = 2 * rx->frame_len + 1;
3501 			char *hex = os_malloc(hex_len);
3502 			if (hex) {
3503 				wpa_snprintf_hex(hex, hex_len,
3504 						 rx->frame, rx->frame_len);
3505 				wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3506 					rx->freq, rx->datarate, rx->ssi_signal,
3507 					hex);
3508 				os_free(hex);
3509 			}
3510 			break;
3511 		}
3512 #endif /* CONFIG_TESTING_OPTIONS */
3513 
3514 		mgmt = (const struct ieee80211_mgmt *)
3515 			data->rx_mgmt.frame;
3516 		fc = le_to_host16(mgmt->frame_control);
3517 		stype = WLAN_FC_GET_STYPE(fc);
3518 
3519 #ifdef CONFIG_AP
3520 		if (wpa_s->ap_iface == NULL) {
3521 #endif /* CONFIG_AP */
3522 #ifdef CONFIG_P2P
3523 			if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3524 			    data->rx_mgmt.frame_len > 24) {
3525 				const u8 *src = mgmt->sa;
3526 				const u8 *ie = mgmt->u.probe_req.variable;
3527 				size_t ie_len = data->rx_mgmt.frame_len -
3528 					(mgmt->u.probe_req.variable -
3529 					 data->rx_mgmt.frame);
3530 				wpas_p2p_probe_req_rx(
3531 					wpa_s, src, mgmt->da,
3532 					mgmt->bssid, ie, ie_len,
3533 					data->rx_mgmt.freq,
3534 					data->rx_mgmt.ssi_signal);
3535 				break;
3536 			}
3537 #endif /* CONFIG_P2P */
3538 #ifdef CONFIG_IBSS_RSN
3539 			if (wpa_s->current_ssid &&
3540 			    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3541 			    stype == WLAN_FC_STYPE_AUTH &&
3542 			    data->rx_mgmt.frame_len >= 30) {
3543 				wpa_supplicant_event_ibss_auth(wpa_s, data);
3544 				break;
3545 			}
3546 #endif /* CONFIG_IBSS_RSN */
3547 
3548 			if (stype == WLAN_FC_STYPE_ACTION) {
3549 				wpas_event_rx_mgmt_action(
3550 					wpa_s, data->rx_mgmt.frame,
3551 					data->rx_mgmt.frame_len,
3552 					data->rx_mgmt.freq,
3553 					data->rx_mgmt.ssi_signal);
3554 				break;
3555 			}
3556 
3557 			if (wpa_s->ifmsh) {
3558 				mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
3559 				break;
3560 			}
3561 
3562 			wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3563 				"management frame in non-AP mode");
3564 			break;
3565 #ifdef CONFIG_AP
3566 		}
3567 
3568 		if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3569 		    data->rx_mgmt.frame_len > 24) {
3570 			const u8 *ie = mgmt->u.probe_req.variable;
3571 			size_t ie_len = data->rx_mgmt.frame_len -
3572 				(mgmt->u.probe_req.variable -
3573 				 data->rx_mgmt.frame);
3574 
3575 			wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3576 					 mgmt->bssid, ie, ie_len,
3577 					 data->rx_mgmt.ssi_signal);
3578 		}
3579 
3580 		ap_mgmt_rx(wpa_s, &data->rx_mgmt);
3581 #endif /* CONFIG_AP */
3582 		break;
3583 		}
3584 	case EVENT_RX_PROBE_REQ:
3585 		if (data->rx_probe_req.sa == NULL ||
3586 		    data->rx_probe_req.ie == NULL)
3587 			break;
3588 #ifdef CONFIG_AP
3589 		if (wpa_s->ap_iface) {
3590 			hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3591 					     data->rx_probe_req.sa,
3592 					     data->rx_probe_req.da,
3593 					     data->rx_probe_req.bssid,
3594 					     data->rx_probe_req.ie,
3595 					     data->rx_probe_req.ie_len,
3596 					     data->rx_probe_req.ssi_signal);
3597 			break;
3598 		}
3599 #endif /* CONFIG_AP */
3600 		wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
3601 				      data->rx_probe_req.da,
3602 				      data->rx_probe_req.bssid,
3603 				      data->rx_probe_req.ie,
3604 				      data->rx_probe_req.ie_len,
3605 				      0,
3606 				      data->rx_probe_req.ssi_signal);
3607 		break;
3608 	case EVENT_REMAIN_ON_CHANNEL:
3609 #ifdef CONFIG_OFFCHANNEL
3610 		offchannel_remain_on_channel_cb(
3611 			wpa_s, data->remain_on_channel.freq,
3612 			data->remain_on_channel.duration);
3613 #endif /* CONFIG_OFFCHANNEL */
3614 		wpas_p2p_remain_on_channel_cb(
3615 			wpa_s, data->remain_on_channel.freq,
3616 			data->remain_on_channel.duration);
3617 		break;
3618 	case EVENT_CANCEL_REMAIN_ON_CHANNEL:
3619 #ifdef CONFIG_OFFCHANNEL
3620 		offchannel_cancel_remain_on_channel_cb(
3621 			wpa_s, data->remain_on_channel.freq);
3622 #endif /* CONFIG_OFFCHANNEL */
3623 		wpas_p2p_cancel_remain_on_channel_cb(
3624 			wpa_s, data->remain_on_channel.freq);
3625 		break;
3626 	case EVENT_EAPOL_RX:
3627 		wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3628 					data->eapol_rx.data,
3629 					data->eapol_rx.data_len);
3630 		break;
3631 	case EVENT_SIGNAL_CHANGE:
3632 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3633 			"above=%d signal=%d noise=%d txrate=%d",
3634 			data->signal_change.above_threshold,
3635 			data->signal_change.current_signal,
3636 			data->signal_change.current_noise,
3637 			data->signal_change.current_txrate);
3638 		wpa_bss_update_level(wpa_s->current_bss,
3639 				     data->signal_change.current_signal);
3640 		bgscan_notify_signal_change(
3641 			wpa_s, data->signal_change.above_threshold,
3642 			data->signal_change.current_signal,
3643 			data->signal_change.current_noise,
3644 			data->signal_change.current_txrate);
3645 		break;
3646 	case EVENT_INTERFACE_ENABLED:
3647 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3648 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3649 			wpa_supplicant_update_mac_addr(wpa_s);
3650 			if (wpa_s->p2p_mgmt) {
3651 				wpa_supplicant_set_state(wpa_s,
3652 							 WPA_DISCONNECTED);
3653 				break;
3654 			}
3655 
3656 #ifdef CONFIG_AP
3657 			if (!wpa_s->ap_iface) {
3658 				wpa_supplicant_set_state(wpa_s,
3659 							 WPA_DISCONNECTED);
3660 				wpa_s->scan_req = NORMAL_SCAN_REQ;
3661 				wpa_supplicant_req_scan(wpa_s, 0, 0);
3662 			} else
3663 				wpa_supplicant_set_state(wpa_s,
3664 							 WPA_COMPLETED);
3665 #else /* CONFIG_AP */
3666 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3667 			wpa_supplicant_req_scan(wpa_s, 0, 0);
3668 #endif /* CONFIG_AP */
3669 		}
3670 		break;
3671 	case EVENT_INTERFACE_DISABLED:
3672 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
3673 #ifdef CONFIG_P2P
3674 		if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3675 		    (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3676 		     wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3677 			/*
3678 			 * Mark interface disabled if this happens to end up not
3679 			 * being removed as a separate P2P group interface.
3680 			 */
3681 			wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3682 			/*
3683 			 * The interface was externally disabled. Remove
3684 			 * it assuming an external entity will start a
3685 			 * new session if needed.
3686 			 */
3687 			if (wpa_s->current_ssid &&
3688 			    wpa_s->current_ssid->p2p_group)
3689 				wpas_p2p_interface_unavailable(wpa_s);
3690 			else
3691 				wpas_p2p_disconnect(wpa_s);
3692 			/*
3693 			 * wpa_s instance may have been freed, so must not use
3694 			 * it here anymore.
3695 			 */
3696 			break;
3697 		}
3698 		if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3699 		    p2p_in_progress(wpa_s->global->p2p) > 1) {
3700 			/* This radio work will be cancelled, so clear P2P
3701 			 * state as well.
3702 			 */
3703 			p2p_stop_find(wpa_s->global->p2p);
3704 		}
3705 #endif /* CONFIG_P2P */
3706 
3707 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3708 			/*
3709 			 * Indicate disconnection to keep ctrl_iface events
3710 			 * consistent.
3711 			 */
3712 			wpa_supplicant_event_disassoc(
3713 				wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3714 		}
3715 		wpa_supplicant_mark_disassoc(wpa_s);
3716 		radio_remove_works(wpa_s, NULL, 0);
3717 
3718 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3719 		break;
3720 	case EVENT_CHANNEL_LIST_CHANGED:
3721 		wpa_supplicant_update_channel_list(
3722 			wpa_s, &data->channel_list_changed);
3723 		break;
3724 	case EVENT_INTERFACE_UNAVAILABLE:
3725 		wpas_p2p_interface_unavailable(wpa_s);
3726 		break;
3727 	case EVENT_BEST_CHANNEL:
3728 		wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3729 			"(%d %d %d)",
3730 			data->best_chan.freq_24, data->best_chan.freq_5,
3731 			data->best_chan.freq_overall);
3732 		wpa_s->best_24_freq = data->best_chan.freq_24;
3733 		wpa_s->best_5_freq = data->best_chan.freq_5;
3734 		wpa_s->best_overall_freq = data->best_chan.freq_overall;
3735 		wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3736 					      data->best_chan.freq_5,
3737 					      data->best_chan.freq_overall);
3738 		break;
3739 	case EVENT_UNPROT_DEAUTH:
3740 		wpa_supplicant_event_unprot_deauth(wpa_s,
3741 						   &data->unprot_deauth);
3742 		break;
3743 	case EVENT_UNPROT_DISASSOC:
3744 		wpa_supplicant_event_unprot_disassoc(wpa_s,
3745 						     &data->unprot_disassoc);
3746 		break;
3747 	case EVENT_STATION_LOW_ACK:
3748 #ifdef CONFIG_AP
3749 		if (wpa_s->ap_iface && data)
3750 			hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3751 						  data->low_ack.addr);
3752 #endif /* CONFIG_AP */
3753 #ifdef CONFIG_TDLS
3754 		if (data)
3755 			wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3756 							  data->low_ack.addr);
3757 #endif /* CONFIG_TDLS */
3758 		break;
3759 	case EVENT_IBSS_PEER_LOST:
3760 #ifdef CONFIG_IBSS_RSN
3761 		ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3762 #endif /* CONFIG_IBSS_RSN */
3763 		break;
3764 	case EVENT_DRIVER_GTK_REKEY:
3765 		if (os_memcmp(data->driver_gtk_rekey.bssid,
3766 			      wpa_s->bssid, ETH_ALEN))
3767 			break;
3768 		if (!wpa_s->wpa)
3769 			break;
3770 		wpa_sm_update_replay_ctr(wpa_s->wpa,
3771 					 data->driver_gtk_rekey.replay_ctr);
3772 		break;
3773 	case EVENT_SCHED_SCAN_STOPPED:
3774 		wpa_s->pno = 0;
3775 		wpa_s->sched_scanning = 0;
3776 		resched = wpa_s->scanning;
3777 		wpa_supplicant_notify_scanning(wpa_s, 0);
3778 
3779 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3780 			break;
3781 
3782 		/*
3783 		 * Start a new sched scan to continue searching for more SSIDs
3784 		 * either if timed out or PNO schedule scan is pending.
3785 		 */
3786 		if (wpa_s->sched_scan_timed_out) {
3787 			wpa_supplicant_req_sched_scan(wpa_s);
3788 		} else if (wpa_s->pno_sched_pending) {
3789 			wpa_s->pno_sched_pending = 0;
3790 			wpas_start_pno(wpa_s);
3791 		} else if (resched) {
3792 			wpa_supplicant_req_scan(wpa_s, 0, 0);
3793 		}
3794 
3795 		break;
3796 	case EVENT_WPS_BUTTON_PUSHED:
3797 #ifdef CONFIG_WPS
3798 		wpas_wps_start_pbc(wpa_s, NULL, 0);
3799 #endif /* CONFIG_WPS */
3800 		break;
3801 	case EVENT_AVOID_FREQUENCIES:
3802 		wpa_supplicant_notify_avoid_freq(wpa_s, data);
3803 		break;
3804 	case EVENT_CONNECT_FAILED_REASON:
3805 #ifdef CONFIG_AP
3806 		if (!wpa_s->ap_iface || !data)
3807 			break;
3808 		hostapd_event_connect_failed_reason(
3809 			wpa_s->ap_iface->bss[0],
3810 			data->connect_failed_reason.addr,
3811 			data->connect_failed_reason.code);
3812 #endif /* CONFIG_AP */
3813 		break;
3814 	case EVENT_NEW_PEER_CANDIDATE:
3815 #ifdef CONFIG_MESH
3816 		if (!wpa_s->ifmsh || !data)
3817 			break;
3818 		wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
3819 				     data->mesh_peer.ies,
3820 				     data->mesh_peer.ie_len);
3821 #endif /* CONFIG_MESH */
3822 		break;
3823 	default:
3824 		wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3825 		break;
3826 	}
3827 }
3828