Moved TKIP countermeasures from hostapd.c to its own file
[libeap.git] / hostapd / tkip_countermeasures.c
1 /*
2  * hostapd / TKIP countermeasures
3  * Copyright (c) 2002-2009, 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 "hostapd.h"
18 #include "eloop.h"
19 #include "driver_i.h"
20 #include "sta_info.h"
21 #include "mlme.h"
22 #include "wpa.h"
23
24
25 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
26                                                 void *timeout_ctx)
27 {
28         struct hostapd_data *hapd = eloop_ctx;
29         hapd->tkip_countermeasures = 0;
30         hostapd_set_countermeasures(hapd, 0);
31         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
32                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
33 }
34
35
36 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
37 {
38         struct sta_info *sta;
39
40         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
41                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
42
43         wpa_auth_countermeasures_start(hapd->wpa_auth);
44         hapd->tkip_countermeasures = 1;
45         hostapd_set_countermeasures(hapd, 1);
46         wpa_gtk_rekey(hapd->wpa_auth);
47         eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
48         eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
49                                hapd, NULL);
50         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
51                 hostapd_sta_deauth(hapd, sta->addr,
52                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
53                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
54                                 WLAN_STA_AUTHORIZED);
55                 hostapd_sta_remove(hapd, sta->addr);
56         }
57 }
58
59
60 void michael_mic_failure(struct hostapd_data *hapd, const u8 *addr, int local)
61 {
62         time_t now;
63
64         if (addr && local) {
65                 struct sta_info *sta = ap_get_sta(hapd, addr);
66                 if (sta != NULL) {
67                         wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
68                         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
69                                        HOSTAPD_LEVEL_INFO,
70                                        "Michael MIC failure detected in "
71                                        "received frame");
72                         mlme_michaelmicfailure_indication(hapd, addr);
73                 } else {
74                         wpa_printf(MSG_DEBUG,
75                                    "MLME-MICHAELMICFAILURE.indication "
76                                    "for not associated STA (" MACSTR
77                                    ") ignored", MAC2STR(addr));
78                         return;
79                 }
80         }
81
82         time(&now);
83         if (now > hapd->michael_mic_failure + 60) {
84                 hapd->michael_mic_failures = 1;
85         } else {
86                 hapd->michael_mic_failures++;
87                 if (hapd->michael_mic_failures > 1)
88                         ieee80211_tkip_countermeasures_start(hapd);
89         }
90         hapd->michael_mic_failure = now;
91 }