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