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