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