events.c revision 1.1
1/*
2 * WPA Supplicant - Driver event processing
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "eapol_supp/eapol_supp_sm.h"
19#include "rsn_supp/wpa.h"
20#include "eloop.h"
21#include "config.h"
22#include "l2_packet/l2_packet.h"
23#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "pcsc_funcs.h"
26#include "rsn_supp/preauth.h"
27#include "rsn_supp/pmksa_cache.h"
28#include "common/wpa_ctrl.h"
29#include "eap_peer/eap.h"
30#include "ap/hostapd.h"
31#include "notify.h"
32#include "common/ieee802_11_defs.h"
33#include "blacklist.h"
34#include "wpas_glue.h"
35#include "wps_supplicant.h"
36#include "ibss_rsn.h"
37#include "sme.h"
38#include "bgscan.h"
39#include "ap.h"
40#include "bss.h"
41#include "mlme.h"
42#include "scan.h"
43
44
45static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
46{
47	struct wpa_ssid *ssid, *old_ssid;
48
49	if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
50		return 0;
51
52	wpa_printf(MSG_DEBUG, "Select network based on association "
53		   "information");
54	ssid = wpa_supplicant_get_ssid(wpa_s);
55	if (ssid == NULL) {
56		wpa_printf(MSG_INFO, "No network configuration found for the "
57			   "current AP");
58		return -1;
59	}
60
61	if (ssid->disabled) {
62		wpa_printf(MSG_DEBUG, "Selected network is disabled");
63		return -1;
64	}
65
66	wpa_printf(MSG_DEBUG, "Network configuration found for the current "
67		   "AP");
68	if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
69			      WPA_KEY_MGMT_WPA_NONE |
70			      WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
71			      WPA_KEY_MGMT_PSK_SHA256 |
72			      WPA_KEY_MGMT_IEEE8021X_SHA256)) {
73		u8 wpa_ie[80];
74		size_t wpa_ie_len = sizeof(wpa_ie);
75		wpa_supplicant_set_suites(wpa_s, NULL, ssid,
76					  wpa_ie, &wpa_ie_len);
77	} else {
78		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
79	}
80
81	if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
82		eapol_sm_invalidate_cached_session(wpa_s->eapol);
83	old_ssid = wpa_s->current_ssid;
84	wpa_s->current_ssid = ssid;
85	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
86	wpa_supplicant_initiate_eapol(wpa_s);
87	if (old_ssid != wpa_s->current_ssid)
88		wpas_notify_network_changed(wpa_s);
89
90	return 0;
91}
92
93
94static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
95						void *sock_ctx)
96{
97	struct wpa_supplicant *wpa_s = eloop_ctx;
98
99	if (wpa_s->countermeasures) {
100		wpa_s->countermeasures = 0;
101		wpa_drv_set_countermeasures(wpa_s, 0);
102		wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
103		wpa_supplicant_req_scan(wpa_s, 0, 0);
104	}
105}
106
107
108void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
109{
110	int bssid_changed;
111
112	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
113	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
114	os_memset(wpa_s->bssid, 0, ETH_ALEN);
115	os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
116	wpa_s->current_bss = NULL;
117	if (bssid_changed)
118		wpas_notify_bssid_changed(wpa_s);
119
120	eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
121	eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
122	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
123		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
124	wpa_s->ap_ies_from_associnfo = 0;
125}
126
127
128static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
129{
130	struct wpa_ie_data ie;
131	int pmksa_set = -1;
132	size_t i;
133
134	if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
135	    ie.pmkid == NULL)
136		return;
137
138	for (i = 0; i < ie.num_pmkid; i++) {
139		pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
140						    ie.pmkid + i * PMKID_LEN,
141						    NULL, NULL, 0);
142		if (pmksa_set == 0) {
143			eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
144			break;
145		}
146	}
147
148	wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
149		   "cache", pmksa_set == 0 ? "" : "not ");
150}
151
152
153static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
154						 union wpa_event_data *data)
155{
156	if (data == NULL) {
157		wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
158		return;
159	}
160	wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
161		   " index=%d preauth=%d",
162		   MAC2STR(data->pmkid_candidate.bssid),
163		   data->pmkid_candidate.index,
164		   data->pmkid_candidate.preauth);
165
166	pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
167			    data->pmkid_candidate.index,
168			    data->pmkid_candidate.preauth);
169}
170
171
172static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
173{
174	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
175	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
176		return 0;
177
178#ifdef IEEE8021X_EAPOL
179	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
180	    wpa_s->current_ssid &&
181	    !(wpa_s->current_ssid->eapol_flags &
182	      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
183	       EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
184		/* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
185		 * plaintext or static WEP keys). */
186		return 0;
187	}
188#endif /* IEEE8021X_EAPOL */
189
190	return 1;
191}
192
193
194/**
195 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
196 * @wpa_s: pointer to wpa_supplicant data
197 * @ssid: Configuration data for the network
198 * Returns: 0 on success, -1 on failure
199 *
200 * This function is called when starting authentication with a network that is
201 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
202 */
203int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
204			      struct wpa_ssid *ssid)
205{
206#ifdef IEEE8021X_EAPOL
207	int aka = 0, sim = 0, type;
208
209	if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
210		return 0;
211
212	if (ssid->eap.eap_methods == NULL) {
213		sim = 1;
214		aka = 1;
215	} else {
216		struct eap_method_type *eap = ssid->eap.eap_methods;
217		while (eap->vendor != EAP_VENDOR_IETF ||
218		       eap->method != EAP_TYPE_NONE) {
219			if (eap->vendor == EAP_VENDOR_IETF) {
220				if (eap->method == EAP_TYPE_SIM)
221					sim = 1;
222				else if (eap->method == EAP_TYPE_AKA)
223					aka = 1;
224			}
225			eap++;
226		}
227	}
228
229	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
230		sim = 0;
231	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
232		aka = 0;
233
234	if (!sim && !aka) {
235		wpa_printf(MSG_DEBUG, "Selected network is configured to use "
236			   "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
237		return 0;
238	}
239
240	wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
241		   "(sim=%d aka=%d) - initialize PCSC", sim, aka);
242	if (sim && aka)
243		type = SCARD_TRY_BOTH;
244	else if (aka)
245		type = SCARD_USIM_ONLY;
246	else
247		type = SCARD_GSM_SIM_ONLY;
248
249	wpa_s->scard = scard_init(type);
250	if (wpa_s->scard == NULL) {
251		wpa_printf(MSG_WARNING, "Failed to initialize SIM "
252			   "(pcsc-lite)");
253		return -1;
254	}
255	wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
256	eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
257#endif /* IEEE8021X_EAPOL */
258
259	return 0;
260}
261
262
263#ifndef CONFIG_NO_SCAN_PROCESSING
264static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
265					struct wpa_ssid *ssid)
266{
267	int i, privacy = 0;
268
269	if (ssid->mixed_cell)
270		return 1;
271
272#ifdef CONFIG_WPS
273	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
274		return 1;
275#endif /* CONFIG_WPS */
276
277	for (i = 0; i < NUM_WEP_KEYS; i++) {
278		if (ssid->wep_key_len[i]) {
279			privacy = 1;
280			break;
281		}
282	}
283#ifdef IEEE8021X_EAPOL
284	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
285	    ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
286				 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
287		privacy = 1;
288#endif /* IEEE8021X_EAPOL */
289
290	if (bss->caps & IEEE80211_CAP_PRIVACY)
291		return privacy;
292	return !privacy;
293}
294
295
296static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
297					 struct wpa_ssid *ssid,
298					 struct wpa_scan_res *bss)
299{
300	struct wpa_ie_data ie;
301	int proto_match = 0;
302	const u8 *rsn_ie, *wpa_ie;
303	int ret;
304
305	ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
306	if (ret >= 0)
307		return ret;
308
309	rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
310	while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
311		proto_match++;
312
313		if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
314			wpa_printf(MSG_DEBUG, "   skip RSN IE - parse failed");
315			break;
316		}
317		if (!(ie.proto & ssid->proto)) {
318			wpa_printf(MSG_DEBUG, "   skip RSN IE - proto "
319				   "mismatch");
320			break;
321		}
322
323		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
324			wpa_printf(MSG_DEBUG, "   skip RSN IE - PTK cipher "
325				   "mismatch");
326			break;
327		}
328
329		if (!(ie.group_cipher & ssid->group_cipher)) {
330			wpa_printf(MSG_DEBUG, "   skip RSN IE - GTK cipher "
331				   "mismatch");
332			break;
333		}
334
335		if (!(ie.key_mgmt & ssid->key_mgmt)) {
336			wpa_printf(MSG_DEBUG, "   skip RSN IE - key mgmt "
337				   "mismatch");
338			break;
339		}
340
341#ifdef CONFIG_IEEE80211W
342		if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
343		    ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
344			wpa_printf(MSG_DEBUG, "   skip RSN IE - no mgmt frame "
345				   "protection");
346			break;
347		}
348#endif /* CONFIG_IEEE80211W */
349
350		wpa_printf(MSG_DEBUG, "   selected based on RSN IE");
351		return 1;
352	}
353
354	wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
355	while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
356		proto_match++;
357
358		if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
359			wpa_printf(MSG_DEBUG, "   skip WPA IE - parse failed");
360			break;
361		}
362		if (!(ie.proto & ssid->proto)) {
363			wpa_printf(MSG_DEBUG, "   skip WPA IE - proto "
364				   "mismatch");
365			break;
366		}
367
368		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
369			wpa_printf(MSG_DEBUG, "   skip WPA IE - PTK cipher "
370				   "mismatch");
371			break;
372		}
373
374		if (!(ie.group_cipher & ssid->group_cipher)) {
375			wpa_printf(MSG_DEBUG, "   skip WPA IE - GTK cipher "
376				   "mismatch");
377			break;
378		}
379
380		if (!(ie.key_mgmt & ssid->key_mgmt)) {
381			wpa_printf(MSG_DEBUG, "   skip WPA IE - key mgmt "
382				   "mismatch");
383			break;
384		}
385
386		wpa_printf(MSG_DEBUG, "   selected based on WPA IE");
387		return 1;
388	}
389
390	if (proto_match == 0)
391		wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN proto match");
392
393	return 0;
394}
395
396
397static int freq_allowed(int *freqs, int freq)
398{
399	int i;
400
401	if (freqs == NULL)
402		return 1;
403
404	for (i = 0; freqs[i]; i++)
405		if (freqs[i] == freq)
406			return 1;
407	return 0;
408}
409
410
411static struct wpa_bss *
412wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
413			      struct wpa_scan_results *scan_res,
414			      struct wpa_ssid *group,
415			      struct wpa_ssid **selected_ssid)
416{
417	struct wpa_ssid *ssid;
418	struct wpa_scan_res *bss;
419	size_t i;
420	struct wpa_blacklist *e;
421	const u8 *ie;
422
423	wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
424	for (i = 0; i < scan_res->num; i++) {
425		const u8 *ssid_;
426		u8 wpa_ie_len, rsn_ie_len, ssid_len;
427		bss = scan_res->res[i];
428
429		ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
430		ssid_ = ie ? ie + 2 : (u8 *) "";
431		ssid_len = ie ? ie[1] : 0;
432
433		ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
434		wpa_ie_len = ie ? ie[1] : 0;
435
436		ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
437		rsn_ie_len = ie ? ie[1] : 0;
438
439		wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
440			   "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
441			   (int) i, MAC2STR(bss->bssid),
442			   wpa_ssid_txt(ssid_, ssid_len),
443			   wpa_ie_len, rsn_ie_len, bss->caps);
444
445		e = wpa_blacklist_get(wpa_s, bss->bssid);
446		if (e && e->count > 1) {
447			wpa_printf(MSG_DEBUG, "   skip - blacklisted");
448			continue;
449		}
450
451		if (ssid_len == 0) {
452			wpa_printf(MSG_DEBUG, "   skip - SSID not known");
453			continue;
454		}
455
456		if (wpa_ie_len == 0 && rsn_ie_len == 0) {
457			wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN IE");
458			continue;
459		}
460
461		for (ssid = group; ssid; ssid = ssid->pnext) {
462			int check_ssid = 1;
463
464			if (ssid->disabled) {
465				wpa_printf(MSG_DEBUG, "   skip - disabled");
466				continue;
467			}
468
469#ifdef CONFIG_WPS
470			if (ssid->ssid_len == 0 &&
471			    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
472				check_ssid = 0;
473#endif /* CONFIG_WPS */
474
475			if (check_ssid &&
476			    (ssid_len != ssid->ssid_len ||
477			     os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
478				wpa_printf(MSG_DEBUG, "   skip - "
479					   "SSID mismatch");
480				continue;
481			}
482
483			if (ssid->bssid_set &&
484			    os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
485			{
486				wpa_printf(MSG_DEBUG, "   skip - "
487					   "BSSID mismatch");
488				continue;
489			}
490
491			if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
492				continue;
493
494			if (!freq_allowed(ssid->freq_list, bss->freq)) {
495				wpa_printf(MSG_DEBUG, "   skip - "
496					   "frequency not allowed");
497				continue;
498			}
499
500			wpa_printf(MSG_DEBUG, "   selected WPA AP "
501				   MACSTR " ssid='%s'",
502				   MAC2STR(bss->bssid),
503				   wpa_ssid_txt(ssid_, ssid_len));
504			*selected_ssid = ssid;
505			return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
506		}
507	}
508
509	return NULL;
510}
511
512
513static struct wpa_bss *
514wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
515				  struct wpa_scan_results *scan_res,
516				  struct wpa_ssid *group,
517				  struct wpa_ssid **selected_ssid)
518{
519	struct wpa_ssid *ssid;
520	struct wpa_scan_res *bss;
521	size_t i;
522	struct wpa_blacklist *e;
523	const u8 *ie;
524
525	wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
526	for (i = 0; i < scan_res->num; i++) {
527		const u8 *ssid_;
528		u8 wpa_ie_len, rsn_ie_len, ssid_len;
529		bss = scan_res->res[i];
530
531		ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
532		ssid_ = ie ? ie + 2 : (u8 *) "";
533		ssid_len = ie ? ie[1] : 0;
534
535		ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
536		wpa_ie_len = ie ? ie[1] : 0;
537
538		ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
539		rsn_ie_len = ie ? ie[1] : 0;
540
541		wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
542			   "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
543			   (int) i, MAC2STR(bss->bssid),
544			   wpa_ssid_txt(ssid_, ssid_len),
545			   wpa_ie_len, rsn_ie_len, bss->caps);
546
547		e = wpa_blacklist_get(wpa_s, bss->bssid);
548		if (e && e->count > 1) {
549			wpa_printf(MSG_DEBUG, "   skip - blacklisted");
550			continue;
551		}
552
553		if (ssid_len == 0) {
554			wpa_printf(MSG_DEBUG, "   skip - SSID not known");
555			continue;
556		}
557
558		for (ssid = group; ssid; ssid = ssid->pnext) {
559			int check_ssid = ssid->ssid_len != 0;
560
561			if (ssid->disabled) {
562				wpa_printf(MSG_DEBUG, "   skip - disabled");
563				continue;
564			}
565
566#ifdef CONFIG_WPS
567			if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
568				/* Only allow wildcard SSID match if an AP
569				 * advertises active WPS operation that matches
570				 * with our mode. */
571				check_ssid = 1;
572				if (ssid->ssid_len == 0 &&
573				    wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
574							      bss))
575					check_ssid = 0;
576			}
577#endif /* CONFIG_WPS */
578
579			if (check_ssid &&
580			    (ssid_len != ssid->ssid_len ||
581			     os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
582				wpa_printf(MSG_DEBUG, "   skip - "
583					   "SSID mismatch");
584				continue;
585			}
586
587			if (ssid->bssid_set &&
588			    os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
589			{
590				wpa_printf(MSG_DEBUG, "   skip - "
591					   "BSSID mismatch");
592				continue;
593			}
594
595			if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
596			    !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
597			    !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
598			{
599				wpa_printf(MSG_DEBUG, "   skip - "
600					   "non-WPA network not allowed");
601				continue;
602			}
603
604			if ((ssid->key_mgmt &
605			     (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
606			      WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
607			      WPA_KEY_MGMT_IEEE8021X_SHA256 |
608			      WPA_KEY_MGMT_PSK_SHA256)) &&
609			    (wpa_ie_len != 0 || rsn_ie_len != 0)) {
610				wpa_printf(MSG_DEBUG, "   skip - "
611					   "WPA network");
612				continue;
613			}
614
615			if (!wpa_supplicant_match_privacy(bss, ssid)) {
616				wpa_printf(MSG_DEBUG, "   skip - "
617					   "privacy mismatch");
618				continue;
619			}
620
621			if (bss->caps & IEEE80211_CAP_IBSS) {
622				wpa_printf(MSG_DEBUG, "   skip - "
623					   "IBSS (adhoc) network");
624				continue;
625			}
626
627			if (!freq_allowed(ssid->freq_list, bss->freq)) {
628				wpa_printf(MSG_DEBUG, "   skip - "
629					   "frequency not allowed");
630				continue;
631			}
632
633			wpa_printf(MSG_DEBUG, "   selected non-WPA AP "
634				   MACSTR " ssid='%s'",
635				   MAC2STR(bss->bssid),
636				   wpa_ssid_txt(ssid_, ssid_len));
637			*selected_ssid = ssid;
638			return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
639		}
640	}
641
642	return NULL;
643}
644
645
646static struct wpa_bss *
647wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
648			  struct wpa_scan_results *scan_res,
649			  struct wpa_ssid *group,
650			  struct wpa_ssid **selected_ssid)
651{
652	struct wpa_bss *selected;
653
654	wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
655		   group->priority);
656
657	/* First, try to find WPA-enabled AP */
658	selected = wpa_supplicant_select_bss_wpa(wpa_s, scan_res, group,
659						 selected_ssid);
660	if (selected)
661		return selected;
662
663	/* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
664	 * allows this. */
665	return wpa_supplicant_select_bss_non_wpa(wpa_s, scan_res, group,
666						 selected_ssid);
667}
668
669
670static struct wpa_bss *
671wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
672			    struct wpa_scan_results *scan_res,
673			    struct wpa_ssid **selected_ssid)
674{
675	struct wpa_bss *selected = NULL;
676	int prio;
677
678	while (selected == NULL) {
679		for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
680			selected = wpa_supplicant_select_bss(
681				wpa_s, scan_res, wpa_s->conf->pssid[prio],
682				selected_ssid);
683			if (selected)
684				break;
685		}
686
687		if (selected == NULL && wpa_s->blacklist) {
688			wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
689				   "and try again");
690			wpa_blacklist_clear(wpa_s);
691			wpa_s->blacklist_cleared++;
692		} else if (selected == NULL)
693			break;
694	}
695
696	return selected;
697}
698
699
700static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
701					int timeout_sec, int timeout_usec)
702{
703	if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
704		/*
705		 * Quick recovery if the initial scan results were not
706		 * complete when fetched before the first scan request.
707		 */
708		wpa_s->scan_res_tried++;
709		timeout_sec = 0;
710		timeout_usec = 0;
711	} else if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
712		/*
713		 * No networks are enabled; short-circuit request so
714		 * we don't wait timeout seconds before transitioning
715		 * to INACTIVE state.
716		 */
717		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
718		return;
719	}
720	wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
721}
722
723
724void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
725			    struct wpa_bss *selected,
726			    struct wpa_ssid *ssid)
727{
728	if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
729		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
730			"PBC session overlap");
731		wpa_supplicant_req_new_scan(wpa_s, 10, 0);
732		return;
733	}
734
735	/*
736	 * Do not trigger new association unless the BSSID has changed or if
737	 * reassociation is requested. If we are in process of associating with
738	 * the selected BSSID, do not trigger new attempt.
739	 */
740	if (wpa_s->reassociate ||
741	    (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
742	     (wpa_s->wpa_state != WPA_ASSOCIATING ||
743	      os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
744	      0))) {
745		if (wpa_supplicant_scard_init(wpa_s, ssid)) {
746			wpa_supplicant_req_new_scan(wpa_s, 10, 0);
747			return;
748		}
749		wpa_supplicant_associate(wpa_s, selected, ssid);
750	} else {
751		wpa_printf(MSG_DEBUG, "Already associated with the selected "
752			   "AP");
753	}
754}
755
756
757static struct wpa_ssid *
758wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
759{
760	int prio;
761	struct wpa_ssid *ssid;
762
763	for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
764		for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
765		{
766			if (ssid->disabled)
767				continue;
768			if (ssid->mode == IEEE80211_MODE_IBSS ||
769			    ssid->mode == IEEE80211_MODE_AP)
770				return ssid;
771		}
772	}
773	return NULL;
774}
775
776
777/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
778 * on BSS added and BSS changed events */
779static void wpa_supplicant_rsn_preauth_scan_results(
780	struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
781{
782	int i;
783
784	if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
785		return;
786
787	for (i = scan_res->num - 1; i >= 0; i--) {
788		const u8 *ssid, *rsn;
789		struct wpa_scan_res *r;
790
791		r = scan_res->res[i];
792
793		ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
794		if (ssid == NULL)
795			continue;
796
797		rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
798		if (rsn == NULL)
799			continue;
800
801		rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
802	}
803
804}
805
806
807static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
808				       struct wpa_bss *selected,
809				       struct wpa_ssid *ssid,
810				       struct wpa_scan_results *scan_res)
811{
812	size_t i;
813	struct wpa_scan_res *current_bss = NULL;
814	int min_diff;
815
816	if (wpa_s->reassociate)
817		return 1; /* explicit request to reassociate */
818	if (wpa_s->wpa_state < WPA_ASSOCIATED)
819		return 1; /* we are not associated; continue */
820	if (wpa_s->current_ssid == NULL)
821		return 1; /* unknown current SSID */
822	if (wpa_s->current_ssid != ssid)
823		return 1; /* different network block */
824
825	for (i = 0; i < scan_res->num; i++) {
826		struct wpa_scan_res *res = scan_res->res[i];
827		const u8 *ie;
828		if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
829			continue;
830
831		ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
832		if (ie == NULL)
833			continue;
834		if (ie[1] != wpa_s->current_ssid->ssid_len ||
835		    os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
836			continue;
837		current_bss = res;
838		break;
839	}
840
841	if (!current_bss)
842		return 1; /* current BSS not seen in scan results */
843
844	wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
845	wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
846		   MAC2STR(current_bss->bssid), current_bss->level);
847	wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
848		   MAC2STR(selected->bssid), selected->level);
849
850	if (wpa_s->current_ssid->bssid_set &&
851	    os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
852	    0) {
853		wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
854			   "preferred BSSID");
855		return 1;
856	}
857
858	min_diff = 2;
859	if (current_bss->level < 0) {
860		if (current_bss->level < -85)
861			min_diff = 1;
862		else if (current_bss->level < -80)
863			min_diff = 2;
864		else if (current_bss->level < -75)
865			min_diff = 3;
866		else if (current_bss->level < -70)
867			min_diff = 4;
868		else
869			min_diff = 5;
870	}
871	if (abs(current_bss->level - selected->level) < min_diff) {
872		wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
873			   "signal level");
874		return 0;
875	}
876
877	return 1;
878}
879
880
881static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
882					      union wpa_event_data *data)
883{
884	struct wpa_bss *selected;
885	struct wpa_ssid *ssid = NULL;
886	struct wpa_scan_results *scan_res;
887
888	wpa_supplicant_notify_scanning(wpa_s, 0);
889
890	scan_res = wpa_supplicant_get_scan_results(wpa_s,
891						   data ? &data->scan_info :
892						   NULL, 1);
893	if (scan_res == NULL) {
894		if (wpa_s->conf->ap_scan == 2)
895			return;
896		wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
897			   "scanning again");
898		wpa_supplicant_req_new_scan(wpa_s, 1, 0);
899		return;
900	}
901
902	if (wpa_s->scan_res_handler) {
903		wpa_s->scan_res_handler(wpa_s, scan_res);
904		wpa_s->scan_res_handler = NULL;
905		wpa_scan_results_free(scan_res);
906		return;
907	}
908
909	/*
910	 * Don't post the results if this was the initial cached
911	 * and there were no results.
912	 */
913	if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
914	    scan_res->num == 0) {
915		wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
916			"empty - not posting");
917	} else {
918		wpa_printf(MSG_DEBUG, "New scan results available");
919		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
920		wpas_notify_scan_results(wpa_s);
921	}
922
923	wpas_notify_scan_done(wpa_s, 1);
924
925	if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
926		wpa_scan_results_free(scan_res);
927		return;
928	}
929
930	if (wpa_s->disconnected) {
931		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
932		wpa_scan_results_free(scan_res);
933		return;
934	}
935
936	if (bgscan_notify_scan(wpa_s) == 1) {
937		wpa_scan_results_free(scan_res);
938		return;
939	}
940
941	wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
942
943	selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
944
945	if (selected) {
946		int skip;
947		skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
948						    scan_res);
949		wpa_scan_results_free(scan_res);
950		if (skip)
951			return;
952		wpa_supplicant_connect(wpa_s, selected, ssid);
953	} else {
954		wpa_scan_results_free(scan_res);
955		wpa_printf(MSG_DEBUG, "No suitable network found");
956		ssid = wpa_supplicant_pick_new_network(wpa_s);
957		if (ssid) {
958			wpa_printf(MSG_DEBUG, "Setup a new network");
959			wpa_supplicant_associate(wpa_s, NULL, ssid);
960		} else {
961			int timeout_sec = 5;
962			int timeout_usec = 0;
963			wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
964						    timeout_usec);
965		}
966	}
967}
968#endif /* CONFIG_NO_SCAN_PROCESSING */
969
970
971static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
972					  union wpa_event_data *data)
973{
974	int l, len, found = 0, wpa_found, rsn_found;
975	const u8 *p;
976
977	wpa_printf(MSG_DEBUG, "Association info event");
978	if (data->assoc_info.req_ies)
979		wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
980			    data->assoc_info.req_ies_len);
981	if (data->assoc_info.resp_ies)
982		wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
983			    data->assoc_info.resp_ies_len);
984	if (data->assoc_info.beacon_ies)
985		wpa_hexdump(MSG_DEBUG, "beacon_ies",
986			    data->assoc_info.beacon_ies,
987			    data->assoc_info.beacon_ies_len);
988	if (data->assoc_info.freq)
989		wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
990
991	p = data->assoc_info.req_ies;
992	l = data->assoc_info.req_ies_len;
993
994	/* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
995	while (p && l >= 2) {
996		len = p[1] + 2;
997		if (len > l) {
998			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
999				    p, l);
1000			break;
1001		}
1002		if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1003		     (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1004		    (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1005			if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1006				break;
1007			found = 1;
1008			wpa_find_assoc_pmkid(wpa_s);
1009			break;
1010		}
1011		l -= len;
1012		p += len;
1013	}
1014	if (!found && data->assoc_info.req_ies)
1015		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1016
1017#ifdef CONFIG_IEEE80211R
1018#ifdef CONFIG_SME
1019	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1020		u8 bssid[ETH_ALEN];
1021		if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1022		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1023						 data->assoc_info.resp_ies,
1024						 data->assoc_info.resp_ies_len,
1025						 bssid) < 0) {
1026			wpa_printf(MSG_DEBUG, "FT: Validation of "
1027				   "Reassociation Response failed");
1028			wpa_supplicant_deauthenticate(
1029				wpa_s, WLAN_REASON_INVALID_IE);
1030			return -1;
1031		}
1032	}
1033
1034	p = data->assoc_info.resp_ies;
1035	l = data->assoc_info.resp_ies_len;
1036
1037	/* Go through the IEs and make a copy of the MDIE, if present. */
1038	while (p && l >= 2) {
1039		len = p[1] + 2;
1040		if (len > l) {
1041			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1042				    p, l);
1043			break;
1044		}
1045		if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1046		    p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1047			wpa_s->sme.ft_used = 1;
1048			os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1049				  MOBILITY_DOMAIN_ID_LEN);
1050			break;
1051		}
1052		l -= len;
1053		p += len;
1054	}
1055#endif /* CONFIG_SME */
1056
1057	wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1058			     data->assoc_info.resp_ies_len);
1059#endif /* CONFIG_IEEE80211R */
1060
1061	/* WPA/RSN IE from Beacon/ProbeResp */
1062	p = data->assoc_info.beacon_ies;
1063	l = data->assoc_info.beacon_ies_len;
1064
1065	/* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1066	 */
1067	wpa_found = rsn_found = 0;
1068	while (p && l >= 2) {
1069		len = p[1] + 2;
1070		if (len > l) {
1071			wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1072				    p, l);
1073			break;
1074		}
1075		if (!wpa_found &&
1076		    p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1077		    os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1078			wpa_found = 1;
1079			wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1080		}
1081
1082		if (!rsn_found &&
1083		    p[0] == WLAN_EID_RSN && p[1] >= 2) {
1084			rsn_found = 1;
1085			wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1086		}
1087
1088		l -= len;
1089		p += len;
1090	}
1091
1092	if (!wpa_found && data->assoc_info.beacon_ies)
1093		wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1094	if (!rsn_found && data->assoc_info.beacon_ies)
1095		wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1096	if (wpa_found || rsn_found)
1097		wpa_s->ap_ies_from_associnfo = 1;
1098
1099	wpa_s->assoc_freq = data->assoc_info.freq;
1100
1101	return 0;
1102}
1103
1104
1105static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1106				       union wpa_event_data *data)
1107{
1108	u8 bssid[ETH_ALEN];
1109	int ft_completed;
1110	int bssid_changed;
1111	struct wpa_driver_capa capa;
1112
1113#ifdef CONFIG_AP
1114	if (wpa_s->ap_iface) {
1115		hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1116				    data->assoc_info.addr,
1117				    data->assoc_info.req_ies,
1118				    data->assoc_info.req_ies_len);
1119		return;
1120	}
1121#endif /* CONFIG_AP */
1122
1123	ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1124	if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1125		return;
1126
1127	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1128	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1129		os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1130	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1131	    (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1132	     os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1133		wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1134			MACSTR, MAC2STR(bssid));
1135		bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1136		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1137		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1138		if (bssid_changed)
1139			wpas_notify_bssid_changed(wpa_s);
1140
1141		if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1142			wpa_clear_keys(wpa_s, bssid);
1143		}
1144		if (wpa_supplicant_select_config(wpa_s) < 0) {
1145			wpa_supplicant_disassociate(
1146				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1147			return;
1148		}
1149		if (wpa_s->current_ssid) {
1150			struct wpa_bss *bss = NULL;
1151			struct wpa_ssid *ssid = wpa_s->current_ssid;
1152			if (ssid->ssid_len > 0)
1153				bss = wpa_bss_get(wpa_s, bssid,
1154						  ssid->ssid, ssid->ssid_len);
1155			if (!bss)
1156				bss = wpa_bss_get_bssid(wpa_s, bssid);
1157			if (bss)
1158				wpa_s->current_bss = bss;
1159		}
1160	}
1161
1162#ifdef CONFIG_SME
1163	os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1164	wpa_s->sme.prev_bssid_set = 1;
1165#endif /* CONFIG_SME */
1166
1167	wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1168	if (wpa_s->current_ssid) {
1169		/* When using scanning (ap_scan=1), SIM PC/SC interface can be
1170		 * initialized before association, but for other modes,
1171		 * initialize PC/SC here, if the current configuration needs
1172		 * smartcard or SIM/USIM. */
1173		wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1174	}
1175	wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1176	l2_packet_notify_auth_start(wpa_s->l2);
1177
1178	/*
1179	 * Set portEnabled first to FALSE in order to get EAP state machine out
1180	 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1181	 * state machine may transit to AUTHENTICATING state based on obsolete
1182	 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1183	 * AUTHENTICATED without ever giving chance to EAP state machine to
1184	 * reset the state.
1185	 */
1186	if (!ft_completed) {
1187		eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1188		eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1189	}
1190	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1191		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1192	/* 802.1X::portControl = Auto */
1193	eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1194	wpa_s->eapol_received = 0;
1195	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1196	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1197	    (wpa_s->current_ssid &&
1198	     wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1199		wpa_supplicant_cancel_auth_timeout(wpa_s);
1200		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1201	} else if (!ft_completed) {
1202		/* Timeout for receiving the first EAPOL packet */
1203		wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1204	}
1205	wpa_supplicant_cancel_scan(wpa_s);
1206
1207	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1208	    wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1209		/*
1210		 * We are done; the driver will take care of RSN 4-way
1211		 * handshake.
1212		 */
1213		wpa_supplicant_cancel_auth_timeout(wpa_s);
1214		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1215		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1216		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1217	}
1218
1219	if (wpa_s->pending_eapol_rx) {
1220		struct os_time now, age;
1221		os_get_time(&now);
1222		os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1223		if (age.sec == 0 && age.usec < 100000 &&
1224		    os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1225		    0) {
1226			wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1227				   "that was received just before association "
1228				   "notification");
1229			wpa_supplicant_rx_eapol(
1230				wpa_s, wpa_s->pending_eapol_rx_src,
1231				wpabuf_head(wpa_s->pending_eapol_rx),
1232				wpabuf_len(wpa_s->pending_eapol_rx));
1233		}
1234		wpabuf_free(wpa_s->pending_eapol_rx);
1235		wpa_s->pending_eapol_rx = NULL;
1236	}
1237
1238#ifdef CONFIG_BGSCAN
1239	if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1240		bgscan_deinit(wpa_s);
1241		if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1242			if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1243				wpa_printf(MSG_DEBUG, "Failed to initialize "
1244					   "bgscan");
1245				/*
1246				 * Live without bgscan; it is only used as a
1247				 * roaming optimization, so the initial
1248				 * connection is not affected.
1249				 */
1250			} else
1251				wpa_s->bgscan_ssid = wpa_s->current_ssid;
1252		} else
1253			wpa_s->bgscan_ssid = NULL;
1254	}
1255#endif /* CONFIG_BGSCAN */
1256
1257	if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1258	     wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1259	    wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1260	    capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1261		/* Set static WEP keys again */
1262		wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1263	}
1264}
1265
1266
1267static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1268					  u16 reason_code)
1269{
1270	const u8 *bssid;
1271#ifdef CONFIG_SME
1272	int authenticating;
1273	u8 prev_pending_bssid[ETH_ALEN];
1274
1275	authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1276	os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1277#endif /* CONFIG_SME */
1278
1279	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1280		/*
1281		 * At least Host AP driver and a Prism3 card seemed to be
1282		 * generating streams of disconnected events when configuring
1283		 * IBSS for WPA-None. Ignore them for now.
1284		 */
1285		wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1286			   "IBSS/WPA-None mode");
1287		return;
1288	}
1289
1290	if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1291	    wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1292		wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1293			"pre-shared key may be incorrect");
1294	}
1295	if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1296		wpa_supplicant_req_scan(wpa_s, 0, 100000);
1297	bssid = wpa_s->bssid;
1298	if (is_zero_ether_addr(bssid))
1299		bssid = wpa_s->pending_bssid;
1300	wpa_blacklist_add(wpa_s, bssid);
1301	wpa_sm_notify_disassoc(wpa_s->wpa);
1302	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1303		" reason=%d",
1304		MAC2STR(bssid), reason_code);
1305	if (wpa_supplicant_dynamic_keys(wpa_s)) {
1306		wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
1307		wpa_s->keys_cleared = 0;
1308		wpa_clear_keys(wpa_s, wpa_s->bssid);
1309	}
1310	wpa_supplicant_mark_disassoc(wpa_s);
1311	bgscan_deinit(wpa_s);
1312#ifdef CONFIG_SME
1313	if (authenticating &&
1314	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1315		/*
1316		 * mac80211-workaround to force deauth on failed auth cmd,
1317		 * requires us to remain in authenticating state to allow the
1318		 * second authentication attempt to be continued properly.
1319		 */
1320		wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1321			   "proceed after disconnection event");
1322		wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1323		os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1324	}
1325#endif /* CONFIG_SME */
1326}
1327
1328
1329#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1330static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1331						    void *sock_ctx)
1332{
1333	struct wpa_supplicant *wpa_s = eloop_ctx;
1334
1335	if (!wpa_s->pending_mic_error_report)
1336		return;
1337
1338	wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1339	wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1340	wpa_s->pending_mic_error_report = 0;
1341}
1342#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1343
1344
1345static void
1346wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1347					 union wpa_event_data *data)
1348{
1349	int pairwise;
1350	struct os_time t;
1351
1352	wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1353	pairwise = (data && data->michael_mic_failure.unicast);
1354	os_get_time(&t);
1355	if ((wpa_s->last_michael_mic_error &&
1356	     t.sec - wpa_s->last_michael_mic_error <= 60) ||
1357	    wpa_s->pending_mic_error_report) {
1358		if (wpa_s->pending_mic_error_report) {
1359			/*
1360			 * Send the pending MIC error report immediately since
1361			 * we are going to start countermeasures and AP better
1362			 * do the same.
1363			 */
1364			wpa_sm_key_request(wpa_s->wpa, 1,
1365					   wpa_s->pending_mic_error_pairwise);
1366		}
1367
1368		/* Send the new MIC error report immediately since we are going
1369		 * to start countermeasures and AP better do the same.
1370		 */
1371		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1372
1373		/* initialize countermeasures */
1374		wpa_s->countermeasures = 1;
1375		wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1376
1377		/*
1378		 * Need to wait for completion of request frame. We do not get
1379		 * any callback for the message completion, so just wait a
1380		 * short while and hope for the best. */
1381		os_sleep(0, 10000);
1382
1383		wpa_drv_set_countermeasures(wpa_s, 1);
1384		wpa_supplicant_deauthenticate(wpa_s,
1385					      WLAN_REASON_MICHAEL_MIC_FAILURE);
1386		eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1387				     wpa_s, NULL);
1388		eloop_register_timeout(60, 0,
1389				       wpa_supplicant_stop_countermeasures,
1390				       wpa_s, NULL);
1391		/* TODO: mark the AP rejected for 60 second. STA is
1392		 * allowed to associate with another AP.. */
1393	} else {
1394#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1395		if (wpa_s->mic_errors_seen) {
1396			/*
1397			 * Reduce the effectiveness of Michael MIC error
1398			 * reports as a means for attacking against TKIP if
1399			 * more than one MIC failure is noticed with the same
1400			 * PTK. We delay the transmission of the reports by a
1401			 * random time between 0 and 60 seconds in order to
1402			 * force the attacker wait 60 seconds before getting
1403			 * the information on whether a frame resulted in a MIC
1404			 * failure.
1405			 */
1406			u8 rval[4];
1407			int sec;
1408
1409			if (os_get_random(rval, sizeof(rval)) < 0)
1410				sec = os_random() % 60;
1411			else
1412				sec = WPA_GET_BE32(rval) % 60;
1413			wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1414				   "seconds", sec);
1415			wpa_s->pending_mic_error_report = 1;
1416			wpa_s->pending_mic_error_pairwise = pairwise;
1417			eloop_cancel_timeout(
1418				wpa_supplicant_delayed_mic_error_report,
1419				wpa_s, NULL);
1420			eloop_register_timeout(
1421				sec, os_random() % 1000000,
1422				wpa_supplicant_delayed_mic_error_report,
1423				wpa_s, NULL);
1424		} else {
1425			wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1426		}
1427#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1428		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1429#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1430	}
1431	wpa_s->last_michael_mic_error = t.sec;
1432	wpa_s->mic_errors_seen++;
1433}
1434
1435
1436#ifdef CONFIG_TERMINATE_ONLASTIF
1437static int any_interfaces(struct wpa_supplicant *head)
1438{
1439	struct wpa_supplicant *wpa_s;
1440
1441	for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1442		if (!wpa_s->interface_removed)
1443			return 1;
1444	return 0;
1445}
1446#endif /* CONFIG_TERMINATE_ONLASTIF */
1447
1448
1449static void
1450wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1451				      union wpa_event_data *data)
1452{
1453	if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1454		return;
1455
1456	switch (data->interface_status.ievent) {
1457	case EVENT_INTERFACE_ADDED:
1458		if (!wpa_s->interface_removed)
1459			break;
1460		wpa_s->interface_removed = 0;
1461		wpa_printf(MSG_DEBUG, "Configured interface was added.");
1462		if (wpa_supplicant_driver_init(wpa_s) < 0) {
1463			wpa_printf(MSG_INFO, "Failed to initialize the driver "
1464				   "after interface was added.");
1465		}
1466		break;
1467	case EVENT_INTERFACE_REMOVED:
1468		wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1469		wpa_s->interface_removed = 1;
1470		wpa_supplicant_mark_disassoc(wpa_s);
1471		l2_packet_deinit(wpa_s->l2);
1472		wpa_s->l2 = NULL;
1473#ifdef CONFIG_TERMINATE_ONLASTIF
1474		/* check if last interface */
1475		if (!any_interfaces(wpa_s->global->ifaces))
1476			eloop_terminate();
1477#endif /* CONFIG_TERMINATE_ONLASTIF */
1478		break;
1479	}
1480}
1481
1482
1483#ifdef CONFIG_PEERKEY
1484static void
1485wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1486			      union wpa_event_data *data)
1487{
1488	if (data == NULL)
1489		return;
1490	wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1491}
1492#endif /* CONFIG_PEERKEY */
1493
1494
1495#ifdef CONFIG_IEEE80211R
1496static void
1497wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1498				 union wpa_event_data *data)
1499{
1500	if (data == NULL)
1501		return;
1502
1503	if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1504				    data->ft_ies.ies_len,
1505				    data->ft_ies.ft_action,
1506				    data->ft_ies.target_ap,
1507				    data->ft_ies.ric_ies,
1508				    data->ft_ies.ric_ies_len) < 0) {
1509		/* TODO: prevent MLME/driver from trying to associate? */
1510	}
1511}
1512#endif /* CONFIG_IEEE80211R */
1513
1514
1515#ifdef CONFIG_IBSS_RSN
1516static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1517						union wpa_event_data *data)
1518{
1519	if (data == NULL)
1520		return;
1521	ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1522}
1523#endif /* CONFIG_IBSS_RSN */
1524
1525
1526#ifdef CONFIG_IEEE80211R
1527static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1528			 size_t len)
1529{
1530	const u8 *sta_addr, *target_ap_addr;
1531	u16 status;
1532
1533	wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1534	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1535		return; /* only SME case supported for now */
1536	if (len < 1 + 2 * ETH_ALEN + 2)
1537		return;
1538	if (data[0] != 2)
1539		return; /* Only FT Action Response is supported for now */
1540	sta_addr = data + 1;
1541	target_ap_addr = data + 1 + ETH_ALEN;
1542	status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1543	wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
1544		   " TargetAP " MACSTR " status %u",
1545		   MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1546
1547	if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1548		wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1549			   " in FT Action Response", MAC2STR(sta_addr));
1550		return;
1551	}
1552
1553	if (status) {
1554		wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
1555			   "failure (status code %d)", status);
1556		/* TODO: report error to FT code(?) */
1557		return;
1558	}
1559
1560	if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1561				    len - (1 + 2 * ETH_ALEN + 2), 1,
1562				    target_ap_addr, NULL, 0) < 0)
1563		return;
1564
1565#ifdef CONFIG_SME
1566	{
1567		struct wpa_bss *bss;
1568		bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1569		if (bss)
1570			wpa_s->sme.freq = bss->freq;
1571		wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1572		sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1573			      WLAN_AUTH_FT);
1574	}
1575#endif /* CONFIG_SME */
1576}
1577#endif /* CONFIG_IEEE80211R */
1578
1579
1580void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1581			  union wpa_event_data *data)
1582{
1583	struct wpa_supplicant *wpa_s = ctx;
1584	u16 reason_code = 0;
1585
1586	switch (event) {
1587	case EVENT_AUTH:
1588		sme_event_auth(wpa_s, data);
1589		break;
1590	case EVENT_ASSOC:
1591		wpa_supplicant_event_assoc(wpa_s, data);
1592		break;
1593	case EVENT_DISASSOC:
1594		wpa_printf(MSG_DEBUG, "Disassociation notification");
1595#ifdef CONFIG_AP
1596		if (wpa_s->ap_iface && data) {
1597			hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1598					       data->disassoc_info.addr);
1599			break;
1600		}
1601#endif /* CONFIG_AP */
1602		if (data)
1603			reason_code = data->deauth_info.reason_code;
1604		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1605			sme_event_disassoc(wpa_s, data);
1606		/* fall through */
1607	case EVENT_DEAUTH:
1608		if (event == EVENT_DEAUTH) {
1609			wpa_printf(MSG_DEBUG, "Deauthentication notification");
1610			if (data)
1611				reason_code = data->deauth_info.reason_code;
1612		}
1613#ifdef CONFIG_AP
1614		if (wpa_s->ap_iface && data) {
1615			hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1616					       data->deauth_info.addr);
1617			break;
1618		}
1619#endif /* CONFIG_AP */
1620		wpa_supplicant_event_disassoc(wpa_s, reason_code);
1621		break;
1622	case EVENT_MICHAEL_MIC_FAILURE:
1623		wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1624		break;
1625#ifndef CONFIG_NO_SCAN_PROCESSING
1626	case EVENT_SCAN_RESULTS:
1627		wpa_supplicant_event_scan_results(wpa_s, data);
1628		break;
1629#endif /* CONFIG_NO_SCAN_PROCESSING */
1630	case EVENT_ASSOCINFO:
1631		wpa_supplicant_event_associnfo(wpa_s, data);
1632		break;
1633	case EVENT_INTERFACE_STATUS:
1634		wpa_supplicant_event_interface_status(wpa_s, data);
1635		break;
1636	case EVENT_PMKID_CANDIDATE:
1637		wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1638		break;
1639#ifdef CONFIG_PEERKEY
1640	case EVENT_STKSTART:
1641		wpa_supplicant_event_stkstart(wpa_s, data);
1642		break;
1643#endif /* CONFIG_PEERKEY */
1644#ifdef CONFIG_IEEE80211R
1645	case EVENT_FT_RESPONSE:
1646		wpa_supplicant_event_ft_response(wpa_s, data);
1647		break;
1648#endif /* CONFIG_IEEE80211R */
1649#ifdef CONFIG_IBSS_RSN
1650	case EVENT_IBSS_RSN_START:
1651		wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1652		break;
1653#endif /* CONFIG_IBSS_RSN */
1654	case EVENT_ASSOC_REJECT:
1655		sme_event_assoc_reject(wpa_s, data);
1656		break;
1657	case EVENT_AUTH_TIMED_OUT:
1658		sme_event_auth_timed_out(wpa_s, data);
1659		break;
1660	case EVENT_ASSOC_TIMED_OUT:
1661		sme_event_assoc_timed_out(wpa_s, data);
1662		break;
1663#ifdef CONFIG_AP
1664	case EVENT_TX_STATUS:
1665		if (wpa_s->ap_iface == NULL)
1666			break;
1667		switch (data->tx_status.type) {
1668		case WLAN_FC_TYPE_MGMT:
1669			ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1670				      data->tx_status.data_len,
1671				      data->tx_status.stype,
1672				      data->tx_status.ack);
1673			break;
1674		case WLAN_FC_TYPE_DATA:
1675			ap_tx_status(wpa_s, data->tx_status.dst,
1676				     data->tx_status.data,
1677				     data->tx_status.data_len,
1678				     data->tx_status.ack);
1679			break;
1680		}
1681		break;
1682	case EVENT_RX_FROM_UNKNOWN:
1683		if (wpa_s->ap_iface == NULL)
1684			break;
1685		ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1686				       data->rx_from_unknown.len);
1687		break;
1688	case EVENT_RX_MGMT:
1689		if (wpa_s->ap_iface == NULL)
1690			break;
1691		ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1692		break;
1693#endif /* CONFIG_AP */
1694	case EVENT_RX_ACTION:
1695		wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
1696			   " Category=%u DataLen=%d freq=%d MHz",
1697			   MAC2STR(data->rx_action.sa),
1698			   data->rx_action.category, (int) data->rx_action.len,
1699			   data->rx_action.freq);
1700#ifdef CONFIG_IEEE80211R
1701		if (data->rx_action.category == WLAN_ACTION_FT) {
1702			ft_rx_action(wpa_s, data->rx_action.data,
1703				     data->rx_action.len);
1704			break;
1705		}
1706#endif /* CONFIG_IEEE80211R */
1707		break;
1708#ifdef CONFIG_CLIENT_MLME
1709	case EVENT_MLME_RX: {
1710		struct ieee80211_rx_status rx_status;
1711		os_memset(&rx_status, 0, sizeof(rx_status));
1712		rx_status.freq = data->mlme_rx.freq;
1713		rx_status.channel = data->mlme_rx.channel;
1714		rx_status.ssi = data->mlme_rx.ssi;
1715		ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
1716				 &rx_status);
1717		break;
1718	}
1719#endif /* CONFIG_CLIENT_MLME */
1720	case EVENT_EAPOL_RX:
1721		wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
1722					data->eapol_rx.data,
1723					data->eapol_rx.data_len);
1724		break;
1725	case EVENT_SIGNAL_CHANGE:
1726		bgscan_notify_signal_change(
1727			wpa_s, data->signal_change.above_threshold);
1728		break;
1729	default:
1730		wpa_printf(MSG_INFO, "Unknown event %d", event);
1731		break;
1732	}
1733}
1734