events.c revision 1f69aa52ea2e0a73ac502565df8c666ee49cab6a
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. */
999static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1000					      union wpa_event_data *data)
1001{
1002	struct wpa_bss *selected;
1003	struct wpa_ssid *ssid = NULL;
1004	struct wpa_scan_results *scan_res;
1005	int ap = 0;
1006
1007#ifdef CONFIG_AP
1008	if (wpa_s->ap_iface)
1009		ap = 1;
1010#endif /* CONFIG_AP */
1011
1012	wpa_supplicant_notify_scanning(wpa_s, 0);
1013
1014#ifdef CONFIG_P2P
1015	if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
1016	    wpa_s->global->p2p != NULL) {
1017		wpa_s->p2p_cb_on_scan_complete = 0;
1018		if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1019			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1020				"stopped scan processing");
1021			return -1;
1022		}
1023	}
1024#endif /* CONFIG_P2P */
1025
1026	scan_res = wpa_supplicant_get_scan_results(wpa_s,
1027						   data ? &data->scan_info :
1028						   NULL, 1);
1029	if (scan_res == NULL) {
1030		if (wpa_s->conf->ap_scan == 2 || ap)
1031			return -1;
1032		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1033			"scanning again");
1034		wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1035		return -1;
1036	}
1037
1038#ifndef CONFIG_NO_RANDOM_POOL
1039	size_t i, num;
1040	num = scan_res->num;
1041	if (num > 10)
1042		num = 10;
1043	for (i = 0; i < num; i++) {
1044		u8 buf[5];
1045		struct wpa_scan_res *res = scan_res->res[i];
1046		buf[0] = res->bssid[5];
1047		buf[1] = res->qual & 0xff;
1048		buf[2] = res->noise & 0xff;
1049		buf[3] = res->level & 0xff;
1050		buf[4] = res->tsf & 0xff;
1051		random_add_randomness(buf, sizeof(buf));
1052	}
1053#endif /* CONFIG_NO_RANDOM_POOL */
1054
1055	if (wpa_s->scan_res_handler) {
1056		void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1057					 struct wpa_scan_results *scan_res);
1058
1059		scan_res_handler = wpa_s->scan_res_handler;
1060		wpa_s->scan_res_handler = NULL;
1061		scan_res_handler(wpa_s, scan_res);
1062
1063		wpa_scan_results_free(scan_res);
1064		return 0;
1065	}
1066
1067	if (ap) {
1068		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1069#ifdef CONFIG_AP
1070		if (wpa_s->ap_iface->scan_cb)
1071			wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1072#endif /* CONFIG_AP */
1073		wpa_scan_results_free(scan_res);
1074		return 0;
1075	}
1076
1077	wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1078	wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1079	wpas_notify_scan_results(wpa_s);
1080
1081	wpas_notify_scan_done(wpa_s, 1);
1082
1083	if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1084		wpa_scan_results_free(scan_res);
1085		return 0;
1086	}
1087
1088	if (wpa_s->disconnected) {
1089		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1090		wpa_scan_results_free(scan_res);
1091		return 0;
1092	}
1093
1094	if (!wpas_driver_bss_selection(wpa_s) &&
1095	    bgscan_notify_scan(wpa_s, scan_res) == 1) {
1096		wpa_scan_results_free(scan_res);
1097		return 0;
1098	}
1099
1100	selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
1101
1102	if (selected) {
1103		int skip;
1104		skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
1105						    scan_res);
1106		wpa_scan_results_free(scan_res);
1107		if (skip) {
1108			wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1109			return 0;
1110		}
1111
1112		if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1113			wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1114			return -1;
1115		}
1116		wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1117	} else {
1118		wpa_scan_results_free(scan_res);
1119		wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1120		ssid = wpa_supplicant_pick_new_network(wpa_s);
1121		if (ssid) {
1122			wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1123			wpa_supplicant_associate(wpa_s, NULL, ssid);
1124			wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1125		} else {
1126			int timeout_sec = wpa_s->scan_interval;
1127			int timeout_usec = 0;
1128#ifdef CONFIG_P2P
1129			if (wpa_s->p2p_in_provisioning) {
1130				/*
1131				 * Use shorter wait during P2P Provisioning
1132				 * state to speed up group formation.
1133				 */
1134				timeout_sec = 0;
1135				timeout_usec = 250000;
1136				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1137							    timeout_usec);
1138				return 0;
1139			}
1140#endif /* CONFIG_P2P */
1141			if (wpa_supplicant_req_sched_scan(wpa_s))
1142				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1143							    timeout_usec);
1144		}
1145	}
1146	return 0;
1147}
1148
1149
1150static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1151					      union wpa_event_data *data)
1152{
1153	const char *rn, *rn2;
1154	struct wpa_supplicant *ifs;
1155
1156	if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
1157		/*
1158		 * If no scan results could be fetched, then no need to
1159		 * notify those interfaces that did not actually request
1160		 * this scan.
1161		 */
1162		return;
1163	}
1164
1165	/*
1166	 * Check other interfaces to see if they have the same radio-name. If
1167	 * so, they get updated with this same scan info.
1168	 */
1169	if (!wpa_s->driver->get_radio_name)
1170		return;
1171
1172	rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1173	if (rn == NULL || rn[0] == '\0')
1174		return;
1175
1176	wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1177		"sharing same radio (%s) in event_scan_results", rn);
1178
1179	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1180		if (ifs == wpa_s || !ifs->driver->get_radio_name)
1181			continue;
1182
1183		rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1184		if (rn2 && os_strcmp(rn, rn2) == 0) {
1185			wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1186				   "sibling", ifs->ifname);
1187			_wpa_supplicant_event_scan_results(ifs, data);
1188		}
1189	}
1190}
1191
1192#endif /* CONFIG_NO_SCAN_PROCESSING */
1193
1194
1195static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1196					  union wpa_event_data *data)
1197{
1198	int l, len, found = 0, wpa_found, rsn_found;
1199	const u8 *p;
1200
1201	wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1202	if (data->assoc_info.req_ies)
1203		wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1204			    data->assoc_info.req_ies_len);
1205	if (data->assoc_info.resp_ies) {
1206		wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1207			    data->assoc_info.resp_ies_len);
1208#ifdef CONFIG_TDLS
1209		wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1210					data->assoc_info.resp_ies_len);
1211#endif /* CONFIG_TDLS */
1212	}
1213	if (data->assoc_info.beacon_ies)
1214		wpa_hexdump(MSG_DEBUG, "beacon_ies",
1215			    data->assoc_info.beacon_ies,
1216			    data->assoc_info.beacon_ies_len);
1217	if (data->assoc_info.freq)
1218		wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1219			data->assoc_info.freq);
1220
1221	p = data->assoc_info.req_ies;
1222	l = data->assoc_info.req_ies_len;
1223
1224	/* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1225	while (p && l >= 2) {
1226		len = p[1] + 2;
1227		if (len > l) {
1228			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1229				    p, l);
1230			break;
1231		}
1232		if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1233		     (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1234		    (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1235			if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1236				break;
1237			found = 1;
1238			wpa_find_assoc_pmkid(wpa_s);
1239			break;
1240		}
1241		l -= len;
1242		p += len;
1243	}
1244	if (!found && data->assoc_info.req_ies)
1245		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1246
1247#ifdef CONFIG_IEEE80211R
1248#ifdef CONFIG_SME
1249	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1250		u8 bssid[ETH_ALEN];
1251		if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1252		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1253						 data->assoc_info.resp_ies,
1254						 data->assoc_info.resp_ies_len,
1255						 bssid) < 0) {
1256			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1257				"Reassociation Response failed");
1258			wpa_supplicant_deauthenticate(
1259				wpa_s, WLAN_REASON_INVALID_IE);
1260			return -1;
1261		}
1262	}
1263
1264	p = data->assoc_info.resp_ies;
1265	l = data->assoc_info.resp_ies_len;
1266
1267#ifdef CONFIG_WPS_STRICT
1268	if (p && wpa_s->current_ssid &&
1269	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1270		struct wpabuf *wps;
1271		wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1272		if (wps == NULL) {
1273			wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1274				"include WPS IE in (Re)Association Response");
1275			return -1;
1276		}
1277
1278		if (wps_validate_assoc_resp(wps) < 0) {
1279			wpabuf_free(wps);
1280			wpa_supplicant_deauthenticate(
1281				wpa_s, WLAN_REASON_INVALID_IE);
1282			return -1;
1283		}
1284		wpabuf_free(wps);
1285	}
1286#endif /* CONFIG_WPS_STRICT */
1287
1288	/* Go through the IEs and make a copy of the MDIE, if present. */
1289	while (p && l >= 2) {
1290		len = p[1] + 2;
1291		if (len > l) {
1292			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1293				    p, l);
1294			break;
1295		}
1296		if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1297		    p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1298			wpa_s->sme.ft_used = 1;
1299			os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1300				  MOBILITY_DOMAIN_ID_LEN);
1301			break;
1302		}
1303		l -= len;
1304		p += len;
1305	}
1306#endif /* CONFIG_SME */
1307
1308	wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1309			     data->assoc_info.resp_ies_len);
1310#endif /* CONFIG_IEEE80211R */
1311
1312	/* WPA/RSN IE from Beacon/ProbeResp */
1313	p = data->assoc_info.beacon_ies;
1314	l = data->assoc_info.beacon_ies_len;
1315
1316	/* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1317	 */
1318	wpa_found = rsn_found = 0;
1319	while (p && l >= 2) {
1320		len = p[1] + 2;
1321		if (len > l) {
1322			wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1323				    p, l);
1324			break;
1325		}
1326		if (!wpa_found &&
1327		    p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1328		    os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1329			wpa_found = 1;
1330			wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1331		}
1332
1333		if (!rsn_found &&
1334		    p[0] == WLAN_EID_RSN && p[1] >= 2) {
1335			rsn_found = 1;
1336			wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1337		}
1338
1339		l -= len;
1340		p += len;
1341	}
1342
1343	if (!wpa_found && data->assoc_info.beacon_ies)
1344		wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1345	if (!rsn_found && data->assoc_info.beacon_ies)
1346		wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1347	if (wpa_found || rsn_found)
1348		wpa_s->ap_ies_from_associnfo = 1;
1349
1350	if (wpa_s->assoc_freq && data->assoc_info.freq &&
1351	    wpa_s->assoc_freq != data->assoc_info.freq) {
1352		wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1353			   "%u to %u MHz",
1354			   wpa_s->assoc_freq, data->assoc_info.freq);
1355		wpa_supplicant_update_scan_results(wpa_s);
1356	}
1357
1358	wpa_s->assoc_freq = data->assoc_info.freq;
1359
1360	return 0;
1361}
1362
1363
1364static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1365				       union wpa_event_data *data)
1366{
1367	u8 bssid[ETH_ALEN];
1368	int ft_completed;
1369	int bssid_changed;
1370	struct wpa_driver_capa capa;
1371
1372#ifdef CONFIG_AP
1373	if (wpa_s->ap_iface) {
1374		hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1375				    data->assoc_info.addr,
1376				    data->assoc_info.req_ies,
1377				    data->assoc_info.req_ies_len,
1378				    data->assoc_info.reassoc);
1379		return;
1380	}
1381#endif /* CONFIG_AP */
1382
1383	ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1384	if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1385		return;
1386
1387	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1388	if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1389	    os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1390		wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1391			MACSTR, MAC2STR(bssid));
1392		random_add_randomness(bssid, ETH_ALEN);
1393		bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1394		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1395		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1396		if (bssid_changed)
1397			wpas_notify_bssid_changed(wpa_s);
1398
1399		if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1400			wpa_clear_keys(wpa_s, bssid);
1401		}
1402		if (wpa_supplicant_select_config(wpa_s) < 0) {
1403			wpa_supplicant_disassociate(
1404				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1405			return;
1406		}
1407		if (wpa_s->current_ssid) {
1408			struct wpa_bss *bss = NULL;
1409			struct wpa_ssid *ssid = wpa_s->current_ssid;
1410			if (ssid->ssid_len > 0)
1411				bss = wpa_bss_get(wpa_s, bssid,
1412						  ssid->ssid, ssid->ssid_len);
1413			if (!bss)
1414				bss = wpa_bss_get_bssid(wpa_s, bssid);
1415			if (bss)
1416				wpa_s->current_bss = bss;
1417		}
1418	}
1419
1420#ifdef CONFIG_SME
1421	os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1422	wpa_s->sme.prev_bssid_set = 1;
1423#endif /* CONFIG_SME */
1424
1425	wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1426	if (wpa_s->current_ssid) {
1427		/* When using scanning (ap_scan=1), SIM PC/SC interface can be
1428		 * initialized before association, but for other modes,
1429		 * initialize PC/SC here, if the current configuration needs
1430		 * smartcard or SIM/USIM. */
1431		wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1432	}
1433	wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1434	if (wpa_s->l2)
1435		l2_packet_notify_auth_start(wpa_s->l2);
1436
1437	/*
1438	 * Set portEnabled first to FALSE in order to get EAP state machine out
1439	 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1440	 * state machine may transit to AUTHENTICATING state based on obsolete
1441	 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1442	 * AUTHENTICATED without ever giving chance to EAP state machine to
1443	 * reset the state.
1444	 */
1445	if (!ft_completed) {
1446		eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1447		eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1448	}
1449	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1450		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1451	/* 802.1X::portControl = Auto */
1452	eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1453	wpa_s->eapol_received = 0;
1454	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1455	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1456	    (wpa_s->current_ssid &&
1457	     wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1458		wpa_supplicant_cancel_auth_timeout(wpa_s);
1459		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1460	} else if (!ft_completed) {
1461		/* Timeout for receiving the first EAPOL packet */
1462		wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1463	}
1464	wpa_supplicant_cancel_scan(wpa_s);
1465
1466	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1467	    wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1468		/*
1469		 * We are done; the driver will take care of RSN 4-way
1470		 * handshake.
1471		 */
1472		wpa_supplicant_cancel_auth_timeout(wpa_s);
1473		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1474		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1475		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1476	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1477		   wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1478		/*
1479		 * The driver will take care of RSN 4-way handshake, so we need
1480		 * to allow EAPOL supplicant to complete its work without
1481		 * waiting for WPA supplicant.
1482		 */
1483		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1484	} else if (ft_completed) {
1485		/*
1486		 * FT protocol completed - make sure EAPOL state machine ends
1487		 * up in authenticated.
1488		 */
1489		wpa_supplicant_cancel_auth_timeout(wpa_s);
1490		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1491		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1492		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1493	}
1494
1495	if (wpa_s->pending_eapol_rx) {
1496		struct os_time now, age;
1497		os_get_time(&now);
1498		os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1499		if (age.sec == 0 && age.usec < 100000 &&
1500		    os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1501		    0) {
1502			wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1503				"frame that was received just before "
1504				"association notification");
1505			wpa_supplicant_rx_eapol(
1506				wpa_s, wpa_s->pending_eapol_rx_src,
1507				wpabuf_head(wpa_s->pending_eapol_rx),
1508				wpabuf_len(wpa_s->pending_eapol_rx));
1509		}
1510		wpabuf_free(wpa_s->pending_eapol_rx);
1511		wpa_s->pending_eapol_rx = NULL;
1512	}
1513
1514	if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1515	     wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1516	    wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1517	    capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1518		/* Set static WEP keys again */
1519		wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1520	}
1521
1522#ifdef CONFIG_IBSS_RSN
1523	if (wpa_s->current_ssid &&
1524	    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1525	    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1526	    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1527	    wpa_s->ibss_rsn == NULL) {
1528		wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1529		if (!wpa_s->ibss_rsn) {
1530			wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1531			wpa_supplicant_deauthenticate(
1532				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1533			return;
1534		}
1535
1536		ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1537	}
1538#endif /* CONFIG_IBSS_RSN */
1539}
1540
1541
1542static int disconnect_reason_recoverable(u16 reason_code)
1543{
1544	return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1545		reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1546		reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1547}
1548
1549
1550static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1551					  u16 reason_code)
1552{
1553	const u8 *bssid;
1554	int authenticating;
1555	u8 prev_pending_bssid[ETH_ALEN];
1556	struct wpa_bss *fast_reconnect = NULL;
1557	struct wpa_ssid *fast_reconnect_ssid = NULL;
1558
1559	authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1560	os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1561
1562	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1563		/*
1564		 * At least Host AP driver and a Prism3 card seemed to be
1565		 * generating streams of disconnected events when configuring
1566		 * IBSS for WPA-None. Ignore them for now.
1567		 */
1568		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1569			"IBSS/WPA-None mode");
1570		return;
1571	}
1572
1573	if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1574	    wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1575		wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1576			"pre-shared key may be incorrect");
1577	}
1578	if (!wpa_s->auto_reconnect_disabled ||
1579	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1580		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1581			"reconnect (wps=%d wpa_state=%d)",
1582			wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1583			wpa_s->wpa_state);
1584		if (wpa_s->wpa_state == WPA_COMPLETED &&
1585		    wpa_s->current_ssid &&
1586		    wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1587		    disconnect_reason_recoverable(reason_code)) {
1588			/*
1589			 * It looks like the AP has dropped association with
1590			 * us, but could allow us to get back in. Try to
1591			 * reconnect to the same BSS without full scan to save
1592			 * time for some common cases.
1593			 */
1594			fast_reconnect = wpa_s->current_bss;
1595			fast_reconnect_ssid = wpa_s->current_ssid;
1596		} else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1597			wpa_supplicant_req_scan(wpa_s, 0, 500000);
1598		else
1599			wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1600				"immediate scan");
1601	} else {
1602		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
1603			"try to re-connect");
1604		wpa_s->reassociate = 0;
1605		wpa_s->disconnected = 1;
1606	}
1607	bssid = wpa_s->bssid;
1608	if (is_zero_ether_addr(bssid))
1609		bssid = wpa_s->pending_bssid;
1610	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1611		wpas_connection_failed(wpa_s, bssid);
1612	wpa_sm_notify_disassoc(wpa_s->wpa);
1613	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1614		" reason=%d",
1615		MAC2STR(bssid), reason_code);
1616	if (wpa_supplicant_dynamic_keys(wpa_s)) {
1617		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1618		wpa_s->keys_cleared = 0;
1619		wpa_clear_keys(wpa_s, wpa_s->bssid);
1620	}
1621	wpa_supplicant_mark_disassoc(wpa_s);
1622
1623	if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1624		sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1625
1626	if (fast_reconnect) {
1627#ifndef CONFIG_NO_SCAN_PROCESSING
1628		wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1629		if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1630					   fast_reconnect_ssid) < 0) {
1631			/* Recover through full scan */
1632			wpa_supplicant_req_scan(wpa_s, 0, 100000);
1633		}
1634#endif /* CONFIG_NO_SCAN_PROCESSING */
1635	}
1636}
1637
1638
1639#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1640void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
1641{
1642	struct wpa_supplicant *wpa_s = eloop_ctx;
1643
1644	if (!wpa_s->pending_mic_error_report)
1645		return;
1646
1647	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1648	wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1649	wpa_s->pending_mic_error_report = 0;
1650}
1651#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1652
1653
1654static void
1655wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1656					 union wpa_event_data *data)
1657{
1658	int pairwise;
1659	struct os_time t;
1660
1661	wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1662	pairwise = (data && data->michael_mic_failure.unicast);
1663	os_get_time(&t);
1664	if ((wpa_s->last_michael_mic_error &&
1665	     t.sec - wpa_s->last_michael_mic_error <= 60) ||
1666	    wpa_s->pending_mic_error_report) {
1667		if (wpa_s->pending_mic_error_report) {
1668			/*
1669			 * Send the pending MIC error report immediately since
1670			 * we are going to start countermeasures and AP better
1671			 * do the same.
1672			 */
1673			wpa_sm_key_request(wpa_s->wpa, 1,
1674					   wpa_s->pending_mic_error_pairwise);
1675		}
1676
1677		/* Send the new MIC error report immediately since we are going
1678		 * to start countermeasures and AP better do the same.
1679		 */
1680		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1681
1682		/* initialize countermeasures */
1683		wpa_s->countermeasures = 1;
1684
1685		wpa_blacklist_add(wpa_s, wpa_s->bssid);
1686
1687		wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1688
1689		/*
1690		 * Need to wait for completion of request frame. We do not get
1691		 * any callback for the message completion, so just wait a
1692		 * short while and hope for the best. */
1693		os_sleep(0, 10000);
1694
1695		wpa_drv_set_countermeasures(wpa_s, 1);
1696		wpa_supplicant_deauthenticate(wpa_s,
1697					      WLAN_REASON_MICHAEL_MIC_FAILURE);
1698		eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1699				     wpa_s, NULL);
1700		eloop_register_timeout(60, 0,
1701				       wpa_supplicant_stop_countermeasures,
1702				       wpa_s, NULL);
1703		/* TODO: mark the AP rejected for 60 second. STA is
1704		 * allowed to associate with another AP.. */
1705	} else {
1706#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1707		if (wpa_s->mic_errors_seen) {
1708			/*
1709			 * Reduce the effectiveness of Michael MIC error
1710			 * reports as a means for attacking against TKIP if
1711			 * more than one MIC failure is noticed with the same
1712			 * PTK. We delay the transmission of the reports by a
1713			 * random time between 0 and 60 seconds in order to
1714			 * force the attacker wait 60 seconds before getting
1715			 * the information on whether a frame resulted in a MIC
1716			 * failure.
1717			 */
1718			u8 rval[4];
1719			int sec;
1720
1721			if (os_get_random(rval, sizeof(rval)) < 0)
1722				sec = os_random() % 60;
1723			else
1724				sec = WPA_GET_BE32(rval) % 60;
1725			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1726				"report %d seconds", sec);
1727			wpa_s->pending_mic_error_report = 1;
1728			wpa_s->pending_mic_error_pairwise = pairwise;
1729			eloop_cancel_timeout(
1730				wpa_supplicant_delayed_mic_error_report,
1731				wpa_s, NULL);
1732			eloop_register_timeout(
1733				sec, os_random() % 1000000,
1734				wpa_supplicant_delayed_mic_error_report,
1735				wpa_s, NULL);
1736		} else {
1737			wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1738		}
1739#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1740		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1741#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1742	}
1743	wpa_s->last_michael_mic_error = t.sec;
1744	wpa_s->mic_errors_seen++;
1745}
1746
1747
1748#ifdef CONFIG_TERMINATE_ONLASTIF
1749static int any_interfaces(struct wpa_supplicant *head)
1750{
1751	struct wpa_supplicant *wpa_s;
1752
1753	for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1754		if (!wpa_s->interface_removed)
1755			return 1;
1756	return 0;
1757}
1758#endif /* CONFIG_TERMINATE_ONLASTIF */
1759
1760
1761static void
1762wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1763				      union wpa_event_data *data)
1764{
1765	if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1766		return;
1767
1768	switch (data->interface_status.ievent) {
1769	case EVENT_INTERFACE_ADDED:
1770		if (!wpa_s->interface_removed)
1771			break;
1772		wpa_s->interface_removed = 0;
1773		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1774		if (wpa_supplicant_driver_init(wpa_s) < 0) {
1775			wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1776				"driver after interface was added");
1777		}
1778		break;
1779	case EVENT_INTERFACE_REMOVED:
1780		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1781		wpa_s->interface_removed = 1;
1782		wpa_supplicant_mark_disassoc(wpa_s);
1783		l2_packet_deinit(wpa_s->l2);
1784		wpa_s->l2 = NULL;
1785#ifdef CONFIG_IBSS_RSN
1786		ibss_rsn_deinit(wpa_s->ibss_rsn);
1787		wpa_s->ibss_rsn = NULL;
1788#endif /* CONFIG_IBSS_RSN */
1789#ifdef CONFIG_TERMINATE_ONLASTIF
1790		/* check if last interface */
1791		if (!any_interfaces(wpa_s->global->ifaces))
1792			eloop_terminate();
1793#endif /* CONFIG_TERMINATE_ONLASTIF */
1794		break;
1795	}
1796}
1797
1798
1799#ifdef CONFIG_PEERKEY
1800static void
1801wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1802			      union wpa_event_data *data)
1803{
1804	if (data == NULL)
1805		return;
1806	wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1807}
1808#endif /* CONFIG_PEERKEY */
1809
1810
1811#ifdef CONFIG_TDLS
1812static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1813				      union wpa_event_data *data)
1814{
1815	if (data == NULL)
1816		return;
1817	switch (data->tdls.oper) {
1818	case TDLS_REQUEST_SETUP:
1819		wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1820		break;
1821	case TDLS_REQUEST_TEARDOWN:
1822		wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
1823				       data->tdls.reason_code);
1824		break;
1825	}
1826}
1827#endif /* CONFIG_TDLS */
1828
1829
1830#ifdef CONFIG_IEEE80211R
1831static void
1832wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1833				 union wpa_event_data *data)
1834{
1835	if (data == NULL)
1836		return;
1837
1838	if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1839				    data->ft_ies.ies_len,
1840				    data->ft_ies.ft_action,
1841				    data->ft_ies.target_ap,
1842				    data->ft_ies.ric_ies,
1843				    data->ft_ies.ric_ies_len) < 0) {
1844		/* TODO: prevent MLME/driver from trying to associate? */
1845	}
1846}
1847#endif /* CONFIG_IEEE80211R */
1848
1849
1850#ifdef CONFIG_IBSS_RSN
1851static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1852						union wpa_event_data *data)
1853{
1854	struct wpa_ssid *ssid;
1855	if (wpa_s->wpa_state < WPA_ASSOCIATED)
1856		return;
1857	if (data == NULL)
1858		return;
1859	ssid = wpa_s->current_ssid;
1860	if (ssid == NULL)
1861		return;
1862	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
1863		return;
1864
1865	ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1866}
1867#endif /* CONFIG_IBSS_RSN */
1868
1869
1870#ifdef CONFIG_IEEE80211R
1871static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1872			 size_t len)
1873{
1874	const u8 *sta_addr, *target_ap_addr;
1875	u16 status;
1876
1877	wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1878	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1879		return; /* only SME case supported for now */
1880	if (len < 1 + 2 * ETH_ALEN + 2)
1881		return;
1882	if (data[0] != 2)
1883		return; /* Only FT Action Response is supported for now */
1884	sta_addr = data + 1;
1885	target_ap_addr = data + 1 + ETH_ALEN;
1886	status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1887	wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
1888		MACSTR " TargetAP " MACSTR " status %u",
1889		MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1890
1891	if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1892		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1893			" in FT Action Response", MAC2STR(sta_addr));
1894		return;
1895	}
1896
1897	if (status) {
1898		wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
1899			"failure (status code %d)", status);
1900		/* TODO: report error to FT code(?) */
1901		return;
1902	}
1903
1904	if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1905				    len - (1 + 2 * ETH_ALEN + 2), 1,
1906				    target_ap_addr, NULL, 0) < 0)
1907		return;
1908
1909#ifdef CONFIG_SME
1910	{
1911		struct wpa_bss *bss;
1912		bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1913		if (bss)
1914			wpa_s->sme.freq = bss->freq;
1915		wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1916		sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1917			      WLAN_AUTH_FT);
1918	}
1919#endif /* CONFIG_SME */
1920}
1921#endif /* CONFIG_IEEE80211R */
1922
1923
1924static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
1925					       struct unprot_deauth *e)
1926{
1927#ifdef CONFIG_IEEE80211W
1928	wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
1929		   "dropped: " MACSTR " -> " MACSTR
1930		   " (reason code %u)",
1931		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1932	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1933#endif /* CONFIG_IEEE80211W */
1934}
1935
1936
1937static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
1938						 struct unprot_disassoc *e)
1939{
1940#ifdef CONFIG_IEEE80211W
1941	wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
1942		   "dropped: " MACSTR " -> " MACSTR
1943		   " (reason code %u)",
1944		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1945	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1946#endif /* CONFIG_IEEE80211W */
1947}
1948
1949
1950static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
1951{
1952	u8 action, mode;
1953	const u8 *pos, *end;
1954
1955	if (rx->data == NULL || rx->len == 0)
1956		return;
1957
1958	pos = rx->data;
1959	end = pos + rx->len;
1960	action = *pos++;
1961
1962	wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
1963		   action, MAC2STR(rx->sa));
1964	switch (action) {
1965	case WNM_BSS_TRANS_MGMT_REQ:
1966		if (pos + 5 > end)
1967			break;
1968		wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
1969			   "Request: dialog_token=%u request_mode=0x%x "
1970			   "disassoc_timer=%u validity_interval=%u",
1971			   pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
1972		mode = pos[1];
1973		pos += 5;
1974		if (mode & 0x08)
1975			pos += 12; /* BSS Termination Duration */
1976		if (mode & 0x10) {
1977			char url[256];
1978			if (pos + 1 > end || pos + 1 + pos[0] > end) {
1979				wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
1980					   "Transition Management Request "
1981					   "(URL)");
1982				break;
1983			}
1984			os_memcpy(url, pos + 1, pos[0]);
1985			url[pos[0]] = '\0';
1986			wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
1987				"Imminent - session_info_url=%s", url);
1988		}
1989		break;
1990	}
1991}
1992
1993
1994void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1995			  union wpa_event_data *data)
1996{
1997	struct wpa_supplicant *wpa_s = ctx;
1998	u16 reason_code = 0;
1999
2000	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2001	    event != EVENT_INTERFACE_ENABLED &&
2002	    event != EVENT_INTERFACE_STATUS &&
2003	    event != EVENT_SCHED_SCAN_STOPPED) {
2004		wpa_dbg(wpa_s, MSG_DEBUG,
2005			"Ignore event %s (%d) while interface is disabled",
2006			event_to_string(event), event);
2007		return;
2008	}
2009
2010#ifndef CONFIG_NO_STDOUT_DEBUG
2011{
2012	int level = MSG_DEBUG;
2013
2014	if (event == EVENT_RX_MGMT && data && data->rx_mgmt.frame &&
2015	    data->rx_mgmt.frame_len >= 24) {
2016		const struct ieee80211_hdr *hdr;
2017		u16 fc;
2018		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2019		fc = le_to_host16(hdr->frame_control);
2020		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2021		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2022			level = MSG_EXCESSIVE;
2023	}
2024
2025	wpa_dbg(wpa_s, level, "Event %s (%d) received",
2026		event_to_string(event), event);
2027}
2028#endif /* CONFIG_NO_STDOUT_DEBUG */
2029
2030	switch (event) {
2031	case EVENT_AUTH:
2032		sme_event_auth(wpa_s, data);
2033		break;
2034	case EVENT_ASSOC:
2035		wpa_supplicant_event_assoc(wpa_s, data);
2036		break;
2037	case EVENT_DISASSOC:
2038		wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2039		if (data) {
2040			wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
2041				data->disassoc_info.reason_code);
2042			if (data->disassoc_info.addr)
2043				wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2044					MAC2STR(data->disassoc_info.addr));
2045		}
2046#ifdef CONFIG_AP
2047		if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2048			hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2049					       data->disassoc_info.addr);
2050			break;
2051		}
2052		if (wpa_s->ap_iface) {
2053			wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2054				"AP mode");
2055			break;
2056		}
2057#endif /* CONFIG_AP */
2058		if (data) {
2059			reason_code = data->disassoc_info.reason_code;
2060			wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2061				    data->disassoc_info.ie,
2062				    data->disassoc_info.ie_len);
2063#ifdef CONFIG_P2P
2064			wpas_p2p_disassoc_notif(
2065				wpa_s, data->disassoc_info.addr, reason_code,
2066				data->disassoc_info.ie,
2067				data->disassoc_info.ie_len);
2068#endif /* CONFIG_P2P */
2069		}
2070		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2071			sme_event_disassoc(wpa_s, data);
2072		/* fall through */
2073	case EVENT_DEAUTH:
2074		if (event == EVENT_DEAUTH) {
2075			wpa_dbg(wpa_s, MSG_DEBUG,
2076				"Deauthentication notification");
2077			if (data) {
2078				reason_code = data->deauth_info.reason_code;
2079				wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
2080					data->deauth_info.reason_code);
2081				if (data->deauth_info.addr) {
2082					wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2083						MACSTR,
2084						MAC2STR(data->deauth_info.
2085							addr));
2086				}
2087				wpa_hexdump(MSG_DEBUG,
2088					    "Deauthentication frame IE(s)",
2089					    data->deauth_info.ie,
2090					    data->deauth_info.ie_len);
2091#ifdef CONFIG_P2P
2092				wpas_p2p_deauth_notif(
2093					wpa_s, data->deauth_info.addr,
2094					reason_code,
2095					data->deauth_info.ie,
2096					data->deauth_info.ie_len);
2097#endif /* CONFIG_P2P */
2098			}
2099		}
2100#ifdef CONFIG_AP
2101		if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2102			hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2103					       data->deauth_info.addr);
2104			break;
2105		}
2106		if (wpa_s->ap_iface) {
2107			wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2108				"AP mode");
2109			break;
2110		}
2111#endif /* CONFIG_AP */
2112		wpa_supplicant_event_disassoc(wpa_s, reason_code);
2113		break;
2114	case EVENT_MICHAEL_MIC_FAILURE:
2115		wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2116		break;
2117#ifndef CONFIG_NO_SCAN_PROCESSING
2118	case EVENT_SCAN_RESULTS:
2119		wpa_supplicant_event_scan_results(wpa_s, data);
2120		break;
2121#endif /* CONFIG_NO_SCAN_PROCESSING */
2122	case EVENT_ASSOCINFO:
2123		wpa_supplicant_event_associnfo(wpa_s, data);
2124		break;
2125	case EVENT_INTERFACE_STATUS:
2126		wpa_supplicant_event_interface_status(wpa_s, data);
2127		break;
2128	case EVENT_PMKID_CANDIDATE:
2129		wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2130		break;
2131#ifdef CONFIG_PEERKEY
2132	case EVENT_STKSTART:
2133		wpa_supplicant_event_stkstart(wpa_s, data);
2134		break;
2135#endif /* CONFIG_PEERKEY */
2136#ifdef CONFIG_TDLS
2137	case EVENT_TDLS:
2138		wpa_supplicant_event_tdls(wpa_s, data);
2139		break;
2140#endif /* CONFIG_TDLS */
2141#ifdef CONFIG_IEEE80211R
2142	case EVENT_FT_RESPONSE:
2143		wpa_supplicant_event_ft_response(wpa_s, data);
2144		break;
2145#endif /* CONFIG_IEEE80211R */
2146#ifdef CONFIG_IBSS_RSN
2147	case EVENT_IBSS_RSN_START:
2148		wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2149		break;
2150#endif /* CONFIG_IBSS_RSN */
2151	case EVENT_ASSOC_REJECT:
2152		if (data->assoc_reject.bssid)
2153			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2154				"bssid=" MACSTR	" status_code=%u",
2155				MAC2STR(data->assoc_reject.bssid),
2156				data->assoc_reject.status_code);
2157		else
2158			wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2159				"status_code=%u",
2160				data->assoc_reject.status_code);
2161		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2162			sme_event_assoc_reject(wpa_s, data);
2163		break;
2164	case EVENT_AUTH_TIMED_OUT:
2165		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2166			sme_event_auth_timed_out(wpa_s, data);
2167		break;
2168	case EVENT_ASSOC_TIMED_OUT:
2169		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2170			sme_event_assoc_timed_out(wpa_s, data);
2171		break;
2172	case EVENT_TX_STATUS:
2173		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2174			" type=%d stype=%d",
2175			MAC2STR(data->tx_status.dst),
2176			data->tx_status.type, data->tx_status.stype);
2177#ifdef CONFIG_AP
2178		if (wpa_s->ap_iface == NULL) {
2179#ifdef CONFIG_OFFCHANNEL
2180			if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2181			    data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2182				offchannel_send_action_tx_status(
2183					wpa_s, data->tx_status.dst,
2184					data->tx_status.data,
2185					data->tx_status.data_len,
2186					data->tx_status.ack ?
2187					OFFCHANNEL_SEND_ACTION_SUCCESS :
2188					OFFCHANNEL_SEND_ACTION_NO_ACK);
2189#endif /* CONFIG_OFFCHANNEL */
2190			break;
2191		}
2192#endif /* CONFIG_AP */
2193#ifdef CONFIG_OFFCHANNEL
2194		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2195			MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2196		/*
2197		 * Catch TX status events for Action frames we sent via group
2198		 * interface in GO mode.
2199		 */
2200		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2201		    data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2202		    os_memcmp(wpa_s->parent->pending_action_dst,
2203			      data->tx_status.dst, ETH_ALEN) == 0) {
2204			offchannel_send_action_tx_status(
2205				wpa_s->parent, data->tx_status.dst,
2206				data->tx_status.data,
2207				data->tx_status.data_len,
2208				data->tx_status.ack ?
2209				OFFCHANNEL_SEND_ACTION_SUCCESS :
2210				OFFCHANNEL_SEND_ACTION_NO_ACK);
2211			break;
2212		}
2213#endif /* CONFIG_OFFCHANNEL */
2214#ifdef CONFIG_AP
2215		switch (data->tx_status.type) {
2216		case WLAN_FC_TYPE_MGMT:
2217			ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2218				      data->tx_status.data_len,
2219				      data->tx_status.stype,
2220				      data->tx_status.ack);
2221			break;
2222		case WLAN_FC_TYPE_DATA:
2223			ap_tx_status(wpa_s, data->tx_status.dst,
2224				     data->tx_status.data,
2225				     data->tx_status.data_len,
2226				     data->tx_status.ack);
2227			break;
2228		}
2229#endif /* CONFIG_AP */
2230		break;
2231#ifdef CONFIG_AP
2232	case EVENT_EAPOL_TX_STATUS:
2233		ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2234				   data->eapol_tx_status.data,
2235				   data->eapol_tx_status.data_len,
2236				   data->eapol_tx_status.ack);
2237		break;
2238	case EVENT_DRIVER_CLIENT_POLL_OK:
2239		ap_client_poll_ok(wpa_s, data->client_poll.addr);
2240		break;
2241	case EVENT_RX_FROM_UNKNOWN:
2242		if (wpa_s->ap_iface == NULL)
2243			break;
2244		ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2245				       data->rx_from_unknown.wds);
2246		break;
2247	case EVENT_RX_MGMT:
2248		if (wpa_s->ap_iface == NULL) {
2249#ifdef CONFIG_P2P
2250			u16 fc, stype;
2251			const struct ieee80211_mgmt *mgmt;
2252			mgmt = (const struct ieee80211_mgmt *)
2253				data->rx_mgmt.frame;
2254			fc = le_to_host16(mgmt->frame_control);
2255			stype = WLAN_FC_GET_STYPE(fc);
2256			if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2257			    data->rx_mgmt.frame_len > 24) {
2258				const u8 *src = mgmt->sa;
2259				const u8 *ie = mgmt->u.probe_req.variable;
2260				size_t ie_len = data->rx_mgmt.frame_len -
2261					(mgmt->u.probe_req.variable -
2262					 data->rx_mgmt.frame);
2263				wpas_p2p_probe_req_rx(wpa_s, src, mgmt->da,
2264						      mgmt->bssid, ie, ie_len);
2265				break;
2266			}
2267#endif /* CONFIG_P2P */
2268			wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2269				"management frame in non-AP mode");
2270			break;
2271		}
2272		ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2273		break;
2274#endif /* CONFIG_AP */
2275	case EVENT_RX_ACTION:
2276		wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2277			" Category=%u DataLen=%d freq=%d MHz",
2278			MAC2STR(data->rx_action.sa),
2279			data->rx_action.category, (int) data->rx_action.len,
2280			data->rx_action.freq);
2281#ifdef CONFIG_IEEE80211R
2282		if (data->rx_action.category == WLAN_ACTION_FT) {
2283			ft_rx_action(wpa_s, data->rx_action.data,
2284				     data->rx_action.len);
2285			break;
2286		}
2287#endif /* CONFIG_IEEE80211R */
2288#ifdef CONFIG_IEEE80211W
2289#ifdef CONFIG_SME
2290		if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2291			sme_sa_query_rx(wpa_s, data->rx_action.sa,
2292					data->rx_action.data,
2293					data->rx_action.len);
2294			break;
2295		}
2296#endif /* CONFIG_SME */
2297#endif /* CONFIG_IEEE80211W */
2298#ifdef CONFIG_GAS
2299		if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2300		    gas_query_rx(wpa_s->gas, data->rx_action.da,
2301				 data->rx_action.sa, data->rx_action.bssid,
2302				 data->rx_action.data, data->rx_action.len,
2303				 data->rx_action.freq) == 0)
2304			break;
2305#endif /* CONFIG_GAS */
2306		if (data->rx_action.category == WLAN_ACTION_WNM) {
2307			wnm_action_rx(wpa_s, &data->rx_action);
2308			break;
2309		}
2310#ifdef CONFIG_TDLS
2311		if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2312		    data->rx_action.len >= 4 &&
2313		    data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2314			wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2315				"Response from " MACSTR,
2316				MAC2STR(data->rx_action.sa));
2317			break;
2318		}
2319#endif /* CONFIG_TDLS */
2320#ifdef CONFIG_P2P
2321		wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2322				   data->rx_action.sa,
2323				   data->rx_action.bssid,
2324				   data->rx_action.category,
2325				   data->rx_action.data,
2326				   data->rx_action.len, data->rx_action.freq);
2327#endif /* CONFIG_P2P */
2328		break;
2329	case EVENT_RX_PROBE_REQ:
2330		if (data->rx_probe_req.sa == NULL ||
2331		    data->rx_probe_req.ie == NULL)
2332			break;
2333#ifdef CONFIG_AP
2334		if (wpa_s->ap_iface) {
2335			hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2336					     data->rx_probe_req.sa,
2337					     data->rx_probe_req.da,
2338					     data->rx_probe_req.bssid,
2339					     data->rx_probe_req.ie,
2340					     data->rx_probe_req.ie_len);
2341			break;
2342		}
2343#endif /* CONFIG_AP */
2344#ifdef CONFIG_P2P
2345		wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2346				      data->rx_probe_req.da,
2347				      data->rx_probe_req.bssid,
2348				      data->rx_probe_req.ie,
2349				      data->rx_probe_req.ie_len);
2350#endif /* CONFIG_P2P */
2351		break;
2352	case EVENT_REMAIN_ON_CHANNEL:
2353#ifdef CONFIG_OFFCHANNEL
2354		offchannel_remain_on_channel_cb(
2355			wpa_s, data->remain_on_channel.freq,
2356			data->remain_on_channel.duration);
2357#endif /* CONFIG_OFFCHANNEL */
2358#ifdef CONFIG_P2P
2359		wpas_p2p_remain_on_channel_cb(
2360			wpa_s, data->remain_on_channel.freq,
2361			data->remain_on_channel.duration);
2362#endif /* CONFIG_P2P */
2363		break;
2364	case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2365#ifdef CONFIG_OFFCHANNEL
2366		offchannel_cancel_remain_on_channel_cb(
2367			wpa_s, data->remain_on_channel.freq);
2368#endif /* CONFIG_OFFCHANNEL */
2369#ifdef CONFIG_P2P
2370		wpas_p2p_cancel_remain_on_channel_cb(
2371			wpa_s, data->remain_on_channel.freq);
2372#endif /* CONFIG_P2P */
2373		break;
2374#ifdef CONFIG_P2P
2375	case EVENT_P2P_DEV_FOUND: {
2376		struct p2p_peer_info peer_info;
2377
2378		os_memset(&peer_info, 0, sizeof(peer_info));
2379		if (data->p2p_dev_found.dev_addr)
2380			os_memcpy(peer_info.p2p_device_addr,
2381				  data->p2p_dev_found.dev_addr, ETH_ALEN);
2382		if (data->p2p_dev_found.pri_dev_type)
2383			os_memcpy(peer_info.pri_dev_type,
2384				  data->p2p_dev_found.pri_dev_type,
2385				  sizeof(peer_info.pri_dev_type));
2386		if (data->p2p_dev_found.dev_name)
2387			os_strlcpy(peer_info.device_name,
2388				   data->p2p_dev_found.dev_name,
2389				   sizeof(peer_info.device_name));
2390		peer_info.config_methods = data->p2p_dev_found.config_methods;
2391		peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2392		peer_info.group_capab = data->p2p_dev_found.group_capab;
2393
2394		/*
2395		 * FIX: new_device=1 is not necessarily correct. We should
2396		 * maintain a P2P peer database in wpa_supplicant and update
2397		 * this information based on whether the peer is truly new.
2398		 */
2399		wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2400		break;
2401		}
2402	case EVENT_P2P_GO_NEG_REQ_RX:
2403		wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2404				   data->p2p_go_neg_req_rx.dev_passwd_id);
2405		break;
2406	case EVENT_P2P_GO_NEG_COMPLETED:
2407		wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2408		break;
2409	case EVENT_P2P_PROV_DISC_REQUEST:
2410		wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2411				   data->p2p_prov_disc_req.config_methods,
2412				   data->p2p_prov_disc_req.dev_addr,
2413				   data->p2p_prov_disc_req.pri_dev_type,
2414				   data->p2p_prov_disc_req.dev_name,
2415				   data->p2p_prov_disc_req.supp_config_methods,
2416				   data->p2p_prov_disc_req.dev_capab,
2417				   data->p2p_prov_disc_req.group_capab,
2418				   NULL, 0);
2419		break;
2420	case EVENT_P2P_PROV_DISC_RESPONSE:
2421		wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2422				    data->p2p_prov_disc_resp.config_methods);
2423		break;
2424	case EVENT_P2P_SD_REQUEST:
2425		wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2426				data->p2p_sd_req.sa,
2427				data->p2p_sd_req.dialog_token,
2428				data->p2p_sd_req.update_indic,
2429				data->p2p_sd_req.tlvs,
2430				data->p2p_sd_req.tlvs_len);
2431		break;
2432	case EVENT_P2P_SD_RESPONSE:
2433		wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2434				 data->p2p_sd_resp.update_indic,
2435				 data->p2p_sd_resp.tlvs,
2436				 data->p2p_sd_resp.tlvs_len);
2437		break;
2438#endif /* CONFIG_P2P */
2439	case EVENT_EAPOL_RX:
2440		wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2441					data->eapol_rx.data,
2442					data->eapol_rx.data_len);
2443		break;
2444	case EVENT_SIGNAL_CHANGE:
2445		bgscan_notify_signal_change(
2446			wpa_s, data->signal_change.above_threshold,
2447			data->signal_change.current_signal,
2448			data->signal_change.current_noise,
2449			data->signal_change.current_txrate);
2450		break;
2451	case EVENT_INTERFACE_ENABLED:
2452		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2453		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2454			wpa_supplicant_update_mac_addr(wpa_s);
2455#ifdef CONFIG_AP
2456			if (!wpa_s->ap_iface) {
2457				wpa_supplicant_set_state(wpa_s,
2458							 WPA_DISCONNECTED);
2459				wpa_supplicant_req_scan(wpa_s, 0, 0);
2460			} else
2461				wpa_supplicant_set_state(wpa_s,
2462							 WPA_COMPLETED);
2463#else /* CONFIG_AP */
2464			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2465			wpa_supplicant_req_scan(wpa_s, 0, 0);
2466#endif /* CONFIG_AP */
2467		}
2468		break;
2469	case EVENT_INTERFACE_DISABLED:
2470		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2471		wpa_supplicant_mark_disassoc(wpa_s);
2472		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2473		break;
2474	case EVENT_CHANNEL_LIST_CHANGED:
2475		if (wpa_s->drv_priv == NULL)
2476			break; /* Ignore event during drv initialization */
2477#ifdef CONFIG_P2P
2478		wpas_p2p_update_channel_list(wpa_s);
2479#endif /* CONFIG_P2P */
2480		break;
2481	case EVENT_INTERFACE_UNAVAILABLE:
2482#ifdef CONFIG_P2P
2483		wpas_p2p_interface_unavailable(wpa_s);
2484#endif /* CONFIG_P2P */
2485		break;
2486	case EVENT_BEST_CHANNEL:
2487		wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2488			"(%d %d %d)",
2489			data->best_chan.freq_24, data->best_chan.freq_5,
2490			data->best_chan.freq_overall);
2491		wpa_s->best_24_freq = data->best_chan.freq_24;
2492		wpa_s->best_5_freq = data->best_chan.freq_5;
2493		wpa_s->best_overall_freq = data->best_chan.freq_overall;
2494#ifdef CONFIG_P2P
2495		wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2496					      data->best_chan.freq_5,
2497					      data->best_chan.freq_overall);
2498#endif /* CONFIG_P2P */
2499		break;
2500	case EVENT_UNPROT_DEAUTH:
2501		wpa_supplicant_event_unprot_deauth(wpa_s,
2502						   &data->unprot_deauth);
2503		break;
2504	case EVENT_UNPROT_DISASSOC:
2505		wpa_supplicant_event_unprot_disassoc(wpa_s,
2506						     &data->unprot_disassoc);
2507		break;
2508	case EVENT_STATION_LOW_ACK:
2509#ifdef CONFIG_AP
2510		if (wpa_s->ap_iface && data)
2511			hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2512						  data->low_ack.addr);
2513#endif /* CONFIG_AP */
2514#ifdef CONFIG_TDLS
2515		if (data)
2516			wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2517#endif /* CONFIG_TDLS */
2518		break;
2519	case EVENT_IBSS_PEER_LOST:
2520#ifdef CONFIG_IBSS_RSN
2521		ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2522#endif /* CONFIG_IBSS_RSN */
2523		break;
2524	case EVENT_DRIVER_GTK_REKEY:
2525		if (os_memcmp(data->driver_gtk_rekey.bssid,
2526			      wpa_s->bssid, ETH_ALEN))
2527			break;
2528		if (!wpa_s->wpa)
2529			break;
2530		wpa_sm_update_replay_ctr(wpa_s->wpa,
2531					 data->driver_gtk_rekey.replay_ctr);
2532		break;
2533	case EVENT_SCHED_SCAN_STOPPED:
2534		wpa_s->sched_scanning = 0;
2535		wpa_supplicant_notify_scanning(wpa_s, 0);
2536
2537		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2538			break;
2539
2540		/*
2541		 * If we timed out, start a new sched scan to continue
2542		 * searching for more SSIDs.
2543		 */
2544		if (wpa_s->sched_scan_timed_out)
2545			wpa_supplicant_req_sched_scan(wpa_s);
2546		break;
2547	case EVENT_WPS_BUTTON_PUSHED:
2548#ifdef CONFIG_WPS
2549		wpas_wps_start_pbc(wpa_s, NULL, 0);
2550#endif /* CONFIG_WPS */
2551		break;
2552	default:
2553		wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2554		break;
2555	}
2556}
2557