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