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