xref: /freebsd/contrib/wpa/wpa_supplicant/events.c (revision a98ff317388a00b992f1bf8404dee596f9383f5e)
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2012, 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 "wnm_sta.h"
27 #include "notify.h"
28 #include "common/ieee802_11_defs.h"
29 #include "common/ieee802_11_common.h"
30 #include "crypto/random.h"
31 #include "blacklist.h"
32 #include "wpas_glue.h"
33 #include "wps_supplicant.h"
34 #include "ibss_rsn.h"
35 #include "sme.h"
36 #include "gas_query.h"
37 #include "p2p_supplicant.h"
38 #include "bgscan.h"
39 #include "autoscan.h"
40 #include "ap.h"
41 #include "bss.h"
42 #include "scan.h"
43 #include "offchannel.h"
44 #include "interworking.h"
45 
46 
47 static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
48 			      struct wpa_ssid *ssid)
49 {
50 	struct os_time now;
51 
52 	if (ssid == NULL || ssid->disabled_until.sec == 0)
53 		return 0;
54 
55 	os_get_time(&now);
56 	if (ssid->disabled_until.sec > now.sec)
57 		return ssid->disabled_until.sec - now.sec;
58 
59 	wpas_clear_temp_disabled(wpa_s, ssid, 0);
60 
61 	return 0;
62 }
63 
64 
65 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
66 {
67 	struct wpa_ssid *ssid, *old_ssid;
68 	int res;
69 
70 	if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
71 		return 0;
72 
73 	wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
74 		"information");
75 	ssid = wpa_supplicant_get_ssid(wpa_s);
76 	if (ssid == NULL) {
77 		wpa_msg(wpa_s, MSG_INFO,
78 			"No network configuration found for the current AP");
79 		return -1;
80 	}
81 
82 	if (wpas_network_disabled(wpa_s, ssid)) {
83 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
84 		return -1;
85 	}
86 
87 	if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
88 	    disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
89 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
90 		return -1;
91 	}
92 
93 	res = wpas_temp_disabled(wpa_s, ssid);
94 	if (res > 0) {
95 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
96 			"disabled for %d second(s)", res);
97 		return -1;
98 	}
99 
100 	wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
101 		"current AP");
102 	if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
103 		u8 wpa_ie[80];
104 		size_t wpa_ie_len = sizeof(wpa_ie);
105 		wpa_supplicant_set_suites(wpa_s, NULL, ssid,
106 					  wpa_ie, &wpa_ie_len);
107 	} else {
108 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
109 	}
110 
111 	if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
112 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
113 	old_ssid = wpa_s->current_ssid;
114 	wpa_s->current_ssid = ssid;
115 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
116 	wpa_supplicant_initiate_eapol(wpa_s);
117 	if (old_ssid != wpa_s->current_ssid)
118 		wpas_notify_network_changed(wpa_s);
119 
120 	return 0;
121 }
122 
123 
124 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
125 {
126 	struct wpa_supplicant *wpa_s = eloop_ctx;
127 
128 	if (wpa_s->countermeasures) {
129 		wpa_s->countermeasures = 0;
130 		wpa_drv_set_countermeasures(wpa_s, 0);
131 		wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
132 		wpa_supplicant_req_scan(wpa_s, 0, 0);
133 	}
134 }
135 
136 
137 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
138 {
139 	int bssid_changed;
140 
141 	wnm_bss_keep_alive_deinit(wpa_s);
142 
143 #ifdef CONFIG_IBSS_RSN
144 	ibss_rsn_deinit(wpa_s->ibss_rsn);
145 	wpa_s->ibss_rsn = NULL;
146 #endif /* CONFIG_IBSS_RSN */
147 
148 #ifdef CONFIG_AP
149 	wpa_supplicant_ap_deinit(wpa_s);
150 #endif /* CONFIG_AP */
151 
152 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
153 		return;
154 
155 	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
156 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
157 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
158 	os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
159 #ifdef CONFIG_SME
160 	wpa_s->sme.prev_bssid_set = 0;
161 #endif /* CONFIG_SME */
162 #ifdef CONFIG_P2P
163 	os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
164 #endif /* CONFIG_P2P */
165 	wpa_s->current_bss = NULL;
166 	wpa_s->assoc_freq = 0;
167 #ifdef CONFIG_IEEE80211R
168 #ifdef CONFIG_SME
169 	if (wpa_s->sme.ft_ies)
170 		sme_update_ft_ies(wpa_s, NULL, NULL, 0);
171 #endif /* CONFIG_SME */
172 #endif /* CONFIG_IEEE80211R */
173 
174 	if (bssid_changed)
175 		wpas_notify_bssid_changed(wpa_s);
176 
177 	eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
178 	eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
179 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
180 		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
181 	wpa_s->ap_ies_from_associnfo = 0;
182 	wpa_s->current_ssid = NULL;
183 	wpa_s->key_mgmt = 0;
184 }
185 
186 
187 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
188 {
189 	struct wpa_ie_data ie;
190 	int pmksa_set = -1;
191 	size_t i;
192 
193 	if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
194 	    ie.pmkid == NULL)
195 		return;
196 
197 	for (i = 0; i < ie.num_pmkid; i++) {
198 		pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
199 						    ie.pmkid + i * PMKID_LEN,
200 						    NULL, NULL, 0);
201 		if (pmksa_set == 0) {
202 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
203 			break;
204 		}
205 	}
206 
207 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
208 		"PMKSA cache", pmksa_set == 0 ? "" : "not ");
209 }
210 
211 
212 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
213 						 union wpa_event_data *data)
214 {
215 	if (data == NULL) {
216 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
217 			"event");
218 		return;
219 	}
220 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
221 		" index=%d preauth=%d",
222 		MAC2STR(data->pmkid_candidate.bssid),
223 		data->pmkid_candidate.index,
224 		data->pmkid_candidate.preauth);
225 
226 	pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
227 			    data->pmkid_candidate.index,
228 			    data->pmkid_candidate.preauth);
229 }
230 
231 
232 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
233 {
234 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
235 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
236 		return 0;
237 
238 #ifdef IEEE8021X_EAPOL
239 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
240 	    wpa_s->current_ssid &&
241 	    !(wpa_s->current_ssid->eapol_flags &
242 	      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
243 	       EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
244 		/* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
245 		 * plaintext or static WEP keys). */
246 		return 0;
247 	}
248 #endif /* IEEE8021X_EAPOL */
249 
250 	return 1;
251 }
252 
253 
254 /**
255  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
256  * @wpa_s: pointer to wpa_supplicant data
257  * @ssid: Configuration data for the network
258  * Returns: 0 on success, -1 on failure
259  *
260  * This function is called when starting authentication with a network that is
261  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
262  */
263 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
264 			      struct wpa_ssid *ssid)
265 {
266 #ifdef IEEE8021X_EAPOL
267 #ifdef PCSC_FUNCS
268 	int aka = 0, sim = 0, type;
269 
270 	if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
271 		return 0;
272 
273 	if (ssid->eap.eap_methods == NULL) {
274 		sim = 1;
275 		aka = 1;
276 	} else {
277 		struct eap_method_type *eap = ssid->eap.eap_methods;
278 		while (eap->vendor != EAP_VENDOR_IETF ||
279 		       eap->method != EAP_TYPE_NONE) {
280 			if (eap->vendor == EAP_VENDOR_IETF) {
281 				if (eap->method == EAP_TYPE_SIM)
282 					sim = 1;
283 				else if (eap->method == EAP_TYPE_AKA ||
284 					 eap->method == EAP_TYPE_AKA_PRIME)
285 					aka = 1;
286 			}
287 			eap++;
288 		}
289 	}
290 
291 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
292 		sim = 0;
293 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
294 	    eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
295 	    NULL)
296 		aka = 0;
297 
298 	if (!sim && !aka) {
299 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
300 			"use SIM, but neither EAP-SIM nor EAP-AKA are "
301 			"enabled");
302 		return 0;
303 	}
304 
305 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
306 		"(sim=%d aka=%d) - initialize PCSC", sim, aka);
307 	if (sim && aka)
308 		type = SCARD_TRY_BOTH;
309 	else if (aka)
310 		type = SCARD_USIM_ONLY;
311 	else
312 		type = SCARD_GSM_SIM_ONLY;
313 
314 	wpa_s->scard = scard_init(type, NULL);
315 	if (wpa_s->scard == NULL) {
316 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
317 			"(pcsc-lite)");
318 		return -1;
319 	}
320 	wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
321 	eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
322 #endif /* PCSC_FUNCS */
323 #endif /* IEEE8021X_EAPOL */
324 
325 	return 0;
326 }
327 
328 
329 #ifndef CONFIG_NO_SCAN_PROCESSING
330 static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
331 					struct wpa_ssid *ssid)
332 {
333 	int i, privacy = 0;
334 
335 	if (ssid->mixed_cell)
336 		return 1;
337 
338 #ifdef CONFIG_WPS
339 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
340 		return 1;
341 #endif /* CONFIG_WPS */
342 
343 	for (i = 0; i < NUM_WEP_KEYS; i++) {
344 		if (ssid->wep_key_len[i]) {
345 			privacy = 1;
346 			break;
347 		}
348 	}
349 #ifdef IEEE8021X_EAPOL
350 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
351 	    ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
352 				 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
353 		privacy = 1;
354 #endif /* IEEE8021X_EAPOL */
355 
356 	if (wpa_key_mgmt_wpa(ssid->key_mgmt))
357 		privacy = 1;
358 
359 	if (bss->caps & IEEE80211_CAP_PRIVACY)
360 		return privacy;
361 	return !privacy;
362 }
363 
364 
365 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
366 					 struct wpa_ssid *ssid,
367 					 struct wpa_bss *bss)
368 {
369 	struct wpa_ie_data ie;
370 	int proto_match = 0;
371 	const u8 *rsn_ie, *wpa_ie;
372 	int ret;
373 	int wep_ok;
374 
375 	ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
376 	if (ret >= 0)
377 		return ret;
378 
379 	/* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
380 	wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
381 		(((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
382 		  ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
383 		 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
384 
385 	rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
386 	while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
387 		proto_match++;
388 
389 		if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
390 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - parse "
391 				"failed");
392 			break;
393 		}
394 
395 		if (wep_ok &&
396 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
397 		{
398 			wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
399 				"in RSN IE");
400 			return 1;
401 		}
402 
403 		if (!(ie.proto & ssid->proto)) {
404 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - proto "
405 				"mismatch");
406 			break;
407 		}
408 
409 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
410 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - PTK "
411 				"cipher mismatch");
412 			break;
413 		}
414 
415 		if (!(ie.group_cipher & ssid->group_cipher)) {
416 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - GTK "
417 				"cipher mismatch");
418 			break;
419 		}
420 
421 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
422 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - key mgmt "
423 				"mismatch");
424 			break;
425 		}
426 
427 #ifdef CONFIG_IEEE80211W
428 		if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
429 		    (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
430 		     wpa_s->conf->pmf : ssid->ieee80211w) ==
431 		    MGMT_FRAME_PROTECTION_REQUIRED) {
432 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - no mgmt "
433 				"frame protection");
434 			break;
435 		}
436 #endif /* CONFIG_IEEE80211W */
437 
438 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on RSN IE");
439 		return 1;
440 	}
441 
442 	wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
443 	while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
444 		proto_match++;
445 
446 		if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
447 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - parse "
448 				"failed");
449 			break;
450 		}
451 
452 		if (wep_ok &&
453 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
454 		{
455 			wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
456 				"in WPA IE");
457 			return 1;
458 		}
459 
460 		if (!(ie.proto & ssid->proto)) {
461 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - proto "
462 				"mismatch");
463 			break;
464 		}
465 
466 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
467 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - PTK "
468 				"cipher mismatch");
469 			break;
470 		}
471 
472 		if (!(ie.group_cipher & ssid->group_cipher)) {
473 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - GTK "
474 				"cipher mismatch");
475 			break;
476 		}
477 
478 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
479 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - key mgmt "
480 				"mismatch");
481 			break;
482 		}
483 
484 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on WPA IE");
485 		return 1;
486 	}
487 
488 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
489 	    !rsn_ie) {
490 		wpa_dbg(wpa_s, MSG_DEBUG, "   allow for non-WPA IEEE 802.1X");
491 		return 1;
492 	}
493 
494 	if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
495 	    wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
496 		wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no WPA/RSN proto match");
497 		return 0;
498 	}
499 
500 	if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
501 		wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
502 		return 1;
503 	}
504 
505 	wpa_dbg(wpa_s, MSG_DEBUG, "   reject due to mismatch with "
506 		"WPA/WPA2");
507 
508 	return 0;
509 }
510 
511 
512 static int freq_allowed(int *freqs, int freq)
513 {
514 	int i;
515 
516 	if (freqs == NULL)
517 		return 1;
518 
519 	for (i = 0; freqs[i]; i++)
520 		if (freqs[i] == freq)
521 			return 1;
522 	return 0;
523 }
524 
525 
526 static int ht_supported(const struct hostapd_hw_modes *mode)
527 {
528 	if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
529 		/*
530 		 * The driver did not indicate whether it supports HT. Assume
531 		 * it does to avoid connection issues.
532 		 */
533 		return 1;
534 	}
535 
536 	/*
537 	 * IEEE Std 802.11n-2009 20.1.1:
538 	 * An HT non-AP STA shall support all EQM rates for one spatial stream.
539 	 */
540 	return mode->mcs_set[0] == 0xff;
541 }
542 
543 
544 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
545 {
546 	const struct hostapd_hw_modes *mode = NULL, *modes;
547 	const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
548 	const u8 *rate_ie;
549 	int i, j, k;
550 
551 	if (bss->freq == 0)
552 		return 1; /* Cannot do matching without knowing band */
553 
554 	modes = wpa_s->hw.modes;
555 	if (modes == NULL) {
556 		/*
557 		 * The driver does not provide any additional information
558 		 * about the utilized hardware, so allow the connection attempt
559 		 * to continue.
560 		 */
561 		return 1;
562 	}
563 
564 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
565 		for (j = 0; j < modes[i].num_channels; j++) {
566 			int freq = modes[i].channels[j].freq;
567 			if (freq == bss->freq) {
568 				if (mode &&
569 				    mode->mode == HOSTAPD_MODE_IEEE80211G)
570 					break; /* do not allow 802.11b replace
571 						* 802.11g */
572 				mode = &modes[i];
573 				break;
574 			}
575 		}
576 	}
577 
578 	if (mode == NULL)
579 		return 0;
580 
581 	for (i = 0; i < (int) sizeof(scan_ie); i++) {
582 		rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
583 		if (rate_ie == NULL)
584 			continue;
585 
586 		for (j = 2; j < rate_ie[1] + 2; j++) {
587 			int flagged = !!(rate_ie[j] & 0x80);
588 			int r = (rate_ie[j] & 0x7f) * 5;
589 
590 			/*
591 			 * IEEE Std 802.11n-2009 7.3.2.2:
592 			 * The new BSS Membership selector value is encoded
593 			 * like a legacy basic rate, but it is not a rate and
594 			 * only indicates if the BSS members are required to
595 			 * support the mandatory features of Clause 20 [HT PHY]
596 			 * in order to join the BSS.
597 			 */
598 			if (flagged && ((rate_ie[j] & 0x7f) ==
599 					BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
600 				if (!ht_supported(mode)) {
601 					wpa_dbg(wpa_s, MSG_DEBUG,
602 						"   hardware does not support "
603 						"HT PHY");
604 					return 0;
605 				}
606 				continue;
607 			}
608 
609 			if (!flagged)
610 				continue;
611 
612 			/* check for legacy basic rates */
613 			for (k = 0; k < mode->num_rates; k++) {
614 				if (mode->rates[k] == r)
615 					break;
616 			}
617 			if (k == mode->num_rates) {
618 				/*
619 				 * IEEE Std 802.11-2007 7.3.2.2 demands that in
620 				 * order to join a BSS all required rates
621 				 * have to be supported by the hardware.
622 				 */
623 				wpa_dbg(wpa_s, MSG_DEBUG, "   hardware does "
624 					"not support required rate %d.%d Mbps",
625 					r / 10, r % 10);
626 				return 0;
627 			}
628 		}
629 	}
630 
631 	return 1;
632 }
633 
634 
635 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
636 					    int i, struct wpa_bss *bss,
637 					    struct wpa_ssid *group)
638 {
639 	u8 wpa_ie_len, rsn_ie_len;
640 	int wpa;
641 	struct wpa_blacklist *e;
642 	const u8 *ie;
643 	struct wpa_ssid *ssid;
644 
645 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
646 	wpa_ie_len = ie ? ie[1] : 0;
647 
648 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
649 	rsn_ie_len = ie ? ie[1] : 0;
650 
651 	wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
652 		"wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
653 		i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
654 		wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
655 		wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
656 
657 	e = wpa_blacklist_get(wpa_s, bss->bssid);
658 	if (e) {
659 		int limit = 1;
660 		if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
661 			/*
662 			 * When only a single network is enabled, we can
663 			 * trigger blacklisting on the first failure. This
664 			 * should not be done with multiple enabled networks to
665 			 * avoid getting forced to move into a worse ESS on
666 			 * single error if there are no other BSSes of the
667 			 * current ESS.
668 			 */
669 			limit = 0;
670 		}
671 		if (e->count > limit) {
672 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
673 				"(count=%d limit=%d)", e->count, limit);
674 			return NULL;
675 		}
676 	}
677 
678 	if (bss->ssid_len == 0) {
679 		wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
680 		return NULL;
681 	}
682 
683 	if (disallowed_bssid(wpa_s, bss->bssid)) {
684 		wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID disallowed");
685 		return NULL;
686 	}
687 
688 	if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
689 		wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID disallowed");
690 		return NULL;
691 	}
692 
693 	wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
694 
695 	for (ssid = group; ssid; ssid = ssid->pnext) {
696 		int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
697 		int res;
698 
699 		if (wpas_network_disabled(wpa_s, ssid)) {
700 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
701 			continue;
702 		}
703 
704 		res = wpas_temp_disabled(wpa_s, ssid);
705 		if (res > 0) {
706 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled "
707 				"temporarily for %d second(s)", res);
708 			continue;
709 		}
710 
711 #ifdef CONFIG_WPS
712 		if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
713 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
714 				"(WPS)");
715 			continue;
716 		}
717 
718 		if (wpa && ssid->ssid_len == 0 &&
719 		    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
720 			check_ssid = 0;
721 
722 		if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
723 			/* Only allow wildcard SSID match if an AP
724 			 * advertises active WPS operation that matches
725 			 * with our mode. */
726 			check_ssid = 1;
727 			if (ssid->ssid_len == 0 &&
728 			    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
729 				check_ssid = 0;
730 		}
731 #endif /* CONFIG_WPS */
732 
733 		if (ssid->bssid_set && ssid->ssid_len == 0 &&
734 		    os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
735 			check_ssid = 0;
736 
737 		if (check_ssid &&
738 		    (bss->ssid_len != ssid->ssid_len ||
739 		     os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
740 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
741 			continue;
742 		}
743 
744 		if (ssid->bssid_set &&
745 		    os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
746 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
747 			continue;
748 		}
749 
750 		if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
751 			continue;
752 
753 		if (!wpa &&
754 		    !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
755 		    !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
756 		    !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
757 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-WPA network "
758 				"not allowed");
759 			continue;
760 		}
761 
762 		if (!wpa_supplicant_match_privacy(bss, ssid)) {
763 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy "
764 				"mismatch");
765 			continue;
766 		}
767 
768 		if (bss->caps & IEEE80211_CAP_IBSS) {
769 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - IBSS (adhoc) "
770 				"network");
771 			continue;
772 		}
773 
774 		if (!freq_allowed(ssid->freq_list, bss->freq)) {
775 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - frequency not "
776 				"allowed");
777 			continue;
778 		}
779 
780 		if (!rate_match(wpa_s, bss)) {
781 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - rate sets do "
782 				"not match");
783 			continue;
784 		}
785 
786 #ifdef CONFIG_P2P
787 		/*
788 		 * TODO: skip the AP if its P2P IE has Group Formation
789 		 * bit set in the P2P Group Capability Bitmap and we
790 		 * are not in Group Formation with that device.
791 		 */
792 #endif /* CONFIG_P2P */
793 
794 		/* Matching configuration found */
795 		return ssid;
796 	}
797 
798 	/* No matching configuration found */
799 	return NULL;
800 }
801 
802 
803 static struct wpa_bss *
804 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
805 			  struct wpa_ssid *group,
806 			  struct wpa_ssid **selected_ssid)
807 {
808 	unsigned int i;
809 
810 	wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
811 		group->priority);
812 
813 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
814 		struct wpa_bss *bss = wpa_s->last_scan_res[i];
815 		*selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
816 		if (!*selected_ssid)
817 			continue;
818 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
819 			" ssid='%s'",
820 			MAC2STR(bss->bssid),
821 			wpa_ssid_txt(bss->ssid, bss->ssid_len));
822 		return bss;
823 	}
824 
825 	return NULL;
826 }
827 
828 
829 static struct wpa_bss *
830 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
831 			    struct wpa_ssid **selected_ssid)
832 {
833 	struct wpa_bss *selected = NULL;
834 	int prio;
835 
836 	if (wpa_s->last_scan_res == NULL ||
837 	    wpa_s->last_scan_res_used == 0)
838 		return NULL; /* no scan results from last update */
839 
840 	while (selected == NULL) {
841 		for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
842 			selected = wpa_supplicant_select_bss(
843 				wpa_s, wpa_s->conf->pssid[prio],
844 				selected_ssid);
845 			if (selected)
846 				break;
847 		}
848 
849 		if (selected == NULL && wpa_s->blacklist &&
850 		    !wpa_s->countermeasures) {
851 			wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
852 				"blacklist and try again");
853 			wpa_blacklist_clear(wpa_s);
854 			wpa_s->blacklist_cleared++;
855 		} else if (selected == NULL)
856 			break;
857 	}
858 
859 	return selected;
860 }
861 
862 
863 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
864 					int timeout_sec, int timeout_usec)
865 {
866 	if (!wpa_supplicant_enabled_networks(wpa_s)) {
867 		/*
868 		 * No networks are enabled; short-circuit request so
869 		 * we don't wait timeout seconds before transitioning
870 		 * to INACTIVE state.
871 		 */
872 		wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
873 			"since there are no enabled networks");
874 		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
875 #ifdef CONFIG_P2P
876 		wpa_s->sta_scan_pending = 0;
877 #endif /* CONFIG_P2P */
878 		return;
879 	}
880 
881 	wpa_s->scan_for_connection = 1;
882 	wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
883 }
884 
885 
886 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
887 			   struct wpa_bss *selected,
888 			   struct wpa_ssid *ssid)
889 {
890 	if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
891 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
892 			"PBC session overlap");
893 #ifdef CONFIG_P2P
894 		if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
895 			return -1;
896 #endif /* CONFIG_P2P */
897 
898 #ifdef CONFIG_WPS
899 		wpas_wps_cancel(wpa_s);
900 #endif /* CONFIG_WPS */
901 		return -1;
902 	}
903 
904 	/*
905 	 * Do not trigger new association unless the BSSID has changed or if
906 	 * reassociation is requested. If we are in process of associating with
907 	 * the selected BSSID, do not trigger new attempt.
908 	 */
909 	if (wpa_s->reassociate ||
910 	    (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
911 	     ((wpa_s->wpa_state != WPA_ASSOCIATING &&
912 	       wpa_s->wpa_state != WPA_AUTHENTICATING) ||
913 	      os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
914 	      0))) {
915 		if (wpa_supplicant_scard_init(wpa_s, ssid)) {
916 			wpa_supplicant_req_new_scan(wpa_s, 10, 0);
917 			return 0;
918 		}
919 		wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
920 			"reassociate: %d  selected: "MACSTR "  bssid: " MACSTR
921 			"  pending: " MACSTR "  wpa_state: %s",
922 			wpa_s->reassociate, MAC2STR(selected->bssid),
923 			MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
924 			wpa_supplicant_state_txt(wpa_s->wpa_state));
925 		wpa_supplicant_associate(wpa_s, selected, ssid);
926 	} else {
927 		wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
928 			"selected AP");
929 	}
930 
931 	return 0;
932 }
933 
934 
935 static struct wpa_ssid *
936 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
937 {
938 	int prio;
939 	struct wpa_ssid *ssid;
940 
941 	for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
942 		for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
943 		{
944 			if (wpas_network_disabled(wpa_s, ssid))
945 				continue;
946 			if (ssid->mode == IEEE80211_MODE_IBSS ||
947 			    ssid->mode == IEEE80211_MODE_AP)
948 				return ssid;
949 		}
950 	}
951 	return NULL;
952 }
953 
954 
955 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
956  * on BSS added and BSS changed events */
957 static void wpa_supplicant_rsn_preauth_scan_results(
958 	struct wpa_supplicant *wpa_s)
959 {
960 	struct wpa_bss *bss;
961 
962 	if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
963 		return;
964 
965 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
966 		const u8 *ssid, *rsn;
967 
968 		ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
969 		if (ssid == NULL)
970 			continue;
971 
972 		rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
973 		if (rsn == NULL)
974 			continue;
975 
976 		rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
977 	}
978 
979 }
980 
981 
982 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
983 				       struct wpa_bss *selected,
984 				       struct wpa_ssid *ssid)
985 {
986 	struct wpa_bss *current_bss = NULL;
987 	int min_diff;
988 
989 	if (wpa_s->reassociate)
990 		return 1; /* explicit request to reassociate */
991 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
992 		return 1; /* we are not associated; continue */
993 	if (wpa_s->current_ssid == NULL)
994 		return 1; /* unknown current SSID */
995 	if (wpa_s->current_ssid != ssid)
996 		return 1; /* different network block */
997 
998 	if (wpas_driver_bss_selection(wpa_s))
999 		return 0; /* Driver-based roaming */
1000 
1001 	if (wpa_s->current_ssid->ssid)
1002 		current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1003 					  wpa_s->current_ssid->ssid,
1004 					  wpa_s->current_ssid->ssid_len);
1005 	if (!current_bss)
1006 		current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
1007 
1008 	if (!current_bss)
1009 		return 1; /* current BSS not seen in scan results */
1010 
1011 	if (current_bss == selected)
1012 		return 0;
1013 
1014 	if (selected->last_update_idx > current_bss->last_update_idx)
1015 		return 1; /* current BSS not seen in the last scan */
1016 
1017 #ifndef CONFIG_NO_ROAMING
1018 	wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1019 	wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1020 		MAC2STR(current_bss->bssid), current_bss->level);
1021 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1022 		MAC2STR(selected->bssid), selected->level);
1023 
1024 	if (wpa_s->current_ssid->bssid_set &&
1025 	    os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1026 	    0) {
1027 		wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1028 			"has preferred BSSID");
1029 		return 1;
1030 	}
1031 
1032 	if (current_bss->level < 0 && current_bss->level > selected->level) {
1033 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1034 			"signal level");
1035 		return 0;
1036 	}
1037 
1038 	min_diff = 2;
1039 	if (current_bss->level < 0) {
1040 		if (current_bss->level < -85)
1041 			min_diff = 1;
1042 		else if (current_bss->level < -80)
1043 			min_diff = 2;
1044 		else if (current_bss->level < -75)
1045 			min_diff = 3;
1046 		else if (current_bss->level < -70)
1047 			min_diff = 4;
1048 		else
1049 			min_diff = 5;
1050 	}
1051 	if (abs(current_bss->level - selected->level) < min_diff) {
1052 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1053 			"in signal level");
1054 		return 0;
1055 	}
1056 
1057 	return 1;
1058 #else /* CONFIG_NO_ROAMING */
1059 	return 0;
1060 #endif /* CONFIG_NO_ROAMING */
1061 }
1062 
1063 
1064 /* Return != 0 if no scan results could be fetched or if scan results should not
1065  * be shared with other virtual interfaces. */
1066 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1067 					      union wpa_event_data *data)
1068 {
1069 	struct wpa_scan_results *scan_res;
1070 	int ap = 0;
1071 #ifndef CONFIG_NO_RANDOM_POOL
1072 	size_t i, num;
1073 #endif /* CONFIG_NO_RANDOM_POOL */
1074 
1075 #ifdef CONFIG_AP
1076 	if (wpa_s->ap_iface)
1077 		ap = 1;
1078 #endif /* CONFIG_AP */
1079 
1080 	wpa_supplicant_notify_scanning(wpa_s, 0);
1081 
1082 #ifdef CONFIG_P2P
1083 	if (wpa_s->global->p2p_cb_on_scan_complete &&
1084 	    !wpa_s->global->p2p_disabled &&
1085 	    wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1086 	    !wpa_s->scan_res_handler) {
1087 		wpa_s->global->p2p_cb_on_scan_complete = 0;
1088 		if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1089 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1090 				"stopped scan processing");
1091 			wpa_s->sta_scan_pending = 1;
1092 			wpa_supplicant_req_scan(wpa_s, 5, 0);
1093 			return -1;
1094 		}
1095 	}
1096 	wpa_s->sta_scan_pending = 0;
1097 #endif /* CONFIG_P2P */
1098 
1099 	scan_res = wpa_supplicant_get_scan_results(wpa_s,
1100 						   data ? &data->scan_info :
1101 						   NULL, 1);
1102 	if (scan_res == NULL) {
1103 		if (wpa_s->conf->ap_scan == 2 || ap)
1104 			return -1;
1105 		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1106 			"scanning again");
1107 		wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1108 		return -1;
1109 	}
1110 
1111 #ifndef CONFIG_NO_RANDOM_POOL
1112 	num = scan_res->num;
1113 	if (num > 10)
1114 		num = 10;
1115 	for (i = 0; i < num; i++) {
1116 		u8 buf[5];
1117 		struct wpa_scan_res *res = scan_res->res[i];
1118 		buf[0] = res->bssid[5];
1119 		buf[1] = res->qual & 0xff;
1120 		buf[2] = res->noise & 0xff;
1121 		buf[3] = res->level & 0xff;
1122 		buf[4] = res->tsf & 0xff;
1123 		random_add_randomness(buf, sizeof(buf));
1124 	}
1125 #endif /* CONFIG_NO_RANDOM_POOL */
1126 
1127 	if (wpa_s->scan_res_handler) {
1128 		void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1129 					 struct wpa_scan_results *scan_res);
1130 
1131 		scan_res_handler = wpa_s->scan_res_handler;
1132 		wpa_s->scan_res_handler = NULL;
1133 		scan_res_handler(wpa_s, scan_res);
1134 
1135 		wpa_scan_results_free(scan_res);
1136 		return -2;
1137 	}
1138 
1139 	if (ap) {
1140 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1141 #ifdef CONFIG_AP
1142 		if (wpa_s->ap_iface->scan_cb)
1143 			wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1144 #endif /* CONFIG_AP */
1145 		wpa_scan_results_free(scan_res);
1146 		return 0;
1147 	}
1148 
1149 	wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1150 	wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1151 	wpas_notify_scan_results(wpa_s);
1152 
1153 	wpas_notify_scan_done(wpa_s, 1);
1154 
1155 	if (sme_proc_obss_scan(wpa_s) > 0) {
1156 		wpa_scan_results_free(scan_res);
1157 		return 0;
1158 	}
1159 
1160 	if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1161 		wpa_scan_results_free(scan_res);
1162 		return 0;
1163 	}
1164 
1165 	if (autoscan_notify_scan(wpa_s, scan_res)) {
1166 		wpa_scan_results_free(scan_res);
1167 		return 0;
1168 	}
1169 
1170 	if (wpa_s->disconnected) {
1171 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1172 		wpa_scan_results_free(scan_res);
1173 		return 0;
1174 	}
1175 
1176 	if (!wpas_driver_bss_selection(wpa_s) &&
1177 	    bgscan_notify_scan(wpa_s, scan_res) == 1) {
1178 		wpa_scan_results_free(scan_res);
1179 		return 0;
1180 	}
1181 
1182 	wpas_wps_update_ap_info(wpa_s, scan_res);
1183 
1184 	wpa_scan_results_free(scan_res);
1185 
1186 	return wpas_select_network_from_last_scan(wpa_s);
1187 }
1188 
1189 
1190 int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s)
1191 {
1192 	struct wpa_bss *selected;
1193 	struct wpa_ssid *ssid = NULL;
1194 
1195 	selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1196 
1197 	if (selected) {
1198 		int skip;
1199 		skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1200 		if (skip) {
1201 			wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1202 			return 0;
1203 		}
1204 
1205 		if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1206 			wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1207 			return -1;
1208 		}
1209 		wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1210 		/*
1211 		 * Do not notify other virtual radios of scan results since we do not
1212 		 * want them to start other associations at the same time.
1213 		 */
1214 		return 1;
1215 	} else {
1216 		wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1217 		ssid = wpa_supplicant_pick_new_network(wpa_s);
1218 		if (ssid) {
1219 			wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1220 			wpa_supplicant_associate(wpa_s, NULL, ssid);
1221 			wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1222 		} else {
1223 			int timeout_sec = wpa_s->scan_interval;
1224 			int timeout_usec = 0;
1225 #ifdef CONFIG_P2P
1226 			if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1227 				return 0;
1228 
1229 			if (wpa_s->p2p_in_provisioning) {
1230 				/*
1231 				 * Use shorter wait during P2P Provisioning
1232 				 * state to speed up group formation.
1233 				 */
1234 				timeout_sec = 0;
1235 				timeout_usec = 250000;
1236 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1237 							    timeout_usec);
1238 				return 0;
1239 			}
1240 #endif /* CONFIG_P2P */
1241 #ifdef CONFIG_INTERWORKING
1242 			if (wpa_s->conf->auto_interworking &&
1243 			    wpa_s->conf->interworking &&
1244 			    wpa_s->conf->cred) {
1245 				wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1246 					"start ANQP fetch since no matching "
1247 					"networks found");
1248 				wpa_s->network_select = 1;
1249 				wpa_s->auto_network_select = 1;
1250 				interworking_start_fetch_anqp(wpa_s);
1251 				return 1;
1252 			}
1253 #endif /* CONFIG_INTERWORKING */
1254 			if (wpa_supplicant_req_sched_scan(wpa_s))
1255 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1256 							    timeout_usec);
1257 		}
1258 	}
1259 	return 0;
1260 }
1261 
1262 
1263 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1264 					      union wpa_event_data *data)
1265 {
1266 	const char *rn, *rn2;
1267 	struct wpa_supplicant *ifs;
1268 
1269 	if (_wpa_supplicant_event_scan_results(wpa_s, data) != 0) {
1270 		/*
1271 		 * If no scan results could be fetched, then no need to
1272 		 * notify those interfaces that did not actually request
1273 		 * this scan. Similarly, if scan results started a new operation on this
1274 		 * interface, do not notify other interfaces to avoid concurrent
1275 		 * operations during a connection attempt.
1276 		 */
1277 		return;
1278 	}
1279 
1280 	/*
1281 	 * Check other interfaces to see if they have the same radio-name. If
1282 	 * so, they get updated with this same scan info.
1283 	 */
1284 	if (!wpa_s->driver->get_radio_name)
1285 		return;
1286 
1287 	rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1288 	if (rn == NULL || rn[0] == '\0')
1289 		return;
1290 
1291 	wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1292 		"sharing same radio (%s) in event_scan_results", rn);
1293 
1294 	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1295 		if (ifs == wpa_s || !ifs->driver->get_radio_name)
1296 			continue;
1297 
1298 		rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1299 		if (rn2 && os_strcmp(rn, rn2) == 0) {
1300 			wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1301 				   "sibling", ifs->ifname);
1302 			_wpa_supplicant_event_scan_results(ifs, data);
1303 		}
1304 	}
1305 }
1306 
1307 #endif /* CONFIG_NO_SCAN_PROCESSING */
1308 
1309 
1310 #ifdef CONFIG_WNM
1311 
1312 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1313 {
1314 	struct wpa_supplicant *wpa_s = eloop_ctx;
1315 
1316 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
1317 		return;
1318 
1319 	if (!wpa_s->no_keep_alive) {
1320 		wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1321 			   MAC2STR(wpa_s->bssid));
1322 		/* TODO: could skip this if normal data traffic has been sent */
1323 		/* TODO: Consider using some more appropriate data frame for
1324 		 * this */
1325 		if (wpa_s->l2)
1326 			l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1327 				       (u8 *) "", 0);
1328 	}
1329 
1330 #ifdef CONFIG_SME
1331 	if (wpa_s->sme.bss_max_idle_period) {
1332 		unsigned int msec;
1333 		msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1334 		if (msec > 100)
1335 			msec -= 100;
1336 		eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1337 				       wnm_bss_keep_alive, wpa_s, NULL);
1338 	}
1339 #endif /* CONFIG_SME */
1340 }
1341 
1342 
1343 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1344 				   const u8 *ies, size_t ies_len)
1345 {
1346 	struct ieee802_11_elems elems;
1347 
1348 	if (ies == NULL)
1349 		return;
1350 
1351 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1352 		return;
1353 
1354 #ifdef CONFIG_SME
1355 	if (elems.bss_max_idle_period) {
1356 		unsigned int msec;
1357 		wpa_s->sme.bss_max_idle_period =
1358 			WPA_GET_LE16(elems.bss_max_idle_period);
1359 		wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1360 			   "TU)%s", wpa_s->sme.bss_max_idle_period,
1361 			   (elems.bss_max_idle_period[2] & 0x01) ?
1362 			   " (protected keep-live required)" : "");
1363 		if (wpa_s->sme.bss_max_idle_period == 0)
1364 			wpa_s->sme.bss_max_idle_period = 1;
1365 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1366 			eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1367 			 /* msec times 1000 */
1368 			msec = wpa_s->sme.bss_max_idle_period * 1024;
1369 			if (msec > 100)
1370 				msec -= 100;
1371 			eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1372 					       wnm_bss_keep_alive, wpa_s,
1373 					       NULL);
1374 		}
1375 	}
1376 #endif /* CONFIG_SME */
1377 }
1378 
1379 #endif /* CONFIG_WNM */
1380 
1381 
1382 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1383 {
1384 #ifdef CONFIG_WNM
1385 	eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1386 #endif /* CONFIG_WNM */
1387 }
1388 
1389 
1390 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1391 					  union wpa_event_data *data)
1392 {
1393 	int l, len, found = 0, wpa_found, rsn_found;
1394 	const u8 *p;
1395 
1396 	wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1397 	if (data->assoc_info.req_ies)
1398 		wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1399 			    data->assoc_info.req_ies_len);
1400 	if (data->assoc_info.resp_ies) {
1401 		wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1402 			    data->assoc_info.resp_ies_len);
1403 #ifdef CONFIG_TDLS
1404 		wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1405 					data->assoc_info.resp_ies_len);
1406 #endif /* CONFIG_TDLS */
1407 #ifdef CONFIG_WNM
1408 		wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1409 				       data->assoc_info.resp_ies_len);
1410 #endif /* CONFIG_WNM */
1411 	}
1412 	if (data->assoc_info.beacon_ies)
1413 		wpa_hexdump(MSG_DEBUG, "beacon_ies",
1414 			    data->assoc_info.beacon_ies,
1415 			    data->assoc_info.beacon_ies_len);
1416 	if (data->assoc_info.freq)
1417 		wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1418 			data->assoc_info.freq);
1419 
1420 	p = data->assoc_info.req_ies;
1421 	l = data->assoc_info.req_ies_len;
1422 
1423 	/* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1424 	while (p && l >= 2) {
1425 		len = p[1] + 2;
1426 		if (len > l) {
1427 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1428 				    p, l);
1429 			break;
1430 		}
1431 		if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1432 		     (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1433 		    (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1434 			if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1435 				break;
1436 			found = 1;
1437 			wpa_find_assoc_pmkid(wpa_s);
1438 			break;
1439 		}
1440 		l -= len;
1441 		p += len;
1442 	}
1443 	if (!found && data->assoc_info.req_ies)
1444 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1445 
1446 #ifdef CONFIG_IEEE80211R
1447 #ifdef CONFIG_SME
1448 	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1449 		u8 bssid[ETH_ALEN];
1450 		if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1451 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1452 						 data->assoc_info.resp_ies,
1453 						 data->assoc_info.resp_ies_len,
1454 						 bssid) < 0) {
1455 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1456 				"Reassociation Response failed");
1457 			wpa_supplicant_deauthenticate(
1458 				wpa_s, WLAN_REASON_INVALID_IE);
1459 			return -1;
1460 		}
1461 	}
1462 
1463 	p = data->assoc_info.resp_ies;
1464 	l = data->assoc_info.resp_ies_len;
1465 
1466 #ifdef CONFIG_WPS_STRICT
1467 	if (p && wpa_s->current_ssid &&
1468 	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1469 		struct wpabuf *wps;
1470 		wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1471 		if (wps == NULL) {
1472 			wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1473 				"include WPS IE in (Re)Association Response");
1474 			return -1;
1475 		}
1476 
1477 		if (wps_validate_assoc_resp(wps) < 0) {
1478 			wpabuf_free(wps);
1479 			wpa_supplicant_deauthenticate(
1480 				wpa_s, WLAN_REASON_INVALID_IE);
1481 			return -1;
1482 		}
1483 		wpabuf_free(wps);
1484 	}
1485 #endif /* CONFIG_WPS_STRICT */
1486 
1487 	/* Go through the IEs and make a copy of the MDIE, if present. */
1488 	while (p && l >= 2) {
1489 		len = p[1] + 2;
1490 		if (len > l) {
1491 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1492 				    p, l);
1493 			break;
1494 		}
1495 		if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1496 		    p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1497 			wpa_s->sme.ft_used = 1;
1498 			os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1499 				  MOBILITY_DOMAIN_ID_LEN);
1500 			break;
1501 		}
1502 		l -= len;
1503 		p += len;
1504 	}
1505 #endif /* CONFIG_SME */
1506 
1507 	wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1508 			     data->assoc_info.resp_ies_len);
1509 #endif /* CONFIG_IEEE80211R */
1510 
1511 	/* WPA/RSN IE from Beacon/ProbeResp */
1512 	p = data->assoc_info.beacon_ies;
1513 	l = data->assoc_info.beacon_ies_len;
1514 
1515 	/* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1516 	 */
1517 	wpa_found = rsn_found = 0;
1518 	while (p && l >= 2) {
1519 		len = p[1] + 2;
1520 		if (len > l) {
1521 			wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1522 				    p, l);
1523 			break;
1524 		}
1525 		if (!wpa_found &&
1526 		    p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1527 		    os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1528 			wpa_found = 1;
1529 			wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1530 		}
1531 
1532 		if (!rsn_found &&
1533 		    p[0] == WLAN_EID_RSN && p[1] >= 2) {
1534 			rsn_found = 1;
1535 			wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1536 		}
1537 
1538 		l -= len;
1539 		p += len;
1540 	}
1541 
1542 	if (!wpa_found && data->assoc_info.beacon_ies)
1543 		wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1544 	if (!rsn_found && data->assoc_info.beacon_ies)
1545 		wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1546 	if (wpa_found || rsn_found)
1547 		wpa_s->ap_ies_from_associnfo = 1;
1548 
1549 	if (wpa_s->assoc_freq && data->assoc_info.freq &&
1550 	    wpa_s->assoc_freq != data->assoc_info.freq) {
1551 		wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1552 			   "%u to %u MHz",
1553 			   wpa_s->assoc_freq, data->assoc_info.freq);
1554 		wpa_supplicant_update_scan_results(wpa_s);
1555 	}
1556 
1557 	wpa_s->assoc_freq = data->assoc_info.freq;
1558 
1559 	return 0;
1560 }
1561 
1562 
1563 static struct wpa_bss * wpa_supplicant_get_new_bss(
1564 	struct wpa_supplicant *wpa_s, const u8 *bssid)
1565 {
1566 	struct wpa_bss *bss = NULL;
1567 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1568 
1569 	if (ssid->ssid_len > 0)
1570 		bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1571 	if (!bss)
1572 		bss = wpa_bss_get_bssid(wpa_s, bssid);
1573 
1574 	return bss;
1575 }
1576 
1577 
1578 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1579 {
1580 	const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1581 
1582 	if (!wpa_s->current_bss || !wpa_s->current_ssid)
1583 		return -1;
1584 
1585 	if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1586 		return 0;
1587 
1588 	bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1589 					WPA_IE_VENDOR_TYPE);
1590 	bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1591 
1592 	if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1593 				 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1594 	    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1595 				 bss_rsn ? 2 + bss_rsn[1] : 0))
1596 		return -1;
1597 
1598 	return 0;
1599 }
1600 
1601 
1602 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1603 				       union wpa_event_data *data)
1604 {
1605 	u8 bssid[ETH_ALEN];
1606 	int ft_completed;
1607 	struct wpa_driver_capa capa;
1608 
1609 #ifdef CONFIG_AP
1610 	if (wpa_s->ap_iface) {
1611 		hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1612 				    data->assoc_info.addr,
1613 				    data->assoc_info.req_ies,
1614 				    data->assoc_info.req_ies_len,
1615 				    data->assoc_info.reassoc);
1616 		return;
1617 	}
1618 #endif /* CONFIG_AP */
1619 
1620 	ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1621 	if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1622 		return;
1623 
1624 	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1625 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
1626 		wpa_supplicant_deauthenticate(
1627 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1628 		return;
1629 	}
1630 
1631 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1632 	if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1633 		wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1634 			MACSTR, MAC2STR(bssid));
1635 		random_add_randomness(bssid, ETH_ALEN);
1636 		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1637 		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1638 		wpas_notify_bssid_changed(wpa_s);
1639 
1640 		if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1641 			wpa_clear_keys(wpa_s, bssid);
1642 		}
1643 		if (wpa_supplicant_select_config(wpa_s) < 0) {
1644 			wpa_supplicant_deauthenticate(
1645 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1646 			return;
1647 		}
1648 		if (wpa_s->current_ssid) {
1649 			struct wpa_bss *bss = NULL;
1650 
1651 			bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1652 			if (!bss) {
1653 				wpa_supplicant_update_scan_results(wpa_s);
1654 
1655 				/* Get the BSS from the new scan results */
1656 				bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1657 			}
1658 
1659 			if (bss)
1660 				wpa_s->current_bss = bss;
1661 		}
1662 
1663 		if (wpa_s->conf->ap_scan == 1 &&
1664 		    wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1665 			if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1666 				wpa_msg(wpa_s, MSG_WARNING,
1667 					"WPA/RSN IEs not updated");
1668 		}
1669 	}
1670 
1671 #ifdef CONFIG_SME
1672 	os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1673 	wpa_s->sme.prev_bssid_set = 1;
1674 #endif /* CONFIG_SME */
1675 
1676 	wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1677 	if (wpa_s->current_ssid) {
1678 		/* When using scanning (ap_scan=1), SIM PC/SC interface can be
1679 		 * initialized before association, but for other modes,
1680 		 * initialize PC/SC here, if the current configuration needs
1681 		 * smartcard or SIM/USIM. */
1682 		wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1683 	}
1684 	wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1685 	if (wpa_s->l2)
1686 		l2_packet_notify_auth_start(wpa_s->l2);
1687 
1688 	/*
1689 	 * Set portEnabled first to FALSE in order to get EAP state machine out
1690 	 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1691 	 * state machine may transit to AUTHENTICATING state based on obsolete
1692 	 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1693 	 * AUTHENTICATED without ever giving chance to EAP state machine to
1694 	 * reset the state.
1695 	 */
1696 	if (!ft_completed) {
1697 		eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1698 		eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1699 	}
1700 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1701 		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1702 	/* 802.1X::portControl = Auto */
1703 	eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1704 	wpa_s->eapol_received = 0;
1705 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1706 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1707 	    (wpa_s->current_ssid &&
1708 	     wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1709 		wpa_supplicant_cancel_auth_timeout(wpa_s);
1710 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1711 	} else if (!ft_completed) {
1712 		/* Timeout for receiving the first EAPOL packet */
1713 		wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1714 	}
1715 	wpa_supplicant_cancel_scan(wpa_s);
1716 
1717 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1718 	    wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1719 		/*
1720 		 * We are done; the driver will take care of RSN 4-way
1721 		 * handshake.
1722 		 */
1723 		wpa_supplicant_cancel_auth_timeout(wpa_s);
1724 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1725 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1726 		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1727 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1728 		   wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1729 		/*
1730 		 * The driver will take care of RSN 4-way handshake, so we need
1731 		 * to allow EAPOL supplicant to complete its work without
1732 		 * waiting for WPA supplicant.
1733 		 */
1734 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1735 	} else if (ft_completed) {
1736 		/*
1737 		 * FT protocol completed - make sure EAPOL state machine ends
1738 		 * up in authenticated.
1739 		 */
1740 		wpa_supplicant_cancel_auth_timeout(wpa_s);
1741 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1742 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1743 		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1744 	}
1745 
1746 	wpa_s->last_eapol_matches_bssid = 0;
1747 
1748 	if (wpa_s->pending_eapol_rx) {
1749 		struct os_time now, age;
1750 		os_get_time(&now);
1751 		os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1752 		if (age.sec == 0 && age.usec < 100000 &&
1753 		    os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1754 		    0) {
1755 			wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1756 				"frame that was received just before "
1757 				"association notification");
1758 			wpa_supplicant_rx_eapol(
1759 				wpa_s, wpa_s->pending_eapol_rx_src,
1760 				wpabuf_head(wpa_s->pending_eapol_rx),
1761 				wpabuf_len(wpa_s->pending_eapol_rx));
1762 		}
1763 		wpabuf_free(wpa_s->pending_eapol_rx);
1764 		wpa_s->pending_eapol_rx = NULL;
1765 	}
1766 
1767 	if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1768 	     wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1769 	    wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1770 	    capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1771 		/* Set static WEP keys again */
1772 		wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1773 	}
1774 
1775 #ifdef CONFIG_IBSS_RSN
1776 	if (wpa_s->current_ssid &&
1777 	    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1778 	    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1779 	    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1780 	    wpa_s->ibss_rsn == NULL) {
1781 		wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1782 		if (!wpa_s->ibss_rsn) {
1783 			wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1784 			wpa_supplicant_deauthenticate(
1785 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1786 			return;
1787 		}
1788 
1789 		ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1790 	}
1791 #endif /* CONFIG_IBSS_RSN */
1792 
1793 	wpas_wps_notify_assoc(wpa_s, bssid);
1794 }
1795 
1796 
1797 static int disconnect_reason_recoverable(u16 reason_code)
1798 {
1799 	return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1800 		reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1801 		reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1802 }
1803 
1804 
1805 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1806 					  u16 reason_code,
1807 					  int locally_generated)
1808 {
1809 	const u8 *bssid;
1810 
1811 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1812 		/*
1813 		 * At least Host AP driver and a Prism3 card seemed to be
1814 		 * generating streams of disconnected events when configuring
1815 		 * IBSS for WPA-None. Ignore them for now.
1816 		 */
1817 		return;
1818 	}
1819 
1820 	bssid = wpa_s->bssid;
1821 	if (is_zero_ether_addr(bssid))
1822 		bssid = wpa_s->pending_bssid;
1823 
1824 	if (!is_zero_ether_addr(bssid) ||
1825 	    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1826 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1827 			" reason=%d%s",
1828 			MAC2STR(bssid), reason_code,
1829 			locally_generated ? " locally_generated=1" : "");
1830 	}
1831 }
1832 
1833 
1834 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1835 				 int locally_generated)
1836 {
1837 	if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1838 	    !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1839 		return 0; /* Not in 4-way handshake with PSK */
1840 
1841 	/*
1842 	 * It looks like connection was lost while trying to go through PSK
1843 	 * 4-way handshake. Filter out known disconnection cases that are caused
1844 	 * by something else than PSK mismatch to avoid confusing reports.
1845 	 */
1846 
1847 	if (locally_generated) {
1848 		if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1849 			return 0;
1850 	}
1851 
1852 	return 1;
1853 }
1854 
1855 
1856 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1857 						 u16 reason_code,
1858 						 int locally_generated)
1859 {
1860 	const u8 *bssid;
1861 	int authenticating;
1862 	u8 prev_pending_bssid[ETH_ALEN];
1863 	struct wpa_bss *fast_reconnect = NULL;
1864 	struct wpa_ssid *fast_reconnect_ssid = NULL;
1865 	struct wpa_ssid *last_ssid;
1866 
1867 	authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1868 	os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1869 
1870 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1871 		/*
1872 		 * At least Host AP driver and a Prism3 card seemed to be
1873 		 * generating streams of disconnected events when configuring
1874 		 * IBSS for WPA-None. Ignore them for now.
1875 		 */
1876 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1877 			"IBSS/WPA-None mode");
1878 		return;
1879 	}
1880 
1881 	if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
1882 		wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1883 			"pre-shared key may be incorrect");
1884 		wpas_auth_failed(wpa_s);
1885 	}
1886 	if (!wpa_s->auto_reconnect_disabled ||
1887 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1888 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1889 			"reconnect (wps=%d wpa_state=%d)",
1890 			wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1891 			wpa_s->wpa_state);
1892 		if (wpa_s->wpa_state == WPA_COMPLETED &&
1893 		    wpa_s->current_ssid &&
1894 		    wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1895 		    !locally_generated &&
1896 		    disconnect_reason_recoverable(reason_code)) {
1897 			/*
1898 			 * It looks like the AP has dropped association with
1899 			 * us, but could allow us to get back in. Try to
1900 			 * reconnect to the same BSS without full scan to save
1901 			 * time for some common cases.
1902 			 */
1903 			fast_reconnect = wpa_s->current_bss;
1904 			fast_reconnect_ssid = wpa_s->current_ssid;
1905 		} else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1906 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
1907 		else
1908 			wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1909 				"immediate scan");
1910 	} else {
1911 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
1912 			"try to re-connect");
1913 		wpa_s->reassociate = 0;
1914 		wpa_s->disconnected = 1;
1915 		wpa_supplicant_cancel_sched_scan(wpa_s);
1916 	}
1917 	bssid = wpa_s->bssid;
1918 	if (is_zero_ether_addr(bssid))
1919 		bssid = wpa_s->pending_bssid;
1920 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1921 		wpas_connection_failed(wpa_s, bssid);
1922 	wpa_sm_notify_disassoc(wpa_s->wpa);
1923 	if (locally_generated)
1924 		wpa_s->disconnect_reason = -reason_code;
1925 	else
1926 		wpa_s->disconnect_reason = reason_code;
1927 	wpas_notify_disconnect_reason(wpa_s);
1928 	if (wpa_supplicant_dynamic_keys(wpa_s)) {
1929 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1930 		wpa_s->keys_cleared = 0;
1931 		wpa_clear_keys(wpa_s, wpa_s->bssid);
1932 	}
1933 	last_ssid = wpa_s->current_ssid;
1934 	wpa_supplicant_mark_disassoc(wpa_s);
1935 
1936 	if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1937 		sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1938 		wpa_s->current_ssid = last_ssid;
1939 	}
1940 
1941 	if (fast_reconnect) {
1942 #ifndef CONFIG_NO_SCAN_PROCESSING
1943 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1944 		if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1945 					   fast_reconnect_ssid) < 0) {
1946 			/* Recover through full scan */
1947 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
1948 		}
1949 #endif /* CONFIG_NO_SCAN_PROCESSING */
1950 	}
1951 }
1952 
1953 
1954 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1955 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
1956 {
1957 	struct wpa_supplicant *wpa_s = eloop_ctx;
1958 
1959 	if (!wpa_s->pending_mic_error_report)
1960 		return;
1961 
1962 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1963 	wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1964 	wpa_s->pending_mic_error_report = 0;
1965 }
1966 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1967 
1968 
1969 static void
1970 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1971 					 union wpa_event_data *data)
1972 {
1973 	int pairwise;
1974 	struct os_time t;
1975 
1976 	wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1977 	pairwise = (data && data->michael_mic_failure.unicast);
1978 	os_get_time(&t);
1979 	if ((wpa_s->last_michael_mic_error &&
1980 	     t.sec - wpa_s->last_michael_mic_error <= 60) ||
1981 	    wpa_s->pending_mic_error_report) {
1982 		if (wpa_s->pending_mic_error_report) {
1983 			/*
1984 			 * Send the pending MIC error report immediately since
1985 			 * we are going to start countermeasures and AP better
1986 			 * do the same.
1987 			 */
1988 			wpa_sm_key_request(wpa_s->wpa, 1,
1989 					   wpa_s->pending_mic_error_pairwise);
1990 		}
1991 
1992 		/* Send the new MIC error report immediately since we are going
1993 		 * to start countermeasures and AP better do the same.
1994 		 */
1995 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1996 
1997 		/* initialize countermeasures */
1998 		wpa_s->countermeasures = 1;
1999 
2000 		wpa_blacklist_add(wpa_s, wpa_s->bssid);
2001 
2002 		wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2003 
2004 		/*
2005 		 * Need to wait for completion of request frame. We do not get
2006 		 * any callback for the message completion, so just wait a
2007 		 * short while and hope for the best. */
2008 		os_sleep(0, 10000);
2009 
2010 		wpa_drv_set_countermeasures(wpa_s, 1);
2011 		wpa_supplicant_deauthenticate(wpa_s,
2012 					      WLAN_REASON_MICHAEL_MIC_FAILURE);
2013 		eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2014 				     wpa_s, NULL);
2015 		eloop_register_timeout(60, 0,
2016 				       wpa_supplicant_stop_countermeasures,
2017 				       wpa_s, NULL);
2018 		/* TODO: mark the AP rejected for 60 second. STA is
2019 		 * allowed to associate with another AP.. */
2020 	} else {
2021 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2022 		if (wpa_s->mic_errors_seen) {
2023 			/*
2024 			 * Reduce the effectiveness of Michael MIC error
2025 			 * reports as a means for attacking against TKIP if
2026 			 * more than one MIC failure is noticed with the same
2027 			 * PTK. We delay the transmission of the reports by a
2028 			 * random time between 0 and 60 seconds in order to
2029 			 * force the attacker wait 60 seconds before getting
2030 			 * the information on whether a frame resulted in a MIC
2031 			 * failure.
2032 			 */
2033 			u8 rval[4];
2034 			int sec;
2035 
2036 			if (os_get_random(rval, sizeof(rval)) < 0)
2037 				sec = os_random() % 60;
2038 			else
2039 				sec = WPA_GET_BE32(rval) % 60;
2040 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2041 				"report %d seconds", sec);
2042 			wpa_s->pending_mic_error_report = 1;
2043 			wpa_s->pending_mic_error_pairwise = pairwise;
2044 			eloop_cancel_timeout(
2045 				wpa_supplicant_delayed_mic_error_report,
2046 				wpa_s, NULL);
2047 			eloop_register_timeout(
2048 				sec, os_random() % 1000000,
2049 				wpa_supplicant_delayed_mic_error_report,
2050 				wpa_s, NULL);
2051 		} else {
2052 			wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2053 		}
2054 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2055 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2056 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2057 	}
2058 	wpa_s->last_michael_mic_error = t.sec;
2059 	wpa_s->mic_errors_seen++;
2060 }
2061 
2062 
2063 #ifdef CONFIG_TERMINATE_ONLASTIF
2064 static int any_interfaces(struct wpa_supplicant *head)
2065 {
2066 	struct wpa_supplicant *wpa_s;
2067 
2068 	for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2069 		if (!wpa_s->interface_removed)
2070 			return 1;
2071 	return 0;
2072 }
2073 #endif /* CONFIG_TERMINATE_ONLASTIF */
2074 
2075 
2076 static void
2077 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2078 				      union wpa_event_data *data)
2079 {
2080 	if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2081 		return;
2082 
2083 	switch (data->interface_status.ievent) {
2084 	case EVENT_INTERFACE_ADDED:
2085 		if (!wpa_s->interface_removed)
2086 			break;
2087 		wpa_s->interface_removed = 0;
2088 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2089 		if (wpa_supplicant_driver_init(wpa_s) < 0) {
2090 			wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2091 				"driver after interface was added");
2092 		}
2093 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2094 		break;
2095 	case EVENT_INTERFACE_REMOVED:
2096 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2097 		wpa_s->interface_removed = 1;
2098 		wpa_supplicant_mark_disassoc(wpa_s);
2099 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2100 		l2_packet_deinit(wpa_s->l2);
2101 		wpa_s->l2 = NULL;
2102 #ifdef CONFIG_IBSS_RSN
2103 		ibss_rsn_deinit(wpa_s->ibss_rsn);
2104 		wpa_s->ibss_rsn = NULL;
2105 #endif /* CONFIG_IBSS_RSN */
2106 #ifdef CONFIG_TERMINATE_ONLASTIF
2107 		/* check if last interface */
2108 		if (!any_interfaces(wpa_s->global->ifaces))
2109 			eloop_terminate();
2110 #endif /* CONFIG_TERMINATE_ONLASTIF */
2111 		break;
2112 	}
2113 }
2114 
2115 
2116 #ifdef CONFIG_PEERKEY
2117 static void
2118 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2119 			      union wpa_event_data *data)
2120 {
2121 	if (data == NULL)
2122 		return;
2123 	wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2124 }
2125 #endif /* CONFIG_PEERKEY */
2126 
2127 
2128 #ifdef CONFIG_TDLS
2129 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2130 				      union wpa_event_data *data)
2131 {
2132 	if (data == NULL)
2133 		return;
2134 	switch (data->tdls.oper) {
2135 	case TDLS_REQUEST_SETUP:
2136 		wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2137 		break;
2138 	case TDLS_REQUEST_TEARDOWN:
2139 		wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
2140 				       data->tdls.reason_code);
2141 		break;
2142 	}
2143 }
2144 #endif /* CONFIG_TDLS */
2145 
2146 
2147 #ifdef CONFIG_WNM
2148 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2149 				     union wpa_event_data *data)
2150 {
2151 	if (data == NULL)
2152 		return;
2153 	switch (data->wnm.oper) {
2154 	case WNM_OPER_SLEEP:
2155 		wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2156 			   "(action=%d, intval=%d)",
2157 			   data->wnm.sleep_action, data->wnm.sleep_intval);
2158 		ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2159 					     data->wnm.sleep_intval, NULL);
2160 		break;
2161 	}
2162 }
2163 #endif /* CONFIG_WNM */
2164 
2165 
2166 #ifdef CONFIG_IEEE80211R
2167 static void
2168 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2169 				 union wpa_event_data *data)
2170 {
2171 	if (data == NULL)
2172 		return;
2173 
2174 	if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2175 				    data->ft_ies.ies_len,
2176 				    data->ft_ies.ft_action,
2177 				    data->ft_ies.target_ap,
2178 				    data->ft_ies.ric_ies,
2179 				    data->ft_ies.ric_ies_len) < 0) {
2180 		/* TODO: prevent MLME/driver from trying to associate? */
2181 	}
2182 }
2183 #endif /* CONFIG_IEEE80211R */
2184 
2185 
2186 #ifdef CONFIG_IBSS_RSN
2187 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2188 						union wpa_event_data *data)
2189 {
2190 	struct wpa_ssid *ssid;
2191 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
2192 		return;
2193 	if (data == NULL)
2194 		return;
2195 	ssid = wpa_s->current_ssid;
2196 	if (ssid == NULL)
2197 		return;
2198 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2199 		return;
2200 
2201 	ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2202 }
2203 #endif /* CONFIG_IBSS_RSN */
2204 
2205 
2206 #ifdef CONFIG_IEEE80211R
2207 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2208 			 size_t len)
2209 {
2210 	const u8 *sta_addr, *target_ap_addr;
2211 	u16 status;
2212 
2213 	wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2214 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2215 		return; /* only SME case supported for now */
2216 	if (len < 1 + 2 * ETH_ALEN + 2)
2217 		return;
2218 	if (data[0] != 2)
2219 		return; /* Only FT Action Response is supported for now */
2220 	sta_addr = data + 1;
2221 	target_ap_addr = data + 1 + ETH_ALEN;
2222 	status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2223 	wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2224 		MACSTR " TargetAP " MACSTR " status %u",
2225 		MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2226 
2227 	if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2228 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2229 			" in FT Action Response", MAC2STR(sta_addr));
2230 		return;
2231 	}
2232 
2233 	if (status) {
2234 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2235 			"failure (status code %d)", status);
2236 		/* TODO: report error to FT code(?) */
2237 		return;
2238 	}
2239 
2240 	if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2241 				    len - (1 + 2 * ETH_ALEN + 2), 1,
2242 				    target_ap_addr, NULL, 0) < 0)
2243 		return;
2244 
2245 #ifdef CONFIG_SME
2246 	{
2247 		struct wpa_bss *bss;
2248 		bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2249 		if (bss)
2250 			wpa_s->sme.freq = bss->freq;
2251 		wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2252 		sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2253 			      WLAN_AUTH_FT);
2254 	}
2255 #endif /* CONFIG_SME */
2256 }
2257 #endif /* CONFIG_IEEE80211R */
2258 
2259 
2260 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2261 					       struct unprot_deauth *e)
2262 {
2263 #ifdef CONFIG_IEEE80211W
2264 	wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2265 		   "dropped: " MACSTR " -> " MACSTR
2266 		   " (reason code %u)",
2267 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2268 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2269 #endif /* CONFIG_IEEE80211W */
2270 }
2271 
2272 
2273 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2274 						 struct unprot_disassoc *e)
2275 {
2276 #ifdef CONFIG_IEEE80211W
2277 	wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2278 		   "dropped: " MACSTR " -> " MACSTR
2279 		   " (reason code %u)",
2280 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2281 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2282 #endif /* CONFIG_IEEE80211W */
2283 }
2284 
2285 
2286 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2287 			  union wpa_event_data *data)
2288 {
2289 	struct wpa_supplicant *wpa_s = ctx;
2290 	u16 reason_code = 0;
2291 	int locally_generated = 0;
2292 
2293 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2294 	    event != EVENT_INTERFACE_ENABLED &&
2295 	    event != EVENT_INTERFACE_STATUS &&
2296 	    event != EVENT_SCHED_SCAN_STOPPED) {
2297 		wpa_dbg(wpa_s, MSG_DEBUG,
2298 			"Ignore event %s (%d) while interface is disabled",
2299 			event_to_string(event), event);
2300 		return;
2301 	}
2302 
2303 #ifndef CONFIG_NO_STDOUT_DEBUG
2304 {
2305 	int level = MSG_DEBUG;
2306 
2307 	if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2308 		const struct ieee80211_hdr *hdr;
2309 		u16 fc;
2310 		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2311 		fc = le_to_host16(hdr->frame_control);
2312 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2313 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2314 			level = MSG_EXCESSIVE;
2315 	}
2316 
2317 	wpa_dbg(wpa_s, level, "Event %s (%d) received",
2318 		event_to_string(event), event);
2319 }
2320 #endif /* CONFIG_NO_STDOUT_DEBUG */
2321 
2322 	switch (event) {
2323 	case EVENT_AUTH:
2324 		sme_event_auth(wpa_s, data);
2325 		break;
2326 	case EVENT_ASSOC:
2327 		wpa_supplicant_event_assoc(wpa_s, data);
2328 		break;
2329 	case EVENT_DISASSOC:
2330 		wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2331 		if (data) {
2332 			wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2333 				data->disassoc_info.reason_code,
2334 				data->disassoc_info.locally_generated ?
2335 				" (locally generated)" : "");
2336 			if (data->disassoc_info.addr)
2337 				wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2338 					MAC2STR(data->disassoc_info.addr));
2339 		}
2340 #ifdef CONFIG_AP
2341 		if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2342 			hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2343 					       data->disassoc_info.addr);
2344 			break;
2345 		}
2346 		if (wpa_s->ap_iface) {
2347 			wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2348 				"AP mode");
2349 			break;
2350 		}
2351 #endif /* CONFIG_AP */
2352 		if (data) {
2353 			reason_code = data->disassoc_info.reason_code;
2354 			locally_generated =
2355 				data->disassoc_info.locally_generated;
2356 			wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2357 				    data->disassoc_info.ie,
2358 				    data->disassoc_info.ie_len);
2359 #ifdef CONFIG_P2P
2360 			wpas_p2p_disassoc_notif(
2361 				wpa_s, data->disassoc_info.addr, reason_code,
2362 				data->disassoc_info.ie,
2363 				data->disassoc_info.ie_len,
2364 				locally_generated);
2365 #endif /* CONFIG_P2P */
2366 		}
2367 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2368 			sme_event_disassoc(wpa_s, data);
2369 		/* fall through */
2370 	case EVENT_DEAUTH:
2371 		if (event == EVENT_DEAUTH) {
2372 			wpa_dbg(wpa_s, MSG_DEBUG,
2373 				"Deauthentication notification");
2374 			if (data) {
2375 				reason_code = data->deauth_info.reason_code;
2376 				locally_generated =
2377 					data->deauth_info.locally_generated;
2378 				wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2379 					data->deauth_info.reason_code,
2380 					data->deauth_info.locally_generated ?
2381 					" (locally generated)" : "");
2382 				if (data->deauth_info.addr) {
2383 					wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2384 						MACSTR,
2385 						MAC2STR(data->deauth_info.
2386 							addr));
2387 				}
2388 				wpa_hexdump(MSG_DEBUG,
2389 					    "Deauthentication frame IE(s)",
2390 					    data->deauth_info.ie,
2391 					    data->deauth_info.ie_len);
2392 			}
2393 		}
2394 #ifdef CONFIG_AP
2395 		if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2396 			hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2397 					       data->deauth_info.addr);
2398 			break;
2399 		}
2400 		if (wpa_s->ap_iface) {
2401 			wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2402 				"AP mode");
2403 			break;
2404 		}
2405 #endif /* CONFIG_AP */
2406 		wpa_supplicant_event_disassoc(wpa_s, reason_code,
2407 					      locally_generated);
2408 		if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2409 		    ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2410 		      (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2411 		     eapol_sm_failed(wpa_s->eapol)))
2412 			wpas_auth_failed(wpa_s);
2413 #ifdef CONFIG_P2P
2414 		if (event == EVENT_DEAUTH && data) {
2415 			if (wpas_p2p_deauth_notif(wpa_s,
2416 						  data->deauth_info.addr,
2417 						  reason_code,
2418 						  data->deauth_info.ie,
2419 						  data->deauth_info.ie_len,
2420 						  locally_generated) > 0) {
2421 				/*
2422 				 * The interface was removed, so cannot
2423 				 * continue processing any additional
2424 				 * operations after this.
2425 				 */
2426 				break;
2427 			}
2428 		}
2429 #endif /* CONFIG_P2P */
2430 		wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2431 						     locally_generated);
2432 		break;
2433 	case EVENT_MICHAEL_MIC_FAILURE:
2434 		wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2435 		break;
2436 #ifndef CONFIG_NO_SCAN_PROCESSING
2437 	case EVENT_SCAN_RESULTS:
2438 		wpa_supplicant_event_scan_results(wpa_s, data);
2439 #ifdef CONFIG_P2P
2440 	if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
2441 	    wpa_s->global->p2p != NULL &&
2442 	    wpa_s->wpa_state != WPA_AUTHENTICATING &&
2443 	    wpa_s->wpa_state != WPA_ASSOCIATING) {
2444 		wpa_s->global->p2p_cb_on_scan_complete = 0;
2445 		if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
2446 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
2447 				"continued after scan result processing");
2448 		}
2449 	}
2450 #endif /* CONFIG_P2P */
2451 		break;
2452 #endif /* CONFIG_NO_SCAN_PROCESSING */
2453 	case EVENT_ASSOCINFO:
2454 		wpa_supplicant_event_associnfo(wpa_s, data);
2455 		break;
2456 	case EVENT_INTERFACE_STATUS:
2457 		wpa_supplicant_event_interface_status(wpa_s, data);
2458 		break;
2459 	case EVENT_PMKID_CANDIDATE:
2460 		wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2461 		break;
2462 #ifdef CONFIG_PEERKEY
2463 	case EVENT_STKSTART:
2464 		wpa_supplicant_event_stkstart(wpa_s, data);
2465 		break;
2466 #endif /* CONFIG_PEERKEY */
2467 #ifdef CONFIG_TDLS
2468 	case EVENT_TDLS:
2469 		wpa_supplicant_event_tdls(wpa_s, data);
2470 		break;
2471 #endif /* CONFIG_TDLS */
2472 #ifdef CONFIG_WNM
2473 	case EVENT_WNM:
2474 		wpa_supplicant_event_wnm(wpa_s, data);
2475 		break;
2476 #endif /* CONFIG_WNM */
2477 #ifdef CONFIG_IEEE80211R
2478 	case EVENT_FT_RESPONSE:
2479 		wpa_supplicant_event_ft_response(wpa_s, data);
2480 		break;
2481 #endif /* CONFIG_IEEE80211R */
2482 #ifdef CONFIG_IBSS_RSN
2483 	case EVENT_IBSS_RSN_START:
2484 		wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2485 		break;
2486 #endif /* CONFIG_IBSS_RSN */
2487 	case EVENT_ASSOC_REJECT:
2488 		if (data->assoc_reject.bssid)
2489 			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2490 				"bssid=" MACSTR	" status_code=%u",
2491 				MAC2STR(data->assoc_reject.bssid),
2492 				data->assoc_reject.status_code);
2493 		else
2494 			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2495 				"status_code=%u",
2496 				data->assoc_reject.status_code);
2497 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2498 			sme_event_assoc_reject(wpa_s, data);
2499 		else {
2500 			const u8 *bssid = data->assoc_reject.bssid;
2501 			if (bssid == NULL || is_zero_ether_addr(bssid))
2502 				bssid = wpa_s->pending_bssid;
2503 			wpas_connection_failed(wpa_s, bssid);
2504 			wpa_supplicant_mark_disassoc(wpa_s);
2505 		}
2506 		break;
2507 	case EVENT_AUTH_TIMED_OUT:
2508 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2509 			sme_event_auth_timed_out(wpa_s, data);
2510 		break;
2511 	case EVENT_ASSOC_TIMED_OUT:
2512 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2513 			sme_event_assoc_timed_out(wpa_s, data);
2514 		break;
2515 	case EVENT_TX_STATUS:
2516 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2517 			" type=%d stype=%d",
2518 			MAC2STR(data->tx_status.dst),
2519 			data->tx_status.type, data->tx_status.stype);
2520 #ifdef CONFIG_AP
2521 		if (wpa_s->ap_iface == NULL) {
2522 #ifdef CONFIG_OFFCHANNEL
2523 			if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2524 			    data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2525 				offchannel_send_action_tx_status(
2526 					wpa_s, data->tx_status.dst,
2527 					data->tx_status.data,
2528 					data->tx_status.data_len,
2529 					data->tx_status.ack ?
2530 					OFFCHANNEL_SEND_ACTION_SUCCESS :
2531 					OFFCHANNEL_SEND_ACTION_NO_ACK);
2532 #endif /* CONFIG_OFFCHANNEL */
2533 			break;
2534 		}
2535 #endif /* CONFIG_AP */
2536 #ifdef CONFIG_OFFCHANNEL
2537 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2538 			MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2539 		/*
2540 		 * Catch TX status events for Action frames we sent via group
2541 		 * interface in GO mode.
2542 		 */
2543 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2544 		    data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2545 		    os_memcmp(wpa_s->parent->pending_action_dst,
2546 			      data->tx_status.dst, ETH_ALEN) == 0) {
2547 			offchannel_send_action_tx_status(
2548 				wpa_s->parent, data->tx_status.dst,
2549 				data->tx_status.data,
2550 				data->tx_status.data_len,
2551 				data->tx_status.ack ?
2552 				OFFCHANNEL_SEND_ACTION_SUCCESS :
2553 				OFFCHANNEL_SEND_ACTION_NO_ACK);
2554 			break;
2555 		}
2556 #endif /* CONFIG_OFFCHANNEL */
2557 #ifdef CONFIG_AP
2558 		switch (data->tx_status.type) {
2559 		case WLAN_FC_TYPE_MGMT:
2560 			ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2561 				      data->tx_status.data_len,
2562 				      data->tx_status.stype,
2563 				      data->tx_status.ack);
2564 			break;
2565 		case WLAN_FC_TYPE_DATA:
2566 			ap_tx_status(wpa_s, data->tx_status.dst,
2567 				     data->tx_status.data,
2568 				     data->tx_status.data_len,
2569 				     data->tx_status.ack);
2570 			break;
2571 		}
2572 #endif /* CONFIG_AP */
2573 		break;
2574 #ifdef CONFIG_AP
2575 	case EVENT_EAPOL_TX_STATUS:
2576 		ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2577 				   data->eapol_tx_status.data,
2578 				   data->eapol_tx_status.data_len,
2579 				   data->eapol_tx_status.ack);
2580 		break;
2581 	case EVENT_DRIVER_CLIENT_POLL_OK:
2582 		ap_client_poll_ok(wpa_s, data->client_poll.addr);
2583 		break;
2584 	case EVENT_RX_FROM_UNKNOWN:
2585 		if (wpa_s->ap_iface == NULL)
2586 			break;
2587 		ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2588 				       data->rx_from_unknown.wds);
2589 		break;
2590 	case EVENT_CH_SWITCH:
2591 		if (!data)
2592 			break;
2593 		if (!wpa_s->ap_iface) {
2594 			wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2595 				"event in non-AP mode");
2596 			break;
2597 		}
2598 
2599 #ifdef CONFIG_AP
2600 		wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2601 				  data->ch_switch.ht_enabled,
2602 				  data->ch_switch.ch_offset);
2603 #endif /* CONFIG_AP */
2604 		break;
2605 	case EVENT_RX_MGMT: {
2606 		u16 fc, stype;
2607 		const struct ieee80211_mgmt *mgmt;
2608 
2609 		mgmt = (const struct ieee80211_mgmt *)
2610 			data->rx_mgmt.frame;
2611 		fc = le_to_host16(mgmt->frame_control);
2612 		stype = WLAN_FC_GET_STYPE(fc);
2613 
2614 		if (wpa_s->ap_iface == NULL) {
2615 #ifdef CONFIG_P2P
2616 			if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2617 			    data->rx_mgmt.frame_len > 24) {
2618 				const u8 *src = mgmt->sa;
2619 				const u8 *ie = mgmt->u.probe_req.variable;
2620 				size_t ie_len = data->rx_mgmt.frame_len -
2621 					(mgmt->u.probe_req.variable -
2622 					 data->rx_mgmt.frame);
2623 				wpas_p2p_probe_req_rx(
2624 					wpa_s, src, mgmt->da,
2625 					mgmt->bssid, ie, ie_len,
2626 					data->rx_mgmt.ssi_signal);
2627 				break;
2628 			}
2629 #endif /* CONFIG_P2P */
2630 			wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2631 				"management frame in non-AP mode");
2632 			break;
2633 		}
2634 
2635 		if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2636 		    data->rx_mgmt.frame_len > 24) {
2637 			const u8 *ie = mgmt->u.probe_req.variable;
2638 			size_t ie_len = data->rx_mgmt.frame_len -
2639 				(mgmt->u.probe_req.variable -
2640 				 data->rx_mgmt.frame);
2641 
2642 			wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2643 					 mgmt->bssid, ie, ie_len,
2644 					 data->rx_mgmt.ssi_signal);
2645 		}
2646 
2647 		ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2648 		break;
2649 		}
2650 #endif /* CONFIG_AP */
2651 	case EVENT_RX_ACTION:
2652 		wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2653 			" Category=%u DataLen=%d freq=%d MHz",
2654 			MAC2STR(data->rx_action.sa),
2655 			data->rx_action.category, (int) data->rx_action.len,
2656 			data->rx_action.freq);
2657 #ifdef CONFIG_IEEE80211R
2658 		if (data->rx_action.category == WLAN_ACTION_FT) {
2659 			ft_rx_action(wpa_s, data->rx_action.data,
2660 				     data->rx_action.len);
2661 			break;
2662 		}
2663 #endif /* CONFIG_IEEE80211R */
2664 #ifdef CONFIG_IEEE80211W
2665 #ifdef CONFIG_SME
2666 		if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2667 			sme_sa_query_rx(wpa_s, data->rx_action.sa,
2668 					data->rx_action.data,
2669 					data->rx_action.len);
2670 			break;
2671 		}
2672 #endif /* CONFIG_SME */
2673 #endif /* CONFIG_IEEE80211W */
2674 #ifdef CONFIG_WNM
2675 		if (data->rx_action.category == WLAN_ACTION_WNM) {
2676 			ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2677 			break;
2678 		}
2679 #endif /* CONFIG_WNM */
2680 #ifdef CONFIG_GAS
2681 		if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2682 		    gas_query_rx(wpa_s->gas, data->rx_action.da,
2683 				 data->rx_action.sa, data->rx_action.bssid,
2684 				 data->rx_action.data, data->rx_action.len,
2685 				 data->rx_action.freq) == 0)
2686 			break;
2687 #endif /* CONFIG_GAS */
2688 #ifdef CONFIG_TDLS
2689 		if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2690 		    data->rx_action.len >= 4 &&
2691 		    data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2692 			wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2693 				"Response from " MACSTR,
2694 				MAC2STR(data->rx_action.sa));
2695 			break;
2696 		}
2697 #endif /* CONFIG_TDLS */
2698 #ifdef CONFIG_P2P
2699 		wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2700 				   data->rx_action.sa,
2701 				   data->rx_action.bssid,
2702 				   data->rx_action.category,
2703 				   data->rx_action.data,
2704 				   data->rx_action.len, data->rx_action.freq);
2705 #endif /* CONFIG_P2P */
2706 		break;
2707 	case EVENT_RX_PROBE_REQ:
2708 		if (data->rx_probe_req.sa == NULL ||
2709 		    data->rx_probe_req.ie == NULL)
2710 			break;
2711 #ifdef CONFIG_AP
2712 		if (wpa_s->ap_iface) {
2713 			hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2714 					     data->rx_probe_req.sa,
2715 					     data->rx_probe_req.da,
2716 					     data->rx_probe_req.bssid,
2717 					     data->rx_probe_req.ie,
2718 					     data->rx_probe_req.ie_len,
2719 					     data->rx_probe_req.ssi_signal);
2720 			break;
2721 		}
2722 #endif /* CONFIG_AP */
2723 #ifdef CONFIG_P2P
2724 		wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2725 				      data->rx_probe_req.da,
2726 				      data->rx_probe_req.bssid,
2727 				      data->rx_probe_req.ie,
2728 				      data->rx_probe_req.ie_len,
2729 				      data->rx_probe_req.ssi_signal);
2730 #endif /* CONFIG_P2P */
2731 		break;
2732 	case EVENT_REMAIN_ON_CHANNEL:
2733 #ifdef CONFIG_OFFCHANNEL
2734 		offchannel_remain_on_channel_cb(
2735 			wpa_s, data->remain_on_channel.freq,
2736 			data->remain_on_channel.duration);
2737 #endif /* CONFIG_OFFCHANNEL */
2738 #ifdef CONFIG_P2P
2739 		wpas_p2p_remain_on_channel_cb(
2740 			wpa_s, data->remain_on_channel.freq,
2741 			data->remain_on_channel.duration);
2742 #endif /* CONFIG_P2P */
2743 		break;
2744 	case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2745 #ifdef CONFIG_OFFCHANNEL
2746 		offchannel_cancel_remain_on_channel_cb(
2747 			wpa_s, data->remain_on_channel.freq);
2748 #endif /* CONFIG_OFFCHANNEL */
2749 #ifdef CONFIG_P2P
2750 		wpas_p2p_cancel_remain_on_channel_cb(
2751 			wpa_s, data->remain_on_channel.freq);
2752 #endif /* CONFIG_P2P */
2753 		break;
2754 #ifdef CONFIG_P2P
2755 	case EVENT_P2P_DEV_FOUND: {
2756 		struct p2p_peer_info peer_info;
2757 
2758 		os_memset(&peer_info, 0, sizeof(peer_info));
2759 		if (data->p2p_dev_found.dev_addr)
2760 			os_memcpy(peer_info.p2p_device_addr,
2761 				  data->p2p_dev_found.dev_addr, ETH_ALEN);
2762 		if (data->p2p_dev_found.pri_dev_type)
2763 			os_memcpy(peer_info.pri_dev_type,
2764 				  data->p2p_dev_found.pri_dev_type,
2765 				  sizeof(peer_info.pri_dev_type));
2766 		if (data->p2p_dev_found.dev_name)
2767 			os_strlcpy(peer_info.device_name,
2768 				   data->p2p_dev_found.dev_name,
2769 				   sizeof(peer_info.device_name));
2770 		peer_info.config_methods = data->p2p_dev_found.config_methods;
2771 		peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2772 		peer_info.group_capab = data->p2p_dev_found.group_capab;
2773 
2774 		/*
2775 		 * FIX: new_device=1 is not necessarily correct. We should
2776 		 * maintain a P2P peer database in wpa_supplicant and update
2777 		 * this information based on whether the peer is truly new.
2778 		 */
2779 		wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2780 		break;
2781 		}
2782 	case EVENT_P2P_GO_NEG_REQ_RX:
2783 		wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2784 				   data->p2p_go_neg_req_rx.dev_passwd_id);
2785 		break;
2786 	case EVENT_P2P_GO_NEG_COMPLETED:
2787 		wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2788 		break;
2789 	case EVENT_P2P_PROV_DISC_REQUEST:
2790 		wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2791 				   data->p2p_prov_disc_req.config_methods,
2792 				   data->p2p_prov_disc_req.dev_addr,
2793 				   data->p2p_prov_disc_req.pri_dev_type,
2794 				   data->p2p_prov_disc_req.dev_name,
2795 				   data->p2p_prov_disc_req.supp_config_methods,
2796 				   data->p2p_prov_disc_req.dev_capab,
2797 				   data->p2p_prov_disc_req.group_capab,
2798 				   NULL, 0);
2799 		break;
2800 	case EVENT_P2P_PROV_DISC_RESPONSE:
2801 		wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2802 				    data->p2p_prov_disc_resp.config_methods);
2803 		break;
2804 	case EVENT_P2P_SD_REQUEST:
2805 		wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2806 				data->p2p_sd_req.sa,
2807 				data->p2p_sd_req.dialog_token,
2808 				data->p2p_sd_req.update_indic,
2809 				data->p2p_sd_req.tlvs,
2810 				data->p2p_sd_req.tlvs_len);
2811 		break;
2812 	case EVENT_P2P_SD_RESPONSE:
2813 		wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2814 				 data->p2p_sd_resp.update_indic,
2815 				 data->p2p_sd_resp.tlvs,
2816 				 data->p2p_sd_resp.tlvs_len);
2817 		break;
2818 #endif /* CONFIG_P2P */
2819 	case EVENT_EAPOL_RX:
2820 		wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2821 					data->eapol_rx.data,
2822 					data->eapol_rx.data_len);
2823 		break;
2824 	case EVENT_SIGNAL_CHANGE:
2825 		bgscan_notify_signal_change(
2826 			wpa_s, data->signal_change.above_threshold,
2827 			data->signal_change.current_signal,
2828 			data->signal_change.current_noise,
2829 			data->signal_change.current_txrate);
2830 		break;
2831 	case EVENT_INTERFACE_ENABLED:
2832 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2833 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2834 			wpa_supplicant_update_mac_addr(wpa_s);
2835 #ifdef CONFIG_AP
2836 			if (!wpa_s->ap_iface) {
2837 				wpa_supplicant_set_state(wpa_s,
2838 							 WPA_DISCONNECTED);
2839 				wpa_supplicant_req_scan(wpa_s, 0, 0);
2840 			} else
2841 				wpa_supplicant_set_state(wpa_s,
2842 							 WPA_COMPLETED);
2843 #else /* CONFIG_AP */
2844 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2845 			wpa_supplicant_req_scan(wpa_s, 0, 0);
2846 #endif /* CONFIG_AP */
2847 		}
2848 		break;
2849 	case EVENT_INTERFACE_DISABLED:
2850 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2851 		wpa_supplicant_mark_disassoc(wpa_s);
2852 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2853 		break;
2854 	case EVENT_CHANNEL_LIST_CHANGED:
2855 		if (wpa_s->drv_priv == NULL)
2856 			break; /* Ignore event during drv initialization */
2857 
2858 		free_hw_features(wpa_s);
2859 		wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2860 			wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2861 
2862 #ifdef CONFIG_P2P
2863 		wpas_p2p_update_channel_list(wpa_s);
2864 #endif /* CONFIG_P2P */
2865 		break;
2866 	case EVENT_INTERFACE_UNAVAILABLE:
2867 #ifdef CONFIG_P2P
2868 		wpas_p2p_interface_unavailable(wpa_s);
2869 #endif /* CONFIG_P2P */
2870 		break;
2871 	case EVENT_BEST_CHANNEL:
2872 		wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2873 			"(%d %d %d)",
2874 			data->best_chan.freq_24, data->best_chan.freq_5,
2875 			data->best_chan.freq_overall);
2876 		wpa_s->best_24_freq = data->best_chan.freq_24;
2877 		wpa_s->best_5_freq = data->best_chan.freq_5;
2878 		wpa_s->best_overall_freq = data->best_chan.freq_overall;
2879 #ifdef CONFIG_P2P
2880 		wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2881 					      data->best_chan.freq_5,
2882 					      data->best_chan.freq_overall);
2883 #endif /* CONFIG_P2P */
2884 		break;
2885 	case EVENT_UNPROT_DEAUTH:
2886 		wpa_supplicant_event_unprot_deauth(wpa_s,
2887 						   &data->unprot_deauth);
2888 		break;
2889 	case EVENT_UNPROT_DISASSOC:
2890 		wpa_supplicant_event_unprot_disassoc(wpa_s,
2891 						     &data->unprot_disassoc);
2892 		break;
2893 	case EVENT_STATION_LOW_ACK:
2894 #ifdef CONFIG_AP
2895 		if (wpa_s->ap_iface && data)
2896 			hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2897 						  data->low_ack.addr);
2898 #endif /* CONFIG_AP */
2899 #ifdef CONFIG_TDLS
2900 		if (data)
2901 			wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2902 #endif /* CONFIG_TDLS */
2903 		break;
2904 	case EVENT_IBSS_PEER_LOST:
2905 #ifdef CONFIG_IBSS_RSN
2906 		ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2907 #endif /* CONFIG_IBSS_RSN */
2908 		break;
2909 	case EVENT_DRIVER_GTK_REKEY:
2910 		if (os_memcmp(data->driver_gtk_rekey.bssid,
2911 			      wpa_s->bssid, ETH_ALEN))
2912 			break;
2913 		if (!wpa_s->wpa)
2914 			break;
2915 		wpa_sm_update_replay_ctr(wpa_s->wpa,
2916 					 data->driver_gtk_rekey.replay_ctr);
2917 		break;
2918 	case EVENT_SCHED_SCAN_STOPPED:
2919 		wpa_s->sched_scanning = 0;
2920 		wpa_supplicant_notify_scanning(wpa_s, 0);
2921 
2922 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2923 			break;
2924 
2925 		/*
2926 		 * If we timed out, start a new sched scan to continue
2927 		 * searching for more SSIDs.
2928 		 */
2929 		if (wpa_s->sched_scan_timed_out)
2930 			wpa_supplicant_req_sched_scan(wpa_s);
2931 		break;
2932 	case EVENT_WPS_BUTTON_PUSHED:
2933 #ifdef CONFIG_WPS
2934 		wpas_wps_start_pbc(wpa_s, NULL, 0);
2935 #endif /* CONFIG_WPS */
2936 		break;
2937 	default:
2938 		wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2939 		break;
2940 	}
2941 }
2942