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