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