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