Updated SA Query procedure to use timeouts per 802.11w/D7.0
[libeap.git] / 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                         /*
909                          * STA has already been associated with MFP and SA
910                          * Query timeout has not been reached. Reject the
911                          * association attempt temporarily and start SA Query,
912                          * if one is not pending.
913                          */
914
915                         if (sta->sa_query_count == 0)
916                                 ap_sta_start_sa_query(hapd, sta);
917
918                         resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
919                         goto fail;
920                 }
921
922                 if (wpa_auth_uses_mfp(sta->wpa_sm))
923                         sta->flags |= WLAN_STA_MFP;
924                 else
925                         sta->flags &= ~WLAN_STA_MFP;
926 #endif /* CONFIG_IEEE80211W */
927
928 #ifdef CONFIG_IEEE80211R
929                 if (sta->auth_alg == WLAN_AUTH_FT) {
930                         if (!reassoc) {
931                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
932                                            "to use association (not "
933                                            "re-association) with FT auth_alg",
934                                            MAC2STR(sta->addr));
935                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
936                                 goto fail;
937                         }
938
939                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
940                         if (resp != WLAN_STATUS_SUCCESS)
941                                 goto fail;
942                 }
943 #endif /* CONFIG_IEEE80211R */
944 #ifdef CONFIG_IEEE80211N
945                 if ((sta->flags & WLAN_STA_HT) &&
946                     wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
947                         wpa_printf(MSG_DEBUG, "HT: " MACSTR " tried to "
948                                    "use TKIP with HT association",
949                                    MAC2STR(sta->addr));
950                         resp = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
951                         goto fail;
952                 }
953 #endif /* CONFIG_IEEE80211N */
954         } else
955                 wpa_auth_sta_no_wpa(sta->wpa_sm);
956
957         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
958                 sta->flags |= WLAN_STA_NONERP;
959         for (i = 0; i < sta->supported_rates_len; i++) {
960                 if ((sta->supported_rates[i] & 0x7f) > 22) {
961                         sta->flags &= ~WLAN_STA_NONERP;
962                         break;
963                 }
964         }
965         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
966                 sta->nonerp_set = 1;
967                 hapd->iface->num_sta_non_erp++;
968                 if (hapd->iface->num_sta_non_erp == 1)
969                         ieee802_11_set_beacons(hapd->iface);
970         }
971
972         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
973             !sta->no_short_slot_time_set) {
974                 sta->no_short_slot_time_set = 1;
975                 hapd->iface->num_sta_no_short_slot_time++;
976                 if (hapd->iface->current_mode->mode ==
977                     HOSTAPD_MODE_IEEE80211G &&
978                     hapd->iface->num_sta_no_short_slot_time == 1)
979                         ieee802_11_set_beacons(hapd->iface);
980         }
981
982         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
983                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
984         else
985                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
986
987         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
988             !sta->no_short_preamble_set) {
989                 sta->no_short_preamble_set = 1;
990                 hapd->iface->num_sta_no_short_preamble++;
991                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
992                     && hapd->iface->num_sta_no_short_preamble == 1)
993                         ieee802_11_set_beacons(hapd->iface);
994         }
995
996 #ifdef CONFIG_IEEE80211N
997         if (sta->flags & WLAN_STA_HT) {
998                 u16 ht_capab = le_to_host16(
999                         sta->ht_capabilities.data.capabilities_info);
1000                 wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
1001                            "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
1002                 if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
1003                         if (!sta->no_ht_gf_set) {
1004                                 sta->no_ht_gf_set = 1;
1005                                 hapd->iface->num_sta_ht_no_gf++;
1006                         }
1007                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1008                                    "greenfield, num of non-gf stations %d",
1009                                    __func__, MAC2STR(sta->addr),
1010                                    hapd->iface->num_sta_ht_no_gf);
1011                 }
1012                 if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1013                         if (!sta->ht_20mhz_set) {
1014                                 sta->ht_20mhz_set = 1;
1015                                 hapd->iface->num_sta_ht_20mhz++;
1016                         }
1017                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1018                                    "num of 20MHz HT STAs %d",
1019                                    __func__, MAC2STR(sta->addr),
1020                                    hapd->iface->num_sta_ht_20mhz);
1021                 }
1022         } else {
1023                 if (!sta->no_ht_set) {
1024                         sta->no_ht_set = 1;
1025                         hapd->iface->num_sta_no_ht++;
1026                 }
1027                 if (hapd->iconf->ieee80211n) {
1028                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1029                                    " - no HT, num of non-HT stations %d",
1030                                    __func__, MAC2STR(sta->addr),
1031                                    hapd->iface->num_sta_no_ht);
1032                 }
1033         }
1034
1035         if (hostapd_ht_operation_update(hapd->iface) > 0)
1036                 ieee802_11_set_beacons(hapd->iface);
1037 #endif /* CONFIG_IEEE80211N */
1038
1039         /* get a unique AID */
1040         if (sta->aid > 0) {
1041                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
1042         } else {
1043                 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1044                         if (hapd->sta_aid[sta->aid - 1] == NULL)
1045                                 break;
1046                 if (sta->aid > MAX_AID_TABLE_SIZE) {
1047                         sta->aid = 0;
1048                         resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1049                         wpa_printf(MSG_ERROR, "  no room for more AIDs");
1050                         goto fail;
1051                 } else {
1052                         hapd->sta_aid[sta->aid - 1] = sta;
1053                         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
1054                 }
1055         }
1056
1057         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1058                        HOSTAPD_LEVEL_DEBUG,
1059                        "association OK (aid %d)", sta->aid);
1060         /* Station will be marked associated, after it acknowledges AssocResp
1061          */
1062
1063         if (reassoc) {
1064                 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1065                           ETH_ALEN);
1066         }
1067
1068         if (sta->last_assoc_req)
1069                 os_free(sta->last_assoc_req);
1070         sta->last_assoc_req = os_malloc(len);
1071         if (sta->last_assoc_req)
1072                 os_memcpy(sta->last_assoc_req, mgmt, len);
1073
1074         /* Make sure that the previously registered inactivity timer will not
1075          * remove the STA immediately. */
1076         sta->timeout_next = STA_NULLFUNC;
1077
1078  fail:
1079         os_memset(buf, 0, sizeof(buf));
1080         reply = (struct ieee80211_mgmt *) buf;
1081         reply->frame_control =
1082                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1083                              (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1084                               (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1085                                WLAN_FC_STYPE_ASSOC_RESP)));
1086         os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1087         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1088         os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1089
1090         send_len = IEEE80211_HDRLEN;
1091         if (send_deauth) {
1092                 send_len += sizeof(reply->u.deauth);
1093                 reply->u.deauth.reason_code = host_to_le16(resp);
1094         } else {
1095                 u8 *p;
1096                 send_len += sizeof(reply->u.assoc_resp);
1097                 reply->u.assoc_resp.capab_info =
1098                         host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1099                 reply->u.assoc_resp.status_code = host_to_le16(resp);
1100                 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1101                                                        | BIT(14) | BIT(15));
1102                 /* Supported rates */
1103                 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1104                 /* Extended supported rates */
1105                 p = hostapd_eid_ext_supp_rates(hapd, p);
1106                 if (sta->flags & WLAN_STA_WME)
1107                         p = hostapd_eid_wme(hapd, p);
1108
1109                 p = hostapd_eid_ht_capabilities_info(hapd, p);
1110                 p = hostapd_eid_ht_operation(hapd, p);
1111
1112 #ifdef CONFIG_IEEE80211R
1113                 if (resp == WLAN_STATUS_SUCCESS) {
1114                         /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1115                          * Transition Information, RSN */
1116                         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1117                                                         buf + sizeof(buf) - p,
1118                                                         sta->auth_alg);
1119                 }
1120 #endif /* CONFIG_IEEE80211R */
1121
1122 #ifdef CONFIG_IEEE80211W
1123                 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1124                         p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1125 #endif /* CONFIG_IEEE80211W */
1126
1127                 send_len += p - reply->u.assoc_resp.variable;
1128         }
1129
1130         if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1131                 perror("handle_assoc: send");
1132 }
1133
1134
1135 static void handle_disassoc(struct hostapd_data *hapd,
1136                             struct ieee80211_mgmt *mgmt, size_t len)
1137 {
1138         struct sta_info *sta;
1139
1140         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1141                 printf("handle_disassoc - too short payload (len=%lu)\n",
1142                        (unsigned long) len);
1143                 return;
1144         }
1145
1146         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1147                    MAC2STR(mgmt->sa),
1148                    le_to_host16(mgmt->u.disassoc.reason_code));
1149
1150         sta = ap_get_sta(hapd, mgmt->sa);
1151         if (sta == NULL) {
1152                 printf("Station " MACSTR " trying to disassociate, but it "
1153                        "is not associated.\n", MAC2STR(mgmt->sa));
1154                 return;
1155         }
1156
1157         sta->flags &= ~WLAN_STA_ASSOC;
1158         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1159         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1160                        HOSTAPD_LEVEL_INFO, "disassociated");
1161         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1162         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1163         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1164          * authenticated. */
1165         accounting_sta_stop(hapd, sta);
1166         ieee802_1x_free_station(sta);
1167         hostapd_sta_remove(hapd, sta->addr);
1168
1169         if (sta->timeout_next == STA_NULLFUNC ||
1170             sta->timeout_next == STA_DISASSOC) {
1171                 sta->timeout_next = STA_DEAUTH;
1172                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1173                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1174                                        hapd, sta);
1175         }
1176
1177         mlme_disassociate_indication(
1178                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1179 }
1180
1181
1182 static void handle_deauth(struct hostapd_data *hapd,
1183                           struct ieee80211_mgmt *mgmt, size_t len)
1184 {
1185         struct sta_info *sta;
1186
1187         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1188                 printf("handle_deauth - too short payload (len=%lu)\n",
1189                        (unsigned long) len);
1190                 return;
1191         }
1192
1193         wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1194                    " reason_code=%d",
1195                    MAC2STR(mgmt->sa),
1196                    le_to_host16(mgmt->u.deauth.reason_code));
1197
1198         sta = ap_get_sta(hapd, mgmt->sa);
1199         if (sta == NULL) {
1200                 printf("Station " MACSTR " trying to deauthenticate, but it "
1201                        "is not authenticated.\n", MAC2STR(mgmt->sa));
1202                 return;
1203         }
1204
1205         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1206         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1207         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1208                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1209         mlme_deauthenticate_indication(
1210                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1211         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1212         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1213         ap_free_sta(hapd, sta);
1214 }
1215
1216
1217 static void handle_beacon(struct hostapd_data *hapd,
1218                           struct ieee80211_mgmt *mgmt, size_t len,
1219                           struct hostapd_frame_info *fi)
1220 {
1221         struct ieee802_11_elems elems;
1222
1223         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1224                 printf("handle_beacon - too short payload (len=%lu)\n",
1225                        (unsigned long) len);
1226                 return;
1227         }
1228
1229         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1230                                       len - (IEEE80211_HDRLEN +
1231                                              sizeof(mgmt->u.beacon)), &elems,
1232                                       0);
1233
1234         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1235 }
1236
1237
1238 #ifdef CONFIG_IEEE80211W
1239 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1240                                     struct ieee80211_mgmt *mgmt, size_t len)
1241 {
1242         struct sta_info *sta;
1243         u8 *end;
1244         int i;
1245
1246         end = mgmt->u.action.u.sa_query_resp.trans_id +
1247                 WLAN_SA_QUERY_TR_ID_LEN;
1248         if (((u8 *) mgmt) + len < end) {
1249                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1250                            "frame (len=%lu)", (unsigned long) len);
1251                 return;
1252         }
1253
1254         if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1255                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1256                            "Action %d", mgmt->u.action.u.sa_query_resp.action);
1257                 return;
1258         }
1259
1260         /* MLME-SAQuery.confirm */
1261
1262         sta = ap_get_sta(hapd, mgmt->sa);
1263         if (sta == NULL || sta->sa_query_trans_id == NULL) {
1264                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1265                            "pending SA Query request found");
1266                 return;
1267         }
1268
1269         for (i = 0; i < sta->sa_query_count; i++) {
1270                 if (os_memcmp(sta->sa_query_trans_id +
1271                               i * WLAN_SA_QUERY_TR_ID_LEN,
1272                               mgmt->u.action.u.sa_query_resp.trans_id,
1273                               WLAN_SA_QUERY_TR_ID_LEN) == 0)
1274                         break;
1275         }
1276
1277         if (i >= sta->sa_query_count) {
1278                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1279                            "transaction identifier found");
1280                 return;
1281         }
1282
1283         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1284                        HOSTAPD_LEVEL_DEBUG,
1285                        "Reply to pending SA Query received");
1286         ap_sta_stop_sa_query(hapd, sta);
1287 }
1288 #endif /* CONFIG_IEEE80211W */
1289
1290
1291 static void handle_action(struct hostapd_data *hapd,
1292                           struct ieee80211_mgmt *mgmt, size_t len)
1293 {
1294         if (len < IEEE80211_HDRLEN + 1) {
1295                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1296                                HOSTAPD_LEVEL_DEBUG,
1297                                "handle_action - too short payload (len=%lu)",
1298                                (unsigned long) len);
1299                 return;
1300         }
1301
1302         switch (mgmt->u.action.category) {
1303 #ifdef CONFIG_IEEE80211R
1304         case WLAN_ACTION_FT:
1305         {
1306                 struct sta_info *sta;
1307
1308                 sta = ap_get_sta(hapd, mgmt->sa);
1309                 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1310                         wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1311                                    "frame from unassociated STA " MACSTR,
1312                                    MAC2STR(mgmt->sa));
1313                         return;
1314                 }
1315
1316                 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1317                                      len - IEEE80211_HDRLEN))
1318                         break;
1319
1320                 return;
1321         }
1322 #endif /* CONFIG_IEEE80211R */
1323         case WLAN_ACTION_WMM:
1324                 hostapd_wme_action(hapd, mgmt, len);
1325                 return;
1326 #ifdef CONFIG_IEEE80211W
1327         case WLAN_ACTION_SA_QUERY:
1328                 hostapd_sa_query_action(hapd, mgmt, len);
1329                 return;
1330 #endif /* CONFIG_IEEE80211W */
1331         }
1332
1333         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1334                        HOSTAPD_LEVEL_DEBUG,
1335                        "handle_action - unknown action category %d or invalid "
1336                        "frame",
1337                        mgmt->u.action.category);
1338         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1339             !(mgmt->sa[0] & 0x01)) {
1340                 /*
1341                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1342                  * Return the Action frame to the source without change
1343                  * except that MSB of the Category set to 1.
1344                  */
1345                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1346                            "frame back to sender");
1347                 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1348                 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1349                 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1350                 mgmt->u.action.category |= 0x80;
1351
1352                 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1353         }
1354 }
1355
1356
1357 /**
1358  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1359  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1360  * sent to)
1361  * @buf: management frame data (starting from IEEE 802.11 header)
1362  * @len: length of frame data in octets
1363  * @stype: management frame subtype from frame control field
1364  *
1365  * Process all incoming IEEE 802.11 management frames. This will be called for
1366  * each frame received from the kernel driver through wlan#ap interface. In
1367  * addition, it can be called to re-inserted pending frames (e.g., when using
1368  * external RADIUS server as an MAC ACL).
1369  */
1370 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1371                      struct hostapd_frame_info *fi)
1372 {
1373         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1374         int broadcast;
1375
1376         if (stype == WLAN_FC_STYPE_BEACON) {
1377                 handle_beacon(hapd, mgmt, len, fi);
1378                 return;
1379         }
1380
1381         if (fi && fi->passive_scan)
1382                 return;
1383
1384         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1385                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1386                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1387
1388         if (!broadcast &&
1389             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1390                 printf("MGMT: BSSID=" MACSTR " not our address\n",
1391                        MAC2STR(mgmt->bssid));
1392                 return;
1393         }
1394
1395
1396         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1397                 handle_probe_req(hapd, mgmt, len);
1398                 return;
1399         }
1400
1401         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1402                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1403                                HOSTAPD_LEVEL_DEBUG,
1404                                "MGMT: DA=" MACSTR " not our address",
1405                                MAC2STR(mgmt->da));
1406                 return;
1407         }
1408
1409         switch (stype) {
1410         case WLAN_FC_STYPE_AUTH:
1411                 wpa_printf(MSG_DEBUG, "mgmt::auth");
1412                 handle_auth(hapd, mgmt, len);
1413                 break;
1414         case WLAN_FC_STYPE_ASSOC_REQ:
1415                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1416                 handle_assoc(hapd, mgmt, len, 0);
1417                 break;
1418         case WLAN_FC_STYPE_REASSOC_REQ:
1419                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1420                 handle_assoc(hapd, mgmt, len, 1);
1421                 break;
1422         case WLAN_FC_STYPE_DISASSOC:
1423                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1424                 handle_disassoc(hapd, mgmt, len);
1425                 break;
1426         case WLAN_FC_STYPE_DEAUTH:
1427                 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1428                 handle_deauth(hapd, mgmt, len);
1429                 break;
1430         case WLAN_FC_STYPE_ACTION:
1431                 wpa_printf(MSG_DEBUG, "mgmt::action");
1432                 handle_action(hapd, mgmt, len);
1433                 break;
1434         default:
1435                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1436                                HOSTAPD_LEVEL_DEBUG,
1437                                "unknown mgmt frame subtype %d", stype);
1438                 break;
1439         }
1440 }
1441
1442
1443 static void handle_auth_cb(struct hostapd_data *hapd,
1444                            struct ieee80211_mgmt *mgmt,
1445                            size_t len, int ok)
1446 {
1447         u16 auth_alg, auth_transaction, status_code;
1448         struct sta_info *sta;
1449
1450         if (!ok) {
1451                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1452                                HOSTAPD_LEVEL_NOTICE,
1453                                "did not acknowledge authentication response");
1454                 return;
1455         }
1456
1457         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1458                 printf("handle_auth_cb - too short payload (len=%lu)\n",
1459                        (unsigned long) len);
1460                 return;
1461         }
1462
1463         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1464         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1465         status_code = le_to_host16(mgmt->u.auth.status_code);
1466
1467         sta = ap_get_sta(hapd, mgmt->da);
1468         if (!sta) {
1469                 printf("handle_auth_cb: STA " MACSTR " not found\n",
1470                        MAC2STR(mgmt->da));
1471                 return;
1472         }
1473
1474         if (status_code == WLAN_STATUS_SUCCESS &&
1475             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1476              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1477                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1478                                HOSTAPD_LEVEL_INFO, "authenticated");
1479                 sta->flags |= WLAN_STA_AUTH;
1480         }
1481 }
1482
1483
1484 static void handle_assoc_cb(struct hostapd_data *hapd,
1485                             struct ieee80211_mgmt *mgmt,
1486                             size_t len, int reassoc, int ok)
1487 {
1488         u16 status;
1489         struct sta_info *sta;
1490         int new_assoc = 1;
1491         struct ht_cap_ie *ht_cap = NULL;
1492
1493         if (!ok) {
1494                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1495                                HOSTAPD_LEVEL_DEBUG,
1496                                "did not acknowledge association response");
1497                 return;
1498         }
1499
1500         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1501                                       sizeof(mgmt->u.assoc_resp))) {
1502                 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1503                        "(len=%lu)\n", reassoc, (unsigned long) len);
1504                 return;
1505         }
1506
1507         if (reassoc)
1508                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1509         else
1510                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1511
1512         sta = ap_get_sta(hapd, mgmt->da);
1513         if (!sta) {
1514                 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1515                        MAC2STR(mgmt->da));
1516                 return;
1517         }
1518
1519         if (status != WLAN_STATUS_SUCCESS)
1520                 goto fail;
1521
1522         /* Stop previous accounting session, if one is started, and allocate
1523          * new session id for the new session. */
1524         accounting_sta_stop(hapd, sta);
1525
1526         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1527                        HOSTAPD_LEVEL_INFO,
1528                        "associated (aid %d)",
1529                        sta->aid);
1530
1531         if (sta->flags & WLAN_STA_ASSOC)
1532                 new_assoc = 0;
1533         sta->flags |= WLAN_STA_ASSOC;
1534
1535         if (reassoc)
1536                 mlme_reassociate_indication(hapd, sta);
1537         else
1538                 mlme_associate_indication(hapd, sta);
1539
1540 #ifdef CONFIG_IEEE80211N
1541         if (sta->flags & WLAN_STA_HT)
1542                 ht_cap = &sta->ht_capabilities;
1543 #endif /* CONFIG_IEEE80211N */
1544
1545 #ifdef CONFIG_IEEE80211W
1546         sta->sa_query_timed_out = 0;
1547 #endif /* CONFIG_IEEE80211W */
1548
1549         if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1550                             sta->capability, sta->supported_rates,
1551                             sta->supported_rates_len, 0, sta->listen_interval,
1552                             ht_cap))
1553         {
1554                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1555                                HOSTAPD_LEVEL_NOTICE,
1556                                "Could not add STA to kernel driver");
1557         }
1558
1559         if (sta->eapol_sm == NULL) {
1560                 /*
1561                  * This STA does not use RADIUS server for EAP authentication,
1562                  * so bind it to the selected VLAN interface now, since the
1563                  * interface selection is not going to change anymore.
1564                  */
1565                 ap_sta_bind_vlan(hapd, sta, 0);
1566         } else if (sta->vlan_id) {
1567                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1568                 ap_sta_bind_vlan(hapd, sta, 0);
1569         }
1570         if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1571                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1572                                       WLAN_STA_SHORT_PREAMBLE, ~0);
1573         } else {
1574                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1575                                       0, ~WLAN_STA_SHORT_PREAMBLE);
1576         }
1577
1578         if (sta->auth_alg == WLAN_AUTH_FT)
1579                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1580         else
1581                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1582         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1583
1584         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1585
1586  fail:
1587         /* Copy of the association request is not needed anymore */
1588         if (sta->last_assoc_req) {
1589                 os_free(sta->last_assoc_req);
1590                 sta->last_assoc_req = NULL;
1591         }
1592 }
1593
1594
1595 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1596                         u16 stype, int ok)
1597 {
1598         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1599
1600         switch (stype) {
1601         case WLAN_FC_STYPE_AUTH:
1602                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1603                 handle_auth_cb(hapd, mgmt, len, ok);
1604                 break;
1605         case WLAN_FC_STYPE_ASSOC_RESP:
1606                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1607                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1608                 break;
1609         case WLAN_FC_STYPE_REASSOC_RESP:
1610                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1611                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1612                 break;
1613         case WLAN_FC_STYPE_PROBE_RESP:
1614                 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1615                 break;
1616         case WLAN_FC_STYPE_DEAUTH:
1617                 /* ignore */
1618                 break;
1619         case WLAN_FC_STYPE_ACTION:
1620                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1621                 break;
1622         default:
1623                 printf("unknown mgmt cb frame subtype %d\n", stype);
1624                 break;
1625         }
1626 }
1627
1628
1629 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1630                                                 void *timeout_ctx)
1631 {
1632         struct hostapd_data *hapd = eloop_ctx;
1633         hapd->tkip_countermeasures = 0;
1634         hostapd_set_countermeasures(hapd, 0);
1635         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1636                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1637 }
1638
1639
1640 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1641 {
1642         struct sta_info *sta;
1643
1644         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1645                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1646
1647         wpa_auth_countermeasures_start(hapd->wpa_auth);
1648         hapd->tkip_countermeasures = 1;
1649         hostapd_set_countermeasures(hapd, 1);
1650         wpa_gtk_rekey(hapd->wpa_auth);
1651         eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
1652         eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
1653                                hapd, NULL);
1654         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
1655                 hostapd_sta_deauth(hapd, sta->addr,
1656                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
1657                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1658                                 WLAN_STA_AUTHORIZED);
1659                 hostapd_sta_remove(hapd, sta->addr);
1660         }
1661 }
1662
1663
1664 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
1665                                    int local)
1666 {
1667         time_t now;
1668
1669         if (addr && local) {
1670                 struct sta_info *sta = ap_get_sta(hapd, addr);
1671                 if (sta != NULL) {
1672                         wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
1673                         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1674                                        HOSTAPD_LEVEL_INFO,
1675                                        "Michael MIC failure detected in "
1676                                        "received frame");
1677                         mlme_michaelmicfailure_indication(hapd, addr);
1678                 } else {
1679                         wpa_printf(MSG_DEBUG,
1680                                    "MLME-MICHAELMICFAILURE.indication "
1681                                    "for not associated STA (" MACSTR
1682                                    ") ignored", MAC2STR(addr));
1683                         return;
1684                 }
1685         }
1686
1687         time(&now);
1688         if (now > hapd->michael_mic_failure + 60) {
1689                 hapd->michael_mic_failures = 1;
1690         } else {
1691                 hapd->michael_mic_failures++;
1692                 if (hapd->michael_mic_failures > 1)
1693                         ieee80211_tkip_countermeasures_start(hapd);
1694         }
1695         hapd->michael_mic_failure = now;
1696 }
1697
1698
1699 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1700 {
1701         /* TODO */
1702         return 0;
1703 }
1704
1705
1706 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1707                            char *buf, size_t buflen)
1708 {
1709         /* TODO */
1710         return 0;
1711 }
1712
1713 #endif /* CONFIG_NATIVE_WINDOWS */