Add an option for driver wrappers to report operational frequency
[libeap.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2009, 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 {
768         struct wpa_scan_res *selected;
769         struct wpa_ssid *ssid = NULL;
770
771         wpa_supplicant_notify_scanning(wpa_s, 0);
772
773         if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
774                 if (wpa_s->conf->ap_scan == 2)
775                         return;
776                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
777                            "scanning again");
778                 wpa_supplicant_req_new_scan(wpa_s, 1);
779                 return;
780         }
781
782         /*
783          * Don't post the results if this was the initial cached
784          * and there were no results.
785          */
786         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
787             wpa_s->scan_res->num == 0) {
788                 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
789                         "empty - not posting");
790         } else {
791                 wpa_printf(MSG_DEBUG, "New scan results available");
792                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
793                 wpas_notify_scan_results(wpa_s);
794         }
795
796         wpas_notify_scan_done(wpa_s, 1);
797
798         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
799                 return;
800
801         if (wpa_s->disconnected) {
802                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
803                 return;
804         }
805
806         if (bgscan_notify_scan(wpa_s) == 1)
807                 return;
808
809         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
810
811         selected = wpa_supplicant_pick_network(wpa_s, &ssid);
812         if (selected) {
813                 wpa_supplicant_connect(wpa_s, selected, ssid);
814         } else {
815                 wpa_printf(MSG_DEBUG, "No suitable network found");
816                 ssid = wpa_supplicant_pick_new_network(wpa_s);
817                 if (ssid) {
818                         wpa_printf(MSG_DEBUG, "Setup a new network");
819                         wpa_supplicant_associate(wpa_s, NULL, ssid);
820                 } else
821                         wpa_supplicant_req_new_scan(wpa_s, 5);
822         }
823 }
824 #endif /* CONFIG_NO_SCAN_PROCESSING */
825
826
827 #ifdef CONFIG_IEEE80211R
828 static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
829                                     const u8 *ftie, const u8 *mdie)
830 {
831         const u8 *mobility_domain = NULL;
832         const u8 *r0kh_id = NULL;
833         size_t r0kh_id_len = 0;
834         const u8 *r1kh_id = NULL;
835         struct rsn_ftie *hdr;
836         const u8 *pos, *end;
837
838         if (mdie == NULL || ftie == NULL)
839                 return;
840
841         if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
842                 mobility_domain = mdie + 2;
843 #ifdef CONFIG_SME
844                 wpa_s->sme.ft_used = 1;
845                 os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
846 #endif /* CONFIG_SME */
847         }
848         if (ftie[1] >= sizeof(struct rsn_ftie)) {
849                 end = ftie + 2 + ftie[1];
850                 hdr = (struct rsn_ftie *) (ftie + 2);
851                 pos = (const u8 *) (hdr + 1);
852                 while (pos + 1 < end) {
853                         if (pos + 2 + pos[1] > end)
854                                 break;
855                         if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
856                             pos[1] == FT_R1KH_ID_LEN)
857                                 r1kh_id = pos + 2;
858                         else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
859                                  pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
860                                 r0kh_id = pos + 2;
861                                 r0kh_id_len = pos[1];
862                         }
863                         pos += 2 + pos[1];
864                 }
865         }
866         wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
867                              r0kh_id_len, r1kh_id);
868 }
869 #endif /* CONFIG_IEEE80211R */
870
871 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
872                                            union wpa_event_data *data)
873 {
874         int l, len, found = 0, wpa_found, rsn_found;
875         const u8 *p;
876 #ifdef CONFIG_IEEE80211R
877         const u8 *mdie = NULL, *ftie = NULL;
878 #endif /* CONFIG_IEEE80211R */
879
880         wpa_printf(MSG_DEBUG, "Association info event");
881         if (data->assoc_info.req_ies)
882                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
883                             data->assoc_info.req_ies_len);
884         if (data->assoc_info.resp_ies)
885                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
886                             data->assoc_info.resp_ies_len);
887         if (data->assoc_info.beacon_ies)
888                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
889                             data->assoc_info.beacon_ies,
890                             data->assoc_info.beacon_ies_len);
891         if (data->assoc_info.freq)
892                 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
893
894         p = data->assoc_info.req_ies;
895         l = data->assoc_info.req_ies_len;
896
897         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
898         while (p && l >= 2) {
899                 len = p[1] + 2;
900                 if (len > l) {
901                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
902                                     p, l);
903                         break;
904                 }
905                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
906                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
907                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
908                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
909                                 break;
910                         found = 1;
911                         wpa_find_assoc_pmkid(wpa_s);
912                         break;
913                 }
914                 l -= len;
915                 p += len;
916         }
917         if (!found && data->assoc_info.req_ies)
918                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
919
920 #ifdef CONFIG_IEEE80211R
921         p = data->assoc_info.resp_ies;
922         l = data->assoc_info.resp_ies_len;
923
924         /* Go through the IEs and make a copy of the FT/MD IE, if present. */
925         while (p && l >= 2) {
926                 len = p[1] + 2;
927                 if (len > l) {
928                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
929                                     p, l);
930                         break;
931                 }
932                 if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
933                         ftie = p;
934                 else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
935                         mdie = p;
936                 l -= len;
937                 p += len;
938         }
939
940         wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
941 #endif /* CONFIG_IEEE80211R */
942
943         /* WPA/RSN IE from Beacon/ProbeResp */
944         p = data->assoc_info.beacon_ies;
945         l = data->assoc_info.beacon_ies_len;
946
947         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
948          */
949         wpa_found = rsn_found = 0;
950         while (p && l >= 2) {
951                 len = p[1] + 2;
952                 if (len > l) {
953                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
954                                     p, l);
955                         break;
956                 }
957                 if (!wpa_found &&
958                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
959                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
960                         wpa_found = 1;
961                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
962                 }
963
964                 if (!rsn_found &&
965                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
966                         rsn_found = 1;
967                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
968                 }
969
970                 l -= len;
971                 p += len;
972         }
973
974         if (!wpa_found && data->assoc_info.beacon_ies)
975                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
976         if (!rsn_found && data->assoc_info.beacon_ies)
977                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
978         if (wpa_found || rsn_found)
979                 wpa_s->ap_ies_from_associnfo = 1;
980
981         wpa_s->assoc_freq = data->assoc_info.freq;
982 }
983
984
985 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
986                                        union wpa_event_data *data)
987 {
988         u8 bssid[ETH_ALEN];
989         int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
990         int bssid_changed;
991         struct wpa_driver_capa capa;
992
993         if (data)
994                 wpa_supplicant_event_associnfo(wpa_s, data);
995
996         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
997         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
998                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
999         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1000             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1001              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1002                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1003                         MACSTR, MAC2STR(bssid));
1004                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1005                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1006                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1007                 if (bssid_changed)
1008                         wpas_notify_bssid_changed(wpa_s);
1009
1010                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1011                         wpa_clear_keys(wpa_s, bssid);
1012                 }
1013                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1014                         wpa_supplicant_disassociate(
1015                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1016                         return;
1017                 }
1018         }
1019
1020 #ifdef CONFIG_SME
1021         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1022         wpa_s->sme.prev_bssid_set = 1;
1023 #endif /* CONFIG_SME */
1024
1025         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1026         if (wpa_s->current_ssid) {
1027                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1028                  * initialized before association, but for other modes,
1029                  * initialize PC/SC here, if the current configuration needs
1030                  * smartcard or SIM/USIM. */
1031                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1032         }
1033         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1034         l2_packet_notify_auth_start(wpa_s->l2);
1035
1036         /*
1037          * Set portEnabled first to FALSE in order to get EAP state machine out
1038          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1039          * state machine may transit to AUTHENTICATING state based on obsolete
1040          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1041          * AUTHENTICATED without ever giving chance to EAP state machine to
1042          * reset the state.
1043          */
1044         if (!ft_completed) {
1045                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1046                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1047         }
1048         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1049                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1050         /* 802.1X::portControl = Auto */
1051         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1052         wpa_s->eapol_received = 0;
1053         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1054             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1055             (wpa_s->current_ssid &&
1056              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1057                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1058                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1059         } else if (!ft_completed) {
1060                 /* Timeout for receiving the first EAPOL packet */
1061                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1062         }
1063         wpa_supplicant_cancel_scan(wpa_s);
1064
1065         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1066             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1067                 /*
1068                  * We are done; the driver will take care of RSN 4-way
1069                  * handshake.
1070                  */
1071                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1072                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1073                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1074                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1075         }
1076
1077         if (wpa_s->pending_eapol_rx) {
1078                 struct os_time now, age;
1079                 os_get_time(&now);
1080                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1081                 if (age.sec == 0 && age.usec < 100000 &&
1082                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1083                     0) {
1084                         wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1085                                    "that was received just before association "
1086                                    "notification");
1087                         wpa_supplicant_rx_eapol(
1088                                 wpa_s, wpa_s->pending_eapol_rx_src,
1089                                 wpabuf_head(wpa_s->pending_eapol_rx),
1090                                 wpabuf_len(wpa_s->pending_eapol_rx));
1091                 }
1092                 wpabuf_free(wpa_s->pending_eapol_rx);
1093                 wpa_s->pending_eapol_rx = NULL;
1094         }
1095
1096 #ifdef CONFIG_BGSCAN
1097         if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1098                 bgscan_deinit(wpa_s);
1099                 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1100                         if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1101                                 wpa_printf(MSG_DEBUG, "Failed to initialize "
1102                                            "bgscan");
1103                                 /*
1104                                  * Live without bgscan; it is only used as a
1105                                  * roaming optimization, so the initial
1106                                  * connection is not affected.
1107                                  */
1108                         } else
1109                                 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1110                 } else
1111                         wpa_s->bgscan_ssid = NULL;
1112         }
1113 #endif /* CONFIG_BGSCAN */
1114
1115         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1116              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1117             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1118             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1119                 /* Set static WEP keys again */
1120                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1121         }
1122 }
1123
1124
1125 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
1126 {
1127         const u8 *bssid;
1128 #ifdef CONFIG_SME
1129         int authenticating;
1130         u8 prev_pending_bssid[ETH_ALEN];
1131
1132         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1133         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1134 #endif /* CONFIG_SME */
1135
1136         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1137                 /*
1138                  * At least Host AP driver and a Prism3 card seemed to be
1139                  * generating streams of disconnected events when configuring
1140                  * IBSS for WPA-None. Ignore them for now.
1141                  */
1142                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1143                            "IBSS/WPA-None mode");
1144                 return;
1145         }
1146
1147         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1148             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1149                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1150                         "pre-shared key may be incorrect");
1151         }
1152         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1153                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1154         bssid = wpa_s->bssid;
1155         if (is_zero_ether_addr(bssid))
1156                 bssid = wpa_s->pending_bssid;
1157         wpa_blacklist_add(wpa_s, bssid);
1158         wpa_sm_notify_disassoc(wpa_s->wpa);
1159         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
1160                 "remove keys");
1161         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1162                 wpa_s->keys_cleared = 0;
1163                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1164         }
1165         wpa_supplicant_mark_disassoc(wpa_s);
1166         bgscan_deinit(wpa_s);
1167 #ifdef CONFIG_SME
1168         if (authenticating &&
1169             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1170                 /*
1171                  * mac80211-workaround to force deauth on failed auth cmd,
1172                  * requires us to remain in authenticating state to allow the
1173                  * second authentication attempt to be continued properly.
1174                  */
1175                 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1176                            "proceed after disconnection event");
1177                 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1178                 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1179         }
1180 #endif /* CONFIG_SME */
1181 }
1182
1183
1184 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1185 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1186                                                     void *sock_ctx)
1187 {
1188         struct wpa_supplicant *wpa_s = eloop_ctx;
1189
1190         if (!wpa_s->pending_mic_error_report)
1191                 return;
1192
1193         wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1194         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1195         wpa_s->pending_mic_error_report = 0;
1196 }
1197 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1198
1199
1200 static void
1201 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1202                                          union wpa_event_data *data)
1203 {
1204         int pairwise;
1205         struct os_time t;
1206
1207         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1208         pairwise = (data && data->michael_mic_failure.unicast);
1209         os_get_time(&t);
1210         if ((wpa_s->last_michael_mic_error &&
1211              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1212             wpa_s->pending_mic_error_report) {
1213                 if (wpa_s->pending_mic_error_report) {
1214                         /*
1215                          * Send the pending MIC error report immediately since
1216                          * we are going to start countermeasures and AP better
1217                          * do the same.
1218                          */
1219                         wpa_sm_key_request(wpa_s->wpa, 1,
1220                                            wpa_s->pending_mic_error_pairwise);
1221                 }
1222
1223                 /* Send the new MIC error report immediately since we are going
1224                  * to start countermeasures and AP better do the same.
1225                  */
1226                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1227
1228                 /* initialize countermeasures */
1229                 wpa_s->countermeasures = 1;
1230                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1231
1232                 /*
1233                  * Need to wait for completion of request frame. We do not get
1234                  * any callback for the message completion, so just wait a
1235                  * short while and hope for the best. */
1236                 os_sleep(0, 10000);
1237
1238                 wpa_drv_set_countermeasures(wpa_s, 1);
1239                 wpa_supplicant_deauthenticate(wpa_s,
1240                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1241                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1242                                      wpa_s, NULL);
1243                 eloop_register_timeout(60, 0,
1244                                        wpa_supplicant_stop_countermeasures,
1245                                        wpa_s, NULL);
1246                 /* TODO: mark the AP rejected for 60 second. STA is
1247                  * allowed to associate with another AP.. */
1248         } else {
1249 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1250                 if (wpa_s->mic_errors_seen) {
1251                         /*
1252                          * Reduce the effectiveness of Michael MIC error
1253                          * reports as a means for attacking against TKIP if
1254                          * more than one MIC failure is noticed with the same
1255                          * PTK. We delay the transmission of the reports by a
1256                          * random time between 0 and 60 seconds in order to
1257                          * force the attacker wait 60 seconds before getting
1258                          * the information on whether a frame resulted in a MIC
1259                          * failure.
1260                          */
1261                         u8 rval[4];
1262                         int sec;
1263
1264                         if (os_get_random(rval, sizeof(rval)) < 0)
1265                                 sec = os_random() % 60;
1266                         else
1267                                 sec = WPA_GET_BE32(rval) % 60;
1268                         wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1269                                    "seconds", sec);
1270                         wpa_s->pending_mic_error_report = 1;
1271                         wpa_s->pending_mic_error_pairwise = pairwise;
1272                         eloop_cancel_timeout(
1273                                 wpa_supplicant_delayed_mic_error_report,
1274                                 wpa_s, NULL);
1275                         eloop_register_timeout(
1276                                 sec, os_random() % 1000000,
1277                                 wpa_supplicant_delayed_mic_error_report,
1278                                 wpa_s, NULL);
1279                 } else {
1280                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1281                 }
1282 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1283                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1284 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1285         }
1286         wpa_s->last_michael_mic_error = t.sec;
1287         wpa_s->mic_errors_seen++;
1288 }
1289
1290
1291 #ifdef CONFIG_TERMINATE_ONLASTIF
1292 static int any_interfaces(struct wpa_supplicant *head)
1293 {
1294         struct wpa_supplicant *wpa_s;
1295
1296         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1297                 if (!wpa_s->interface_removed)
1298                         return 1;
1299         return 0;
1300 }
1301 #endif /* CONFIG_TERMINATE_ONLASTIF */
1302
1303
1304 static void
1305 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1306                                       union wpa_event_data *data)
1307 {
1308         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1309                 return;
1310
1311         switch (data->interface_status.ievent) {
1312         case EVENT_INTERFACE_ADDED:
1313                 if (!wpa_s->interface_removed)
1314                         break;
1315                 wpa_s->interface_removed = 0;
1316                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1317                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1318                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
1319                                    "after interface was added.");
1320                 }
1321                 break;
1322         case EVENT_INTERFACE_REMOVED:
1323                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1324                 wpa_s->interface_removed = 1;
1325                 wpa_supplicant_mark_disassoc(wpa_s);
1326                 l2_packet_deinit(wpa_s->l2);
1327                 wpa_s->l2 = NULL;
1328 #ifdef CONFIG_TERMINATE_ONLASTIF
1329                 /* check if last interface */
1330                 if (!any_interfaces(wpa_s->global->ifaces))
1331                         eloop_terminate();
1332 #endif /* CONFIG_TERMINATE_ONLASTIF */
1333                 break;
1334         }
1335 }
1336
1337
1338 #ifdef CONFIG_PEERKEY
1339 static void
1340 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1341                               union wpa_event_data *data)
1342 {
1343         if (data == NULL)
1344                 return;
1345         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1346 }
1347 #endif /* CONFIG_PEERKEY */
1348
1349
1350 #ifdef CONFIG_IEEE80211R
1351 static void
1352 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1353                                  union wpa_event_data *data)
1354 {
1355         if (data == NULL)
1356                 return;
1357
1358         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1359                                     data->ft_ies.ies_len,
1360                                     data->ft_ies.ft_action,
1361                                     data->ft_ies.target_ap,
1362                                     data->ft_ies.ric_ies,
1363                                     data->ft_ies.ric_ies_len) < 0) {
1364                 /* TODO: prevent MLME/driver from trying to associate? */
1365         }
1366 }
1367 #endif /* CONFIG_IEEE80211R */
1368
1369
1370 #ifdef CONFIG_IBSS_RSN
1371 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1372                                                 union wpa_event_data *data)
1373 {
1374         if (data == NULL)
1375                 return;
1376         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1377 }
1378 #endif /* CONFIG_IBSS_RSN */
1379
1380
1381 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1382                           union wpa_event_data *data)
1383 {
1384         struct wpa_supplicant *wpa_s = ctx;
1385
1386         switch (event) {
1387         case EVENT_AUTH:
1388                 sme_event_auth(wpa_s, data);
1389                 break;
1390         case EVENT_ASSOC:
1391                 wpa_supplicant_event_assoc(wpa_s, data);
1392                 break;
1393         case EVENT_DISASSOC:
1394                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1395                         sme_event_disassoc(wpa_s, data);
1396                 /* fall through */
1397         case EVENT_DEAUTH:
1398                 wpa_supplicant_event_disassoc(wpa_s);
1399                 break;
1400         case EVENT_MICHAEL_MIC_FAILURE:
1401                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1402                 break;
1403 #ifndef CONFIG_NO_SCAN_PROCESSING
1404         case EVENT_SCAN_RESULTS:
1405                 wpa_supplicant_event_scan_results(wpa_s);
1406                 break;
1407 #endif /* CONFIG_NO_SCAN_PROCESSING */
1408         case EVENT_ASSOCINFO:
1409                 wpa_supplicant_event_associnfo(wpa_s, data);
1410                 break;
1411         case EVENT_INTERFACE_STATUS:
1412                 wpa_supplicant_event_interface_status(wpa_s, data);
1413                 break;
1414         case EVENT_PMKID_CANDIDATE:
1415                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1416                 break;
1417 #ifdef CONFIG_PEERKEY
1418         case EVENT_STKSTART:
1419                 wpa_supplicant_event_stkstart(wpa_s, data);
1420                 break;
1421 #endif /* CONFIG_PEERKEY */
1422 #ifdef CONFIG_IEEE80211R
1423         case EVENT_FT_RESPONSE:
1424                 wpa_supplicant_event_ft_response(wpa_s, data);
1425                 break;
1426 #endif /* CONFIG_IEEE80211R */
1427 #ifdef CONFIG_IBSS_RSN
1428         case EVENT_IBSS_RSN_START:
1429                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1430                 break;
1431 #endif /* CONFIG_IBSS_RSN */
1432         case EVENT_ASSOC_REJECT:
1433                 sme_event_assoc_reject(wpa_s, data);
1434                 break;
1435         case EVENT_AUTH_TIMED_OUT:
1436                 sme_event_auth_timed_out(wpa_s, data);
1437                 break;
1438         case EVENT_ASSOC_TIMED_OUT:
1439                 sme_event_assoc_timed_out(wpa_s, data);
1440                 break;
1441 #ifdef CONFIG_AP
1442         case EVENT_TX_STATUS:
1443                 if (wpa_s->ap_iface == NULL)
1444                         break;
1445                 switch (data->tx_status.type) {
1446                 case WLAN_FC_TYPE_MGMT:
1447                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1448                                       data->tx_status.data_len,
1449                                       data->tx_status.stype,
1450                                       data->tx_status.ack);
1451                         break;
1452                 case WLAN_FC_TYPE_DATA:
1453                         ap_tx_status(wpa_s, data->tx_status.dst,
1454                                      data->tx_status.data,
1455                                      data->tx_status.data_len,
1456                                      data->tx_status.ack);
1457                         break;
1458                 }
1459                 break;
1460         case EVENT_RX_FROM_UNKNOWN:
1461                 if (wpa_s->ap_iface == NULL)
1462                         break;
1463                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.hdr,
1464                                        data->rx_from_unknown.len);
1465                 break;
1466         case EVENT_RX_MGMT:
1467                 if (wpa_s->ap_iface == NULL)
1468                         break;
1469                 ap_mgmt_rx(wpa_s, data->rx_mgmt.frame,
1470                            data->rx_mgmt.frame_len, data->rx_mgmt.fi);
1471                 break;
1472 #endif /* CONFIG_AP */
1473         default:
1474                 wpa_printf(MSG_INFO, "Unknown event %d", event);
1475                 break;
1476         }
1477 }