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