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