Do not use SA Query procedure if reassociation is part of FT
[wpasupplicant] / hostapd / ieee802_11.c
1 /*
2  * hostapd / IEEE 802.11 Management
3  * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2007-2008, Intel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17
18 #ifndef CONFIG_NATIVE_WINDOWS
19
20 #include <net/if.h>
21
22 #include "eloop.h"
23 #include "hostapd.h"
24 #include "ieee802_11.h"
25 #include "beacon.h"
26 #include "hw_features.h"
27 #include "radius/radius.h"
28 #include "radius/radius_client.h"
29 #include "ieee802_11_auth.h"
30 #include "sta_info.h"
31 #include "rc4.h"
32 #include "ieee802_1x.h"
33 #include "wpa.h"
34 #include "wme.h"
35 #include "ap_list.h"
36 #include "accounting.h"
37 #include "driver.h"
38 #include "mlme.h"
39
40
41 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
42 {
43         u8 *pos = eid;
44         int i, num, count;
45
46         if (hapd->iface->current_rates == NULL)
47                 return eid;
48
49         *pos++ = WLAN_EID_SUPP_RATES;
50         num = hapd->iface->num_rates;
51         if (num > 8) {
52                 /* rest of the rates are encoded in Extended supported
53                  * rates element */
54                 num = 8;
55         }
56
57         *pos++ = num;
58         count = 0;
59         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
60              i++) {
61                 count++;
62                 *pos = hapd->iface->current_rates[i].rate / 5;
63                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
64                         *pos |= 0x80;
65                 pos++;
66         }
67
68         return pos;
69 }
70
71
72 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
73 {
74         u8 *pos = eid;
75         int i, num, count;
76
77         if (hapd->iface->current_rates == NULL)
78                 return eid;
79
80         num = hapd->iface->num_rates;
81         if (num <= 8)
82                 return eid;
83         num -= 8;
84
85         *pos++ = WLAN_EID_EXT_SUPP_RATES;
86         *pos++ = num;
87         count = 0;
88         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
89              i++) {
90                 count++;
91                 if (count <= 8)
92                         continue; /* already in SuppRates IE */
93                 *pos = hapd->iface->current_rates[i].rate / 5;
94                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
95                         *pos |= 0x80;
96                 pos++;
97         }
98
99         return pos;
100 }
101
102
103 u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
104 {
105 #ifdef CONFIG_IEEE80211N
106         struct ieee80211_ht_capability *cap;
107         u8 *pos = eid;
108
109         if (!hapd->iconf->ieee80211n)
110                 return eid;
111
112         *pos++ = WLAN_EID_HT_CAP;
113         *pos++ = sizeof(*cap);
114
115         cap = (struct ieee80211_ht_capability *) pos;
116         os_memset(cap, 0, sizeof(*cap));
117         SET_2BIT_U8(&cap->mac_ht_params_info,
118                     MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
119                     MAX_RX_AMPDU_FACTOR_64KB);
120
121         cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
122
123         cap->supported_mcs_set[0] = 0xff;
124         cap->supported_mcs_set[1] = 0xff;
125
126         pos += sizeof(*cap);
127
128         return pos;
129 #else /* CONFIG_IEEE80211N */
130         return eid;
131 #endif /* CONFIG_IEEE80211N */
132 }
133
134
135 u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
136 {
137 #ifdef CONFIG_IEEE80211N
138         struct ieee80211_ht_operation *oper;
139         u8 *pos = eid;
140
141         if (!hapd->iconf->ieee80211n)
142                 return eid;
143
144         *pos++ = WLAN_EID_HT_OPERATION;
145         *pos++ = sizeof(*oper);
146
147         oper = (struct ieee80211_ht_operation *) pos;
148         os_memset(oper, 0, sizeof(*oper));
149
150         oper->control_chan = hapd->iconf->channel;
151         oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
152         if (hapd->iconf->secondary_channel == 1)
153                 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
154                         HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
155         if (hapd->iconf->secondary_channel == -1)
156                 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
157                         HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
158
159         pos += sizeof(*oper);
160
161         return pos;
162 #else /* CONFIG_IEEE80211N */
163         return eid;
164 #endif /* CONFIG_IEEE80211N */
165 }
166
167
168 #ifdef CONFIG_IEEE80211N
169
170 /*
171 op_mode
172 Set to 0 (HT pure) under the followign conditions
173         - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
174         - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
175 Set to 1 (HT non-member protection) if there may be non-HT STAs
176         in both the primary and the secondary channel
177 Set to 2 if only HT STAs are associated in BSS,
178         however and at least one 20 MHz HT STA is associated
179 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
180         (currently non-GF HT station is considered as non-HT STA also)
181 */
182 int hostapd_ht_operation_update(struct hostapd_iface *iface)
183 {
184         u16 cur_op_mode, new_op_mode;
185         int op_mode_changes = 0;
186
187         if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
188                 return 0;
189
190         wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
191                    __func__, iface->ht_op_mode);
192
193         if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
194             && iface->num_sta_ht_no_gf) {
195                 iface->ht_op_mode |=
196                         HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
197                 op_mode_changes++;
198         } else if ((iface->ht_op_mode &
199                     HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
200                    iface->num_sta_ht_no_gf == 0) {
201                 iface->ht_op_mode &=
202                         ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
203                 op_mode_changes++;
204         }
205
206         if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
207             (iface->num_sta_no_ht || iface->olbc_ht)) {
208                 iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
209                 op_mode_changes++;
210         } else if ((iface->ht_op_mode &
211                     HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
212                    (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
213                 iface->ht_op_mode &=
214                         ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
215                 op_mode_changes++;
216         }
217
218         /* Note: currently we switch to the MIXED op mode if HT non-greenfield
219          * station is associated. Probably it's a theoretical case, since
220          * it looks like all known HT STAs support greenfield.
221          */
222         new_op_mode = 0;
223         if (iface->num_sta_no_ht ||
224             (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
225                 new_op_mode = OP_MODE_MIXED;
226         else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
227                  && iface->num_sta_ht_20mhz)
228                 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
229         else if (iface->olbc_ht)
230                 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
231         else
232                 new_op_mode = OP_MODE_PURE;
233
234         cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
235         if (cur_op_mode != new_op_mode) {
236                 iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
237                 iface->ht_op_mode |= new_op_mode;
238                 op_mode_changes++;
239         }
240
241         wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
242                    __func__, iface->ht_op_mode, op_mode_changes);
243
244         return op_mode_changes;
245 }
246
247 #endif /* CONFIG_IEEE80211N */
248
249
250 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
251                            int probe)
252 {
253         int capab = WLAN_CAPABILITY_ESS;
254         int privacy;
255
256         if (hapd->iface->num_sta_no_short_preamble == 0 &&
257             hapd->iconf->preamble == SHORT_PREAMBLE)
258                 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
259
260         privacy = hapd->conf->ssid.wep.keys_set;
261
262         if (hapd->conf->ieee802_1x &&
263             (hapd->conf->default_wep_key_len ||
264              hapd->conf->individual_wep_key_len))
265                 privacy = 1;
266
267         if (hapd->conf->wpa)
268                 privacy = 1;
269
270         if (sta) {
271                 int policy, def_klen;
272                 if (probe && sta->ssid_probe) {
273                         policy = sta->ssid_probe->security_policy;
274                         def_klen = sta->ssid_probe->wep.default_len;
275                 } else {
276                         policy = sta->ssid->security_policy;
277                         def_klen = sta->ssid->wep.default_len;
278                 }
279                 privacy = policy != SECURITY_PLAINTEXT;
280                 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
281                         privacy = 0;
282         }
283
284         if (privacy)
285                 capab |= WLAN_CAPABILITY_PRIVACY;
286
287         if (hapd->iface->current_mode &&
288             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
289             hapd->iface->num_sta_no_short_slot_time == 0)
290                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
291
292         return capab;
293 }
294
295
296 #ifdef CONFIG_IEEE80211W
297 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
298                                             struct sta_info *sta, u8 *eid)
299 {
300         u8 *pos = eid;
301         u32 timeout, tu;
302         struct os_time now, passed;
303
304         *pos++ = WLAN_EID_ASSOC_COMEBACK_TIME;
305         *pos++ = 4;
306         os_get_time(&now);
307         os_time_sub(&now, &sta->sa_query_start, &passed);
308         tu = (passed.sec * 1000000 + passed.usec) / 1024;
309         if (hapd->conf->assoc_sa_query_max_timeout > tu)
310                 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
311         else
312                 timeout = 0;
313         if (timeout < hapd->conf->assoc_sa_query_max_timeout)
314                 timeout++; /* add some extra time for local timers */
315         WPA_PUT_LE32(pos, timeout);
316         pos += 4;
317
318         return pos;
319 }
320 #endif /* CONFIG_IEEE80211W */
321
322
323 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
324 {
325         int i;
326         if (len > HOSTAPD_MAX_SSID_LEN)
327                 len = HOSTAPD_MAX_SSID_LEN;
328         for (i = 0; i < len; i++) {
329                 if (ssid[i] >= 32 && ssid[i] < 127)
330                         buf[i] = ssid[i];
331                 else
332                         buf[i] = '.';
333         }
334         buf[len] = '\0';
335 }
336
337
338 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
339 {
340         struct ieee80211_mgmt mgmt;
341
342         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
343                        HOSTAPD_LEVEL_DEBUG,
344                        "deauthenticate - reason %d", reason);
345         os_memset(&mgmt, 0, sizeof(mgmt));
346         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
347                                           WLAN_FC_STYPE_DEAUTH);
348         os_memcpy(mgmt.da, addr, ETH_ALEN);
349         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
350         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
351         mgmt.u.deauth.reason_code = host_to_le16(reason);
352         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
353                                     sizeof(mgmt.u.deauth), 0) < 0)
354                 perror("ieee802_11_send_deauth: send");
355 }
356
357
358 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
359                            u16 auth_transaction, u8 *challenge, int iswep)
360 {
361         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
362                        HOSTAPD_LEVEL_DEBUG,
363                        "authentication (shared key, transaction %d)",
364                        auth_transaction);
365
366         if (auth_transaction == 1) {
367                 if (!sta->challenge) {
368                         /* Generate a pseudo-random challenge */
369                         u8 key[8];
370                         time_t now;
371                         int r;
372                         sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
373                         if (sta->challenge == NULL)
374                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
375
376                         now = time(NULL);
377                         r = random();
378                         os_memcpy(key, &now, 4);
379                         os_memcpy(key + 4, &r, 4);
380                         rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
381                             key, sizeof(key));
382                 }
383                 return 0;
384         }
385
386         if (auth_transaction != 3)
387                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
388
389         /* Transaction 3 */
390         if (!iswep || !sta->challenge || !challenge ||
391             os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
392                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
393                                HOSTAPD_LEVEL_INFO,
394                                "shared key authentication - invalid "
395                                "challenge-response");
396                 return WLAN_STATUS_CHALLENGE_FAIL;
397         }
398
399         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
400                        HOSTAPD_LEVEL_DEBUG,
401                        "authentication OK (shared key)");
402 #ifdef IEEE80211_REQUIRE_AUTH_ACK
403         /* Station will be marked authenticated if it ACKs the
404          * authentication reply. */
405 #else
406         sta->flags |= WLAN_STA_AUTH;
407         wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
408 #endif
409         os_free(sta->challenge);
410         sta->challenge = NULL;
411
412         return 0;
413 }
414
415
416 static void send_auth_reply(struct hostapd_data *hapd,
417                             const u8 *dst, const u8 *bssid,
418                             u16 auth_alg, u16 auth_transaction, u16 resp,
419                             const u8 *ies, size_t ies_len)
420 {
421         struct ieee80211_mgmt *reply;
422         u8 *buf;
423         size_t rlen;
424
425         rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
426         buf = os_zalloc(rlen);
427         if (buf == NULL)
428                 return;
429
430         reply = (struct ieee80211_mgmt *) buf;
431         reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
432                                             WLAN_FC_STYPE_AUTH);
433         os_memcpy(reply->da, dst, ETH_ALEN);
434         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
435         os_memcpy(reply->bssid, bssid, ETH_ALEN);
436
437         reply->u.auth.auth_alg = host_to_le16(auth_alg);
438         reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
439         reply->u.auth.status_code = host_to_le16(resp);
440
441         if (ies && ies_len)
442                 os_memcpy(reply->u.auth.variable, ies, ies_len);
443
444         wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
445                    " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
446                    MAC2STR(dst), auth_alg, auth_transaction,
447                    resp, (unsigned long) ies_len);
448         if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
449                 perror("send_auth_reply: send");
450
451         os_free(buf);
452 }
453
454
455 #ifdef CONFIG_IEEE80211R
456 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
457                                   u16 auth_transaction, u16 status,
458                                   const u8 *ies, size_t ies_len)
459 {
460         struct hostapd_data *hapd = ctx;
461         struct sta_info *sta;
462
463         send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
464                         status, ies, ies_len);
465
466         if (status != WLAN_STATUS_SUCCESS)
467                 return;
468
469         sta = ap_get_sta(hapd, dst);
470         if (sta == NULL)
471                 return;
472
473         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
474                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
475         sta->flags |= WLAN_STA_AUTH;
476         mlme_authenticate_indication(hapd, sta);
477 }
478 #endif /* CONFIG_IEEE80211R */
479
480
481 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
482                         size_t len)
483 {
484         u16 auth_alg, auth_transaction, status_code;
485         u16 resp = WLAN_STATUS_SUCCESS;
486         struct sta_info *sta = NULL;
487         int res;
488         u16 fc;
489         u8 *challenge = NULL;
490         u32 session_timeout, acct_interim_interval;
491         int vlan_id = 0;
492         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
493         size_t resp_ies_len = 0;
494
495         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
496                 printf("handle_auth - too short payload (len=%lu)\n",
497                        (unsigned long) len);
498                 return;
499         }
500
501         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
502         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
503         status_code = le_to_host16(mgmt->u.auth.status_code);
504         fc = le_to_host16(mgmt->frame_control);
505
506         if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
507             2 + WLAN_AUTH_CHALLENGE_LEN &&
508             mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
509             mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
510                 challenge = &mgmt->u.auth.variable[2];
511
512         wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
513                    "auth_transaction=%d status_code=%d wep=%d%s",
514                    MAC2STR(mgmt->sa), auth_alg, auth_transaction,
515                    status_code, !!(fc & WLAN_FC_ISWEP),
516                    challenge ? " challenge" : "");
517
518         if (hapd->tkip_countermeasures) {
519                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
520                 goto fail;
521         }
522
523         if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
524                auth_alg == WLAN_AUTH_OPEN) ||
525 #ifdef CONFIG_IEEE80211R
526               (hapd->conf->wpa &&
527                (hapd->conf->wpa_key_mgmt &
528                 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
529                auth_alg == WLAN_AUTH_FT) ||
530 #endif /* CONFIG_IEEE80211R */
531               ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
532                auth_alg == WLAN_AUTH_SHARED_KEY))) {
533                 printf("Unsupported authentication algorithm (%d)\n",
534                        auth_alg);
535                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
536                 goto fail;
537         }
538
539         if (!(auth_transaction == 1 ||
540               (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
541                 printf("Unknown authentication transaction number (%d)\n",
542                        auth_transaction);
543                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
544                 goto fail;
545         }
546
547         if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
548                 printf("Station " MACSTR " not allowed to authenticate.\n",
549                        MAC2STR(mgmt->sa));
550                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
551                 goto fail;
552         }
553
554         res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
555                                       &session_timeout,
556                                       &acct_interim_interval, &vlan_id);
557         if (res == HOSTAPD_ACL_REJECT) {
558                 printf("Station " MACSTR " not allowed to authenticate.\n",
559                        MAC2STR(mgmt->sa));
560                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
561                 goto fail;
562         }
563         if (res == HOSTAPD_ACL_PENDING) {
564                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
565                            " waiting for an external authentication",
566                            MAC2STR(mgmt->sa));
567                 /* Authentication code will re-send the authentication frame
568                  * after it has received (and cached) information from the
569                  * external source. */
570                 return;
571         }
572
573         sta = ap_sta_add(hapd, mgmt->sa);
574         if (!sta) {
575                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
576                 goto fail;
577         }
578
579         if (vlan_id > 0) {
580                 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
581                                                sta->vlan_id) == NULL) {
582                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
583                                        HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
584                                        "%d received from RADIUS server",
585                                        vlan_id);
586                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
587                         goto fail;
588                 }
589                 sta->vlan_id = vlan_id;
590                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
591                                HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
592         }
593
594         sta->flags &= ~WLAN_STA_PREAUTH;
595         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
596
597         if (hapd->conf->radius->acct_interim_interval == 0 &&
598             acct_interim_interval)
599                 sta->acct_interim_interval = acct_interim_interval;
600         if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
601                 ap_sta_session_timeout(hapd, sta, session_timeout);
602         else
603                 ap_sta_no_session_timeout(hapd, sta);
604
605         switch (auth_alg) {
606         case WLAN_AUTH_OPEN:
607                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
608                                HOSTAPD_LEVEL_DEBUG,
609                                "authentication OK (open system)");
610 #ifdef IEEE80211_REQUIRE_AUTH_ACK
611                 /* Station will be marked authenticated if it ACKs the
612                  * authentication reply. */
613 #else
614                 sta->flags |= WLAN_STA_AUTH;
615                 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
616                 sta->auth_alg = WLAN_AUTH_OPEN;
617                 mlme_authenticate_indication(hapd, sta);
618 #endif
619                 break;
620         case WLAN_AUTH_SHARED_KEY:
621                 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
622                                        fc & WLAN_FC_ISWEP);
623                 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
624                 mlme_authenticate_indication(hapd, sta);
625                 if (sta->challenge && auth_transaction == 1) {
626                         resp_ies[0] = WLAN_EID_CHALLENGE;
627                         resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
628                         os_memcpy(resp_ies + 2, sta->challenge,
629                                   WLAN_AUTH_CHALLENGE_LEN);
630                         resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
631                 }
632                 break;
633 #ifdef CONFIG_IEEE80211R
634         case WLAN_AUTH_FT:
635                 sta->auth_alg = WLAN_AUTH_FT;
636                 if (sta->wpa_sm == NULL)
637                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
638                                                         sta->addr);
639                 if (sta->wpa_sm == NULL) {
640                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
641                                    "state machine");
642                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
643                         goto fail;
644                 }
645                 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
646                                     auth_transaction, mgmt->u.auth.variable,
647                                     len - IEEE80211_HDRLEN -
648                                     sizeof(mgmt->u.auth),
649                                     handle_auth_ft_finish, hapd);
650                 /* handle_auth_ft_finish() callback will complete auth. */
651                 return;
652 #endif /* CONFIG_IEEE80211R */
653         }
654
655  fail:
656         send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
657                         auth_transaction + 1, resp, resp_ies, resp_ies_len);
658 }
659
660
661 static void handle_assoc(struct hostapd_data *hapd,
662                          struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
663 {
664         u16 capab_info, listen_interval;
665         u16 resp = WLAN_STATUS_SUCCESS;
666         u8 *pos, *wpa_ie;
667         size_t wpa_ie_len;
668         int send_deauth = 0, send_len, left, i;
669         struct sta_info *sta;
670         struct ieee802_11_elems elems;
671         u8 buf[sizeof(struct ieee80211_mgmt) + 512];
672         struct ieee80211_mgmt *reply;
673
674         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
675                                       sizeof(mgmt->u.assoc_req))) {
676                 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
677                        "\n", reassoc, (unsigned long) len);
678                 return;
679         }
680
681         if (reassoc) {
682                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
683                 listen_interval = le_to_host16(
684                         mgmt->u.reassoc_req.listen_interval);
685                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
686                            " capab_info=0x%02x listen_interval=%d current_ap="
687                            MACSTR,
688                            MAC2STR(mgmt->sa), capab_info, listen_interval,
689                            MAC2STR(mgmt->u.reassoc_req.current_ap));
690                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
691                 pos = mgmt->u.reassoc_req.variable;
692         } else {
693                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
694                 listen_interval = le_to_host16(
695                         mgmt->u.assoc_req.listen_interval);
696                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
697                            " capab_info=0x%02x listen_interval=%d",
698                            MAC2STR(mgmt->sa), capab_info, listen_interval);
699                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
700                 pos = mgmt->u.assoc_req.variable;
701         }
702
703         sta = ap_get_sta(hapd, mgmt->sa);
704 #ifdef CONFIG_IEEE80211R
705         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
706             (sta->flags & WLAN_STA_AUTH) == 0) {
707                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
708                            "prior to authentication since it is using "
709                            "over-the-DS FT", MAC2STR(mgmt->sa));
710         } else
711 #endif /* CONFIG_IEEE80211R */
712         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
713                 printf("STA " MACSTR " trying to associate before "
714                        "authentication\n", MAC2STR(mgmt->sa));
715                 if (sta) {
716                         printf("  sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
717                                MAC2STR(sta->addr), sta->aid, sta->flags);
718                 }
719                 send_deauth = 1;
720                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
721                 goto fail;
722         }
723
724         if (hapd->tkip_countermeasures) {
725                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
726                 goto fail;
727         }
728
729         if (listen_interval > hapd->conf->max_listen_interval) {
730                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
731                                HOSTAPD_LEVEL_DEBUG,
732                                "Too large Listen Interval (%d)",
733                                listen_interval);
734                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
735                 goto fail;
736         }
737
738         sta->capability = capab_info;
739         sta->listen_interval = listen_interval;
740
741         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
742          * is used */
743         if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
744             !elems.ssid) {
745                 printf("STA " MACSTR " sent invalid association request\n",
746                        MAC2STR(sta->addr));
747                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
748                 goto fail;
749         }
750
751         if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
752             os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
753         {
754                 char ssid_txt[33];
755                 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
756                 printf("Station " MACSTR " tried to associate with "
757                        "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
758                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
759                 goto fail;
760         }
761
762         sta->flags &= ~WLAN_STA_WME;
763         if (elems.wme && hapd->conf->wme_enabled) {
764                 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
765                         hostapd_logger(hapd, sta->addr,
766                                        HOSTAPD_MODULE_WPA,
767                                        HOSTAPD_LEVEL_DEBUG,
768                                        "invalid WME element in association "
769                                        "request");
770                 else
771                         sta->flags |= WLAN_STA_WME;
772         }
773
774         if (!elems.supp_rates) {
775                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
776                                HOSTAPD_LEVEL_DEBUG,
777                                "No supported rates element in AssocReq");
778                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
779                 goto fail;
780         }
781
782         if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
783                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
784                                HOSTAPD_LEVEL_DEBUG,
785                                "Invalid supported rates element length %d",
786                                elems.supp_rates_len);
787                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
788                 goto fail;
789         }
790
791         os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
792         os_memcpy(sta->supported_rates, elems.supp_rates,
793                   elems.supp_rates_len);
794         sta->supported_rates_len = elems.supp_rates_len;
795
796         if (elems.ext_supp_rates) {
797                 if (elems.supp_rates_len + elems.ext_supp_rates_len >
798                     sizeof(sta->supported_rates)) {
799                         hostapd_logger(hapd, mgmt->sa,
800                                        HOSTAPD_MODULE_IEEE80211,
801                                        HOSTAPD_LEVEL_DEBUG,
802                                        "Invalid supported rates element length"
803                                        " %d+%d", elems.supp_rates_len,
804                                        elems.ext_supp_rates_len);
805                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
806                         goto fail;
807                 }
808
809                 os_memcpy(sta->supported_rates + elems.supp_rates_len,
810                           elems.ext_supp_rates, elems.ext_supp_rates_len);
811                 sta->supported_rates_len += elems.ext_supp_rates_len;
812         }
813
814 #ifdef CONFIG_IEEE80211N
815         /* save HT capabilities in the sta object */
816         os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
817         if (elems.ht_capabilities &&
818             elems.ht_capabilities_len >=
819             sizeof(struct ieee80211_ht_capability)) {
820                 sta->flags |= WLAN_STA_HT;
821                 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
822                 sta->ht_capabilities.length =
823                         sizeof(struct ieee80211_ht_capability);
824                 os_memcpy(&sta->ht_capabilities.data,
825                           elems.ht_capabilities,
826                           sizeof(struct ieee80211_ht_capability));
827         } else
828                 sta->flags &= ~WLAN_STA_HT;
829 #endif /* CONFIG_IEEE80211N */
830
831         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
832                 wpa_ie = elems.rsn_ie;
833                 wpa_ie_len = elems.rsn_ie_len;
834         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
835                    elems.wpa_ie) {
836                 wpa_ie = elems.wpa_ie;
837                 wpa_ie_len = elems.wpa_ie_len;
838         } else {
839                 wpa_ie = NULL;
840                 wpa_ie_len = 0;
841         }
842 #ifdef CONFIG_WPS
843         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
844         if (hapd->conf->wps_state && wpa_ie == NULL) {
845                 if (elems.wps_ie) {
846                         wpa_printf(MSG_DEBUG, "STA included WPS IE in "
847                                    "(Re)Association Request - assume WPS is "
848                                    "used");
849                         sta->flags |= WLAN_STA_WPS;
850                         wpabuf_free(sta->wps_ie);
851                         sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
852                                                         elems.wps_ie_len - 4);
853                 } else {
854                         wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
855                                    "in (Re)Association Request - possible WPS "
856                                    "use");
857                         sta->flags |= WLAN_STA_MAYBE_WPS;
858                 }
859         } else
860 #endif /* CONFIG_WPS */
861         if (hapd->conf->wpa && wpa_ie == NULL) {
862                 printf("STA " MACSTR ": No WPA/RSN IE in association "
863                        "request\n", MAC2STR(sta->addr));
864                 resp = WLAN_STATUS_INVALID_IE;
865                 goto fail;
866         }
867
868         if (hapd->conf->wpa && wpa_ie) {
869                 int res;
870                 wpa_ie -= 2;
871                 wpa_ie_len += 2;
872                 if (sta->wpa_sm == NULL)
873                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
874                                                         sta->addr);
875                 if (sta->wpa_sm == NULL) {
876                         printf("Failed to initialize WPA state machine\n");
877                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
878                         goto fail;
879                 }
880                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
881                                           wpa_ie, wpa_ie_len,
882                                           elems.mdie, elems.mdie_len);
883                 if (res == WPA_INVALID_GROUP)
884                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
885                 else if (res == WPA_INVALID_PAIRWISE)
886                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
887                 else if (res == WPA_INVALID_AKMP)
888                         resp = WLAN_STATUS_AKMP_NOT_VALID;
889                 else if (res == WPA_ALLOC_FAIL)
890                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
891 #ifdef CONFIG_IEEE80211W
892                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
893                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
894                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
895                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
896 #endif /* CONFIG_IEEE80211W */
897                 else if (res == WPA_INVALID_MDIE)
898                         resp = WLAN_STATUS_INVALID_MDIE;
899                 else if (res != WPA_IE_OK)
900                         resp = WLAN_STATUS_INVALID_IE;
901                 if (resp != WLAN_STATUS_SUCCESS)
902                         goto fail;
903 #ifdef CONFIG_IEEE80211W
904                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
905                     sta->sa_query_count > 0)
906                         ap_check_sa_query_timeout(hapd, sta);
907                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
908                     (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
909                         /*
910                          * STA has already been associated with MFP and SA
911                          * Query timeout has not been reached. Reject the
912                          * association attempt temporarily and start SA Query,
913                          * if one is not pending.
914                          */
915
916                         if (sta->sa_query_count == 0)
917                                 ap_sta_start_sa_query(hapd, sta);
918
919                         resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
920                         goto fail;
921                 }
922
923                 if (wpa_auth_uses_mfp(sta->wpa_sm))
924                         sta->flags |= WLAN_STA_MFP;
925                 else
926                         sta->flags &= ~WLAN_STA_MFP;
927 #endif /* CONFIG_IEEE80211W */
928
929 #ifdef CONFIG_IEEE80211R
930                 if (sta->auth_alg == WLAN_AUTH_FT) {
931                         if (!reassoc) {
932                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
933                                            "to use association (not "
934                                            "re-association) with FT auth_alg",
935                                            MAC2STR(sta->addr));
936                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
937                                 goto fail;
938                         }
939
940                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
941                         if (resp != WLAN_STATUS_SUCCESS)
942                                 goto fail;
943                 }
944 #endif /* CONFIG_IEEE80211R */
945 #ifdef CONFIG_IEEE80211N
946                 if ((sta->flags & WLAN_STA_HT) &&
947                     wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
948                         wpa_printf(MSG_DEBUG, "HT: " MACSTR " tried to "
949                                    "use TKIP with HT association",
950                                    MAC2STR(sta->addr));
951                         resp = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
952                         goto fail;
953                 }
954 #endif /* CONFIG_IEEE80211N */
955         } else
956                 wpa_auth_sta_no_wpa(sta->wpa_sm);
957
958         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
959                 sta->flags |= WLAN_STA_NONERP;
960         for (i = 0; i < sta->supported_rates_len; i++) {
961                 if ((sta->supported_rates[i] & 0x7f) > 22) {
962                         sta->flags &= ~WLAN_STA_NONERP;
963                         break;
964                 }
965         }
966         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
967                 sta->nonerp_set = 1;
968                 hapd->iface->num_sta_non_erp++;
969                 if (hapd->iface->num_sta_non_erp == 1)
970                         ieee802_11_set_beacons(hapd->iface);
971         }
972
973         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
974             !sta->no_short_slot_time_set) {
975                 sta->no_short_slot_time_set = 1;
976                 hapd->iface->num_sta_no_short_slot_time++;
977                 if (hapd->iface->current_mode->mode ==
978                     HOSTAPD_MODE_IEEE80211G &&
979                     hapd->iface->num_sta_no_short_slot_time == 1)
980                         ieee802_11_set_beacons(hapd->iface);
981         }
982
983         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
984                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
985         else
986                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
987
988         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
989             !sta->no_short_preamble_set) {
990                 sta->no_short_preamble_set = 1;
991                 hapd->iface->num_sta_no_short_preamble++;
992                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
993                     && hapd->iface->num_sta_no_short_preamble == 1)
994                         ieee802_11_set_beacons(hapd->iface);
995         }
996
997 #ifdef CONFIG_IEEE80211N
998         if (sta->flags & WLAN_STA_HT) {
999                 u16 ht_capab = le_to_host16(
1000                         sta->ht_capabilities.data.capabilities_info);
1001                 wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
1002                            "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
1003                 if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
1004                         if (!sta->no_ht_gf_set) {
1005                                 sta->no_ht_gf_set = 1;
1006                                 hapd->iface->num_sta_ht_no_gf++;
1007                         }
1008                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1009                                    "greenfield, num of non-gf stations %d",
1010                                    __func__, MAC2STR(sta->addr),
1011                                    hapd->iface->num_sta_ht_no_gf);
1012                 }
1013                 if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1014                         if (!sta->ht_20mhz_set) {
1015                                 sta->ht_20mhz_set = 1;
1016                                 hapd->iface->num_sta_ht_20mhz++;
1017                         }
1018                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1019                                    "num of 20MHz HT STAs %d",
1020                                    __func__, MAC2STR(sta->addr),
1021                                    hapd->iface->num_sta_ht_20mhz);
1022                 }
1023         } else {
1024                 if (!sta->no_ht_set) {
1025                         sta->no_ht_set = 1;
1026                         hapd->iface->num_sta_no_ht++;
1027                 }
1028                 if (hapd->iconf->ieee80211n) {
1029                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1030                                    " - no HT, num of non-HT stations %d",
1031                                    __func__, MAC2STR(sta->addr),
1032                                    hapd->iface->num_sta_no_ht);
1033                 }
1034         }
1035
1036         if (hostapd_ht_operation_update(hapd->iface) > 0)
1037                 ieee802_11_set_beacons(hapd->iface);
1038 #endif /* CONFIG_IEEE80211N */
1039
1040         /* get a unique AID */
1041         if (sta->aid > 0) {
1042                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
1043         } else {
1044                 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1045                         if (hapd->sta_aid[sta->aid - 1] == NULL)
1046                                 break;
1047                 if (sta->aid > MAX_AID_TABLE_SIZE) {
1048                         sta->aid = 0;
1049                         resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1050                         wpa_printf(MSG_ERROR, "  no room for more AIDs");
1051                         goto fail;
1052                 } else {
1053                         hapd->sta_aid[sta->aid - 1] = sta;
1054                         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
1055                 }
1056         }
1057
1058         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1059                        HOSTAPD_LEVEL_DEBUG,
1060                        "association OK (aid %d)", sta->aid);
1061         /* Station will be marked associated, after it acknowledges AssocResp
1062          */
1063
1064         if (reassoc) {
1065                 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1066                           ETH_ALEN);
1067         }
1068
1069         if (sta->last_assoc_req)
1070                 os_free(sta->last_assoc_req);
1071         sta->last_assoc_req = os_malloc(len);
1072         if (sta->last_assoc_req)
1073                 os_memcpy(sta->last_assoc_req, mgmt, len);
1074
1075         /* Make sure that the previously registered inactivity timer will not
1076          * remove the STA immediately. */
1077         sta->timeout_next = STA_NULLFUNC;
1078
1079  fail:
1080         os_memset(buf, 0, sizeof(buf));
1081         reply = (struct ieee80211_mgmt *) buf;
1082         reply->frame_control =
1083                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1084                              (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1085                               (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1086                                WLAN_FC_STYPE_ASSOC_RESP)));
1087         os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1088         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1089         os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1090
1091         send_len = IEEE80211_HDRLEN;
1092         if (send_deauth) {
1093                 send_len += sizeof(reply->u.deauth);
1094                 reply->u.deauth.reason_code = host_to_le16(resp);
1095         } else {
1096                 u8 *p;
1097                 send_len += sizeof(reply->u.assoc_resp);
1098                 reply->u.assoc_resp.capab_info =
1099                         host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1100                 reply->u.assoc_resp.status_code = host_to_le16(resp);
1101                 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1102                                                        | BIT(14) | BIT(15));
1103                 /* Supported rates */
1104                 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1105                 /* Extended supported rates */
1106                 p = hostapd_eid_ext_supp_rates(hapd, p);
1107                 if (sta->flags & WLAN_STA_WME)
1108                         p = hostapd_eid_wme(hapd, p);
1109
1110                 p = hostapd_eid_ht_capabilities_info(hapd, p);
1111                 p = hostapd_eid_ht_operation(hapd, p);
1112
1113 #ifdef CONFIG_IEEE80211R
1114                 if (resp == WLAN_STATUS_SUCCESS) {
1115                         /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1116                          * Transition Information, RSN */
1117                         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1118                                                         buf + sizeof(buf) - p,
1119                                                         sta->auth_alg);
1120                 }
1121 #endif /* CONFIG_IEEE80211R */
1122
1123 #ifdef CONFIG_IEEE80211W
1124                 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1125                         p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1126 #endif /* CONFIG_IEEE80211W */
1127
1128                 send_len += p - reply->u.assoc_resp.variable;
1129         }
1130
1131         if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1132                 perror("handle_assoc: send");
1133 }
1134
1135
1136 static void handle_disassoc(struct hostapd_data *hapd,
1137                             struct ieee80211_mgmt *mgmt, size_t len)
1138 {
1139         struct sta_info *sta;
1140
1141         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1142                 printf("handle_disassoc - too short payload (len=%lu)\n",
1143                        (unsigned long) len);
1144                 return;
1145         }
1146
1147         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1148                    MAC2STR(mgmt->sa),
1149                    le_to_host16(mgmt->u.disassoc.reason_code));
1150
1151         sta = ap_get_sta(hapd, mgmt->sa);
1152         if (sta == NULL) {
1153                 printf("Station " MACSTR " trying to disassociate, but it "
1154                        "is not associated.\n", MAC2STR(mgmt->sa));
1155                 return;
1156         }
1157
1158         sta->flags &= ~WLAN_STA_ASSOC;
1159         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1160         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1161                        HOSTAPD_LEVEL_INFO, "disassociated");
1162         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1163         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1164         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1165          * authenticated. */
1166         accounting_sta_stop(hapd, sta);
1167         ieee802_1x_free_station(sta);
1168         hostapd_sta_remove(hapd, sta->addr);
1169
1170         if (sta->timeout_next == STA_NULLFUNC ||
1171             sta->timeout_next == STA_DISASSOC) {
1172                 sta->timeout_next = STA_DEAUTH;
1173                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1174                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1175                                        hapd, sta);
1176         }
1177
1178         mlme_disassociate_indication(
1179                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1180 }
1181
1182
1183 static void handle_deauth(struct hostapd_data *hapd,
1184                           struct ieee80211_mgmt *mgmt, size_t len)
1185 {
1186         struct sta_info *sta;
1187
1188         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1189                 printf("handle_deauth - too short payload (len=%lu)\n",
1190                        (unsigned long) len);
1191                 return;
1192         }
1193
1194         wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1195                    " reason_code=%d",
1196                    MAC2STR(mgmt->sa),
1197                    le_to_host16(mgmt->u.deauth.reason_code));
1198
1199         sta = ap_get_sta(hapd, mgmt->sa);
1200         if (sta == NULL) {
1201                 printf("Station " MACSTR " trying to deauthenticate, but it "
1202                        "is not authenticated.\n", MAC2STR(mgmt->sa));
1203                 return;
1204         }
1205
1206         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1207         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1208         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1209                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1210         mlme_deauthenticate_indication(
1211                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1212         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1213         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1214         ap_free_sta(hapd, sta);
1215 }
1216
1217
1218 static void handle_beacon(struct hostapd_data *hapd,
1219                           struct ieee80211_mgmt *mgmt, size_t len,
1220                           struct hostapd_frame_info *fi)
1221 {
1222         struct ieee802_11_elems elems;
1223
1224         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1225                 printf("handle_beacon - too short payload (len=%lu)\n",
1226                        (unsigned long) len);
1227                 return;
1228         }
1229
1230         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1231                                       len - (IEEE80211_HDRLEN +
1232                                              sizeof(mgmt->u.beacon)), &elems,
1233                                       0);
1234
1235         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1236 }
1237
1238
1239 #ifdef CONFIG_IEEE80211W
1240 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1241                                     struct ieee80211_mgmt *mgmt, size_t len)
1242 {
1243         struct sta_info *sta;
1244         u8 *end;
1245         int i;
1246
1247         end = mgmt->u.action.u.sa_query_resp.trans_id +
1248                 WLAN_SA_QUERY_TR_ID_LEN;
1249         if (((u8 *) mgmt) + len < end) {
1250                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1251                            "frame (len=%lu)", (unsigned long) len);
1252                 return;
1253         }
1254
1255         if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1256                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1257                            "Action %d", mgmt->u.action.u.sa_query_resp.action);
1258                 return;
1259         }
1260
1261         /* MLME-SAQuery.confirm */
1262
1263         sta = ap_get_sta(hapd, mgmt->sa);
1264         if (sta == NULL || sta->sa_query_trans_id == NULL) {
1265                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1266                            "pending SA Query request found");
1267                 return;
1268         }
1269
1270         for (i = 0; i < sta->sa_query_count; i++) {
1271                 if (os_memcmp(sta->sa_query_trans_id +
1272                               i * WLAN_SA_QUERY_TR_ID_LEN,
1273                               mgmt->u.action.u.sa_query_resp.trans_id,
1274                               WLAN_SA_QUERY_TR_ID_LEN) == 0)
1275                         break;
1276         }
1277
1278         if (i >= sta->sa_query_count) {
1279                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1280                            "transaction identifier found");
1281                 return;
1282         }
1283
1284         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1285                        HOSTAPD_LEVEL_DEBUG,
1286                        "Reply to pending SA Query received");
1287         ap_sta_stop_sa_query(hapd, sta);
1288 }
1289 #endif /* CONFIG_IEEE80211W */
1290
1291
1292 static void handle_action(struct hostapd_data *hapd,
1293                           struct ieee80211_mgmt *mgmt, size_t len)
1294 {
1295         if (len < IEEE80211_HDRLEN + 1) {
1296                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1297                                HOSTAPD_LEVEL_DEBUG,
1298                                "handle_action - too short payload (len=%lu)",
1299                                (unsigned long) len);
1300                 return;
1301         }
1302
1303         switch (mgmt->u.action.category) {
1304 #ifdef CONFIG_IEEE80211R
1305         case WLAN_ACTION_FT:
1306         {
1307                 struct sta_info *sta;
1308
1309                 sta = ap_get_sta(hapd, mgmt->sa);
1310                 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1311                         wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1312                                    "frame from unassociated STA " MACSTR,
1313                                    MAC2STR(mgmt->sa));
1314                         return;
1315                 }
1316
1317                 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1318                                      len - IEEE80211_HDRLEN))
1319                         break;
1320
1321                 return;
1322         }
1323 #endif /* CONFIG_IEEE80211R */
1324         case WLAN_ACTION_WMM:
1325                 hostapd_wme_action(hapd, mgmt, len);
1326                 return;
1327 #ifdef CONFIG_IEEE80211W
1328         case WLAN_ACTION_SA_QUERY:
1329                 hostapd_sa_query_action(hapd, mgmt, len);
1330                 return;
1331 #endif /* CONFIG_IEEE80211W */
1332         }
1333
1334         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1335                        HOSTAPD_LEVEL_DEBUG,
1336                        "handle_action - unknown action category %d or invalid "
1337                        "frame",
1338                        mgmt->u.action.category);
1339         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1340             !(mgmt->sa[0] & 0x01)) {
1341                 /*
1342                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1343                  * Return the Action frame to the source without change
1344                  * except that MSB of the Category set to 1.
1345                  */
1346                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1347                            "frame back to sender");
1348                 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1349                 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1350                 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1351                 mgmt->u.action.category |= 0x80;
1352
1353                 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1354         }
1355 }
1356
1357
1358 /**
1359  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1360  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1361  * sent to)
1362  * @buf: management frame data (starting from IEEE 802.11 header)
1363  * @len: length of frame data in octets
1364  * @stype: management frame subtype from frame control field
1365  *
1366  * Process all incoming IEEE 802.11 management frames. This will be called for
1367  * each frame received from the kernel driver through wlan#ap interface. In
1368  * addition, it can be called to re-inserted pending frames (e.g., when using
1369  * external RADIUS server as an MAC ACL).
1370  */
1371 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1372                      struct hostapd_frame_info *fi)
1373 {
1374         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1375         int broadcast;
1376
1377         if (stype == WLAN_FC_STYPE_BEACON) {
1378                 handle_beacon(hapd, mgmt, len, fi);
1379                 return;
1380         }
1381
1382         if (fi && fi->passive_scan)
1383                 return;
1384
1385         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1386                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1387                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1388
1389         if (!broadcast &&
1390             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1391                 printf("MGMT: BSSID=" MACSTR " not our address\n",
1392                        MAC2STR(mgmt->bssid));
1393                 return;
1394         }
1395
1396
1397         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1398                 handle_probe_req(hapd, mgmt, len);
1399                 return;
1400         }
1401
1402         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1403                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1404                                HOSTAPD_LEVEL_DEBUG,
1405                                "MGMT: DA=" MACSTR " not our address",
1406                                MAC2STR(mgmt->da));
1407                 return;
1408         }
1409
1410         switch (stype) {
1411         case WLAN_FC_STYPE_AUTH:
1412                 wpa_printf(MSG_DEBUG, "mgmt::auth");
1413                 handle_auth(hapd, mgmt, len);
1414                 break;
1415         case WLAN_FC_STYPE_ASSOC_REQ:
1416                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1417                 handle_assoc(hapd, mgmt, len, 0);
1418                 break;
1419         case WLAN_FC_STYPE_REASSOC_REQ:
1420                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1421                 handle_assoc(hapd, mgmt, len, 1);
1422                 break;
1423         case WLAN_FC_STYPE_DISASSOC:
1424                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1425                 handle_disassoc(hapd, mgmt, len);
1426                 break;
1427         case WLAN_FC_STYPE_DEAUTH:
1428                 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1429                 handle_deauth(hapd, mgmt, len);
1430                 break;
1431         case WLAN_FC_STYPE_ACTION:
1432                 wpa_printf(MSG_DEBUG, "mgmt::action");
1433                 handle_action(hapd, mgmt, len);
1434                 break;
1435         default:
1436                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1437                                HOSTAPD_LEVEL_DEBUG,
1438                                "unknown mgmt frame subtype %d", stype);
1439                 break;
1440         }
1441 }
1442
1443
1444 static void handle_auth_cb(struct hostapd_data *hapd,
1445                            struct ieee80211_mgmt *mgmt,
1446                            size_t len, int ok)
1447 {
1448         u16 auth_alg, auth_transaction, status_code;
1449         struct sta_info *sta;
1450
1451         if (!ok) {
1452                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1453                                HOSTAPD_LEVEL_NOTICE,
1454                                "did not acknowledge authentication response");
1455                 return;
1456         }
1457
1458         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1459                 printf("handle_auth_cb - too short payload (len=%lu)\n",
1460                        (unsigned long) len);
1461                 return;
1462         }
1463
1464         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1465         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1466         status_code = le_to_host16(mgmt->u.auth.status_code);
1467
1468         sta = ap_get_sta(hapd, mgmt->da);
1469         if (!sta) {
1470                 printf("handle_auth_cb: STA " MACSTR " not found\n",
1471                        MAC2STR(mgmt->da));
1472                 return;
1473         }
1474
1475         if (status_code == WLAN_STATUS_SUCCESS &&
1476             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1477              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1478                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1479                                HOSTAPD_LEVEL_INFO, "authenticated");
1480                 sta->flags |= WLAN_STA_AUTH;
1481         }
1482 }
1483
1484
1485 static void handle_assoc_cb(struct hostapd_data *hapd,
1486                             struct ieee80211_mgmt *mgmt,
1487                             size_t len, int reassoc, int ok)
1488 {
1489         u16 status;
1490         struct sta_info *sta;
1491         int new_assoc = 1;
1492         struct ht_cap_ie *ht_cap = NULL;
1493
1494         if (!ok) {
1495                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1496                                HOSTAPD_LEVEL_DEBUG,
1497                                "did not acknowledge association response");
1498                 return;
1499         }
1500
1501         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1502                                       sizeof(mgmt->u.assoc_resp))) {
1503                 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1504                        "(len=%lu)\n", reassoc, (unsigned long) len);
1505                 return;
1506         }
1507
1508         if (reassoc)
1509                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1510         else
1511                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1512
1513         sta = ap_get_sta(hapd, mgmt->da);
1514         if (!sta) {
1515                 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1516                        MAC2STR(mgmt->da));
1517                 return;
1518         }
1519
1520         if (status != WLAN_STATUS_SUCCESS)
1521                 goto fail;
1522
1523         /* Stop previous accounting session, if one is started, and allocate
1524          * new session id for the new session. */
1525         accounting_sta_stop(hapd, sta);
1526
1527         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1528                        HOSTAPD_LEVEL_INFO,
1529                        "associated (aid %d)",
1530                        sta->aid);
1531
1532         if (sta->flags & WLAN_STA_ASSOC)
1533                 new_assoc = 0;
1534         sta->flags |= WLAN_STA_ASSOC;
1535
1536         if (reassoc)
1537                 mlme_reassociate_indication(hapd, sta);
1538         else
1539                 mlme_associate_indication(hapd, sta);
1540
1541 #ifdef CONFIG_IEEE80211N
1542         if (sta->flags & WLAN_STA_HT)
1543                 ht_cap = &sta->ht_capabilities;
1544 #endif /* CONFIG_IEEE80211N */
1545
1546 #ifdef CONFIG_IEEE80211W
1547         sta->sa_query_timed_out = 0;
1548 #endif /* CONFIG_IEEE80211W */
1549
1550         if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1551                             sta->capability, sta->supported_rates,
1552                             sta->supported_rates_len, 0, sta->listen_interval,
1553                             ht_cap))
1554         {
1555                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1556                                HOSTAPD_LEVEL_NOTICE,
1557                                "Could not add STA to kernel driver");
1558         }
1559
1560         if (sta->eapol_sm == NULL) {
1561                 /*
1562                  * This STA does not use RADIUS server for EAP authentication,
1563                  * so bind it to the selected VLAN interface now, since the
1564                  * interface selection is not going to change anymore.
1565                  */
1566                 ap_sta_bind_vlan(hapd, sta, 0);
1567         } else if (sta->vlan_id) {
1568                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1569                 ap_sta_bind_vlan(hapd, sta, 0);
1570         }
1571         if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1572                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1573                                       WLAN_STA_SHORT_PREAMBLE, ~0);
1574         } else {
1575                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1576                                       0, ~WLAN_STA_SHORT_PREAMBLE);
1577         }
1578
1579         if (sta->auth_alg == WLAN_AUTH_FT)
1580                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1581         else
1582                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1583         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1584
1585         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1586
1587  fail:
1588         /* Copy of the association request is not needed anymore */
1589         if (sta->last_assoc_req) {
1590                 os_free(sta->last_assoc_req);
1591                 sta->last_assoc_req = NULL;
1592         }
1593 }
1594
1595
1596 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1597                         u16 stype, int ok)
1598 {
1599         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1600
1601         switch (stype) {
1602         case WLAN_FC_STYPE_AUTH:
1603                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1604                 handle_auth_cb(hapd, mgmt, len, ok);
1605                 break;
1606         case WLAN_FC_STYPE_ASSOC_RESP:
1607                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1608                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1609                 break;
1610         case WLAN_FC_STYPE_REASSOC_RESP:
1611                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1612                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1613                 break;
1614         case WLAN_FC_STYPE_PROBE_RESP:
1615                 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1616                 break;
1617         case WLAN_FC_STYPE_DEAUTH:
1618                 /* ignore */
1619                 break;
1620         case WLAN_FC_STYPE_ACTION:
1621                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1622                 break;
1623         default:
1624                 printf("unknown mgmt cb frame subtype %d\n", stype);
1625                 break;
1626         }
1627 }
1628
1629
1630 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1631                                                 void *timeout_ctx)
1632 {
1633         struct hostapd_data *hapd = eloop_ctx;
1634         hapd->tkip_countermeasures = 0;
1635         hostapd_set_countermeasures(hapd, 0);
1636         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1637                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1638 }
1639
1640
1641 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1642 {
1643         struct sta_info *sta;
1644
1645         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1646                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1647
1648         wpa_auth_countermeasures_start(hapd->wpa_auth);
1649         hapd->tkip_countermeasures = 1;
1650         hostapd_set_countermeasures(hapd, 1);
1651         wpa_gtk_rekey(hapd->wpa_auth);
1652         eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
1653         eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
1654                                hapd, NULL);
1655         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
1656                 hostapd_sta_deauth(hapd, sta->addr,
1657                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
1658                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1659                                 WLAN_STA_AUTHORIZED);
1660                 hostapd_sta_remove(hapd, sta->addr);
1661         }
1662 }
1663
1664
1665 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
1666                                    int local)
1667 {
1668         time_t now;
1669
1670         if (addr && local) {
1671                 struct sta_info *sta = ap_get_sta(hapd, addr);
1672                 if (sta != NULL) {
1673                         wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
1674                         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1675                                        HOSTAPD_LEVEL_INFO,
1676                                        "Michael MIC failure detected in "
1677                                        "received frame");
1678                         mlme_michaelmicfailure_indication(hapd, addr);
1679                 } else {
1680                         wpa_printf(MSG_DEBUG,
1681                                    "MLME-MICHAELMICFAILURE.indication "
1682                                    "for not associated STA (" MACSTR
1683                                    ") ignored", MAC2STR(addr));
1684                         return;
1685                 }
1686         }
1687
1688         time(&now);
1689         if (now > hapd->michael_mic_failure + 60) {
1690                 hapd->michael_mic_failures = 1;
1691         } else {
1692                 hapd->michael_mic_failures++;
1693                 if (hapd->michael_mic_failures > 1)
1694                         ieee80211_tkip_countermeasures_start(hapd);
1695         }
1696         hapd->michael_mic_failure = now;
1697 }
1698
1699
1700 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1701 {
1702         /* TODO */
1703         return 0;
1704 }
1705
1706
1707 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1708                            char *buf, size_t buflen)
1709 {
1710         /* TODO */
1711         return 0;
1712 }
1713
1714 #endif /* CONFIG_NATIVE_WINDOWS */