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