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