Don't post scan results when initial scan is emtpy
[wpasupplicant] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2008, 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 "wpa.h"
20 #include "eloop.h"
21 #include "drivers/driver.h"
22 #include "config.h"
23 #include "l2_packet/l2_packet.h"
24 #include "wpa_supplicant_i.h"
25 #include "pcsc_funcs.h"
26 #include "preauth.h"
27 #include "pmksa_cache.h"
28 #include "wpa_ctrl.h"
29 #include "eap_peer/eap.h"
30 #include "ctrl_iface_dbus.h"
31 #include "ieee802_11_defs.h"
32 #include "blacklist.h"
33 #include "wpas_glue.h"
34
35
36 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
37 {
38         struct wpa_ssid *ssid;
39
40         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
41                 return 0;
42
43         wpa_printf(MSG_DEBUG, "Select network based on association "
44                    "information");
45         ssid = wpa_supplicant_get_ssid(wpa_s);
46         if (ssid == NULL) {
47                 wpa_printf(MSG_INFO, "No network configuration found for the "
48                            "current AP");
49                 return -1;
50         }
51
52         if (ssid->disabled) {
53                 wpa_printf(MSG_DEBUG, "Selected network is disabled");
54                 return -1;
55         }
56
57         wpa_printf(MSG_DEBUG, "Network configuration found for the current "
58                    "AP");
59         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
60                               WPA_KEY_MGMT_WPA_NONE |
61                               WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
62                               WPA_KEY_MGMT_PSK_SHA256 |
63                               WPA_KEY_MGMT_IEEE8021X_SHA256)) {
64                 u8 wpa_ie[80];
65                 size_t wpa_ie_len = sizeof(wpa_ie);
66                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
67                                           wpa_ie, &wpa_ie_len);
68         } else {
69                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
70         }
71
72         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
73                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
74         wpa_s->current_ssid = ssid;
75         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
76         wpa_supplicant_initiate_eapol(wpa_s);
77
78         return 0;
79 }
80
81
82 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
83                                                 void *sock_ctx)
84 {
85         struct wpa_supplicant *wpa_s = eloop_ctx;
86
87         if (wpa_s->countermeasures) {
88                 wpa_s->countermeasures = 0;
89                 wpa_drv_set_countermeasures(wpa_s, 0);
90                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
91                 wpa_supplicant_req_scan(wpa_s, 0, 0);
92         }
93 }
94
95
96 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
97 {
98         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
99         os_memset(wpa_s->bssid, 0, ETH_ALEN);
100         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
101         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
102         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
103         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
104                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
105         wpa_s->ap_ies_from_associnfo = 0;
106 }
107
108
109 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
110 {
111         struct wpa_ie_data ie;
112         int pmksa_set = -1;
113         size_t i;
114
115         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
116             ie.pmkid == NULL)
117                 return;
118
119         for (i = 0; i < ie.num_pmkid; i++) {
120                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
121                                                     ie.pmkid + i * PMKID_LEN,
122                                                     NULL, NULL, 0);
123                 if (pmksa_set == 0) {
124                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
125                         break;
126                 }
127         }
128
129         wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
130                    "cache", pmksa_set == 0 ? "" : "not ");
131 }
132
133
134 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
135                                                  union wpa_event_data *data)
136 {
137         if (data == NULL) {
138                 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
139                 return;
140         }
141         wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
142                    " index=%d preauth=%d",
143                    MAC2STR(data->pmkid_candidate.bssid),
144                    data->pmkid_candidate.index,
145                    data->pmkid_candidate.preauth);
146
147         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
148                             data->pmkid_candidate.index,
149                             data->pmkid_candidate.preauth);
150 }
151
152
153 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
154 {
155         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
156             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
157                 return 0;
158
159 #ifdef IEEE8021X_EAPOL
160         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
161             wpa_s->current_ssid &&
162             !(wpa_s->current_ssid->eapol_flags &
163               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
164                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
165                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
166                  * plaintext or static WEP keys). */
167                 return 0;
168         }
169 #endif /* IEEE8021X_EAPOL */
170
171         return 1;
172 }
173
174
175 /**
176  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
177  * @wpa_s: pointer to wpa_supplicant data
178  * @ssid: Configuration data for the network
179  * Returns: 0 on success, -1 on failure
180  *
181  * This function is called when starting authentication with a network that is
182  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
183  */
184 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
185                               struct wpa_ssid *ssid)
186 {
187 #ifdef IEEE8021X_EAPOL
188         int aka = 0, sim = 0, type;
189
190         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
191                 return 0;
192
193         if (ssid->eap.eap_methods == NULL) {
194                 sim = 1;
195                 aka = 1;
196         } else {
197                 struct eap_method_type *eap = ssid->eap.eap_methods;
198                 while (eap->vendor != EAP_VENDOR_IETF ||
199                        eap->method != EAP_TYPE_NONE) {
200                         if (eap->vendor == EAP_VENDOR_IETF) {
201                                 if (eap->method == EAP_TYPE_SIM)
202                                         sim = 1;
203                                 else if (eap->method == EAP_TYPE_AKA)
204                                         aka = 1;
205                         }
206                         eap++;
207                 }
208         }
209
210         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
211                 sim = 0;
212         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
213                 aka = 0;
214
215         if (!sim && !aka) {
216                 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
217                            "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
218                 return 0;
219         }
220
221         wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
222                    "(sim=%d aka=%d) - initialize PCSC", sim, aka);
223         if (sim && aka)
224                 type = SCARD_TRY_BOTH;
225         else if (aka)
226                 type = SCARD_USIM_ONLY;
227         else
228                 type = SCARD_GSM_SIM_ONLY;
229
230         wpa_s->scard = scard_init(type);
231         if (wpa_s->scard == NULL) {
232                 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
233                            "(pcsc-lite)");
234                 return -1;
235         }
236         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
237         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
238 #endif /* IEEE8021X_EAPOL */
239
240         return 0;
241 }
242
243
244 #ifndef CONFIG_NO_SCAN_PROCESSING
245 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
246                                         struct wpa_ssid *ssid)
247 {
248         int i, privacy = 0;
249
250         if (ssid->mixed_cell)
251                 return 1;
252
253         for (i = 0; i < NUM_WEP_KEYS; i++) {
254                 if (ssid->wep_key_len[i]) {
255                         privacy = 1;
256                         break;
257                 }
258         }
259 #ifdef IEEE8021X_EAPOL
260         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
261             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
262                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
263                 privacy = 1;
264 #endif /* IEEE8021X_EAPOL */
265
266         if (bss->caps & IEEE80211_CAP_PRIVACY)
267                 return privacy;
268         return !privacy;
269 }
270
271
272 static int wpa_supplicant_ssid_bss_match(struct wpa_ssid *ssid,
273                                          struct wpa_scan_res *bss)
274 {
275         struct wpa_ie_data ie;
276         int proto_match = 0;
277         const u8 *rsn_ie, *wpa_ie;
278
279         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
280         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
281                 proto_match++;
282
283                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
284                         wpa_printf(MSG_DEBUG, "   skip RSN IE - parse failed");
285                         break;
286                 }
287                 if (!(ie.proto & ssid->proto)) {
288                         wpa_printf(MSG_DEBUG, "   skip RSN IE - proto "
289                                    "mismatch");
290                         break;
291                 }
292
293                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
294                         wpa_printf(MSG_DEBUG, "   skip RSN IE - PTK cipher "
295                                    "mismatch");
296                         break;
297                 }
298
299                 if (!(ie.group_cipher & ssid->group_cipher)) {
300                         wpa_printf(MSG_DEBUG, "   skip RSN IE - GTK cipher "
301                                    "mismatch");
302                         break;
303                 }
304
305                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
306                         wpa_printf(MSG_DEBUG, "   skip RSN IE - key mgmt "
307                                    "mismatch");
308                         break;
309                 }
310
311 #ifdef CONFIG_IEEE80211W
312                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
313                     ssid->ieee80211w == IEEE80211W_REQUIRED) {
314                         wpa_printf(MSG_DEBUG, "   skip RSN IE - no mgmt frame "
315                                    "protection");
316                         break;
317                 }
318 #endif /* CONFIG_IEEE80211W */
319
320                 wpa_printf(MSG_DEBUG, "   selected based on RSN IE");
321                 return 1;
322         }
323
324         wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
325         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
326                 proto_match++;
327
328                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
329                         wpa_printf(MSG_DEBUG, "   skip WPA IE - parse failed");
330                         break;
331                 }
332                 if (!(ie.proto & ssid->proto)) {
333                         wpa_printf(MSG_DEBUG, "   skip WPA IE - proto "
334                                    "mismatch");
335                         break;
336                 }
337
338                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
339                         wpa_printf(MSG_DEBUG, "   skip WPA IE - PTK cipher "
340                                    "mismatch");
341                         break;
342                 }
343
344                 if (!(ie.group_cipher & ssid->group_cipher)) {
345                         wpa_printf(MSG_DEBUG, "   skip WPA IE - GTK cipher "
346                                    "mismatch");
347                         break;
348                 }
349
350                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
351                         wpa_printf(MSG_DEBUG, "   skip WPA IE - key mgmt "
352                                    "mismatch");
353                         break;
354                 }
355
356                 wpa_printf(MSG_DEBUG, "   selected based on WPA IE");
357                 return 1;
358         }
359
360         if (proto_match == 0)
361                 wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN proto match");
362
363         return 0;
364 }
365
366
367 static struct wpa_scan_res *
368 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
369                           struct wpa_ssid **selected_ssid)
370 {
371         struct wpa_ssid *ssid;
372         struct wpa_scan_res *bss, *selected = NULL;
373         size_t i;
374         struct wpa_blacklist *e;
375         const u8 *ie;
376
377         wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
378                    group->priority);
379
380         bss = NULL;
381         ssid = NULL;
382         /* First, try to find WPA-enabled AP */
383         wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
384         for (i = 0; i < wpa_s->scan_res->num && !selected; i++) {
385                 const u8 *ssid_;
386                 u8 wpa_ie_len, rsn_ie_len, ssid_len;
387                 bss = wpa_s->scan_res->res[i];
388
389                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
390                 ssid_ = ie ? ie + 2 : (u8 *) "";
391                 ssid_len = ie ? ie[1] : 0;
392
393                 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
394                 wpa_ie_len = ie ? ie[1] : 0;
395
396                 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
397                 rsn_ie_len = ie ? ie[1] : 0;
398
399                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
400                            "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
401                            (int) i, MAC2STR(bss->bssid),
402                            wpa_ssid_txt(ssid_, ssid_len),
403                            wpa_ie_len, rsn_ie_len, bss->caps);
404                 e = wpa_blacklist_get(wpa_s, bss->bssid);
405                 if (e && e->count > 1) {
406                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
407                         continue;
408                 }
409
410                 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
411                         wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN IE");
412                         continue;
413                 }
414
415                 for (ssid = group; ssid; ssid = ssid->pnext) {
416                         if (ssid->disabled) {
417                                 wpa_printf(MSG_DEBUG, "   skip - disabled");
418                                 continue;
419                         }
420                         if (ssid_len != ssid->ssid_len ||
421                             os_memcmp(ssid_, ssid->ssid, ssid_len) != 0) {
422                                 wpa_printf(MSG_DEBUG, "   skip - "
423                                            "SSID mismatch");
424                                 continue;
425                         }
426                         if (ssid->bssid_set &&
427                             os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
428                         {
429                                 wpa_printf(MSG_DEBUG, "   skip - "
430                                            "BSSID mismatch");
431                                 continue;
432                         }
433                         if (wpa_supplicant_ssid_bss_match(ssid, bss)) {
434                                 selected = bss;
435                                 *selected_ssid = ssid;
436                                 wpa_printf(MSG_DEBUG, "   selected WPA AP "
437                                            MACSTR " ssid='%s'",
438                                            MAC2STR(bss->bssid),
439                                            wpa_ssid_txt(ssid_, ssid_len));
440                                 break;
441                         }
442                 }
443         }
444
445         /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
446          * allows this. */
447         wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
448         for (i = 0; i < wpa_s->scan_res->num && !selected; i++) {
449                 const u8 *ssid_;
450                 u8 wpa_ie_len, rsn_ie_len, ssid_len;
451                 bss = wpa_s->scan_res->res[i];
452
453                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
454                 ssid_ = ie ? ie + 2 : (u8 *) "";
455                 ssid_len = ie ? ie[1] : 0;
456
457                 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
458                 wpa_ie_len = ie ? ie[1] : 0;
459
460                 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
461                 rsn_ie_len = ie ? ie[1] : 0;
462
463                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
464                            "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
465                            (int) i, MAC2STR(bss->bssid),
466                            wpa_ssid_txt(ssid_, ssid_len),
467                            wpa_ie_len, rsn_ie_len, bss->caps);
468                 e = wpa_blacklist_get(wpa_s, bss->bssid);
469                 if (e && e->count > 1) {
470                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
471                         continue;
472                 }
473                 for (ssid = group; ssid; ssid = ssid->pnext) {
474                         if (ssid->disabled) {
475                                 wpa_printf(MSG_DEBUG, "   skip - disabled");
476                                 continue;
477                         }
478                         if (ssid->ssid_len != 0 &&
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 (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
495                             !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
496                         {
497                                 wpa_printf(MSG_DEBUG, "   skip - "
498                                            "non-WPA network not allowed");
499                                 continue;
500                         }
501
502                         if ((ssid->key_mgmt & 
503                              (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
504                               WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
505                               WPA_KEY_MGMT_IEEE8021X_SHA256 |
506                               WPA_KEY_MGMT_PSK_SHA256)) &&
507                             (wpa_ie_len != 0 || rsn_ie_len != 0)) {
508                                 wpa_printf(MSG_DEBUG, "   skip - "
509                                            "WPA network");
510                                 continue;
511                         }
512
513                         if (!wpa_supplicant_match_privacy(bss, ssid)) {
514                                 wpa_printf(MSG_DEBUG, "   skip - "
515                                            "privacy mismatch");
516                                 continue;
517                         }
518
519                         if (bss->caps & IEEE80211_CAP_IBSS) {
520                                 wpa_printf(MSG_DEBUG, "   skip - "
521                                            "IBSS (adhoc) network");
522                                 continue;
523                         }
524
525                         selected = bss;
526                         *selected_ssid = ssid;
527                         wpa_printf(MSG_DEBUG, "   selected non-WPA AP "
528                                    MACSTR " ssid='%s'",
529                                    MAC2STR(bss->bssid),
530                                    wpa_ssid_txt(ssid_, ssid_len));
531                         break;
532                 }
533         }
534
535         return selected;
536 }
537
538
539 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
540 {
541         int prio, timeout;
542         struct wpa_scan_res *selected = NULL;
543         struct wpa_ssid *ssid = NULL;
544
545         if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
546                 if (wpa_s->conf->ap_scan == 2)
547                         return;
548                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
549                            "scanning again");
550                 timeout = 1;
551                 goto req_scan;
552         }
553
554         /*
555          * Don't post the results if this was the initial cached
556          * and there were no results.
557          */
558         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
559             wpa_s->scan_res->num == 0) {
560                 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
561                         "empty - not posting");
562         } else {
563                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
564                 wpa_supplicant_dbus_notify_scan_results(wpa_s);
565         }
566
567         if (wpa_s->conf->ap_scan == 2 || wpa_s->disconnected)
568                 return;
569
570         while (selected == NULL) {
571                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
572                         selected = wpa_supplicant_select_bss(
573                                 wpa_s, wpa_s->conf->pssid[prio], &ssid);
574                         if (selected)
575                                 break;
576                 }
577
578                 if (selected == NULL && wpa_s->blacklist) {
579                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
580                                    "and try again");
581                         wpa_blacklist_clear(wpa_s);
582                 } else if (selected == NULL) {
583                         break;
584                 }
585         }
586
587         if (selected) {
588                 /* Do not trigger new association unless the BSSID has changed
589                  * or if reassociation is requested. If we are in process of
590                  * associating with the selected BSSID, do not trigger new
591                  * attempt. */
592                 if (wpa_s->reassociate ||
593                     (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
594                      (wpa_s->wpa_state != WPA_ASSOCIATING ||
595                       os_memcmp(selected->bssid, wpa_s->pending_bssid,
596                                 ETH_ALEN) != 0))) {
597                         if (wpa_supplicant_scard_init(wpa_s, ssid)) {
598                                 wpa_supplicant_req_scan(wpa_s, 10, 0);
599                                 return;
600                         }
601                         wpa_supplicant_associate(wpa_s, selected, ssid);
602                 } else {
603                         wpa_printf(MSG_DEBUG, "Already associated with the "
604                                    "selected AP.");
605                 }
606                 rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
607         } else {
608                 wpa_printf(MSG_DEBUG, "No suitable AP found.");
609                 timeout = 5;
610                 goto req_scan;
611         }
612
613         return;
614
615 req_scan:
616         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
617                 /*
618                  * Quick recovery if the initial scan results were not
619                  * complete when fetched before the first scan request.
620                  */
621                 wpa_s->scan_res_tried++;
622                 timeout = 0;
623         }
624         wpa_supplicant_req_scan(wpa_s, timeout, 0);
625 }
626 #endif /* CONFIG_NO_SCAN_PROCESSING */
627
628
629 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
630                                            union wpa_event_data *data)
631 {
632         int l, len, found = 0, wpa_found, rsn_found;
633         u8 *p;
634
635         wpa_printf(MSG_DEBUG, "Association info event");
636         if (data->assoc_info.req_ies)
637                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
638                             data->assoc_info.req_ies_len);
639         if (data->assoc_info.resp_ies)
640                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
641                             data->assoc_info.resp_ies_len);
642         if (data->assoc_info.beacon_ies)
643                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
644                             data->assoc_info.beacon_ies,
645                             data->assoc_info.beacon_ies_len);
646
647         p = data->assoc_info.req_ies;
648         l = data->assoc_info.req_ies_len;
649
650         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
651         while (p && l >= 2) {
652                 len = p[1] + 2;
653                 if (len > l) {
654                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
655                                     p, l);
656                         break;
657                 }
658                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
659                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
660                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
661                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
662                                 break;
663                         found = 1;
664                         wpa_find_assoc_pmkid(wpa_s);
665                         break;
666                 }
667                 l -= len;
668                 p += len;
669         }
670         if (!found && data->assoc_info.req_ies)
671                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
672
673         /* WPA/RSN IE from Beacon/ProbeResp */
674         p = data->assoc_info.beacon_ies;
675         l = data->assoc_info.beacon_ies_len;
676
677         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
678          */
679         wpa_found = rsn_found = 0;
680         while (p && l >= 2) {
681                 len = p[1] + 2;
682                 if (len > l) {
683                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
684                                     p, l);
685                         break;
686                 }
687                 if (!wpa_found &&
688                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
689                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
690                         wpa_found = 1;
691                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
692                 }
693
694                 if (!rsn_found &&
695                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
696                         rsn_found = 1;
697                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
698                 }
699
700                 l -= len;
701                 p += len;
702         }
703
704         if (!wpa_found && data->assoc_info.beacon_ies)
705                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
706         if (!rsn_found && data->assoc_info.beacon_ies)
707                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
708         if (wpa_found || rsn_found)
709                 wpa_s->ap_ies_from_associnfo = 1;
710 }
711
712
713 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
714                                        union wpa_event_data *data)
715 {
716         u8 bssid[ETH_ALEN];
717         int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
718
719         if (data)
720                 wpa_supplicant_event_associnfo(wpa_s, data);
721
722         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
723         if (wpa_s->use_client_mlme)
724                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
725         if (wpa_s->use_client_mlme ||
726             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
727              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
728                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
729                         MACSTR, MAC2STR(bssid));
730                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
731                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
732                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
733                         wpa_clear_keys(wpa_s, bssid);
734                 }
735                 if (wpa_supplicant_select_config(wpa_s) < 0) {
736                         wpa_supplicant_disassociate(
737                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
738                         return;
739                 }
740         }
741
742         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
743         if (wpa_s->current_ssid) {
744                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
745                  * initialized before association, but for other modes,
746                  * initialize PC/SC here, if the current configuration needs
747                  * smartcard or SIM/USIM. */
748                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
749         }
750         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
751         l2_packet_notify_auth_start(wpa_s->l2);
752
753         /*
754          * Set portEnabled first to FALSE in order to get EAP state machine out
755          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
756          * state machine may transit to AUTHENTICATING state based on obsolete
757          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
758          * AUTHENTICATED without ever giving chance to EAP state machine to
759          * reset the state.
760          */
761         if (!ft_completed) {
762                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
763                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
764         }
765         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
766                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
767         /* 802.1X::portControl = Auto */
768         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
769         wpa_s->eapol_received = 0;
770         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
771             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
772                 wpa_supplicant_cancel_auth_timeout(wpa_s);
773                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
774         } else if (!ft_completed) {
775                 /* Timeout for receiving the first EAPOL packet */
776                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
777         }
778         wpa_supplicant_cancel_scan(wpa_s);
779
780         if (wpa_s->driver_4way_handshake &&
781             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
782                 /*
783                  * We are done; the driver will take care of RSN 4-way
784                  * handshake.
785                  */
786                 wpa_supplicant_cancel_auth_timeout(wpa_s);
787                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
788                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
789                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
790         }
791 }
792
793
794 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
795 {
796         const u8 *bssid;
797
798         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
799                 /*
800                  * At least Host AP driver and a Prism3 card seemed to be
801                  * generating streams of disconnected events when configuring
802                  * IBSS for WPA-None. Ignore them for now.
803                  */
804                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
805                            "IBSS/WPA-None mode");
806                 return;
807         }
808
809         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
810             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
811                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
812                         "pre-shared key may be incorrect");
813         }
814         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
815                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
816         bssid = wpa_s->bssid;
817         if (is_zero_ether_addr(bssid))
818                 bssid = wpa_s->pending_bssid;
819         wpa_blacklist_add(wpa_s, bssid);
820         wpa_sm_notify_disassoc(wpa_s->wpa);
821         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
822                 "remove keys");
823         if (wpa_supplicant_dynamic_keys(wpa_s)) {
824                 wpa_s->keys_cleared = 0;
825                 wpa_clear_keys(wpa_s, wpa_s->bssid);
826         }
827         wpa_supplicant_mark_disassoc(wpa_s);
828 }
829
830
831 static void
832 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
833                                          union wpa_event_data *data)
834 {
835         int pairwise;
836         struct os_time t;
837
838         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
839         pairwise = (data && data->michael_mic_failure.unicast);
840         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
841         os_get_time(&t);
842         if (wpa_s->last_michael_mic_error &&
843             t.sec - wpa_s->last_michael_mic_error <= 60) {
844                 /* initialize countermeasures */
845                 wpa_s->countermeasures = 1;
846                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
847
848                 /*
849                  * Need to wait for completion of request frame. We do not get
850                  * any callback for the message completion, so just wait a
851                  * short while and hope for the best. */
852                 os_sleep(0, 10000);
853
854                 wpa_drv_set_countermeasures(wpa_s, 1);
855                 wpa_supplicant_deauthenticate(wpa_s,
856                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
857                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
858                                      wpa_s, NULL);
859                 eloop_register_timeout(60, 0,
860                                        wpa_supplicant_stop_countermeasures,
861                                        wpa_s, NULL);
862                 /* TODO: mark the AP rejected for 60 second. STA is
863                  * allowed to associate with another AP.. */
864         }
865         wpa_s->last_michael_mic_error = t.sec;
866 }
867
868
869 static void
870 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
871                                       union wpa_event_data *data)
872 {
873         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
874                 return;
875
876         switch (data->interface_status.ievent) {
877         case EVENT_INTERFACE_ADDED:
878                 if (!wpa_s->interface_removed)
879                         break;
880                 wpa_s->interface_removed = 0;
881                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
882                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
883                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
884                                    "after interface was added.");
885                 }
886                 break;
887         case EVENT_INTERFACE_REMOVED:
888                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
889                 wpa_s->interface_removed = 1;
890                 wpa_supplicant_mark_disassoc(wpa_s);
891                 l2_packet_deinit(wpa_s->l2);
892                 wpa_s->l2 = NULL;
893                 break;
894         }
895 }
896
897
898 #ifdef CONFIG_PEERKEY
899 static void
900 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
901                               union wpa_event_data *data)
902 {
903         if (data == NULL)
904                 return;
905         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
906 }
907 #endif /* CONFIG_PEERKEY */
908
909
910 #ifdef CONFIG_IEEE80211R
911 static void
912 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
913                                  union wpa_event_data *data)
914 {
915         if (data == NULL)
916                 return;
917
918         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
919                                     data->ft_ies.ies_len,
920                                     data->ft_ies.ft_action,
921                                     data->ft_ies.target_ap) < 0) {
922                 /* TODO: prevent MLME/driver from trying to associate? */
923         }
924 }
925 #endif /* CONFIG_IEEE80211R */
926
927
928 void wpa_supplicant_event(void *ctx, wpa_event_type event,
929                           union wpa_event_data *data)
930 {
931         struct wpa_supplicant *wpa_s = ctx;
932
933         switch (event) {
934         case EVENT_ASSOC:
935                 wpa_supplicant_event_assoc(wpa_s, data);
936                 break;
937         case EVENT_DISASSOC:
938                 wpa_supplicant_event_disassoc(wpa_s);
939                 break;
940         case EVENT_MICHAEL_MIC_FAILURE:
941                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
942                 break;
943 #ifndef CONFIG_NO_SCAN_PROCESSING
944         case EVENT_SCAN_RESULTS:
945                 wpa_supplicant_event_scan_results(wpa_s);
946                 break;
947 #endif /* CONFIG_NO_SCAN_PROCESSING */
948         case EVENT_ASSOCINFO:
949                 wpa_supplicant_event_associnfo(wpa_s, data);
950                 break;
951         case EVENT_INTERFACE_STATUS:
952                 wpa_supplicant_event_interface_status(wpa_s, data);
953                 break;
954         case EVENT_PMKID_CANDIDATE:
955                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
956                 break;
957 #ifdef CONFIG_PEERKEY
958         case EVENT_STKSTART:
959                 wpa_supplicant_event_stkstart(wpa_s, data);
960                 break;
961 #endif /* CONFIG_PEERKEY */
962 #ifdef CONFIG_IEEE80211R
963         case EVENT_FT_RESPONSE:
964                 wpa_supplicant_event_ft_response(wpa_s, data);
965                 break;
966 #endif /* CONFIG_IEEE80211R */
967         default:
968                 wpa_printf(MSG_INFO, "Unknown event %d", event);
969                 break;
970         }
971 }