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