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