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