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