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