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