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