Use BSS table entry instead of raw scan result for connection
[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_ssid *group,
396                               struct wpa_ssid **selected_ssid)
397 {
398         struct wpa_ssid *ssid;
399         struct wpa_scan_res *bss;
400         size_t i;
401         struct wpa_blacklist *e;
402         const u8 *ie;
403
404         wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
405         for (i = 0; i < wpa_s->scan_res->num; i++) {
406                 const u8 *ssid_;
407                 u8 wpa_ie_len, rsn_ie_len, ssid_len;
408                 bss = wpa_s->scan_res->res[i];
409
410                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
411                 ssid_ = ie ? ie + 2 : (u8 *) "";
412                 ssid_len = ie ? ie[1] : 0;
413
414                 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
415                 wpa_ie_len = ie ? ie[1] : 0;
416
417                 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
418                 rsn_ie_len = ie ? ie[1] : 0;
419
420                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
421                            "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
422                            (int) i, MAC2STR(bss->bssid),
423                            wpa_ssid_txt(ssid_, ssid_len),
424                            wpa_ie_len, rsn_ie_len, bss->caps);
425
426                 e = wpa_blacklist_get(wpa_s, bss->bssid);
427                 if (e && e->count > 1) {
428                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
429                         continue;
430                 }
431
432                 if (ssid_len == 0) {
433                         wpa_printf(MSG_DEBUG, "   skip - SSID not known");
434                         continue;
435                 }
436
437                 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
438                         wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN IE");
439                         continue;
440                 }
441
442                 for (ssid = group; ssid; ssid = ssid->pnext) {
443                         int check_ssid = 1;
444
445                         if (ssid->disabled) {
446                                 wpa_printf(MSG_DEBUG, "   skip - disabled");
447                                 continue;
448                         }
449
450 #ifdef CONFIG_WPS
451                         if (ssid->ssid_len == 0 &&
452                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
453                                 check_ssid = 0;
454 #endif /* CONFIG_WPS */
455
456                         if (check_ssid &&
457                             (ssid_len != ssid->ssid_len ||
458                              os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
459                                 wpa_printf(MSG_DEBUG, "   skip - "
460                                            "SSID mismatch");
461                                 continue;
462                         }
463
464                         if (ssid->bssid_set &&
465                             os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
466                         {
467                                 wpa_printf(MSG_DEBUG, "   skip - "
468                                            "BSSID mismatch");
469                                 continue;
470                         }
471
472                         if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
473                                 continue;
474
475                         wpa_printf(MSG_DEBUG, "   selected WPA AP "
476                                    MACSTR " ssid='%s'",
477                                    MAC2STR(bss->bssid),
478                                    wpa_ssid_txt(ssid_, ssid_len));
479                         *selected_ssid = ssid;
480                         return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
481                 }
482         }
483
484         return NULL;
485 }
486
487
488 static struct wpa_bss *
489 wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
490                                   struct wpa_ssid *group,
491                                   struct wpa_ssid **selected_ssid)
492 {
493         struct wpa_ssid *ssid;
494         struct wpa_scan_res *bss;
495         size_t i;
496         struct wpa_blacklist *e;
497         const u8 *ie;
498
499         wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
500         for (i = 0; i < wpa_s->scan_res->num; i++) {
501                 const u8 *ssid_;
502                 u8 wpa_ie_len, rsn_ie_len, ssid_len;
503                 bss = wpa_s->scan_res->res[i];
504
505                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
506                 ssid_ = ie ? ie + 2 : (u8 *) "";
507                 ssid_len = ie ? ie[1] : 0;
508
509                 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
510                 wpa_ie_len = ie ? ie[1] : 0;
511
512                 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
513                 rsn_ie_len = ie ? ie[1] : 0;
514
515                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
516                            "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
517                            (int) i, MAC2STR(bss->bssid),
518                            wpa_ssid_txt(ssid_, ssid_len),
519                            wpa_ie_len, rsn_ie_len, bss->caps);
520
521                 e = wpa_blacklist_get(wpa_s, bss->bssid);
522                 if (e && e->count > 1) {
523                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
524                         continue;
525                 }
526
527                 if (ssid_len == 0) {
528                         wpa_printf(MSG_DEBUG, "   skip - SSID not known");
529                         continue;
530                 }
531
532                 for (ssid = group; ssid; ssid = ssid->pnext) {
533                         int check_ssid = ssid->ssid_len != 0;
534
535                         if (ssid->disabled) {
536                                 wpa_printf(MSG_DEBUG, "   skip - disabled");
537                                 continue;
538                         }
539
540 #ifdef CONFIG_WPS
541                         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
542                                 /* Only allow wildcard SSID match if an AP
543                                  * advertises active WPS operation that matches
544                                  * with our mode. */
545                                 check_ssid = 1;
546                                 if (ssid->ssid_len == 0 &&
547                                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
548                                                               bss))
549                                         check_ssid = 0;
550                         }
551 #endif /* CONFIG_WPS */
552
553                         if (check_ssid &&
554                             (ssid_len != ssid->ssid_len ||
555                              os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
556                                 wpa_printf(MSG_DEBUG, "   skip - "
557                                            "SSID mismatch");
558                                 continue;
559                         }
560
561                         if (ssid->bssid_set &&
562                             os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
563                         {
564                                 wpa_printf(MSG_DEBUG, "   skip - "
565                                            "BSSID mismatch");
566                                 continue;
567                         }
568
569                         if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
570                             !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
571                             !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
572                         {
573                                 wpa_printf(MSG_DEBUG, "   skip - "
574                                            "non-WPA network not allowed");
575                                 continue;
576                         }
577
578                         if ((ssid->key_mgmt &
579                              (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
580                               WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
581                               WPA_KEY_MGMT_IEEE8021X_SHA256 |
582                               WPA_KEY_MGMT_PSK_SHA256)) &&
583                             (wpa_ie_len != 0 || rsn_ie_len != 0)) {
584                                 wpa_printf(MSG_DEBUG, "   skip - "
585                                            "WPA network");
586                                 continue;
587                         }
588
589                         if (!wpa_supplicant_match_privacy(bss, ssid)) {
590                                 wpa_printf(MSG_DEBUG, "   skip - "
591                                            "privacy mismatch");
592                                 continue;
593                         }
594
595                         if (bss->caps & IEEE80211_CAP_IBSS) {
596                                 wpa_printf(MSG_DEBUG, "   skip - "
597                                            "IBSS (adhoc) network");
598                                 continue;
599                         }
600
601                         wpa_printf(MSG_DEBUG, "   selected non-WPA AP "
602                                    MACSTR " ssid='%s'",
603                                    MAC2STR(bss->bssid),
604                                    wpa_ssid_txt(ssid_, ssid_len));
605                         *selected_ssid = ssid;
606                         return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
607                 }
608         }
609
610         return NULL;
611 }
612
613
614 static struct wpa_bss *
615 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
616                           struct wpa_ssid **selected_ssid)
617 {
618         struct wpa_bss *selected;
619
620         wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
621                    group->priority);
622
623         /* First, try to find WPA-enabled AP */
624         selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
625         if (selected)
626                 return selected;
627
628         /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
629          * allows this. */
630         return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
631 }
632
633
634 static struct wpa_bss *
635 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
636                             struct wpa_ssid **selected_ssid)
637 {
638         struct wpa_bss *selected = NULL;
639         int prio;
640
641         while (selected == NULL) {
642                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
643                         selected = wpa_supplicant_select_bss(
644                                 wpa_s, wpa_s->conf->pssid[prio],
645                                 selected_ssid);
646                         if (selected)
647                                 break;
648                 }
649
650                 if (selected == NULL && wpa_s->blacklist) {
651                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
652                                    "and try again");
653                         wpa_blacklist_clear(wpa_s);
654                         wpa_s->blacklist_cleared++;
655                 } else if (selected == NULL)
656                         break;
657         }
658
659         return selected;
660 }
661
662
663 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
664                                         int timeout)
665 {
666         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
667                 /*
668                  * Quick recovery if the initial scan results were not
669                  * complete when fetched before the first scan request.
670                  */
671                 wpa_s->scan_res_tried++;
672                 timeout = 0;
673         } else if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
674                 /*
675                  * No networks are enabled; short-circuit request so
676                  * we don't wait timeout seconds before transitioning
677                  * to INACTIVE state.
678                  */
679                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
680                 return;
681         }
682         wpa_supplicant_req_scan(wpa_s, timeout, 0);
683 }
684
685
686 static void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
687                                    struct wpa_bss *selected,
688                                    struct wpa_ssid *ssid)
689 {
690         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
691                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
692                         "PBC session overlap");
693                 wpa_supplicant_req_new_scan(wpa_s, 10);
694                 return;
695         }
696
697         /*
698          * Do not trigger new association unless the BSSID has changed or if
699          * reassociation is requested. If we are in process of associating with
700          * the selected BSSID, do not trigger new attempt.
701          */
702         if (wpa_s->reassociate ||
703             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
704              (wpa_s->wpa_state != WPA_ASSOCIATING ||
705               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
706               0))) {
707                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
708                         wpa_supplicant_req_new_scan(wpa_s, 10);
709                         return;
710                 }
711                 wpa_supplicant_associate(wpa_s, selected, ssid);
712         } else {
713                 wpa_printf(MSG_DEBUG, "Already associated with the selected "
714                            "AP");
715         }
716 }
717
718
719 static struct wpa_ssid *
720 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
721 {
722         int prio;
723         struct wpa_ssid *ssid;
724
725         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
726                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
727                 {
728                         if (ssid->disabled)
729                                 continue;
730                         if (ssid->mode == IEEE80211_MODE_IBSS ||
731                             ssid->mode == IEEE80211_MODE_AP)
732                                 return ssid;
733                 }
734         }
735         return NULL;
736 }
737
738
739 static void wpa_supplicant_rsn_preauth_scan_results(
740         struct wpa_supplicant *wpa_s)
741 {
742         int i;
743
744         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
745                 return;
746
747         for (i = wpa_s->scan_res->num - 1; i >= 0; i--) {
748                 const u8 *ssid, *rsn;
749                 struct wpa_scan_res *r;
750
751                 r = wpa_s->scan_res->res[i];
752
753                 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
754                 if (ssid == NULL)
755                         continue;
756
757                 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
758                 if (rsn == NULL)
759                         continue;
760
761                 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
762         }
763
764 }
765
766
767 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
768                                               union wpa_event_data *data)
769 {
770         struct wpa_bss *selected;
771         struct wpa_ssid *ssid = NULL;
772
773         wpa_supplicant_notify_scanning(wpa_s, 0);
774
775         if (wpa_supplicant_get_scan_results(wpa_s, data ? &data->scan_info :
776                                             NULL, 1) < 0) {
777                 if (wpa_s->conf->ap_scan == 2)
778                         return;
779                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
780                            "scanning again");
781                 wpa_supplicant_req_new_scan(wpa_s, 1);
782                 return;
783         }
784
785         /*
786          * Don't post the results if this was the initial cached
787          * and there were no results.
788          */
789         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
790             wpa_s->scan_res->num == 0) {
791                 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
792                         "empty - not posting");
793         } else {
794                 wpa_printf(MSG_DEBUG, "New scan results available");
795                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
796                 wpas_notify_scan_results(wpa_s);
797         }
798
799         wpas_notify_scan_done(wpa_s, 1);
800
801         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
802                 return;
803
804         if (wpa_s->disconnected) {
805                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
806                 return;
807         }
808
809         if (bgscan_notify_scan(wpa_s) == 1)
810                 return;
811
812         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
813
814         selected = wpa_supplicant_pick_network(wpa_s, &ssid);
815         if (selected) {
816                 wpa_supplicant_connect(wpa_s, selected, ssid);
817         } else {
818                 wpa_printf(MSG_DEBUG, "No suitable network found");
819                 ssid = wpa_supplicant_pick_new_network(wpa_s);
820                 if (ssid) {
821                         wpa_printf(MSG_DEBUG, "Setup a new network");
822                         wpa_supplicant_associate(wpa_s, NULL, ssid);
823                 } else
824                         wpa_supplicant_req_new_scan(wpa_s, 5);
825         }
826 }
827 #endif /* CONFIG_NO_SCAN_PROCESSING */
828
829
830 #ifdef CONFIG_IEEE80211R
831 static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
832                                     const u8 *ftie, const u8 *mdie)
833 {
834         const u8 *mobility_domain = NULL;
835         const u8 *r0kh_id = NULL;
836         size_t r0kh_id_len = 0;
837         const u8 *r1kh_id = NULL;
838         struct rsn_ftie *hdr;
839         const u8 *pos, *end;
840
841         if (mdie == NULL || ftie == NULL)
842                 return;
843
844         if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
845                 mobility_domain = mdie + 2;
846 #ifdef CONFIG_SME
847                 wpa_s->sme.ft_used = 1;
848                 os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
849 #endif /* CONFIG_SME */
850         }
851         if (ftie[1] >= sizeof(struct rsn_ftie)) {
852                 end = ftie + 2 + ftie[1];
853                 hdr = (struct rsn_ftie *) (ftie + 2);
854                 pos = (const u8 *) (hdr + 1);
855                 while (pos + 1 < end) {
856                         if (pos + 2 + pos[1] > end)
857                                 break;
858                         if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
859                             pos[1] == FT_R1KH_ID_LEN)
860                                 r1kh_id = pos + 2;
861                         else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
862                                  pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
863                                 r0kh_id = pos + 2;
864                                 r0kh_id_len = pos[1];
865                         }
866                         pos += 2 + pos[1];
867                 }
868         }
869         wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
870                              r0kh_id_len, r1kh_id);
871 }
872 #endif /* CONFIG_IEEE80211R */
873
874 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
875                                            union wpa_event_data *data)
876 {
877         int l, len, found = 0, wpa_found, rsn_found;
878         const u8 *p;
879 #ifdef CONFIG_IEEE80211R
880         const u8 *mdie = NULL, *ftie = NULL;
881 #endif /* CONFIG_IEEE80211R */
882
883         wpa_printf(MSG_DEBUG, "Association info event");
884         if (data->assoc_info.req_ies)
885                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
886                             data->assoc_info.req_ies_len);
887         if (data->assoc_info.resp_ies)
888                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
889                             data->assoc_info.resp_ies_len);
890         if (data->assoc_info.beacon_ies)
891                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
892                             data->assoc_info.beacon_ies,
893                             data->assoc_info.beacon_ies_len);
894         if (data->assoc_info.freq)
895                 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
896
897         p = data->assoc_info.req_ies;
898         l = data->assoc_info.req_ies_len;
899
900         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
901         while (p && l >= 2) {
902                 len = p[1] + 2;
903                 if (len > l) {
904                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
905                                     p, l);
906                         break;
907                 }
908                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
909                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
910                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
911                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
912                                 break;
913                         found = 1;
914                         wpa_find_assoc_pmkid(wpa_s);
915                         break;
916                 }
917                 l -= len;
918                 p += len;
919         }
920         if (!found && data->assoc_info.req_ies)
921                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
922
923 #ifdef CONFIG_IEEE80211R
924         p = data->assoc_info.resp_ies;
925         l = data->assoc_info.resp_ies_len;
926
927         /* Go through the IEs and make a copy of the FT/MD IE, if present. */
928         while (p && l >= 2) {
929                 len = p[1] + 2;
930                 if (len > l) {
931                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
932                                     p, l);
933                         break;
934                 }
935                 if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
936                         ftie = p;
937                 else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
938                         mdie = p;
939                 l -= len;
940                 p += len;
941         }
942
943         wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
944 #endif /* CONFIG_IEEE80211R */
945
946         /* WPA/RSN IE from Beacon/ProbeResp */
947         p = data->assoc_info.beacon_ies;
948         l = data->assoc_info.beacon_ies_len;
949
950         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
951          */
952         wpa_found = rsn_found = 0;
953         while (p && l >= 2) {
954                 len = p[1] + 2;
955                 if (len > l) {
956                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
957                                     p, l);
958                         break;
959                 }
960                 if (!wpa_found &&
961                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
962                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
963                         wpa_found = 1;
964                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
965                 }
966
967                 if (!rsn_found &&
968                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
969                         rsn_found = 1;
970                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
971                 }
972
973                 l -= len;
974                 p += len;
975         }
976
977         if (!wpa_found && data->assoc_info.beacon_ies)
978                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
979         if (!rsn_found && data->assoc_info.beacon_ies)
980                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
981         if (wpa_found || rsn_found)
982                 wpa_s->ap_ies_from_associnfo = 1;
983
984         wpa_s->assoc_freq = data->assoc_info.freq;
985 }
986
987
988 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
989                                        union wpa_event_data *data)
990 {
991         u8 bssid[ETH_ALEN];
992         int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
993         int bssid_changed;
994         struct wpa_driver_capa capa;
995
996         if (data)
997                 wpa_supplicant_event_associnfo(wpa_s, data);
998
999         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1000         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1001                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1002         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1003             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1004              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1005                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1006                         MACSTR, MAC2STR(bssid));
1007                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1008                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1009                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1010                 if (bssid_changed)
1011                         wpas_notify_bssid_changed(wpa_s);
1012
1013                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1014                         wpa_clear_keys(wpa_s, bssid);
1015                 }
1016                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1017                         wpa_supplicant_disassociate(
1018                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1019                         return;
1020                 }
1021         }
1022
1023 #ifdef CONFIG_SME
1024         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1025         wpa_s->sme.prev_bssid_set = 1;
1026 #endif /* CONFIG_SME */
1027
1028         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1029         if (wpa_s->current_ssid) {
1030                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1031                  * initialized before association, but for other modes,
1032                  * initialize PC/SC here, if the current configuration needs
1033                  * smartcard or SIM/USIM. */
1034                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1035         }
1036         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1037         l2_packet_notify_auth_start(wpa_s->l2);
1038
1039         /*
1040          * Set portEnabled first to FALSE in order to get EAP state machine out
1041          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1042          * state machine may transit to AUTHENTICATING state based on obsolete
1043          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1044          * AUTHENTICATED without ever giving chance to EAP state machine to
1045          * reset the state.
1046          */
1047         if (!ft_completed) {
1048                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1049                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1050         }
1051         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1052                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1053         /* 802.1X::portControl = Auto */
1054         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1055         wpa_s->eapol_received = 0;
1056         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1057             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1058             (wpa_s->current_ssid &&
1059              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1060                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1061                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1062         } else if (!ft_completed) {
1063                 /* Timeout for receiving the first EAPOL packet */
1064                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1065         }
1066         wpa_supplicant_cancel_scan(wpa_s);
1067
1068         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1069             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1070                 /*
1071                  * We are done; the driver will take care of RSN 4-way
1072                  * handshake.
1073                  */
1074                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1075                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1076                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1077                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1078         }
1079
1080         if (wpa_s->pending_eapol_rx) {
1081                 struct os_time now, age;
1082                 os_get_time(&now);
1083                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1084                 if (age.sec == 0 && age.usec < 100000 &&
1085                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1086                     0) {
1087                         wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1088                                    "that was received just before association "
1089                                    "notification");
1090                         wpa_supplicant_rx_eapol(
1091                                 wpa_s, wpa_s->pending_eapol_rx_src,
1092                                 wpabuf_head(wpa_s->pending_eapol_rx),
1093                                 wpabuf_len(wpa_s->pending_eapol_rx));
1094                 }
1095                 wpabuf_free(wpa_s->pending_eapol_rx);
1096                 wpa_s->pending_eapol_rx = NULL;
1097         }
1098
1099 #ifdef CONFIG_BGSCAN
1100         if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1101                 bgscan_deinit(wpa_s);
1102                 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1103                         if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1104                                 wpa_printf(MSG_DEBUG, "Failed to initialize "
1105                                            "bgscan");
1106                                 /*
1107                                  * Live without bgscan; it is only used as a
1108                                  * roaming optimization, so the initial
1109                                  * connection is not affected.
1110                                  */
1111                         } else
1112                                 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1113                 } else
1114                         wpa_s->bgscan_ssid = NULL;
1115         }
1116 #endif /* CONFIG_BGSCAN */
1117
1118         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1119              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1120             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1121             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1122                 /* Set static WEP keys again */
1123                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1124         }
1125 }
1126
1127
1128 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
1129 {
1130         const u8 *bssid;
1131 #ifdef CONFIG_SME
1132         int authenticating;
1133         u8 prev_pending_bssid[ETH_ALEN];
1134
1135         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1136         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1137 #endif /* CONFIG_SME */
1138
1139         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1140                 /*
1141                  * At least Host AP driver and a Prism3 card seemed to be
1142                  * generating streams of disconnected events when configuring
1143                  * IBSS for WPA-None. Ignore them for now.
1144                  */
1145                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1146                            "IBSS/WPA-None mode");
1147                 return;
1148         }
1149
1150         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1151             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1152                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1153                         "pre-shared key may be incorrect");
1154         }
1155         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1156                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1157         bssid = wpa_s->bssid;
1158         if (is_zero_ether_addr(bssid))
1159                 bssid = wpa_s->pending_bssid;
1160         wpa_blacklist_add(wpa_s, bssid);
1161         wpa_sm_notify_disassoc(wpa_s->wpa);
1162         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
1163                 "remove keys");
1164         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1165                 wpa_s->keys_cleared = 0;
1166                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1167         }
1168         wpa_supplicant_mark_disassoc(wpa_s);
1169         bgscan_deinit(wpa_s);
1170 #ifdef CONFIG_SME
1171         if (authenticating &&
1172             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1173                 /*
1174                  * mac80211-workaround to force deauth on failed auth cmd,
1175                  * requires us to remain in authenticating state to allow the
1176                  * second authentication attempt to be continued properly.
1177                  */
1178                 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1179                            "proceed after disconnection event");
1180                 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1181                 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1182         }
1183 #endif /* CONFIG_SME */
1184 }
1185
1186
1187 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1188 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1189                                                     void *sock_ctx)
1190 {
1191         struct wpa_supplicant *wpa_s = eloop_ctx;
1192
1193         if (!wpa_s->pending_mic_error_report)
1194                 return;
1195
1196         wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1197         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1198         wpa_s->pending_mic_error_report = 0;
1199 }
1200 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1201
1202
1203 static void
1204 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1205                                          union wpa_event_data *data)
1206 {
1207         int pairwise;
1208         struct os_time t;
1209
1210         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1211         pairwise = (data && data->michael_mic_failure.unicast);
1212         os_get_time(&t);
1213         if ((wpa_s->last_michael_mic_error &&
1214              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1215             wpa_s->pending_mic_error_report) {
1216                 if (wpa_s->pending_mic_error_report) {
1217                         /*
1218                          * Send the pending MIC error report immediately since
1219                          * we are going to start countermeasures and AP better
1220                          * do the same.
1221                          */
1222                         wpa_sm_key_request(wpa_s->wpa, 1,
1223                                            wpa_s->pending_mic_error_pairwise);
1224                 }
1225
1226                 /* Send the new MIC error report immediately since we are going
1227                  * to start countermeasures and AP better do the same.
1228                  */
1229                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1230
1231                 /* initialize countermeasures */
1232                 wpa_s->countermeasures = 1;
1233                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1234
1235                 /*
1236                  * Need to wait for completion of request frame. We do not get
1237                  * any callback for the message completion, so just wait a
1238                  * short while and hope for the best. */
1239                 os_sleep(0, 10000);
1240
1241                 wpa_drv_set_countermeasures(wpa_s, 1);
1242                 wpa_supplicant_deauthenticate(wpa_s,
1243                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1244                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1245                                      wpa_s, NULL);
1246                 eloop_register_timeout(60, 0,
1247                                        wpa_supplicant_stop_countermeasures,
1248                                        wpa_s, NULL);
1249                 /* TODO: mark the AP rejected for 60 second. STA is
1250                  * allowed to associate with another AP.. */
1251         } else {
1252 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1253                 if (wpa_s->mic_errors_seen) {
1254                         /*
1255                          * Reduce the effectiveness of Michael MIC error
1256                          * reports as a means for attacking against TKIP if
1257                          * more than one MIC failure is noticed with the same
1258                          * PTK. We delay the transmission of the reports by a
1259                          * random time between 0 and 60 seconds in order to
1260                          * force the attacker wait 60 seconds before getting
1261                          * the information on whether a frame resulted in a MIC
1262                          * failure.
1263                          */
1264                         u8 rval[4];
1265                         int sec;
1266
1267                         if (os_get_random(rval, sizeof(rval)) < 0)
1268                                 sec = os_random() % 60;
1269                         else
1270                                 sec = WPA_GET_BE32(rval) % 60;
1271                         wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1272                                    "seconds", sec);
1273                         wpa_s->pending_mic_error_report = 1;
1274                         wpa_s->pending_mic_error_pairwise = pairwise;
1275                         eloop_cancel_timeout(
1276                                 wpa_supplicant_delayed_mic_error_report,
1277                                 wpa_s, NULL);
1278                         eloop_register_timeout(
1279                                 sec, os_random() % 1000000,
1280                                 wpa_supplicant_delayed_mic_error_report,
1281                                 wpa_s, NULL);
1282                 } else {
1283                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1284                 }
1285 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1286                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1287 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1288         }
1289         wpa_s->last_michael_mic_error = t.sec;
1290         wpa_s->mic_errors_seen++;
1291 }
1292
1293
1294 #ifdef CONFIG_TERMINATE_ONLASTIF
1295 static int any_interfaces(struct wpa_supplicant *head)
1296 {
1297         struct wpa_supplicant *wpa_s;
1298
1299         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1300                 if (!wpa_s->interface_removed)
1301                         return 1;
1302         return 0;
1303 }
1304 #endif /* CONFIG_TERMINATE_ONLASTIF */
1305
1306
1307 static void
1308 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1309                                       union wpa_event_data *data)
1310 {
1311         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1312                 return;
1313
1314         switch (data->interface_status.ievent) {
1315         case EVENT_INTERFACE_ADDED:
1316                 if (!wpa_s->interface_removed)
1317                         break;
1318                 wpa_s->interface_removed = 0;
1319                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1320                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1321                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
1322                                    "after interface was added.");
1323                 }
1324                 break;
1325         case EVENT_INTERFACE_REMOVED:
1326                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1327                 wpa_s->interface_removed = 1;
1328                 wpa_supplicant_mark_disassoc(wpa_s);
1329                 l2_packet_deinit(wpa_s->l2);
1330                 wpa_s->l2 = NULL;
1331 #ifdef CONFIG_TERMINATE_ONLASTIF
1332                 /* check if last interface */
1333                 if (!any_interfaces(wpa_s->global->ifaces))
1334                         eloop_terminate();
1335 #endif /* CONFIG_TERMINATE_ONLASTIF */
1336                 break;
1337         }
1338 }
1339
1340
1341 #ifdef CONFIG_PEERKEY
1342 static void
1343 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1344                               union wpa_event_data *data)
1345 {
1346         if (data == NULL)
1347                 return;
1348         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1349 }
1350 #endif /* CONFIG_PEERKEY */
1351
1352
1353 #ifdef CONFIG_IEEE80211R
1354 static void
1355 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1356                                  union wpa_event_data *data)
1357 {
1358         if (data == NULL)
1359                 return;
1360
1361         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1362                                     data->ft_ies.ies_len,
1363                                     data->ft_ies.ft_action,
1364                                     data->ft_ies.target_ap,
1365                                     data->ft_ies.ric_ies,
1366                                     data->ft_ies.ric_ies_len) < 0) {
1367                 /* TODO: prevent MLME/driver from trying to associate? */
1368         }
1369 }
1370 #endif /* CONFIG_IEEE80211R */
1371
1372
1373 #ifdef CONFIG_IBSS_RSN
1374 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1375                                                 union wpa_event_data *data)
1376 {
1377         if (data == NULL)
1378                 return;
1379         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1380 }
1381 #endif /* CONFIG_IBSS_RSN */
1382
1383
1384 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1385                           union wpa_event_data *data)
1386 {
1387         struct wpa_supplicant *wpa_s = ctx;
1388
1389         switch (event) {
1390         case EVENT_AUTH:
1391                 sme_event_auth(wpa_s, data);
1392                 break;
1393         case EVENT_ASSOC:
1394                 wpa_supplicant_event_assoc(wpa_s, data);
1395                 break;
1396         case EVENT_DISASSOC:
1397                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1398                         sme_event_disassoc(wpa_s, data);
1399                 /* fall through */
1400         case EVENT_DEAUTH:
1401                 wpa_supplicant_event_disassoc(wpa_s);
1402                 break;
1403         case EVENT_MICHAEL_MIC_FAILURE:
1404                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1405                 break;
1406 #ifndef CONFIG_NO_SCAN_PROCESSING
1407         case EVENT_SCAN_RESULTS:
1408                 wpa_supplicant_event_scan_results(wpa_s, data);
1409                 break;
1410 #endif /* CONFIG_NO_SCAN_PROCESSING */
1411         case EVENT_ASSOCINFO:
1412                 wpa_supplicant_event_associnfo(wpa_s, data);
1413                 break;
1414         case EVENT_INTERFACE_STATUS:
1415                 wpa_supplicant_event_interface_status(wpa_s, data);
1416                 break;
1417         case EVENT_PMKID_CANDIDATE:
1418                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1419                 break;
1420 #ifdef CONFIG_PEERKEY
1421         case EVENT_STKSTART:
1422                 wpa_supplicant_event_stkstart(wpa_s, data);
1423                 break;
1424 #endif /* CONFIG_PEERKEY */
1425 #ifdef CONFIG_IEEE80211R
1426         case EVENT_FT_RESPONSE:
1427                 wpa_supplicant_event_ft_response(wpa_s, data);
1428                 break;
1429 #endif /* CONFIG_IEEE80211R */
1430 #ifdef CONFIG_IBSS_RSN
1431         case EVENT_IBSS_RSN_START:
1432                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1433                 break;
1434 #endif /* CONFIG_IBSS_RSN */
1435         case EVENT_ASSOC_REJECT:
1436                 sme_event_assoc_reject(wpa_s, data);
1437                 break;
1438         case EVENT_AUTH_TIMED_OUT:
1439                 sme_event_auth_timed_out(wpa_s, data);
1440                 break;
1441         case EVENT_ASSOC_TIMED_OUT:
1442                 sme_event_assoc_timed_out(wpa_s, data);
1443                 break;
1444 #ifdef CONFIG_AP
1445         case EVENT_TX_STATUS:
1446                 if (wpa_s->ap_iface == NULL)
1447                         break;
1448                 switch (data->tx_status.type) {
1449                 case WLAN_FC_TYPE_MGMT:
1450                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1451                                       data->tx_status.data_len,
1452                                       data->tx_status.stype,
1453                                       data->tx_status.ack);
1454                         break;
1455                 case WLAN_FC_TYPE_DATA:
1456                         ap_tx_status(wpa_s, data->tx_status.dst,
1457                                      data->tx_status.data,
1458                                      data->tx_status.data_len,
1459                                      data->tx_status.ack);
1460                         break;
1461                 }
1462                 break;
1463         case EVENT_RX_FROM_UNKNOWN:
1464                 if (wpa_s->ap_iface == NULL)
1465                         break;
1466                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.hdr,
1467                                        data->rx_from_unknown.len);
1468                 break;
1469         case EVENT_RX_MGMT:
1470                 if (wpa_s->ap_iface == NULL)
1471                         break;
1472                 ap_mgmt_rx(wpa_s, data->rx_mgmt.frame,
1473                            data->rx_mgmt.frame_len, data->rx_mgmt.fi);
1474                 break;
1475 #endif /* CONFIG_AP */
1476         default:
1477                 wpa_printf(MSG_INFO, "Unknown event %d", event);
1478                 break;
1479         }
1480 }