80a9f858ccf41061daee09baad77ec83124d4c99
[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_supplicant_enabled_networks(wpa_s->conf)) {
709                 /*
710                  * No networks are enabled; short-circuit request so
711                  * we don't wait timeout seconds before transitioning
712                  * to INACTIVE state.
713                  */
714                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
715                 return;
716         }
717         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
718 }
719
720
721 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
722                             struct wpa_bss *selected,
723                             struct wpa_ssid *ssid)
724 {
725         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
726                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
727                         "PBC session overlap");
728                 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
729                 return;
730         }
731
732         /*
733          * Do not trigger new association unless the BSSID has changed or if
734          * reassociation is requested. If we are in process of associating with
735          * the selected BSSID, do not trigger new attempt.
736          */
737         if (wpa_s->reassociate ||
738             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
739              (wpa_s->wpa_state != WPA_ASSOCIATING ||
740               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
741               0))) {
742                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
743                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
744                         return;
745                 }
746                 wpa_supplicant_associate(wpa_s, selected, ssid);
747         } else {
748                 wpa_printf(MSG_DEBUG, "Already associated with the selected "
749                            "AP");
750         }
751 }
752
753
754 static struct wpa_ssid *
755 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
756 {
757         int prio;
758         struct wpa_ssid *ssid;
759
760         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
761                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
762                 {
763                         if (ssid->disabled)
764                                 continue;
765                         if (ssid->mode == IEEE80211_MODE_IBSS ||
766                             ssid->mode == IEEE80211_MODE_AP)
767                                 return ssid;
768                 }
769         }
770         return NULL;
771 }
772
773
774 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
775  * on BSS added and BSS changed events */
776 static void wpa_supplicant_rsn_preauth_scan_results(
777         struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
778 {
779         int i;
780
781         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
782                 return;
783
784         for (i = scan_res->num - 1; i >= 0; i--) {
785                 const u8 *ssid, *rsn;
786                 struct wpa_scan_res *r;
787
788                 r = scan_res->res[i];
789
790                 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
791                 if (ssid == NULL)
792                         continue;
793
794                 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
795                 if (rsn == NULL)
796                         continue;
797
798                 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
799         }
800
801 }
802
803
804 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
805                                        struct wpa_bss *selected,
806                                        struct wpa_ssid *ssid,
807                                        struct wpa_scan_results *scan_res)
808 {
809         size_t i;
810         struct wpa_scan_res *current_bss = NULL;
811         int min_diff;
812
813         if (wpa_s->reassociate)
814                 return 1; /* explicit request to reassociate */
815         if (wpa_s->wpa_state < WPA_ASSOCIATED)
816                 return 1; /* we are not associated; continue */
817         if (wpa_s->current_ssid == NULL)
818                 return 1; /* unknown current SSID */
819         if (wpa_s->current_ssid != ssid)
820                 return 1; /* different network block */
821
822         for (i = 0; i < scan_res->num; i++) {
823                 struct wpa_scan_res *res = scan_res->res[i];
824                 const u8 *ie;
825                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
826                         continue;
827
828                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
829                 if (ie == NULL)
830                         continue;
831                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
832                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
833                         continue;
834                 current_bss = res;
835                 break;
836         }
837
838         if (!current_bss)
839                 return 1; /* current BSS not seen in scan results */
840
841         wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
842         wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
843                    MAC2STR(current_bss->bssid), current_bss->level);
844         wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
845                    MAC2STR(selected->bssid), selected->level);
846
847         if (wpa_s->current_ssid->bssid_set &&
848             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
849             0) {
850                 wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
851                            "preferred BSSID");
852                 return 1;
853         }
854
855         min_diff = 2;
856         if (current_bss->level < 0) {
857                 if (current_bss->level < -85)
858                         min_diff = 1;
859                 else if (current_bss->level < -80)
860                         min_diff = 2;
861                 else if (current_bss->level < -75)
862                         min_diff = 3;
863                 else if (current_bss->level < -70)
864                         min_diff = 4;
865                 else
866                         min_diff = 5;
867         }
868         if (abs(current_bss->level - selected->level) < min_diff) {
869                 wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
870                            "signal level");
871                 return 0;
872         }
873
874         return 1;
875 }
876
877
878 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
879                                               union wpa_event_data *data)
880 {
881         struct wpa_bss *selected;
882         struct wpa_ssid *ssid = NULL;
883         struct wpa_scan_results *scan_res;
884         int ap = 0;
885
886 #ifdef CONFIG_AP
887         if (wpa_s->ap_iface)
888                 ap = 1;
889 #endif /* CONFIG_AP */
890
891         wpa_supplicant_notify_scanning(wpa_s, 0);
892
893         scan_res = wpa_supplicant_get_scan_results(wpa_s,
894                                                    data ? &data->scan_info :
895                                                    NULL, 1);
896         if (scan_res == NULL) {
897                 if (wpa_s->conf->ap_scan == 2 || ap)
898                         return;
899                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
900                            "scanning again");
901                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
902                 return;
903         }
904
905         if (wpa_s->scan_res_handler) {
906                 wpa_s->scan_res_handler(wpa_s, scan_res);
907                 wpa_s->scan_res_handler = NULL;
908                 wpa_scan_results_free(scan_res);
909                 return;
910         }
911
912         if (ap) {
913                 wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
914                 wpa_scan_results_free(scan_res);
915                 return;
916         }
917
918         wpa_printf(MSG_DEBUG, "New scan results available");
919         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
920         wpas_notify_scan_results(wpa_s);
921
922         wpas_notify_scan_done(wpa_s, 1);
923
924         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
925                 wpa_scan_results_free(scan_res);
926                 return;
927         }
928
929         if (wpa_s->disconnected) {
930                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
931                 wpa_scan_results_free(scan_res);
932                 return;
933         }
934
935         if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
936                 wpa_scan_results_free(scan_res);
937                 return;
938         }
939
940         wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
941
942         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
943
944         if (selected) {
945                 int skip;
946                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
947                                                     scan_res);
948                 wpa_scan_results_free(scan_res);
949                 if (skip)
950                         return;
951                 wpa_supplicant_connect(wpa_s, selected, ssid);
952         } else {
953                 wpa_scan_results_free(scan_res);
954                 wpa_printf(MSG_DEBUG, "No suitable network found");
955                 ssid = wpa_supplicant_pick_new_network(wpa_s);
956                 if (ssid) {
957                         wpa_printf(MSG_DEBUG, "Setup a new network");
958                         wpa_supplicant_associate(wpa_s, NULL, ssid);
959                 } else {
960                         int timeout_sec = 5;
961                         int timeout_usec = 0;
962                         wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
963                                                     timeout_usec);
964                 }
965         }
966 }
967 #endif /* CONFIG_NO_SCAN_PROCESSING */
968
969
970 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
971                                           union wpa_event_data *data)
972 {
973         int l, len, found = 0, wpa_found, rsn_found;
974         const u8 *p;
975
976         wpa_printf(MSG_DEBUG, "Association info event");
977         if (data->assoc_info.req_ies)
978                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
979                             data->assoc_info.req_ies_len);
980         if (data->assoc_info.resp_ies)
981                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
982                             data->assoc_info.resp_ies_len);
983         if (data->assoc_info.beacon_ies)
984                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
985                             data->assoc_info.beacon_ies,
986                             data->assoc_info.beacon_ies_len);
987         if (data->assoc_info.freq)
988                 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
989
990         p = data->assoc_info.req_ies;
991         l = data->assoc_info.req_ies_len;
992
993         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
994         while (p && l >= 2) {
995                 len = p[1] + 2;
996                 if (len > l) {
997                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
998                                     p, l);
999                         break;
1000                 }
1001                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1002                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1003                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1004                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1005                                 break;
1006                         found = 1;
1007                         wpa_find_assoc_pmkid(wpa_s);
1008                         break;
1009                 }
1010                 l -= len;
1011                 p += len;
1012         }
1013         if (!found && data->assoc_info.req_ies)
1014                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1015
1016 #ifdef CONFIG_IEEE80211R
1017 #ifdef CONFIG_SME
1018         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1019                 u8 bssid[ETH_ALEN];
1020                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1021                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1022                                                  data->assoc_info.resp_ies,
1023                                                  data->assoc_info.resp_ies_len,
1024                                                  bssid) < 0) {
1025                         wpa_printf(MSG_DEBUG, "FT: Validation of "
1026                                    "Reassociation Response failed");
1027                         wpa_supplicant_deauthenticate(
1028                                 wpa_s, WLAN_REASON_INVALID_IE);
1029                         return -1;
1030                 }
1031         }
1032
1033         p = data->assoc_info.resp_ies;
1034         l = data->assoc_info.resp_ies_len;
1035
1036         /* Go through the IEs and make a copy of the MDIE, if present. */
1037         while (p && l >= 2) {
1038                 len = p[1] + 2;
1039                 if (len > l) {
1040                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1041                                     p, l);
1042                         break;
1043                 }
1044                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1045                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1046                         wpa_s->sme.ft_used = 1;
1047                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1048                                   MOBILITY_DOMAIN_ID_LEN);
1049                         break;
1050                 }
1051                 l -= len;
1052                 p += len;
1053         }
1054 #endif /* CONFIG_SME */
1055
1056         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1057                              data->assoc_info.resp_ies_len);
1058 #endif /* CONFIG_IEEE80211R */
1059
1060         /* WPA/RSN IE from Beacon/ProbeResp */
1061         p = data->assoc_info.beacon_ies;
1062         l = data->assoc_info.beacon_ies_len;
1063
1064         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1065          */
1066         wpa_found = rsn_found = 0;
1067         while (p && l >= 2) {
1068                 len = p[1] + 2;
1069                 if (len > l) {
1070                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1071                                     p, l);
1072                         break;
1073                 }
1074                 if (!wpa_found &&
1075                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1076                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1077                         wpa_found = 1;
1078                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1079                 }
1080
1081                 if (!rsn_found &&
1082                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1083                         rsn_found = 1;
1084                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1085                 }
1086
1087                 l -= len;
1088                 p += len;
1089         }
1090
1091         if (!wpa_found && data->assoc_info.beacon_ies)
1092                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1093         if (!rsn_found && data->assoc_info.beacon_ies)
1094                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1095         if (wpa_found || rsn_found)
1096                 wpa_s->ap_ies_from_associnfo = 1;
1097
1098         wpa_s->assoc_freq = data->assoc_info.freq;
1099
1100         return 0;
1101 }
1102
1103
1104 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1105                                        union wpa_event_data *data)
1106 {
1107         u8 bssid[ETH_ALEN];
1108         int ft_completed;
1109         int bssid_changed;
1110         struct wpa_driver_capa capa;
1111
1112 #ifdef CONFIG_AP
1113         if (wpa_s->ap_iface) {
1114                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1115                                     data->assoc_info.addr,
1116                                     data->assoc_info.req_ies,
1117                                     data->assoc_info.req_ies_len);
1118                 return;
1119         }
1120 #endif /* CONFIG_AP */
1121
1122         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1123         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1124                 return;
1125
1126         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1127         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1128                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1129         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1130             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1131              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1132                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1133                         MACSTR, MAC2STR(bssid));
1134                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1135                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1136                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1137                 if (bssid_changed)
1138                         wpas_notify_bssid_changed(wpa_s);
1139
1140                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1141                         wpa_clear_keys(wpa_s, bssid);
1142                 }
1143                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1144                         wpa_supplicant_disassociate(
1145                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1146                         return;
1147                 }
1148                 if (wpa_s->current_ssid) {
1149                         struct wpa_bss *bss = NULL;
1150                         struct wpa_ssid *ssid = wpa_s->current_ssid;
1151                         if (ssid->ssid_len > 0)
1152                                 bss = wpa_bss_get(wpa_s, bssid,
1153                                                   ssid->ssid, ssid->ssid_len);
1154                         if (!bss)
1155                                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1156                         if (bss)
1157                                 wpa_s->current_bss = bss;
1158                 }
1159         }
1160
1161 #ifdef CONFIG_SME
1162         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1163         wpa_s->sme.prev_bssid_set = 1;
1164 #endif /* CONFIG_SME */
1165
1166         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1167         if (wpa_s->current_ssid) {
1168                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1169                  * initialized before association, but for other modes,
1170                  * initialize PC/SC here, if the current configuration needs
1171                  * smartcard or SIM/USIM. */
1172                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1173         }
1174         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1175         if (wpa_s->l2)
1176                 l2_packet_notify_auth_start(wpa_s->l2);
1177
1178         /*
1179          * Set portEnabled first to FALSE in order to get EAP state machine out
1180          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1181          * state machine may transit to AUTHENTICATING state based on obsolete
1182          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1183          * AUTHENTICATED without ever giving chance to EAP state machine to
1184          * reset the state.
1185          */
1186         if (!ft_completed) {
1187                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1188                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1189         }
1190         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1191                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1192         /* 802.1X::portControl = Auto */
1193         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1194         wpa_s->eapol_received = 0;
1195         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1196             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1197             (wpa_s->current_ssid &&
1198              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1199                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1200                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1201         } else if (!ft_completed) {
1202                 /* Timeout for receiving the first EAPOL packet */
1203                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1204         }
1205         wpa_supplicant_cancel_scan(wpa_s);
1206
1207         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1208             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1209                 /*
1210                  * We are done; the driver will take care of RSN 4-way
1211                  * handshake.
1212                  */
1213                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1214                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1215                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1216                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1217         }
1218
1219         if (wpa_s->pending_eapol_rx) {
1220                 struct os_time now, age;
1221                 os_get_time(&now);
1222                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1223                 if (age.sec == 0 && age.usec < 100000 &&
1224                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1225                     0) {
1226                         wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1227                                    "that was received just before association "
1228                                    "notification");
1229                         wpa_supplicant_rx_eapol(
1230                                 wpa_s, wpa_s->pending_eapol_rx_src,
1231                                 wpabuf_head(wpa_s->pending_eapol_rx),
1232                                 wpabuf_len(wpa_s->pending_eapol_rx));
1233                 }
1234                 wpabuf_free(wpa_s->pending_eapol_rx);
1235                 wpa_s->pending_eapol_rx = NULL;
1236         }
1237
1238 #ifdef CONFIG_BGSCAN
1239         if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1240                 bgscan_deinit(wpa_s);
1241                 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1242                         if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1243                                 wpa_printf(MSG_DEBUG, "Failed to initialize "
1244                                            "bgscan");
1245                                 /*
1246                                  * Live without bgscan; it is only used as a
1247                                  * roaming optimization, so the initial
1248                                  * connection is not affected.
1249                                  */
1250                         } else
1251                                 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1252                 } else
1253                         wpa_s->bgscan_ssid = NULL;
1254         }
1255 #endif /* CONFIG_BGSCAN */
1256
1257         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1258              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1259             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1260             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1261                 /* Set static WEP keys again */
1262                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1263         }
1264 }
1265
1266
1267 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1268                                           u16 reason_code)
1269 {
1270         const u8 *bssid;
1271 #ifdef CONFIG_SME
1272         int authenticating;
1273         u8 prev_pending_bssid[ETH_ALEN];
1274
1275         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1276         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1277 #endif /* CONFIG_SME */
1278
1279         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1280                 /*
1281                  * At least Host AP driver and a Prism3 card seemed to be
1282                  * generating streams of disconnected events when configuring
1283                  * IBSS for WPA-None. Ignore them for now.
1284                  */
1285                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1286                            "IBSS/WPA-None mode");
1287                 return;
1288         }
1289
1290         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1291             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1292                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1293                         "pre-shared key may be incorrect");
1294         }
1295         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1296                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1297         bssid = wpa_s->bssid;
1298         if (is_zero_ether_addr(bssid))
1299                 bssid = wpa_s->pending_bssid;
1300         wpa_blacklist_add(wpa_s, bssid);
1301         wpa_sm_notify_disassoc(wpa_s->wpa);
1302         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1303                 " reason=%d",
1304                 MAC2STR(bssid), reason_code);
1305         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1306                 wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
1307                 wpa_s->keys_cleared = 0;
1308                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1309         }
1310         wpa_supplicant_mark_disassoc(wpa_s);
1311         bgscan_deinit(wpa_s);
1312         wpa_s->bgscan_ssid = NULL;
1313 #ifdef CONFIG_SME
1314         if (authenticating &&
1315             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1316                 /*
1317                  * mac80211-workaround to force deauth on failed auth cmd,
1318                  * requires us to remain in authenticating state to allow the
1319                  * second authentication attempt to be continued properly.
1320                  */
1321                 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1322                            "proceed after disconnection event");
1323                 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1324                 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1325         }
1326 #endif /* CONFIG_SME */
1327 }
1328
1329
1330 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1331 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1332                                                     void *sock_ctx)
1333 {
1334         struct wpa_supplicant *wpa_s = eloop_ctx;
1335
1336         if (!wpa_s->pending_mic_error_report)
1337                 return;
1338
1339         wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1340         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1341         wpa_s->pending_mic_error_report = 0;
1342 }
1343 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1344
1345
1346 static void
1347 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1348                                          union wpa_event_data *data)
1349 {
1350         int pairwise;
1351         struct os_time t;
1352
1353         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1354         pairwise = (data && data->michael_mic_failure.unicast);
1355         os_get_time(&t);
1356         if ((wpa_s->last_michael_mic_error &&
1357              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1358             wpa_s->pending_mic_error_report) {
1359                 if (wpa_s->pending_mic_error_report) {
1360                         /*
1361                          * Send the pending MIC error report immediately since
1362                          * we are going to start countermeasures and AP better
1363                          * do the same.
1364                          */
1365                         wpa_sm_key_request(wpa_s->wpa, 1,
1366                                            wpa_s->pending_mic_error_pairwise);
1367                 }
1368
1369                 /* Send the new MIC error report immediately since we are going
1370                  * to start countermeasures and AP better do the same.
1371                  */
1372                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1373
1374                 /* initialize countermeasures */
1375                 wpa_s->countermeasures = 1;
1376                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1377
1378                 /*
1379                  * Need to wait for completion of request frame. We do not get
1380                  * any callback for the message completion, so just wait a
1381                  * short while and hope for the best. */
1382                 os_sleep(0, 10000);
1383
1384                 wpa_drv_set_countermeasures(wpa_s, 1);
1385                 wpa_supplicant_deauthenticate(wpa_s,
1386                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1387                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1388                                      wpa_s, NULL);
1389                 eloop_register_timeout(60, 0,
1390                                        wpa_supplicant_stop_countermeasures,
1391                                        wpa_s, NULL);
1392                 /* TODO: mark the AP rejected for 60 second. STA is
1393                  * allowed to associate with another AP.. */
1394         } else {
1395 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1396                 if (wpa_s->mic_errors_seen) {
1397                         /*
1398                          * Reduce the effectiveness of Michael MIC error
1399                          * reports as a means for attacking against TKIP if
1400                          * more than one MIC failure is noticed with the same
1401                          * PTK. We delay the transmission of the reports by a
1402                          * random time between 0 and 60 seconds in order to
1403                          * force the attacker wait 60 seconds before getting
1404                          * the information on whether a frame resulted in a MIC
1405                          * failure.
1406                          */
1407                         u8 rval[4];
1408                         int sec;
1409
1410                         if (os_get_random(rval, sizeof(rval)) < 0)
1411                                 sec = os_random() % 60;
1412                         else
1413                                 sec = WPA_GET_BE32(rval) % 60;
1414                         wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1415                                    "seconds", sec);
1416                         wpa_s->pending_mic_error_report = 1;
1417                         wpa_s->pending_mic_error_pairwise = pairwise;
1418                         eloop_cancel_timeout(
1419                                 wpa_supplicant_delayed_mic_error_report,
1420                                 wpa_s, NULL);
1421                         eloop_register_timeout(
1422                                 sec, os_random() % 1000000,
1423                                 wpa_supplicant_delayed_mic_error_report,
1424                                 wpa_s, NULL);
1425                 } else {
1426                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1427                 }
1428 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1429                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1430 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1431         }
1432         wpa_s->last_michael_mic_error = t.sec;
1433         wpa_s->mic_errors_seen++;
1434 }
1435
1436
1437 #ifdef CONFIG_TERMINATE_ONLASTIF
1438 static int any_interfaces(struct wpa_supplicant *head)
1439 {
1440         struct wpa_supplicant *wpa_s;
1441
1442         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1443                 if (!wpa_s->interface_removed)
1444                         return 1;
1445         return 0;
1446 }
1447 #endif /* CONFIG_TERMINATE_ONLASTIF */
1448
1449
1450 static void
1451 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1452                                       union wpa_event_data *data)
1453 {
1454         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1455                 return;
1456
1457         switch (data->interface_status.ievent) {
1458         case EVENT_INTERFACE_ADDED:
1459                 if (!wpa_s->interface_removed)
1460                         break;
1461                 wpa_s->interface_removed = 0;
1462                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1463                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1464                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
1465                                    "after interface was added.");
1466                 }
1467                 break;
1468         case EVENT_INTERFACE_REMOVED:
1469                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1470                 wpa_s->interface_removed = 1;
1471                 wpa_supplicant_mark_disassoc(wpa_s);
1472                 l2_packet_deinit(wpa_s->l2);
1473                 wpa_s->l2 = NULL;
1474 #ifdef CONFIG_TERMINATE_ONLASTIF
1475                 /* check if last interface */
1476                 if (!any_interfaces(wpa_s->global->ifaces))
1477                         eloop_terminate();
1478 #endif /* CONFIG_TERMINATE_ONLASTIF */
1479                 break;
1480         }
1481 }
1482
1483
1484 #ifdef CONFIG_PEERKEY
1485 static void
1486 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1487                               union wpa_event_data *data)
1488 {
1489         if (data == NULL)
1490                 return;
1491         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1492 }
1493 #endif /* CONFIG_PEERKEY */
1494
1495
1496 #ifdef CONFIG_IEEE80211R
1497 static void
1498 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1499                                  union wpa_event_data *data)
1500 {
1501         if (data == NULL)
1502                 return;
1503
1504         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1505                                     data->ft_ies.ies_len,
1506                                     data->ft_ies.ft_action,
1507                                     data->ft_ies.target_ap,
1508                                     data->ft_ies.ric_ies,
1509                                     data->ft_ies.ric_ies_len) < 0) {
1510                 /* TODO: prevent MLME/driver from trying to associate? */
1511         }
1512 }
1513 #endif /* CONFIG_IEEE80211R */
1514
1515
1516 #ifdef CONFIG_IBSS_RSN
1517 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1518                                                 union wpa_event_data *data)
1519 {
1520         if (data == NULL)
1521                 return;
1522         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1523 }
1524 #endif /* CONFIG_IBSS_RSN */
1525
1526
1527 #ifdef CONFIG_IEEE80211R
1528 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1529                          size_t len)
1530 {
1531         const u8 *sta_addr, *target_ap_addr;
1532         u16 status;
1533
1534         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1535         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1536                 return; /* only SME case supported for now */
1537         if (len < 1 + 2 * ETH_ALEN + 2)
1538                 return;
1539         if (data[0] != 2)
1540                 return; /* Only FT Action Response is supported for now */
1541         sta_addr = data + 1;
1542         target_ap_addr = data + 1 + ETH_ALEN;
1543         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1544         wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
1545                    " TargetAP " MACSTR " status %u",
1546                    MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1547
1548         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1549                 wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1550                            " in FT Action Response", MAC2STR(sta_addr));
1551                 return;
1552         }
1553
1554         if (status) {
1555                 wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
1556                            "failure (status code %d)", status);
1557                 /* TODO: report error to FT code(?) */
1558                 return;
1559         }
1560
1561         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1562                                     len - (1 + 2 * ETH_ALEN + 2), 1,
1563                                     target_ap_addr, NULL, 0) < 0)
1564                 return;
1565
1566 #ifdef CONFIG_SME
1567         {
1568                 struct wpa_bss *bss;
1569                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1570                 if (bss)
1571                         wpa_s->sme.freq = bss->freq;
1572                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1573                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1574                               WLAN_AUTH_FT);
1575         }
1576 #endif /* CONFIG_SME */
1577 }
1578 #endif /* CONFIG_IEEE80211R */
1579
1580
1581 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1582                           union wpa_event_data *data)
1583 {
1584         struct wpa_supplicant *wpa_s = ctx;
1585         u16 reason_code = 0;
1586
1587         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1588             event != EVENT_INTERFACE_ENABLED &&
1589             event != EVENT_INTERFACE_STATUS) {
1590                 wpa_printf(MSG_DEBUG, "Ignore event %d while interface is "
1591                            "disabled", event);
1592                 return;
1593         }
1594
1595         wpa_printf(MSG_DEBUG, "Event %d received on interface %s",
1596                    event, wpa_s->ifname);
1597
1598         switch (event) {
1599         case EVENT_AUTH:
1600                 sme_event_auth(wpa_s, data);
1601                 break;
1602         case EVENT_ASSOC:
1603                 wpa_supplicant_event_assoc(wpa_s, data);
1604                 break;
1605         case EVENT_DISASSOC:
1606                 wpa_printf(MSG_DEBUG, "Disassociation notification");
1607                 if (data) {
1608                         wpa_printf(MSG_DEBUG, " * reason %u",
1609                                    data->disassoc_info.reason_code);
1610                         if (data->disassoc_info.addr)
1611                                 wpa_printf(MSG_DEBUG, " * address " MACSTR,
1612                                            MAC2STR(data->disassoc_info.addr));
1613                 }
1614 #ifdef CONFIG_AP
1615                 if (wpa_s->ap_iface && data) {
1616                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1617                                                data->disassoc_info.addr);
1618                         break;
1619                 }
1620 #endif /* CONFIG_AP */
1621                 if (data)
1622                         reason_code = data->deauth_info.reason_code;
1623                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1624                         sme_event_disassoc(wpa_s, data);
1625                 /* fall through */
1626         case EVENT_DEAUTH:
1627                 if (event == EVENT_DEAUTH) {
1628                         wpa_printf(MSG_DEBUG, "Deauthentication notification");
1629                         if (data) {
1630                                 reason_code = data->deauth_info.reason_code;
1631                                 wpa_printf(MSG_DEBUG, " * reason %u",
1632                                            data->deauth_info.reason_code);
1633                                 if (data->deauth_info.addr) {
1634                                         wpa_printf(MSG_DEBUG, " * address "
1635                                                    MACSTR,
1636                                                    MAC2STR(data->deauth_info.
1637                                                            addr));
1638                                 }
1639                         }
1640                 }
1641 #ifdef CONFIG_AP
1642                 if (wpa_s->ap_iface && data) {
1643                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1644                                                data->deauth_info.addr);
1645                         break;
1646                 }
1647 #endif /* CONFIG_AP */
1648                 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1649                 break;
1650         case EVENT_MICHAEL_MIC_FAILURE:
1651                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1652                 break;
1653 #ifndef CONFIG_NO_SCAN_PROCESSING
1654         case EVENT_SCAN_RESULTS:
1655                 wpa_supplicant_event_scan_results(wpa_s, data);
1656                 break;
1657 #endif /* CONFIG_NO_SCAN_PROCESSING */
1658         case EVENT_ASSOCINFO:
1659                 wpa_supplicant_event_associnfo(wpa_s, data);
1660                 break;
1661         case EVENT_INTERFACE_STATUS:
1662                 wpa_supplicant_event_interface_status(wpa_s, data);
1663                 break;
1664         case EVENT_PMKID_CANDIDATE:
1665                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1666                 break;
1667 #ifdef CONFIG_PEERKEY
1668         case EVENT_STKSTART:
1669                 wpa_supplicant_event_stkstart(wpa_s, data);
1670                 break;
1671 #endif /* CONFIG_PEERKEY */
1672 #ifdef CONFIG_IEEE80211R
1673         case EVENT_FT_RESPONSE:
1674                 wpa_supplicant_event_ft_response(wpa_s, data);
1675                 break;
1676 #endif /* CONFIG_IEEE80211R */
1677 #ifdef CONFIG_IBSS_RSN
1678         case EVENT_IBSS_RSN_START:
1679                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1680                 break;
1681 #endif /* CONFIG_IBSS_RSN */
1682         case EVENT_ASSOC_REJECT:
1683                 sme_event_assoc_reject(wpa_s, data);
1684                 break;
1685         case EVENT_AUTH_TIMED_OUT:
1686                 sme_event_auth_timed_out(wpa_s, data);
1687                 break;
1688         case EVENT_ASSOC_TIMED_OUT:
1689                 sme_event_assoc_timed_out(wpa_s, data);
1690                 break;
1691 #ifdef CONFIG_AP
1692         case EVENT_TX_STATUS:
1693                 if (wpa_s->ap_iface == NULL)
1694                         break;
1695                 switch (data->tx_status.type) {
1696                 case WLAN_FC_TYPE_MGMT:
1697                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1698                                       data->tx_status.data_len,
1699                                       data->tx_status.stype,
1700                                       data->tx_status.ack);
1701                         break;
1702                 case WLAN_FC_TYPE_DATA:
1703                         ap_tx_status(wpa_s, data->tx_status.dst,
1704                                      data->tx_status.data,
1705                                      data->tx_status.data_len,
1706                                      data->tx_status.ack);
1707                         break;
1708                 }
1709                 break;
1710         case EVENT_RX_FROM_UNKNOWN:
1711                 if (wpa_s->ap_iface == NULL)
1712                         break;
1713                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1714                                        data->rx_from_unknown.len);
1715                 break;
1716         case EVENT_RX_MGMT:
1717                 if (wpa_s->ap_iface == NULL)
1718                         break;
1719                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1720                 break;
1721 #endif /* CONFIG_AP */
1722         case EVENT_RX_ACTION:
1723                 wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
1724                            " Category=%u DataLen=%d freq=%d MHz",
1725                            MAC2STR(data->rx_action.sa),
1726                            data->rx_action.category, (int) data->rx_action.len,
1727                            data->rx_action.freq);
1728 #ifdef CONFIG_IEEE80211R
1729                 if (data->rx_action.category == WLAN_ACTION_FT) {
1730                         ft_rx_action(wpa_s, data->rx_action.data,
1731                                      data->rx_action.len);
1732                         break;
1733                 }
1734 #endif /* CONFIG_IEEE80211R */
1735                 break;
1736 #ifdef CONFIG_CLIENT_MLME
1737         case EVENT_MLME_RX: {
1738                 struct ieee80211_rx_status rx_status;
1739                 os_memset(&rx_status, 0, sizeof(rx_status));
1740                 rx_status.freq = data->mlme_rx.freq;
1741                 rx_status.channel = data->mlme_rx.channel;
1742                 rx_status.ssi = data->mlme_rx.ssi;
1743                 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
1744                                  &rx_status);
1745                 break;
1746         }
1747 #endif /* CONFIG_CLIENT_MLME */
1748         case EVENT_EAPOL_RX:
1749                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
1750                                         data->eapol_rx.data,
1751                                         data->eapol_rx.data_len);
1752                 break;
1753         case EVENT_SIGNAL_CHANGE:
1754                 bgscan_notify_signal_change(
1755                         wpa_s, data->signal_change.above_threshold,
1756                         data->signal_change.current_signal);
1757                 break;
1758         case EVENT_INTERFACE_ENABLED:
1759                 wpa_printf(MSG_DEBUG, "Interface was enabled");
1760                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1761                         wpa_supplicant_set_state(wpa_s,
1762                                                  WPA_DISCONNECTED);
1763                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1764                 }
1765                 break;
1766         case EVENT_INTERFACE_DISABLED:
1767                 wpa_printf(MSG_DEBUG, "Interface was disabled");
1768                 wpa_supplicant_mark_disassoc(wpa_s);
1769                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
1770                 break;
1771         default:
1772                 wpa_printf(MSG_INFO, "Unknown event %d", event);
1773                 break;
1774         }
1775 }