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