revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / WirelessManager / wpa_supplicant / events.c
blobab11b4122578e4024a9a4203c5f6d8f767a43fd6
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"
44 #ifdef MUI_GUI
45 void wpa_gui_amiga_inspect_scan_results(struct wpa_supplicant *wpa_s,
46 struct wpa_scan_results *scan_res);
47 #endif
49 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
51 struct wpa_ssid *ssid, *old_ssid;
53 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
54 return 0;
56 wpa_printf(MSG_DEBUG, "Select network based on association "
57 "information");
58 ssid = wpa_supplicant_get_ssid(wpa_s);
59 if (ssid == NULL) {
60 wpa_printf(MSG_INFO, "No network configuration found for the "
61 "current AP");
62 return -1;
65 if (ssid->disabled) {
66 wpa_printf(MSG_DEBUG, "Selected network is disabled");
67 return -1;
70 wpa_printf(MSG_DEBUG, "Network configuration found for the current "
71 "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);
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;
98 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
99 void *sock_ctx)
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);
112 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
114 int bssid_changed;
116 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
117 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
118 os_memset(wpa_s->bssid, 0, ETH_ALEN);
119 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
120 wpa_s->current_bss = NULL;
121 if (bssid_changed)
122 wpas_notify_bssid_changed(wpa_s);
124 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
125 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
126 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
127 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
128 wpa_s->ap_ies_from_associnfo = 0;
132 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
134 struct wpa_ie_data ie;
135 int pmksa_set = -1;
136 size_t i;
138 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
139 ie.pmkid == NULL)
140 return;
142 for (i = 0; i < ie.num_pmkid; i++) {
143 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
144 ie.pmkid + i * PMKID_LEN,
145 NULL, NULL, 0);
146 if (pmksa_set == 0) {
147 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
148 break;
152 wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
153 "cache", pmksa_set == 0 ? "" : "not ");
157 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
158 union wpa_event_data *data)
160 if (data == NULL) {
161 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
162 return;
164 wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
165 " index=%d preauth=%d",
166 MAC2STR(data->pmkid_candidate.bssid),
167 data->pmkid_candidate.index,
168 data->pmkid_candidate.preauth);
170 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
171 data->pmkid_candidate.index,
172 data->pmkid_candidate.preauth);
176 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
178 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
179 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
180 return 0;
182 #ifdef IEEE8021X_EAPOL
183 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
184 wpa_s->current_ssid &&
185 !(wpa_s->current_ssid->eapol_flags &
186 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
187 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
188 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
189 * plaintext or static WEP keys). */
190 return 0;
192 #endif /* IEEE8021X_EAPOL */
194 return 1;
199 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
200 * @wpa_s: pointer to wpa_supplicant data
201 * @ssid: Configuration data for the network
202 * Returns: 0 on success, -1 on failure
204 * This function is called when starting authentication with a network that is
205 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
207 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
208 struct wpa_ssid *ssid)
210 #ifdef IEEE8021X_EAPOL
211 int aka = 0, sim = 0;
212 #ifdef PCSC_FUNCS
213 int type;
214 #endif
216 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
217 return 0;
219 if (ssid->eap.eap_methods == NULL) {
220 sim = 1;
221 aka = 1;
222 } else {
223 struct eap_method_type *eap = ssid->eap.eap_methods;
224 while (eap->vendor != EAP_VENDOR_IETF ||
225 eap->method != EAP_TYPE_NONE) {
226 if (eap->vendor == EAP_VENDOR_IETF) {
227 if (eap->method == EAP_TYPE_SIM)
228 sim = 1;
229 else if (eap->method == EAP_TYPE_AKA)
230 aka = 1;
232 eap++;
236 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
237 sim = 0;
238 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
239 aka = 0;
241 if (!sim && !aka) {
242 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
243 "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
244 return 0;
247 wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
248 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
249 #ifdef PCSC_FUNCS
250 if (sim && aka)
251 type = SCARD_TRY_BOTH;
252 else if (aka)
253 type = SCARD_USIM_ONLY;
254 else
255 type = SCARD_GSM_SIM_ONLY;
257 wpa_s->scard = scard_init(type);
258 #else
259 wpa_s->scard = NULL;
260 #endif /* PCSC_FUNCS */
261 if (wpa_s->scard == NULL) {
262 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
263 "(pcsc-lite)");
264 return -1;
266 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
267 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
268 #endif /* IEEE8021X_EAPOL */
270 return 0;
274 #ifndef CONFIG_NO_SCAN_PROCESSING
275 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
276 struct wpa_ssid *ssid)
278 int i, privacy = 0;
280 if (ssid->mixed_cell)
281 return 1;
283 #ifdef CONFIG_WPS
284 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
285 return 1;
286 #endif /* CONFIG_WPS */
288 for (i = 0; i < NUM_WEP_KEYS; i++) {
289 if (ssid->wep_key_len[i]) {
290 privacy = 1;
291 break;
294 #ifdef IEEE8021X_EAPOL
295 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
296 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
297 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
298 privacy = 1;
299 #endif /* IEEE8021X_EAPOL */
301 if (bss->caps & IEEE80211_CAP_PRIVACY)
302 return privacy;
303 return !privacy;
307 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
308 struct wpa_ssid *ssid,
309 struct wpa_scan_res *bss)
311 struct wpa_ie_data ie;
312 int proto_match = 0;
313 const u8 *rsn_ie, *wpa_ie;
314 int ret;
316 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
317 if (ret >= 0)
318 return ret;
320 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
321 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
322 proto_match++;
324 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
325 wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
326 break;
328 if (!(ie.proto & ssid->proto)) {
329 wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
330 "mismatch");
331 break;
334 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
335 wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
336 "mismatch");
337 break;
340 if (!(ie.group_cipher & ssid->group_cipher)) {
341 wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
342 "mismatch");
343 break;
346 if (!(ie.key_mgmt & ssid->key_mgmt)) {
347 wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
348 "mismatch");
349 break;
352 #ifdef CONFIG_IEEE80211W
353 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
354 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
355 wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
356 "protection");
357 break;
359 #endif /* CONFIG_IEEE80211W */
361 wpa_printf(MSG_DEBUG, " selected based on RSN IE");
362 return 1;
365 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
366 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
367 proto_match++;
369 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
370 wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
371 break;
373 if (!(ie.proto & ssid->proto)) {
374 wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
375 "mismatch");
376 break;
379 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
380 wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
381 "mismatch");
382 break;
385 if (!(ie.group_cipher & ssid->group_cipher)) {
386 wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
387 "mismatch");
388 break;
391 if (!(ie.key_mgmt & ssid->key_mgmt)) {
392 wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
393 "mismatch");
394 break;
397 wpa_printf(MSG_DEBUG, " selected based on WPA IE");
398 return 1;
401 if (proto_match == 0)
402 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
404 return 0;
408 static int freq_allowed(int *freqs, int freq)
410 int i;
412 if (freqs == NULL)
413 return 1;
415 for (i = 0; freqs[i]; i++)
416 if (freqs[i] == freq)
417 return 1;
418 return 0;
422 static struct wpa_bss *
423 wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
424 struct wpa_scan_results *scan_res,
425 struct wpa_ssid *group,
426 struct wpa_ssid **selected_ssid)
428 struct wpa_ssid *ssid;
429 struct wpa_scan_res *bss;
430 size_t i;
431 struct wpa_blacklist *e;
432 const u8 *ie;
434 wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
435 for (i = 0; i < scan_res->num; i++) {
436 const u8 *ssid_;
437 u8 wpa_ie_len, rsn_ie_len, ssid_len;
438 bss = scan_res->res[i];
440 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
441 ssid_ = ie ? ie + 2 : (u8 *) "";
442 ssid_len = ie ? ie[1] : 0;
444 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
445 wpa_ie_len = ie ? ie[1] : 0;
447 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
448 rsn_ie_len = ie ? ie[1] : 0;
450 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
451 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
452 (int) i, MAC2STR(bss->bssid),
453 wpa_ssid_txt(ssid_, ssid_len),
454 wpa_ie_len, rsn_ie_len, bss->caps);
456 e = wpa_blacklist_get(wpa_s, bss->bssid);
457 if (e && e->count > 1) {
458 wpa_printf(MSG_DEBUG, " skip - blacklisted");
459 continue;
462 if (ssid_len == 0) {
463 wpa_printf(MSG_DEBUG, " skip - SSID not known");
464 continue;
467 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
468 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
469 continue;
472 for (ssid = group; ssid; ssid = ssid->pnext) {
473 int check_ssid = 1;
475 if (ssid->disabled) {
476 wpa_printf(MSG_DEBUG, " skip - disabled");
477 continue;
480 #ifdef CONFIG_WPS
481 if (ssid->ssid_len == 0 &&
482 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
483 check_ssid = 0;
484 #endif /* CONFIG_WPS */
486 if (check_ssid &&
487 (ssid_len != ssid->ssid_len ||
488 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
489 wpa_printf(MSG_DEBUG, " skip - "
490 "SSID mismatch");
491 continue;
494 if (ssid->bssid_set &&
495 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
497 wpa_printf(MSG_DEBUG, " skip - "
498 "BSSID mismatch");
499 continue;
502 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
503 continue;
505 if (!freq_allowed(ssid->freq_list, bss->freq)) {
506 wpa_printf(MSG_DEBUG, " skip - "
507 "frequency not allowed");
508 continue;
511 wpa_printf(MSG_DEBUG, " selected WPA AP "
512 MACSTR " ssid='%s'",
513 MAC2STR(bss->bssid),
514 wpa_ssid_txt(ssid_, ssid_len));
515 *selected_ssid = ssid;
516 return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
520 return NULL;
524 static struct wpa_bss *
525 wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
526 struct wpa_scan_results *scan_res,
527 struct wpa_ssid *group,
528 struct wpa_ssid **selected_ssid)
530 struct wpa_ssid *ssid;
531 struct wpa_scan_res *bss;
532 size_t i;
533 struct wpa_blacklist *e;
534 const u8 *ie;
536 wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
537 for (i = 0; i < scan_res->num; i++) {
538 const u8 *ssid_;
539 u8 wpa_ie_len, rsn_ie_len, ssid_len;
540 bss = scan_res->res[i];
542 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
543 ssid_ = ie ? ie + 2 : (u8 *) "";
544 ssid_len = ie ? ie[1] : 0;
546 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
547 wpa_ie_len = ie ? ie[1] : 0;
549 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
550 rsn_ie_len = ie ? ie[1] : 0;
552 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
553 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
554 (int) i, MAC2STR(bss->bssid),
555 wpa_ssid_txt(ssid_, ssid_len),
556 wpa_ie_len, rsn_ie_len, bss->caps);
558 e = wpa_blacklist_get(wpa_s, bss->bssid);
559 if (e && e->count > 1) {
560 wpa_printf(MSG_DEBUG, " skip - blacklisted");
561 continue;
564 if (ssid_len == 0) {
565 wpa_printf(MSG_DEBUG, " skip - SSID not known");
566 continue;
569 for (ssid = group; ssid; ssid = ssid->pnext) {
570 int check_ssid = ssid->ssid_len != 0;
572 if (ssid->disabled) {
573 wpa_printf(MSG_DEBUG, " skip - disabled");
574 continue;
577 #ifdef CONFIG_WPS
578 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
579 /* Only allow wildcard SSID match if an AP
580 * advertises active WPS operation that matches
581 * with our mode. */
582 check_ssid = 1;
583 if (ssid->ssid_len == 0 &&
584 wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
585 bss))
586 check_ssid = 0;
588 #endif /* CONFIG_WPS */
590 if (check_ssid &&
591 (ssid_len != ssid->ssid_len ||
592 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
593 wpa_printf(MSG_DEBUG, " skip - "
594 "SSID mismatch");
595 continue;
598 if (ssid->bssid_set &&
599 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
601 wpa_printf(MSG_DEBUG, " skip - "
602 "BSSID mismatch");
603 continue;
606 if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
607 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
608 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
610 wpa_printf(MSG_DEBUG, " skip - "
611 "non-WPA network not allowed");
612 continue;
615 if ((ssid->key_mgmt &
616 (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
617 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
618 WPA_KEY_MGMT_IEEE8021X_SHA256 |
619 WPA_KEY_MGMT_PSK_SHA256)) &&
620 (wpa_ie_len != 0 || rsn_ie_len != 0)) {
621 wpa_printf(MSG_DEBUG, " skip - "
622 "WPA network");
623 continue;
626 if (!wpa_supplicant_match_privacy(bss, ssid)) {
627 wpa_printf(MSG_DEBUG, " skip - "
628 "privacy mismatch");
629 continue;
632 if (bss->caps & IEEE80211_CAP_IBSS) {
633 wpa_printf(MSG_DEBUG, " skip - "
634 "IBSS (adhoc) network");
635 continue;
638 if (!freq_allowed(ssid->freq_list, bss->freq)) {
639 wpa_printf(MSG_DEBUG, " skip - "
640 "frequency not allowed");
641 continue;
644 wpa_printf(MSG_DEBUG, " selected non-WPA AP "
645 MACSTR " ssid='%s'",
646 MAC2STR(bss->bssid),
647 wpa_ssid_txt(ssid_, ssid_len));
648 *selected_ssid = ssid;
649 return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
653 return NULL;
657 static struct wpa_bss *
658 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
659 struct wpa_scan_results *scan_res,
660 struct wpa_ssid *group,
661 struct wpa_ssid **selected_ssid)
663 struct wpa_bss *selected;
665 wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
666 group->priority);
668 /* First, try to find WPA-enabled AP */
669 selected = wpa_supplicant_select_bss_wpa(wpa_s, scan_res, group,
670 selected_ssid);
671 if (selected)
672 return selected;
674 /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
675 * allows this. */
676 return wpa_supplicant_select_bss_non_wpa(wpa_s, scan_res, group,
677 selected_ssid);
681 static struct wpa_bss *
682 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
683 struct wpa_scan_results *scan_res,
684 struct wpa_ssid **selected_ssid)
686 struct wpa_bss *selected = NULL;
687 int prio;
689 while (selected == NULL) {
690 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
691 selected = wpa_supplicant_select_bss(
692 wpa_s, scan_res, wpa_s->conf->pssid[prio],
693 selected_ssid);
694 if (selected)
695 break;
698 if (selected == NULL && wpa_s->blacklist) {
699 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
700 "and try again");
701 wpa_blacklist_clear(wpa_s);
702 wpa_s->blacklist_cleared++;
703 } else if (selected == NULL)
704 break;
707 return selected;
711 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
712 int timeout_sec, int timeout_usec)
714 if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
716 * No networks are enabled; short-circuit request so
717 * we don't wait timeout seconds before transitioning
718 * to INACTIVE state.
720 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
721 return;
723 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
727 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
728 struct wpa_bss *selected,
729 struct wpa_ssid *ssid)
731 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
732 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
733 "PBC session overlap");
734 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
735 return;
739 * Do not trigger new association unless the BSSID has changed or if
740 * reassociation is requested. If we are in process of associating with
741 * the selected BSSID, do not trigger new attempt.
743 if (wpa_s->reassociate ||
744 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
745 (wpa_s->wpa_state != WPA_ASSOCIATING ||
746 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
747 0))) {
748 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
749 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
750 return;
752 wpa_supplicant_associate(wpa_s, selected, ssid);
753 } else {
754 wpa_printf(MSG_DEBUG, "Already associated with the selected "
755 "AP");
760 static struct wpa_ssid *
761 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
763 int prio;
764 struct wpa_ssid *ssid;
766 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
767 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
769 if (ssid->disabled)
770 continue;
771 if (ssid->mode == IEEE80211_MODE_IBSS ||
772 ssid->mode == IEEE80211_MODE_AP)
773 return ssid;
776 return NULL;
780 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
781 * on BSS added and BSS changed events */
782 static void wpa_supplicant_rsn_preauth_scan_results(
783 struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
785 int i;
787 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
788 return;
790 for (i = scan_res->num - 1; i >= 0; i--) {
791 const u8 *ssid, *rsn;
792 struct wpa_scan_res *r;
794 r = scan_res->res[i];
796 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
797 if (ssid == NULL)
798 continue;
800 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
801 if (rsn == NULL)
802 continue;
804 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
810 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
811 struct wpa_bss *selected,
812 struct wpa_ssid *ssid,
813 struct wpa_scan_results *scan_res)
815 size_t i;
816 struct wpa_scan_res *current_bss = NULL;
817 int min_diff;
819 if (wpa_s->reassociate)
820 return 1; /* explicit request to reassociate */
821 if (wpa_s->wpa_state < WPA_ASSOCIATED)
822 return 1; /* we are not associated; continue */
823 if (wpa_s->current_ssid == NULL)
824 return 1; /* unknown current SSID */
825 if (wpa_s->current_ssid != ssid)
826 return 1; /* different network block */
828 for (i = 0; i < scan_res->num; i++) {
829 struct wpa_scan_res *res = scan_res->res[i];
830 const u8 *ie;
831 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
832 continue;
834 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
835 if (ie == NULL)
836 continue;
837 if (ie[1] != wpa_s->current_ssid->ssid_len ||
838 os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
839 continue;
840 current_bss = res;
841 break;
844 if (!current_bss)
845 return 1; /* current BSS not seen in scan results */
847 wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
848 wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
849 MAC2STR(current_bss->bssid), current_bss->level);
850 wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
851 MAC2STR(selected->bssid), selected->level);
853 if (wpa_s->current_ssid->bssid_set &&
854 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
855 0) {
856 wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
857 "preferred BSSID");
858 return 1;
861 min_diff = 2;
862 if (current_bss->level < 0) {
863 if (current_bss->level < -85)
864 min_diff = 1;
865 else if (current_bss->level < -80)
866 min_diff = 2;
867 else if (current_bss->level < -75)
868 min_diff = 3;
869 else if (current_bss->level < -70)
870 min_diff = 4;
871 else
872 min_diff = 5;
874 if (abs(current_bss->level - selected->level) < min_diff) {
875 wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
876 "signal level");
877 return 0;
880 return 1;
884 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
885 union wpa_event_data *data)
887 struct wpa_bss *selected;
888 struct wpa_ssid *ssid = NULL;
889 struct wpa_scan_results *scan_res;
890 int ap = 0;
892 #ifdef CONFIG_AP
893 if (wpa_s->ap_iface)
894 ap = 1;
895 #endif /* CONFIG_AP */
897 wpa_supplicant_notify_scanning(wpa_s, 0);
899 scan_res = wpa_supplicant_get_scan_results(wpa_s,
900 data ? &data->scan_info :
901 NULL, 1);
902 if (scan_res == NULL) {
903 if (wpa_s->conf->ap_scan == 2 || ap)
904 return;
905 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
906 "scanning again");
907 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
908 return;
911 if (wpa_s->scan_res_handler) {
912 wpa_s->scan_res_handler(wpa_s, scan_res);
913 wpa_s->scan_res_handler = NULL;
914 wpa_scan_results_free(scan_res);
915 return;
918 if (ap) {
919 wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
920 wpa_scan_results_free(scan_res);
921 return;
924 wpa_printf(MSG_DEBUG, "New scan results available");
925 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
926 wpas_notify_scan_results(wpa_s);
927 #if MUI_GUI
928 wpa_gui_amiga_inspect_scan_results(wpa_s, scan_res);
929 #endif
931 wpas_notify_scan_done(wpa_s, 1);
933 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
934 wpa_scan_results_free(scan_res);
935 return;
938 if (wpa_s->disconnected) {
939 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
940 wpa_scan_results_free(scan_res);
941 return;
944 if (bgscan_notify_scan(wpa_s) == 1) {
945 wpa_scan_results_free(scan_res);
946 return;
949 wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
951 selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
953 if (selected) {
954 int skip;
955 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
956 scan_res);
957 wpa_scan_results_free(scan_res);
958 if (skip)
959 return;
960 wpa_supplicant_connect(wpa_s, selected, ssid);
961 } else {
962 wpa_scan_results_free(scan_res);
963 wpa_printf(MSG_DEBUG, "No suitable network found");
964 ssid = wpa_supplicant_pick_new_network(wpa_s);
965 if (ssid) {
966 wpa_printf(MSG_DEBUG, "Setup a new network");
967 wpa_supplicant_associate(wpa_s, NULL, ssid);
968 } else {
969 int timeout_sec = 5;
970 int timeout_usec = 0;
971 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
972 timeout_usec);
976 #endif /* CONFIG_NO_SCAN_PROCESSING */
979 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
980 union wpa_event_data *data)
982 int l, len, found = 0, wpa_found, rsn_found;
983 const u8 *p;
985 wpa_printf(MSG_DEBUG, "Association info event");
986 if (data->assoc_info.req_ies)
987 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
988 data->assoc_info.req_ies_len);
989 if (data->assoc_info.resp_ies)
990 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
991 data->assoc_info.resp_ies_len);
992 if (data->assoc_info.beacon_ies)
993 wpa_hexdump(MSG_DEBUG, "beacon_ies",
994 data->assoc_info.beacon_ies,
995 data->assoc_info.beacon_ies_len);
996 if (data->assoc_info.freq)
997 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
999 p = data->assoc_info.req_ies;
1000 l = data->assoc_info.req_ies_len;
1002 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1003 while (p && l >= 2) {
1004 len = p[1] + 2;
1005 if (len > l) {
1006 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1007 p, l);
1008 break;
1010 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1011 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1012 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1013 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1014 break;
1015 found = 1;
1016 wpa_find_assoc_pmkid(wpa_s);
1017 break;
1019 l -= len;
1020 p += len;
1022 if (!found && data->assoc_info.req_ies)
1023 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1025 #ifdef CONFIG_IEEE80211R
1026 #ifdef CONFIG_SME
1027 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1028 u8 bssid[ETH_ALEN];
1029 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1030 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1031 data->assoc_info.resp_ies,
1032 data->assoc_info.resp_ies_len,
1033 bssid) < 0) {
1034 wpa_printf(MSG_DEBUG, "FT: Validation of "
1035 "Reassociation Response failed");
1036 wpa_supplicant_deauthenticate(
1037 wpa_s, WLAN_REASON_INVALID_IE);
1038 return -1;
1042 p = data->assoc_info.resp_ies;
1043 l = data->assoc_info.resp_ies_len;
1045 /* Go through the IEs and make a copy of the MDIE, if present. */
1046 while (p && l >= 2) {
1047 len = p[1] + 2;
1048 if (len > l) {
1049 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1050 p, l);
1051 break;
1053 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1054 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1055 wpa_s->sme.ft_used = 1;
1056 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1057 MOBILITY_DOMAIN_ID_LEN);
1058 break;
1060 l -= len;
1061 p += len;
1063 #endif /* CONFIG_SME */
1065 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1066 data->assoc_info.resp_ies_len);
1067 #endif /* CONFIG_IEEE80211R */
1069 /* WPA/RSN IE from Beacon/ProbeResp */
1070 p = data->assoc_info.beacon_ies;
1071 l = data->assoc_info.beacon_ies_len;
1073 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1075 wpa_found = rsn_found = 0;
1076 while (p && l >= 2) {
1077 len = p[1] + 2;
1078 if (len > l) {
1079 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1080 p, l);
1081 break;
1083 if (!wpa_found &&
1084 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1085 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1086 wpa_found = 1;
1087 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1090 if (!rsn_found &&
1091 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1092 rsn_found = 1;
1093 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1096 l -= len;
1097 p += len;
1100 if (!wpa_found && data->assoc_info.beacon_ies)
1101 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1102 if (!rsn_found && data->assoc_info.beacon_ies)
1103 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1104 if (wpa_found || rsn_found)
1105 wpa_s->ap_ies_from_associnfo = 1;
1107 wpa_s->assoc_freq = data->assoc_info.freq;
1109 return 0;
1113 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1114 union wpa_event_data *data)
1116 u8 bssid[ETH_ALEN];
1117 int ft_completed;
1118 int bssid_changed;
1119 struct wpa_driver_capa capa;
1121 #ifdef CONFIG_AP
1122 if (wpa_s->ap_iface) {
1123 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1124 data->assoc_info.addr,
1125 data->assoc_info.req_ies,
1126 data->assoc_info.req_ies_len);
1127 return;
1129 #endif /* CONFIG_AP */
1131 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1132 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1133 return;
1135 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1136 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1137 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1138 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1139 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1140 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1141 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1142 MACSTR, MAC2STR(bssid));
1143 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1144 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1145 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1146 if (bssid_changed)
1147 wpas_notify_bssid_changed(wpa_s);
1149 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1150 wpa_clear_keys(wpa_s, bssid);
1152 if (wpa_supplicant_select_config(wpa_s) < 0) {
1153 wpa_supplicant_disassociate(
1154 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1155 return;
1157 if (wpa_s->current_ssid) {
1158 struct wpa_bss *bss = NULL;
1159 struct wpa_ssid *ssid = wpa_s->current_ssid;
1160 if (ssid->ssid_len > 0)
1161 bss = wpa_bss_get(wpa_s, bssid,
1162 ssid->ssid, ssid->ssid_len);
1163 if (!bss)
1164 bss = wpa_bss_get_bssid(wpa_s, bssid);
1165 if (bss)
1166 wpa_s->current_bss = bss;
1170 #ifdef CONFIG_SME
1171 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1172 wpa_s->sme.prev_bssid_set = 1;
1173 #endif /* CONFIG_SME */
1175 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1176 if (wpa_s->current_ssid) {
1177 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1178 * initialized before association, but for other modes,
1179 * initialize PC/SC here, if the current configuration needs
1180 * smartcard or SIM/USIM. */
1181 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1183 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1184 if (wpa_s->l2)
1185 l2_packet_notify_auth_start(wpa_s->l2);
1188 * Set portEnabled first to FALSE in order to get EAP state machine out
1189 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1190 * state machine may transit to AUTHENTICATING state based on obsolete
1191 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1192 * AUTHENTICATED without ever giving chance to EAP state machine to
1193 * reset the state.
1195 if (!ft_completed) {
1196 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1197 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1199 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1200 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1201 /* 802.1X::portControl = Auto */
1202 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1203 wpa_s->eapol_received = 0;
1204 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1205 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1206 (wpa_s->current_ssid &&
1207 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1208 wpa_supplicant_cancel_auth_timeout(wpa_s);
1209 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1210 } else if (!ft_completed) {
1211 /* Timeout for receiving the first EAPOL packet */
1212 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1214 wpa_supplicant_cancel_scan(wpa_s);
1216 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1217 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1219 * We are done; the driver will take care of RSN 4-way
1220 * handshake.
1222 wpa_supplicant_cancel_auth_timeout(wpa_s);
1223 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1224 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1225 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1228 if (wpa_s->pending_eapol_rx) {
1229 struct os_time now, age;
1230 os_get_time(&now);
1231 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1232 if (age.sec == 0 && age.usec < 100000 &&
1233 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1234 0) {
1235 wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1236 "that was received just before association "
1237 "notification");
1238 wpa_supplicant_rx_eapol(
1239 wpa_s, wpa_s->pending_eapol_rx_src,
1240 wpabuf_head(wpa_s->pending_eapol_rx),
1241 wpabuf_len(wpa_s->pending_eapol_rx));
1243 wpabuf_free(wpa_s->pending_eapol_rx);
1244 wpa_s->pending_eapol_rx = NULL;
1247 #ifdef CONFIG_BGSCAN
1248 if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1249 bgscan_deinit(wpa_s);
1250 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1251 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1252 wpa_printf(MSG_DEBUG, "Failed to initialize "
1253 "bgscan");
1255 * Live without bgscan; it is only used as a
1256 * roaming optimization, so the initial
1257 * connection is not affected.
1259 } else
1260 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1261 } else
1262 wpa_s->bgscan_ssid = NULL;
1264 #endif /* CONFIG_BGSCAN */
1266 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1267 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1268 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1269 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1270 /* Set static WEP keys again */
1271 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1276 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1277 u16 reason_code)
1279 const u8 *bssid;
1280 #ifdef CONFIG_SME
1281 int authenticating;
1282 u8 prev_pending_bssid[ETH_ALEN];
1284 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1285 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1286 #endif /* CONFIG_SME */
1288 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1290 * At least Host AP driver and a Prism3 card seemed to be
1291 * generating streams of disconnected events when configuring
1292 * IBSS for WPA-None. Ignore them for now.
1294 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1295 "IBSS/WPA-None mode");
1296 return;
1299 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1300 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1301 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1302 "pre-shared key may be incorrect");
1304 if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1305 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1306 bssid = wpa_s->bssid;
1307 if (is_zero_ether_addr(bssid))
1308 bssid = wpa_s->pending_bssid;
1309 wpa_blacklist_add(wpa_s, bssid);
1310 wpa_sm_notify_disassoc(wpa_s->wpa);
1311 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1312 " reason=%d",
1313 MAC2STR(bssid), reason_code);
1314 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1315 wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
1316 wpa_s->keys_cleared = 0;
1317 wpa_clear_keys(wpa_s, wpa_s->bssid);
1319 wpa_supplicant_mark_disassoc(wpa_s);
1320 bgscan_deinit(wpa_s);
1321 wpa_s->bgscan_ssid = NULL;
1322 #ifdef CONFIG_SME
1323 if (authenticating &&
1324 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1326 * mac80211-workaround to force deauth on failed auth cmd,
1327 * requires us to remain in authenticating state to allow the
1328 * second authentication attempt to be continued properly.
1330 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1331 "proceed after disconnection event");
1332 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1333 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1335 #endif /* CONFIG_SME */
1339 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1340 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1341 void *sock_ctx)
1343 struct wpa_supplicant *wpa_s = eloop_ctx;
1345 if (!wpa_s->pending_mic_error_report)
1346 return;
1348 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1349 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1350 wpa_s->pending_mic_error_report = 0;
1352 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1355 static void
1356 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1357 union wpa_event_data *data)
1359 int pairwise;
1360 struct os_time t;
1362 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1363 pairwise = (data && data->michael_mic_failure.unicast);
1364 os_get_time(&t);
1365 if ((wpa_s->last_michael_mic_error &&
1366 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1367 wpa_s->pending_mic_error_report) {
1368 if (wpa_s->pending_mic_error_report) {
1370 * Send the pending MIC error report immediately since
1371 * we are going to start countermeasures and AP better
1372 * do the same.
1374 wpa_sm_key_request(wpa_s->wpa, 1,
1375 wpa_s->pending_mic_error_pairwise);
1378 /* Send the new MIC error report immediately since we are going
1379 * to start countermeasures and AP better do the same.
1381 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1383 /* initialize countermeasures */
1384 wpa_s->countermeasures = 1;
1385 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1388 * Need to wait for completion of request frame. We do not get
1389 * any callback for the message completion, so just wait a
1390 * short while and hope for the best. */
1391 os_sleep(0, 10000);
1393 wpa_drv_set_countermeasures(wpa_s, 1);
1394 wpa_supplicant_deauthenticate(wpa_s,
1395 WLAN_REASON_MICHAEL_MIC_FAILURE);
1396 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1397 wpa_s, NULL);
1398 eloop_register_timeout(60, 0,
1399 wpa_supplicant_stop_countermeasures,
1400 wpa_s, NULL);
1401 /* TODO: mark the AP rejected for 60 second. STA is
1402 * allowed to associate with another AP.. */
1403 } else {
1404 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1405 if (wpa_s->mic_errors_seen) {
1407 * Reduce the effectiveness of Michael MIC error
1408 * reports as a means for attacking against TKIP if
1409 * more than one MIC failure is noticed with the same
1410 * PTK. We delay the transmission of the reports by a
1411 * random time between 0 and 60 seconds in order to
1412 * force the attacker wait 60 seconds before getting
1413 * the information on whether a frame resulted in a MIC
1414 * failure.
1416 u8 rval[4];
1417 int sec;
1419 if (os_get_random(rval, sizeof(rval)) < 0)
1420 sec = os_random() % 60;
1421 else
1422 sec = WPA_GET_BE32(rval) % 60;
1423 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1424 "seconds", sec);
1425 wpa_s->pending_mic_error_report = 1;
1426 wpa_s->pending_mic_error_pairwise = pairwise;
1427 eloop_cancel_timeout(
1428 wpa_supplicant_delayed_mic_error_report,
1429 wpa_s, NULL);
1430 eloop_register_timeout(
1431 sec, os_random() % 1000000,
1432 wpa_supplicant_delayed_mic_error_report,
1433 wpa_s, NULL);
1434 } else {
1435 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1437 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1438 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1439 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1441 wpa_s->last_michael_mic_error = t.sec;
1442 wpa_s->mic_errors_seen++;
1446 #ifdef CONFIG_TERMINATE_ONLASTIF
1447 static int any_interfaces(struct wpa_supplicant *head)
1449 struct wpa_supplicant *wpa_s;
1451 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1452 if (!wpa_s->interface_removed)
1453 return 1;
1454 return 0;
1456 #endif /* CONFIG_TERMINATE_ONLASTIF */
1459 static void
1460 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1461 union wpa_event_data *data)
1463 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1464 return;
1466 switch (data->interface_status.ievent) {
1467 case EVENT_INTERFACE_ADDED:
1468 if (!wpa_s->interface_removed)
1469 break;
1470 wpa_s->interface_removed = 0;
1471 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1472 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1473 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1474 "after interface was added.");
1476 break;
1477 case EVENT_INTERFACE_REMOVED:
1478 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1479 wpa_s->interface_removed = 1;
1480 wpa_supplicant_mark_disassoc(wpa_s);
1481 l2_packet_deinit(wpa_s->l2);
1482 wpa_s->l2 = NULL;
1483 #ifdef CONFIG_TERMINATE_ONLASTIF
1484 /* check if last interface */
1485 if (!any_interfaces(wpa_s->global->ifaces))
1486 eloop_terminate();
1487 #endif /* CONFIG_TERMINATE_ONLASTIF */
1488 break;
1493 #ifdef CONFIG_PEERKEY
1494 static void
1495 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1496 union wpa_event_data *data)
1498 if (data == NULL)
1499 return;
1500 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1502 #endif /* CONFIG_PEERKEY */
1505 #ifdef CONFIG_IEEE80211R
1506 static void
1507 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1508 union wpa_event_data *data)
1510 if (data == NULL)
1511 return;
1513 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1514 data->ft_ies.ies_len,
1515 data->ft_ies.ft_action,
1516 data->ft_ies.target_ap,
1517 data->ft_ies.ric_ies,
1518 data->ft_ies.ric_ies_len) < 0) {
1519 /* TODO: prevent MLME/driver from trying to associate? */
1522 #endif /* CONFIG_IEEE80211R */
1525 #ifdef CONFIG_IBSS_RSN
1526 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1527 union wpa_event_data *data)
1529 if (data == NULL)
1530 return;
1531 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1533 #endif /* CONFIG_IBSS_RSN */
1536 #ifdef CONFIG_IEEE80211R
1537 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1538 size_t len)
1540 const u8 *sta_addr, *target_ap_addr;
1541 u16 status;
1543 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1544 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1545 return; /* only SME case supported for now */
1546 if (len < 1 + 2 * ETH_ALEN + 2)
1547 return;
1548 if (data[0] != 2)
1549 return; /* Only FT Action Response is supported for now */
1550 sta_addr = data + 1;
1551 target_ap_addr = data + 1 + ETH_ALEN;
1552 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1553 wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
1554 " TargetAP " MACSTR " status %u",
1555 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1557 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1558 wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1559 " in FT Action Response", MAC2STR(sta_addr));
1560 return;
1563 if (status) {
1564 wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
1565 "failure (status code %d)", status);
1566 /* TODO: report error to FT code(?) */
1567 return;
1570 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1571 len - (1 + 2 * ETH_ALEN + 2), 1,
1572 target_ap_addr, NULL, 0) < 0)
1573 return;
1575 #ifdef CONFIG_SME
1577 struct wpa_bss *bss;
1578 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1579 if (bss)
1580 wpa_s->sme.freq = bss->freq;
1581 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1582 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1583 WLAN_AUTH_FT);
1585 #endif /* CONFIG_SME */
1587 #endif /* CONFIG_IEEE80211R */
1590 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1591 union wpa_event_data *data)
1593 struct wpa_supplicant *wpa_s = ctx;
1594 u16 reason_code = 0;
1596 switch (event) {
1597 case EVENT_AUTH:
1598 sme_event_auth(wpa_s, data);
1599 break;
1600 case EVENT_ASSOC:
1601 wpa_supplicant_event_assoc(wpa_s, data);
1602 break;
1603 case EVENT_DISASSOC:
1604 wpa_printf(MSG_DEBUG, "Disassociation notification");
1605 #ifdef CONFIG_AP
1606 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1607 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1608 data->disassoc_info.addr);
1609 break;
1611 #endif /* CONFIG_AP */
1612 if (data)
1613 reason_code = data->deauth_info.reason_code;
1614 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1615 sme_event_disassoc(wpa_s, data);
1616 /* fall through */
1617 case EVENT_DEAUTH:
1618 if (event == EVENT_DEAUTH) {
1619 wpa_printf(MSG_DEBUG, "Deauthentication notification");
1620 if (data)
1621 reason_code = data->deauth_info.reason_code;
1623 #ifdef CONFIG_AP
1624 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1625 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1626 data->deauth_info.addr);
1627 break;
1629 #endif /* CONFIG_AP */
1630 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1631 break;
1632 case EVENT_MICHAEL_MIC_FAILURE:
1633 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1634 break;
1635 #ifndef CONFIG_NO_SCAN_PROCESSING
1636 case EVENT_SCAN_RESULTS:
1637 wpa_supplicant_event_scan_results(wpa_s, data);
1638 break;
1639 #endif /* CONFIG_NO_SCAN_PROCESSING */
1640 case EVENT_ASSOCINFO:
1641 wpa_supplicant_event_associnfo(wpa_s, data);
1642 break;
1643 case EVENT_INTERFACE_STATUS:
1644 wpa_supplicant_event_interface_status(wpa_s, data);
1645 break;
1646 case EVENT_PMKID_CANDIDATE:
1647 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1648 break;
1649 #ifdef CONFIG_PEERKEY
1650 case EVENT_STKSTART:
1651 wpa_supplicant_event_stkstart(wpa_s, data);
1652 break;
1653 #endif /* CONFIG_PEERKEY */
1654 #ifdef CONFIG_IEEE80211R
1655 case EVENT_FT_RESPONSE:
1656 wpa_supplicant_event_ft_response(wpa_s, data);
1657 break;
1658 #endif /* CONFIG_IEEE80211R */
1659 #ifdef CONFIG_IBSS_RSN
1660 case EVENT_IBSS_RSN_START:
1661 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1662 break;
1663 #endif /* CONFIG_IBSS_RSN */
1664 case EVENT_ASSOC_REJECT:
1665 sme_event_assoc_reject(wpa_s, data);
1666 break;
1667 case EVENT_AUTH_TIMED_OUT:
1668 sme_event_auth_timed_out(wpa_s, data);
1669 break;
1670 case EVENT_ASSOC_TIMED_OUT:
1671 sme_event_assoc_timed_out(wpa_s, data);
1672 break;
1673 #ifdef CONFIG_AP
1674 case EVENT_TX_STATUS:
1675 if (wpa_s->ap_iface == NULL)
1676 break;
1677 switch (data->tx_status.type) {
1678 case WLAN_FC_TYPE_MGMT:
1679 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1680 data->tx_status.data_len,
1681 data->tx_status.stype,
1682 data->tx_status.ack);
1683 break;
1684 case WLAN_FC_TYPE_DATA:
1685 ap_tx_status(wpa_s, data->tx_status.dst,
1686 data->tx_status.data,
1687 data->tx_status.data_len,
1688 data->tx_status.ack);
1689 break;
1691 break;
1692 case EVENT_RX_FROM_UNKNOWN:
1693 if (wpa_s->ap_iface == NULL)
1694 break;
1695 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1696 data->rx_from_unknown.len);
1697 break;
1698 case EVENT_RX_MGMT:
1699 if (wpa_s->ap_iface == NULL)
1700 break;
1701 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1702 break;
1703 #endif /* CONFIG_AP */
1704 case EVENT_RX_ACTION:
1705 wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
1706 " Category=%u DataLen=%d freq=%d MHz",
1707 MAC2STR(data->rx_action.sa),
1708 data->rx_action.category, (int) data->rx_action.len,
1709 data->rx_action.freq);
1710 #ifdef CONFIG_IEEE80211R
1711 if (data->rx_action.category == WLAN_ACTION_FT) {
1712 ft_rx_action(wpa_s, data->rx_action.data,
1713 data->rx_action.len);
1714 break;
1716 #endif /* CONFIG_IEEE80211R */
1717 break;
1718 #ifdef CONFIG_CLIENT_MLME
1719 case EVENT_MLME_RX: {
1720 struct ieee80211_rx_status rx_status;
1721 os_memset(&rx_status, 0, sizeof(rx_status));
1722 rx_status.freq = data->mlme_rx.freq;
1723 rx_status.channel = data->mlme_rx.channel;
1724 rx_status.ssi = data->mlme_rx.ssi;
1725 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
1726 &rx_status);
1727 break;
1729 #endif /* CONFIG_CLIENT_MLME */
1730 case EVENT_EAPOL_RX:
1731 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
1732 data->eapol_rx.data,
1733 data->eapol_rx.data_len);
1734 break;
1735 case EVENT_SIGNAL_CHANGE:
1736 bgscan_notify_signal_change(
1737 wpa_s, data->signal_change.above_threshold);
1738 break;
1739 default:
1740 wpa_printf(MSG_INFO, "Unknown event %d", event);
1741 break;