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