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