Import of WPA supplicant 0.4.9
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.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 <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <time.h>
20
21 #include "common.h"
22 #include "eapol_sm.h"
23 #include "wpa.h"
24 #include "eloop.h"
25 #include "wpa_supplicant.h"
26 #include "config.h"
27 #include "l2_packet.h"
28 #include "wpa_supplicant_i.h"
29 #include "pcsc_funcs.h"
30 #include "preauth.h"
31 #include "wpa_ctrl.h"
32 #include "eap.h"
33
34
35 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
36 {
37         struct wpa_ssid *ssid;
38
39         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
40                 return 0;
41
42         ssid = wpa_supplicant_get_ssid(wpa_s);
43         if (ssid == NULL) {
44                 wpa_printf(MSG_INFO, "No network configuration found for the "
45                            "current AP");
46                 return -1;
47         }
48
49         if (ssid->disabled) {
50                 wpa_printf(MSG_DEBUG, "Selected network is disabled");
51                 return -1;
52         }
53
54         wpa_printf(MSG_DEBUG, "Network configuration found for the current "
55                    "AP");
56         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
57                               WPA_KEY_MGMT_WPA_NONE)) {
58                 u8 wpa_ie[80];
59                 size_t wpa_ie_len = sizeof(wpa_ie);
60                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
61                                           wpa_ie, &wpa_ie_len);
62         } else {
63                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
64         }
65
66         wpa_s->current_ssid = ssid;
67         wpa_sm_set_config(wpa_s->wpa, wpa_s->current_ssid);
68         wpa_supplicant_initiate_eapol(wpa_s);
69
70         return 0;
71 }
72
73
74 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
75                                                 void *sock_ctx)
76 {
77         struct wpa_supplicant *wpa_s = eloop_ctx;
78
79         if (wpa_s->countermeasures) {
80                 wpa_s->countermeasures = 0;
81                 wpa_drv_set_countermeasures(wpa_s, 0);
82                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
83                 wpa_supplicant_req_scan(wpa_s, 0, 0);
84         }
85 }
86
87
88 static void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
89 {
90         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
91         memset(wpa_s->bssid, 0, ETH_ALEN);
92         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
93         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
94         if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK)
95                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
96 }
97
98
99 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
100 {
101         struct wpa_ie_data ie;
102         int i, pmksa_set = -1;
103
104         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
105             ie.pmkid == NULL)
106                 return;
107
108         for (i = 0; i < ie.num_pmkid; i++) {
109                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
110                                                     ie.pmkid + i * PMKID_LEN,
111                                                     NULL, NULL, 0);
112                 if (pmksa_set == 0) {
113                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
114                         break;
115                 }
116         }
117
118         wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
119                    "cache", pmksa_set == 0 ? "" : "not ");
120 }
121
122
123 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
124                                                  union wpa_event_data *data)
125 {
126         if (data == NULL) {
127                 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
128                 return;
129         }
130         wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
131                    " index=%d preauth=%d",
132                    MAC2STR(data->pmkid_candidate.bssid),
133                    data->pmkid_candidate.index,
134                    data->pmkid_candidate.preauth);
135
136         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
137                             data->pmkid_candidate.index,
138                             data->pmkid_candidate.preauth);
139 }
140
141
142 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
143 {
144         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
145             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
146                 return 0;
147
148         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
149             wpa_s->current_ssid &&
150             !(wpa_s->current_ssid->eapol_flags &
151               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
152                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
153                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
154                  * plaintext or static WEP keys). */
155                 return 0;
156         }
157
158         return 1;
159 }
160
161
162 /**
163  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
164  * @wpa_s: pointer to wpa_supplicant data
165  * @ssid: Configuration data for the network
166  * Returns: 0 on success, -1 on failure
167  *
168  * This function is called when starting authentication with a network that is
169  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
170  */
171 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
172                               struct wpa_ssid *ssid)
173 {
174         int aka = 0, sim = 0, type;
175
176         if (ssid->pcsc == NULL || wpa_s->scard != NULL)
177                 return 0;
178
179         if (ssid->eap_methods == NULL) {
180                 sim = 1;
181                 aka = 1;
182         } else {
183                 u8 *eap = ssid->eap_methods;
184                 while (*eap != EAP_TYPE_NONE) {
185                         if (*eap == EAP_TYPE_SIM)
186                                 sim = 1;
187                         else if (*eap == EAP_TYPE_AKA)
188                                 aka = 1;
189                         eap++;
190                 }
191         }
192
193 #ifndef EAP_SIM
194         sim = 0;
195 #endif /* EAP_SIM */
196 #ifndef EAP_AKA
197         aka = 0;
198 #endif /* EAP_AKA */
199
200         if (!sim && !aka) {
201                 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
202                            "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
203                 return 0;
204         }
205
206         wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
207                    "(sim=%d aka=%d) - initialize PCSC", sim, aka);
208         if (sim && aka)
209                 type = SCARD_TRY_BOTH;
210         else if (aka)
211                 type = SCARD_USIM_ONLY;
212         else
213                 type = SCARD_GSM_SIM_ONLY;
214
215         wpa_s->scard = scard_init(type);
216         if (wpa_s->scard == NULL) {
217                 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
218                            "(pcsc-lite)");
219                 return -1;
220         }
221         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
222         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
223
224         return 0;
225 }
226
227
228 static int wpa_supplicant_match_privacy(struct wpa_scan_result *bss,
229                                         struct wpa_ssid *ssid)
230 {
231         int i, privacy = 0;
232
233         for (i = 0; i < NUM_WEP_KEYS; i++) {
234                 if (ssid->wep_key_len[i]) {
235                         privacy = 1;
236                         break;
237                 }
238         }
239         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
240             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
241                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
242                 privacy = 1;
243
244         if (bss->caps & IEEE80211_CAP_PRIVACY)
245                 return privacy;
246         return !privacy;
247 }
248
249
250 static int wpa_supplicant_ssid_bss_match(struct wpa_ssid *ssid,
251                                          struct wpa_scan_result *bss)
252 {
253         struct wpa_ie_data ie;
254         int proto_match = 0;
255
256         while ((ssid->proto & WPA_PROTO_RSN) && bss->rsn_ie_len > 0) {
257                 proto_match++;
258
259                 if (wpa_parse_wpa_ie(bss->rsn_ie, bss->rsn_ie_len, &ie)) {
260                         wpa_printf(MSG_DEBUG, "   skip RSN IE - parse failed");
261                         break;
262                 }
263                 if (!(ie.proto & ssid->proto)) {
264                         wpa_printf(MSG_DEBUG, "   skip RSN IE - proto "
265                                    "mismatch");
266                         break;
267                 }
268
269                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
270                         wpa_printf(MSG_DEBUG, "   skip RSN IE - PTK cipher "
271                                    "mismatch");
272                         break;
273                 }
274
275                 if (!(ie.group_cipher & ssid->group_cipher)) {
276                         wpa_printf(MSG_DEBUG, "   skip RSN IE - GTK cipher "
277                                    "mismatch");
278                         break;
279                 }
280
281                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
282                         wpa_printf(MSG_DEBUG, "   skip RSN IE - key mgmt "
283                                    "mismatch");
284                         break;
285                 }
286
287                 wpa_printf(MSG_DEBUG, "   selected based on RSN IE");
288                 return 1;
289         }
290
291         while ((ssid->proto & WPA_PROTO_WPA) && bss->wpa_ie_len > 0) {
292                 proto_match++;
293
294                 if (wpa_parse_wpa_ie(bss->wpa_ie, bss->wpa_ie_len, &ie)) {
295                         wpa_printf(MSG_DEBUG, "   skip WPA IE - parse failed");
296                         break;
297                 }
298                 if (!(ie.proto & ssid->proto)) {
299                         wpa_printf(MSG_DEBUG, "   skip WPA IE - proto "
300                                    "mismatch");
301                         break;
302                 }
303
304                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
305                         wpa_printf(MSG_DEBUG, "   skip WPA IE - PTK cipher "
306                                    "mismatch");
307                         break;
308                 }
309
310                 if (!(ie.group_cipher & ssid->group_cipher)) {
311                         wpa_printf(MSG_DEBUG, "   skip WPA IE - GTK cipher "
312                                    "mismatch");
313                         break;
314                 }
315
316                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
317                         wpa_printf(MSG_DEBUG, "   skip WPA IE - key mgmt "
318                                    "mismatch");
319                         break;
320                 }
321
322                 wpa_printf(MSG_DEBUG, "   selected based on WPA IE");
323                 return 1;
324         }
325
326         if (proto_match == 0)
327                 wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN proto match");
328
329         return 0;
330 }
331
332
333 static struct wpa_scan_result *
334 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
335                           struct wpa_scan_result *results, int num,
336                           struct wpa_ssid **selected_ssid)
337 {
338         struct wpa_ssid *ssid;
339         struct wpa_scan_result *bss, *selected = NULL;
340         int i;
341         struct wpa_blacklist *e;
342
343         wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
344                    group->priority);
345
346         bss = NULL;
347         ssid = NULL;
348         /* First, try to find WPA-enabled AP */
349         for (i = 0; i < num && !selected; i++) {
350                 bss = &results[i];
351                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
352                            "wpa_ie_len=%lu rsn_ie_len=%lu caps=0x%x",
353                            i, MAC2STR(bss->bssid),
354                            wpa_ssid_txt(bss->ssid, bss->ssid_len),
355                            (unsigned long) bss->wpa_ie_len,
356                            (unsigned long) bss->rsn_ie_len, bss->caps);
357                 if ((e = wpa_blacklist_get(wpa_s, bss->bssid)) &&
358                     e->count > 1) {
359                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
360                         continue;
361                 }
362
363                 if (bss->wpa_ie_len == 0 && bss->rsn_ie_len == 0) {
364                         wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN IE");
365                         continue;
366                 }
367
368                 for (ssid = group; ssid; ssid = ssid->pnext) {
369                         if (ssid->disabled)
370                                 continue;
371                         if (bss->ssid_len != ssid->ssid_len ||
372                             memcmp(bss->ssid, ssid->ssid,
373                                    bss->ssid_len) != 0) {
374                                 wpa_printf(MSG_DEBUG, "   skip - "
375                                            "SSID mismatch");
376                                 continue;
377                         }
378                         if (ssid->bssid_set &&
379                             memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
380                                 wpa_printf(MSG_DEBUG, "   skip - "
381                                            "BSSID mismatch");
382                                 continue;
383                         }
384                         if (wpa_supplicant_ssid_bss_match(ssid, bss)) {
385                                 selected = bss;
386                                 *selected_ssid = ssid;
387                                 break;
388                         }
389                 }
390         }
391
392         /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
393          * allows this. */
394         for (i = 0; i < num && !selected; i++) {
395                 bss = &results[i];
396                 if ((e = wpa_blacklist_get(wpa_s, bss->bssid)) &&
397                     e->count > 1) {
398                         continue;
399                 }
400                 for (ssid = group; ssid; ssid = ssid->pnext) {
401                         if (!ssid->disabled &&
402                             (ssid->ssid_len == 0 ||
403                              (bss->ssid_len == ssid->ssid_len &&
404                               memcmp(bss->ssid, ssid->ssid, bss->ssid_len) ==
405                               0)) &&
406                             (!ssid->bssid_set ||
407                              memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0) &&
408                             ((ssid->key_mgmt & WPA_KEY_MGMT_NONE) ||
409                              (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
410                             && bss->wpa_ie_len == 0 && bss->rsn_ie_len == 0 &&
411                             wpa_supplicant_match_privacy(bss, ssid) &&
412                             !(bss->caps & IEEE80211_CAP_IBSS))
413                         {
414                                 selected = bss;
415                                 *selected_ssid = ssid;
416                                 wpa_printf(MSG_DEBUG, "   selected non-WPA AP "
417                                            MACSTR " ssid='%s'",
418                                            MAC2STR(bss->bssid),
419                                            wpa_ssid_txt(bss->ssid,
420                                                         bss->ssid_len));
421                                 break;
422                         }
423                 }
424         }
425
426         return selected;
427 }
428
429
430 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
431 {
432         int num, prio;
433         struct wpa_scan_result *selected = NULL;
434         struct wpa_ssid *ssid = NULL;
435         struct wpa_scan_result *results;
436
437         if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
438                 if (wpa_s->conf->ap_scan == 2)
439                         return;
440                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
441                            "scanning again");
442                 wpa_supplicant_req_scan(wpa_s, 1, 0);
443                 return;
444         }
445         if (wpa_s->conf->ap_scan == 2)
446                 return;
447         results = wpa_s->scan_results;
448         num = wpa_s->num_scan_results;
449
450         while (selected == NULL) {
451                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
452                         selected = wpa_supplicant_select_bss(
453                                 wpa_s, wpa_s->conf->pssid[prio], results, num,
454                                 &ssid);
455                         if (selected)
456                                 break;
457                 }
458
459                 if (selected == NULL && wpa_s->blacklist) {
460                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
461                                    "and try again");
462                         wpa_blacklist_clear(wpa_s);
463                 } else if (selected == NULL) {
464                         break;
465                 }
466         }
467
468         if (selected) {
469                 if (wpa_s->reassociate ||
470                     memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0) {
471                         if (wpa_supplicant_scard_init(wpa_s, ssid)) {
472                                 wpa_supplicant_req_scan(wpa_s, 10, 0);
473                                 return;
474                         }
475                         wpa_supplicant_associate(wpa_s, selected, ssid);
476                 } else {
477                         wpa_printf(MSG_DEBUG, "Already associated with the "
478                                    "selected AP.");
479                 }
480                 rsn_preauth_scan_results(wpa_s->wpa, results, num);
481         } else {
482                 wpa_printf(MSG_DEBUG, "No suitable AP found.");
483                 wpa_supplicant_req_scan(wpa_s, 5, 0);
484         }
485 }
486
487
488 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
489                                            union wpa_event_data *data)
490 {
491         int l, len, found = 0, wpa_found, rsn_found;
492         u8 *p;
493
494         wpa_printf(MSG_DEBUG, "Association info event");
495         if (data->assoc_info.req_ies)
496                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
497                             data->assoc_info.req_ies_len);
498         if (data->assoc_info.resp_ies)
499                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
500                             data->assoc_info.resp_ies_len);
501         if (data->assoc_info.beacon_ies)
502                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
503                             data->assoc_info.beacon_ies,
504                             data->assoc_info.beacon_ies_len);
505
506         p = data->assoc_info.req_ies;
507         l = data->assoc_info.req_ies_len;
508
509         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
510         while (p && l >= 2) {
511                 len = p[1] + 2;
512                 if (len > l) {
513                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
514                                     p, l);
515                         break;
516                 }
517                 if ((p[0] == GENERIC_INFO_ELEM && p[1] >= 6 &&
518                      (memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
519                     (p[0] == RSN_INFO_ELEM && p[1] >= 2)) {
520                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
521                                 break;
522                         found = 1;
523                         wpa_find_assoc_pmkid(wpa_s);
524                         break;
525                 }
526                 l -= len;
527                 p += len;
528         }
529         if (!found && data->assoc_info.req_ies)
530                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
531
532         /* WPA/RSN IE from Beacon/ProbeResp */
533         p = data->assoc_info.beacon_ies;
534         l = data->assoc_info.beacon_ies_len;
535
536         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
537          */
538         wpa_found = rsn_found = 0;
539         while (p && l >= 2) {
540                 len = p[1] + 2;
541                 if (len > l) {
542                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
543                                     p, l);
544                         break;
545                 }
546                 if (!wpa_found &&
547                     p[0] == GENERIC_INFO_ELEM && p[1] >= 6 &&
548                     memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
549                         wpa_found = 1;
550                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
551                 }
552
553                 if (!rsn_found &&
554                     p[0] == RSN_INFO_ELEM && p[1] >= 2) {
555                         rsn_found = 1;
556                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
557                 }
558
559                 l -= len;
560                 p += len;
561         }
562
563         if (!wpa_found && data->assoc_info.beacon_ies)
564                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
565         if (!rsn_found && data->assoc_info.beacon_ies)
566                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
567 }
568
569
570 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
571                                        union wpa_event_data *data)
572 {
573         u8 bssid[ETH_ALEN];
574
575         if (data)
576                 wpa_supplicant_event_associnfo(wpa_s, data);
577
578         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
579         if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
580             memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
581                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
582                         MACSTR, MAC2STR(bssid));
583                 memcpy(wpa_s->bssid, bssid, ETH_ALEN);
584                 if (wpa_supplicant_dynamic_keys(wpa_s)) {
585                         wpa_clear_keys(wpa_s, bssid);
586                 }
587                 if (wpa_supplicant_select_config(wpa_s) < 0) {
588                         wpa_supplicant_disassociate(wpa_s,
589                                                     REASON_DEAUTH_LEAVING);
590                         return;
591                 }
592         }
593
594         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
595         if (wpa_s->current_ssid) {
596                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
597                  * initialized before association, but for other modes,
598                  * initialize PC/SC here, if the current configuration needs
599                  * smartcard or SIM/USIM. */
600                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
601         }
602         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
603         l2_packet_notify_auth_start(wpa_s->l2);
604
605         /*
606          * Set portEnabled first to FALSE in order to get EAP state machine out
607          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
608          * state machine may transit to AUTHENTICATING state based on obsolete
609          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
610          * AUTHENTICATED without ever giving chance to EAP state machine to
611          * reset the state.
612          */
613         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
614         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
615         if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK)
616                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
617         /* 802.1X::portControl = Auto */
618         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
619         wpa_s->eapol_received = 0;
620         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
621             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
622                 wpa_supplicant_cancel_auth_timeout(wpa_s);
623                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
624         } else {
625                 /* Timeout for receiving the first EAPOL packet */
626                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
627         }
628 }
629
630
631 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
632 {
633         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
634                 /*
635                  * At least Host AP driver and a Prism3 card seemed to be
636                  * generating streams of disconnected events when configuring
637                  * IBSS for WPA-None. Ignore them for now.
638                  */
639                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
640                            "IBSS/WPA-None mode");
641                 return;
642         }
643
644         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
645             wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
646                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
647                         "pre-shared key may be incorrect");
648         }
649         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
650                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
651         wpa_blacklist_add(wpa_s, wpa_s->bssid);
652         wpa_sm_notify_disassoc(wpa_s->wpa);
653         wpa_supplicant_mark_disassoc(wpa_s);
654         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
655                 "remove keys");
656         if (wpa_supplicant_dynamic_keys(wpa_s)) {
657                 wpa_s->keys_cleared = 0;
658                 wpa_clear_keys(wpa_s, wpa_s->bssid);
659         }
660 }
661
662
663 static void
664 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
665                                          union wpa_event_data *data)
666 {
667         int pairwise;
668         time_t now;
669
670         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
671         pairwise = (data && data->michael_mic_failure.unicast);
672         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
673         time(&now);
674         if (wpa_s->last_michael_mic_error &&
675             now - wpa_s->last_michael_mic_error <= 60) {
676                 /* initialize countermeasures */
677                 wpa_s->countermeasures = 1;
678                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
679
680                 /*
681                  * Need to wait for completion of request frame. We do not get
682                  * any callback for the message completion, so just wait a
683                  * short while and hope for the best. */
684                 usleep(10000);
685
686                 wpa_drv_set_countermeasures(wpa_s, 1);
687                 wpa_supplicant_deauthenticate(wpa_s,
688                                               REASON_MICHAEL_MIC_FAILURE);
689                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
690                                      wpa_s, NULL);
691                 eloop_register_timeout(60, 0,
692                                        wpa_supplicant_stop_countermeasures,
693                                        wpa_s, NULL);
694                 /* TODO: mark the AP rejected for 60 second. STA is
695                  * allowed to associate with another AP.. */
696         }
697         wpa_s->last_michael_mic_error = now;
698 }
699
700
701 static void
702 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
703                                       union wpa_event_data *data)
704 {
705         if (strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
706                 return;
707
708         switch (data->interface_status.ievent) {
709         case EVENT_INTERFACE_ADDED:
710                 if (!wpa_s->interface_removed)
711                         break;
712                 wpa_s->interface_removed = 0;
713                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
714                 if (wpa_supplicant_driver_init(wpa_s, 1) < 0) {
715                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
716                                    "after interface was added.");
717                 }
718                 break;
719         case EVENT_INTERFACE_REMOVED:
720                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
721                 wpa_s->interface_removed = 1;
722                 wpa_supplicant_mark_disassoc(wpa_s);
723                 l2_packet_deinit(wpa_s->l2);
724                 wpa_s->l2 = NULL;
725                 break;
726         }
727 }
728
729
730 void wpa_supplicant_event(struct wpa_supplicant *wpa_s, wpa_event_type event,
731                           union wpa_event_data *data)
732 {
733         switch (event) {
734         case EVENT_ASSOC:
735                 wpa_supplicant_event_assoc(wpa_s, data);
736                 break;
737         case EVENT_DISASSOC:
738                 wpa_supplicant_event_disassoc(wpa_s);
739                 break;
740         case EVENT_MICHAEL_MIC_FAILURE:
741                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
742                 break;
743         case EVENT_SCAN_RESULTS:
744                 wpa_supplicant_event_scan_results(wpa_s);
745                 break;
746         case EVENT_ASSOCINFO:
747                 wpa_supplicant_event_associnfo(wpa_s, data);
748                 break;
749         case EVENT_INTERFACE_STATUS:
750                 wpa_supplicant_event_interface_status(wpa_s, data);
751                 break;
752         case EVENT_PMKID_CANDIDATE:
753                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
754                 break;
755         default:
756                 wpa_printf(MSG_INFO, "Unknown event %d", event);
757                 break;
758         }
759 }