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