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