Fixed TKIP countermeasueres for drivers that do not use hostapd MLME
[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_i.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 /**
339  * ieee802_11_send_deauth - Send Deauthentication frame
340  * @hapd: hostapd BSS data
341  * @addr: Address of the destination STA
342  * @reason: Reason code for Deauthentication
343  */
344 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
345 {
346         struct ieee80211_mgmt mgmt;
347
348         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
349                        HOSTAPD_LEVEL_DEBUG,
350                        "deauthenticate - reason %d", reason);
351         os_memset(&mgmt, 0, sizeof(mgmt));
352         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
353                                           WLAN_FC_STYPE_DEAUTH);
354         os_memcpy(mgmt.da, addr, ETH_ALEN);
355         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
356         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
357         mgmt.u.deauth.reason_code = host_to_le16(reason);
358         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
359                                     sizeof(mgmt.u.deauth), 0) < 0)
360                 perror("ieee802_11_send_deauth: send");
361 }
362
363
364 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
365                            u16 auth_transaction, u8 *challenge, int iswep)
366 {
367         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
368                        HOSTAPD_LEVEL_DEBUG,
369                        "authentication (shared key, transaction %d)",
370                        auth_transaction);
371
372         if (auth_transaction == 1) {
373                 if (!sta->challenge) {
374                         /* Generate a pseudo-random challenge */
375                         u8 key[8];
376                         time_t now;
377                         int r;
378                         sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
379                         if (sta->challenge == NULL)
380                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
381
382                         now = time(NULL);
383                         r = random();
384                         os_memcpy(key, &now, 4);
385                         os_memcpy(key + 4, &r, 4);
386                         rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
387                             key, sizeof(key));
388                 }
389                 return 0;
390         }
391
392         if (auth_transaction != 3)
393                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
394
395         /* Transaction 3 */
396         if (!iswep || !sta->challenge || !challenge ||
397             os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
398                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
399                                HOSTAPD_LEVEL_INFO,
400                                "shared key authentication - invalid "
401                                "challenge-response");
402                 return WLAN_STATUS_CHALLENGE_FAIL;
403         }
404
405         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
406                        HOSTAPD_LEVEL_DEBUG,
407                        "authentication OK (shared key)");
408 #ifdef IEEE80211_REQUIRE_AUTH_ACK
409         /* Station will be marked authenticated if it ACKs the
410          * authentication reply. */
411 #else
412         sta->flags |= WLAN_STA_AUTH;
413         wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
414 #endif
415         os_free(sta->challenge);
416         sta->challenge = NULL;
417
418         return 0;
419 }
420
421
422 static void send_auth_reply(struct hostapd_data *hapd,
423                             const u8 *dst, const u8 *bssid,
424                             u16 auth_alg, u16 auth_transaction, u16 resp,
425                             const u8 *ies, size_t ies_len)
426 {
427         struct ieee80211_mgmt *reply;
428         u8 *buf;
429         size_t rlen;
430
431         rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
432         buf = os_zalloc(rlen);
433         if (buf == NULL)
434                 return;
435
436         reply = (struct ieee80211_mgmt *) buf;
437         reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
438                                             WLAN_FC_STYPE_AUTH);
439         os_memcpy(reply->da, dst, ETH_ALEN);
440         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
441         os_memcpy(reply->bssid, bssid, ETH_ALEN);
442
443         reply->u.auth.auth_alg = host_to_le16(auth_alg);
444         reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
445         reply->u.auth.status_code = host_to_le16(resp);
446
447         if (ies && ies_len)
448                 os_memcpy(reply->u.auth.variable, ies, ies_len);
449
450         wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
451                    " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
452                    MAC2STR(dst), auth_alg, auth_transaction,
453                    resp, (unsigned long) ies_len);
454         if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
455                 perror("send_auth_reply: send");
456
457         os_free(buf);
458 }
459
460
461 #ifdef CONFIG_IEEE80211R
462 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
463                                   u16 auth_transaction, u16 status,
464                                   const u8 *ies, size_t ies_len)
465 {
466         struct hostapd_data *hapd = ctx;
467         struct sta_info *sta;
468
469         send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
470                         status, ies, ies_len);
471
472         if (status != WLAN_STATUS_SUCCESS)
473                 return;
474
475         sta = ap_get_sta(hapd, dst);
476         if (sta == NULL)
477                 return;
478
479         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
480                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
481         sta->flags |= WLAN_STA_AUTH;
482         mlme_authenticate_indication(hapd, sta);
483 }
484 #endif /* CONFIG_IEEE80211R */
485
486
487 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
488                         size_t len)
489 {
490         u16 auth_alg, auth_transaction, status_code;
491         u16 resp = WLAN_STATUS_SUCCESS;
492         struct sta_info *sta = NULL;
493         int res;
494         u16 fc;
495         u8 *challenge = NULL;
496         u32 session_timeout, acct_interim_interval;
497         int vlan_id = 0;
498         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
499         size_t resp_ies_len = 0;
500
501         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
502                 printf("handle_auth - too short payload (len=%lu)\n",
503                        (unsigned long) len);
504                 return;
505         }
506
507         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
508         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
509         status_code = le_to_host16(mgmt->u.auth.status_code);
510         fc = le_to_host16(mgmt->frame_control);
511
512         if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
513             2 + WLAN_AUTH_CHALLENGE_LEN &&
514             mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
515             mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
516                 challenge = &mgmt->u.auth.variable[2];
517
518         wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
519                    "auth_transaction=%d status_code=%d wep=%d%s",
520                    MAC2STR(mgmt->sa), auth_alg, auth_transaction,
521                    status_code, !!(fc & WLAN_FC_ISWEP),
522                    challenge ? " challenge" : "");
523
524         if (hapd->tkip_countermeasures) {
525                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
526                 goto fail;
527         }
528
529         if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
530                auth_alg == WLAN_AUTH_OPEN) ||
531 #ifdef CONFIG_IEEE80211R
532               (hapd->conf->wpa &&
533                (hapd->conf->wpa_key_mgmt &
534                 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
535                auth_alg == WLAN_AUTH_FT) ||
536 #endif /* CONFIG_IEEE80211R */
537               ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
538                auth_alg == WLAN_AUTH_SHARED_KEY))) {
539                 printf("Unsupported authentication algorithm (%d)\n",
540                        auth_alg);
541                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
542                 goto fail;
543         }
544
545         if (!(auth_transaction == 1 ||
546               (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
547                 printf("Unknown authentication transaction number (%d)\n",
548                        auth_transaction);
549                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
550                 goto fail;
551         }
552
553         if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
554                 printf("Station " MACSTR " not allowed to authenticate.\n",
555                        MAC2STR(mgmt->sa));
556                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
557                 goto fail;
558         }
559
560         res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
561                                       &session_timeout,
562                                       &acct_interim_interval, &vlan_id);
563         if (res == HOSTAPD_ACL_REJECT) {
564                 printf("Station " MACSTR " not allowed to authenticate.\n",
565                        MAC2STR(mgmt->sa));
566                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
567                 goto fail;
568         }
569         if (res == HOSTAPD_ACL_PENDING) {
570                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
571                            " waiting for an external authentication",
572                            MAC2STR(mgmt->sa));
573                 /* Authentication code will re-send the authentication frame
574                  * after it has received (and cached) information from the
575                  * external source. */
576                 return;
577         }
578
579         sta = ap_sta_add(hapd, mgmt->sa);
580         if (!sta) {
581                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
582                 goto fail;
583         }
584
585         if (vlan_id > 0) {
586                 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
587                                                sta->vlan_id) == NULL) {
588                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
589                                        HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
590                                        "%d received from RADIUS server",
591                                        vlan_id);
592                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
593                         goto fail;
594                 }
595                 sta->vlan_id = vlan_id;
596                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
597                                HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
598         }
599
600         sta->flags &= ~WLAN_STA_PREAUTH;
601         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
602
603         if (hapd->conf->radius->acct_interim_interval == 0 &&
604             acct_interim_interval)
605                 sta->acct_interim_interval = acct_interim_interval;
606         if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
607                 ap_sta_session_timeout(hapd, sta, session_timeout);
608         else
609                 ap_sta_no_session_timeout(hapd, sta);
610
611         switch (auth_alg) {
612         case WLAN_AUTH_OPEN:
613                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
614                                HOSTAPD_LEVEL_DEBUG,
615                                "authentication OK (open system)");
616 #ifdef IEEE80211_REQUIRE_AUTH_ACK
617                 /* Station will be marked authenticated if it ACKs the
618                  * authentication reply. */
619 #else
620                 sta->flags |= WLAN_STA_AUTH;
621                 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
622                 sta->auth_alg = WLAN_AUTH_OPEN;
623                 mlme_authenticate_indication(hapd, sta);
624 #endif
625                 break;
626         case WLAN_AUTH_SHARED_KEY:
627                 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
628                                        fc & WLAN_FC_ISWEP);
629                 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
630                 mlme_authenticate_indication(hapd, sta);
631                 if (sta->challenge && auth_transaction == 1) {
632                         resp_ies[0] = WLAN_EID_CHALLENGE;
633                         resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
634                         os_memcpy(resp_ies + 2, sta->challenge,
635                                   WLAN_AUTH_CHALLENGE_LEN);
636                         resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
637                 }
638                 break;
639 #ifdef CONFIG_IEEE80211R
640         case WLAN_AUTH_FT:
641                 sta->auth_alg = WLAN_AUTH_FT;
642                 if (sta->wpa_sm == NULL)
643                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
644                                                         sta->addr);
645                 if (sta->wpa_sm == NULL) {
646                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
647                                    "state machine");
648                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
649                         goto fail;
650                 }
651                 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
652                                     auth_transaction, mgmt->u.auth.variable,
653                                     len - IEEE80211_HDRLEN -
654                                     sizeof(mgmt->u.auth),
655                                     handle_auth_ft_finish, hapd);
656                 /* handle_auth_ft_finish() callback will complete auth. */
657                 return;
658 #endif /* CONFIG_IEEE80211R */
659         }
660
661  fail:
662         send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
663                         auth_transaction + 1, resp, resp_ies, resp_ies_len);
664 }
665
666
667 static void handle_assoc(struct hostapd_data *hapd,
668                          struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
669 {
670         u16 capab_info, listen_interval;
671         u16 resp = WLAN_STATUS_SUCCESS;
672         u8 *pos, *wpa_ie;
673         size_t wpa_ie_len;
674         int send_deauth = 0, send_len, left, i;
675         struct sta_info *sta;
676         struct ieee802_11_elems elems;
677         u8 buf[sizeof(struct ieee80211_mgmt) + 512];
678         struct ieee80211_mgmt *reply;
679
680         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
681                                       sizeof(mgmt->u.assoc_req))) {
682                 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
683                        "\n", reassoc, (unsigned long) len);
684                 return;
685         }
686
687         if (reassoc) {
688                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
689                 listen_interval = le_to_host16(
690                         mgmt->u.reassoc_req.listen_interval);
691                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
692                            " capab_info=0x%02x listen_interval=%d current_ap="
693                            MACSTR,
694                            MAC2STR(mgmt->sa), capab_info, listen_interval,
695                            MAC2STR(mgmt->u.reassoc_req.current_ap));
696                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
697                 pos = mgmt->u.reassoc_req.variable;
698         } else {
699                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
700                 listen_interval = le_to_host16(
701                         mgmt->u.assoc_req.listen_interval);
702                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
703                            " capab_info=0x%02x listen_interval=%d",
704                            MAC2STR(mgmt->sa), capab_info, listen_interval);
705                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
706                 pos = mgmt->u.assoc_req.variable;
707         }
708
709         sta = ap_get_sta(hapd, mgmt->sa);
710 #ifdef CONFIG_IEEE80211R
711         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
712             (sta->flags & WLAN_STA_AUTH) == 0) {
713                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
714                            "prior to authentication since it is using "
715                            "over-the-DS FT", MAC2STR(mgmt->sa));
716         } else
717 #endif /* CONFIG_IEEE80211R */
718         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
719                 printf("STA " MACSTR " trying to associate before "
720                        "authentication\n", MAC2STR(mgmt->sa));
721                 if (sta) {
722                         printf("  sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
723                                MAC2STR(sta->addr), sta->aid, sta->flags);
724                 }
725                 send_deauth = 1;
726                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
727                 goto fail;
728         }
729
730         if (hapd->tkip_countermeasures) {
731                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
732                 goto fail;
733         }
734
735         if (listen_interval > hapd->conf->max_listen_interval) {
736                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
737                                HOSTAPD_LEVEL_DEBUG,
738                                "Too large Listen Interval (%d)",
739                                listen_interval);
740                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
741                 goto fail;
742         }
743
744         sta->capability = capab_info;
745         sta->listen_interval = listen_interval;
746
747         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
748          * is used */
749         if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
750             !elems.ssid) {
751                 printf("STA " MACSTR " sent invalid association request\n",
752                        MAC2STR(sta->addr));
753                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
754                 goto fail;
755         }
756
757         if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
758             os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
759         {
760                 char ssid_txt[33];
761                 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
762                 printf("Station " MACSTR " tried to associate with "
763                        "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
764                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
765                 goto fail;
766         }
767
768         sta->flags &= ~WLAN_STA_WME;
769         if (elems.wme && hapd->conf->wme_enabled) {
770                 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
771                         hostapd_logger(hapd, sta->addr,
772                                        HOSTAPD_MODULE_WPA,
773                                        HOSTAPD_LEVEL_DEBUG,
774                                        "invalid WME element in association "
775                                        "request");
776                 else
777                         sta->flags |= WLAN_STA_WME;
778         }
779
780         if (!elems.supp_rates) {
781                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
782                                HOSTAPD_LEVEL_DEBUG,
783                                "No supported rates element in AssocReq");
784                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
785                 goto fail;
786         }
787
788         if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
789                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
790                                HOSTAPD_LEVEL_DEBUG,
791                                "Invalid supported rates element length %d",
792                                elems.supp_rates_len);
793                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
794                 goto fail;
795         }
796
797         os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
798         os_memcpy(sta->supported_rates, elems.supp_rates,
799                   elems.supp_rates_len);
800         sta->supported_rates_len = elems.supp_rates_len;
801
802         if (elems.ext_supp_rates) {
803                 if (elems.supp_rates_len + elems.ext_supp_rates_len >
804                     sizeof(sta->supported_rates)) {
805                         hostapd_logger(hapd, mgmt->sa,
806                                        HOSTAPD_MODULE_IEEE80211,
807                                        HOSTAPD_LEVEL_DEBUG,
808                                        "Invalid supported rates element length"
809                                        " %d+%d", elems.supp_rates_len,
810                                        elems.ext_supp_rates_len);
811                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
812                         goto fail;
813                 }
814
815                 os_memcpy(sta->supported_rates + elems.supp_rates_len,
816                           elems.ext_supp_rates, elems.ext_supp_rates_len);
817                 sta->supported_rates_len += elems.ext_supp_rates_len;
818         }
819
820 #ifdef CONFIG_IEEE80211N
821         /* save HT capabilities in the sta object */
822         os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
823         if (elems.ht_capabilities &&
824             elems.ht_capabilities_len >=
825             sizeof(struct ieee80211_ht_capability)) {
826                 sta->flags |= WLAN_STA_HT;
827                 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
828                 sta->ht_capabilities.length =
829                         sizeof(struct ieee80211_ht_capability);
830                 os_memcpy(&sta->ht_capabilities.data,
831                           elems.ht_capabilities,
832                           sizeof(struct ieee80211_ht_capability));
833         } else
834                 sta->flags &= ~WLAN_STA_HT;
835 #endif /* CONFIG_IEEE80211N */
836
837         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
838                 wpa_ie = elems.rsn_ie;
839                 wpa_ie_len = elems.rsn_ie_len;
840         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
841                    elems.wpa_ie) {
842                 wpa_ie = elems.wpa_ie;
843                 wpa_ie_len = elems.wpa_ie_len;
844         } else {
845                 wpa_ie = NULL;
846                 wpa_ie_len = 0;
847         }
848 #ifdef CONFIG_WPS
849         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
850         if (hapd->conf->wps_state && wpa_ie == NULL) {
851                 if (elems.wps_ie) {
852                         wpa_printf(MSG_DEBUG, "STA included WPS IE in "
853                                    "(Re)Association Request - assume WPS is "
854                                    "used");
855                         sta->flags |= WLAN_STA_WPS;
856                         wpabuf_free(sta->wps_ie);
857                         sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
858                                                         elems.wps_ie_len - 4);
859                 } else {
860                         wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
861                                    "in (Re)Association Request - possible WPS "
862                                    "use");
863                         sta->flags |= WLAN_STA_MAYBE_WPS;
864                 }
865         } else
866 #endif /* CONFIG_WPS */
867         if (hapd->conf->wpa && wpa_ie == NULL) {
868                 printf("STA " MACSTR ": No WPA/RSN IE in association "
869                        "request\n", MAC2STR(sta->addr));
870                 resp = WLAN_STATUS_INVALID_IE;
871                 goto fail;
872         }
873
874         if (hapd->conf->wpa && wpa_ie) {
875                 int res;
876                 wpa_ie -= 2;
877                 wpa_ie_len += 2;
878                 if (sta->wpa_sm == NULL)
879                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
880                                                         sta->addr);
881                 if (sta->wpa_sm == NULL) {
882                         printf("Failed to initialize WPA state machine\n");
883                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
884                         goto fail;
885                 }
886                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
887                                           wpa_ie, wpa_ie_len,
888                                           elems.mdie, elems.mdie_len);
889                 if (res == WPA_INVALID_GROUP)
890                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
891                 else if (res == WPA_INVALID_PAIRWISE)
892                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
893                 else if (res == WPA_INVALID_AKMP)
894                         resp = WLAN_STATUS_AKMP_NOT_VALID;
895                 else if (res == WPA_ALLOC_FAIL)
896                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
897 #ifdef CONFIG_IEEE80211W
898                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
899                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
900                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
901                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
902 #endif /* CONFIG_IEEE80211W */
903                 else if (res == WPA_INVALID_MDIE)
904                         resp = WLAN_STATUS_INVALID_MDIE;
905                 else if (res != WPA_IE_OK)
906                         resp = WLAN_STATUS_INVALID_IE;
907                 if (resp != WLAN_STATUS_SUCCESS)
908                         goto fail;
909 #ifdef CONFIG_IEEE80211W
910                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
911                     sta->sa_query_count > 0)
912                         ap_check_sa_query_timeout(hapd, sta);
913                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
914                     (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
915                         /*
916                          * STA has already been associated with MFP and SA
917                          * Query timeout has not been reached. Reject the
918                          * association attempt temporarily and start SA Query,
919                          * if one is not pending.
920                          */
921
922                         if (sta->sa_query_count == 0)
923                                 ap_sta_start_sa_query(hapd, sta);
924
925                         resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
926                         goto fail;
927                 }
928
929                 if (wpa_auth_uses_mfp(sta->wpa_sm))
930                         sta->flags |= WLAN_STA_MFP;
931                 else
932                         sta->flags &= ~WLAN_STA_MFP;
933 #endif /* CONFIG_IEEE80211W */
934
935 #ifdef CONFIG_IEEE80211R
936                 if (sta->auth_alg == WLAN_AUTH_FT) {
937                         if (!reassoc) {
938                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
939                                            "to use association (not "
940                                            "re-association) with FT auth_alg",
941                                            MAC2STR(sta->addr));
942                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
943                                 goto fail;
944                         }
945
946                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
947                         if (resp != WLAN_STATUS_SUCCESS)
948                                 goto fail;
949                 }
950 #endif /* CONFIG_IEEE80211R */
951 #ifdef CONFIG_IEEE80211N
952                 if ((sta->flags & WLAN_STA_HT) &&
953                     wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
954                         wpa_printf(MSG_DEBUG, "HT: " MACSTR " tried to "
955                                    "use TKIP with HT association",
956                                    MAC2STR(sta->addr));
957                         resp = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
958                         goto fail;
959                 }
960 #endif /* CONFIG_IEEE80211N */
961         } else
962                 wpa_auth_sta_no_wpa(sta->wpa_sm);
963
964         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
965                 sta->flags |= WLAN_STA_NONERP;
966         for (i = 0; i < sta->supported_rates_len; i++) {
967                 if ((sta->supported_rates[i] & 0x7f) > 22) {
968                         sta->flags &= ~WLAN_STA_NONERP;
969                         break;
970                 }
971         }
972         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
973                 sta->nonerp_set = 1;
974                 hapd->iface->num_sta_non_erp++;
975                 if (hapd->iface->num_sta_non_erp == 1)
976                         ieee802_11_set_beacons(hapd->iface);
977         }
978
979         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
980             !sta->no_short_slot_time_set) {
981                 sta->no_short_slot_time_set = 1;
982                 hapd->iface->num_sta_no_short_slot_time++;
983                 if (hapd->iface->current_mode->mode ==
984                     HOSTAPD_MODE_IEEE80211G &&
985                     hapd->iface->num_sta_no_short_slot_time == 1)
986                         ieee802_11_set_beacons(hapd->iface);
987         }
988
989         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
990                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
991         else
992                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
993
994         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
995             !sta->no_short_preamble_set) {
996                 sta->no_short_preamble_set = 1;
997                 hapd->iface->num_sta_no_short_preamble++;
998                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
999                     && hapd->iface->num_sta_no_short_preamble == 1)
1000                         ieee802_11_set_beacons(hapd->iface);
1001         }
1002
1003 #ifdef CONFIG_IEEE80211N
1004         if (sta->flags & WLAN_STA_HT) {
1005                 u16 ht_capab = le_to_host16(
1006                         sta->ht_capabilities.data.capabilities_info);
1007                 wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
1008                            "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
1009                 if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
1010                         if (!sta->no_ht_gf_set) {
1011                                 sta->no_ht_gf_set = 1;
1012                                 hapd->iface->num_sta_ht_no_gf++;
1013                         }
1014                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1015                                    "greenfield, num of non-gf stations %d",
1016                                    __func__, MAC2STR(sta->addr),
1017                                    hapd->iface->num_sta_ht_no_gf);
1018                 }
1019                 if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1020                         if (!sta->ht_20mhz_set) {
1021                                 sta->ht_20mhz_set = 1;
1022                                 hapd->iface->num_sta_ht_20mhz++;
1023                         }
1024                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1025                                    "num of 20MHz HT STAs %d",
1026                                    __func__, MAC2STR(sta->addr),
1027                                    hapd->iface->num_sta_ht_20mhz);
1028                 }
1029         } else {
1030                 if (!sta->no_ht_set) {
1031                         sta->no_ht_set = 1;
1032                         hapd->iface->num_sta_no_ht++;
1033                 }
1034                 if (hapd->iconf->ieee80211n) {
1035                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1036                                    " - no HT, num of non-HT stations %d",
1037                                    __func__, MAC2STR(sta->addr),
1038                                    hapd->iface->num_sta_no_ht);
1039                 }
1040         }
1041
1042         if (hostapd_ht_operation_update(hapd->iface) > 0)
1043                 ieee802_11_set_beacons(hapd->iface);
1044 #endif /* CONFIG_IEEE80211N */
1045
1046         /* get a unique AID */
1047         if (sta->aid > 0) {
1048                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
1049         } else {
1050                 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1051                         if (hapd->sta_aid[sta->aid - 1] == NULL)
1052                                 break;
1053                 if (sta->aid > MAX_AID_TABLE_SIZE) {
1054                         sta->aid = 0;
1055                         resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1056                         wpa_printf(MSG_ERROR, "  no room for more AIDs");
1057                         goto fail;
1058                 } else {
1059                         hapd->sta_aid[sta->aid - 1] = sta;
1060                         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
1061                 }
1062         }
1063
1064         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1065                        HOSTAPD_LEVEL_DEBUG,
1066                        "association OK (aid %d)", sta->aid);
1067         /* Station will be marked associated, after it acknowledges AssocResp
1068          */
1069
1070 #ifdef CONFIG_IEEE80211W
1071         if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1072                 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1073                            "SA Query procedure", reassoc ? "re" : "");
1074                 /* TODO: Send a protected Disassociate frame to the STA using
1075                  * the old key and Reason Code "Previous Authentication no
1076                  * longer valid". Make sure this is only sent protected since
1077                  * unprotected frame would be received by the STA that is now
1078                  * trying to associate.
1079                  */
1080         }
1081 #endif /* CONFIG_IEEE80211W */
1082
1083         if (reassoc) {
1084                 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1085                           ETH_ALEN);
1086         }
1087
1088         if (sta->last_assoc_req)
1089                 os_free(sta->last_assoc_req);
1090         sta->last_assoc_req = os_malloc(len);
1091         if (sta->last_assoc_req)
1092                 os_memcpy(sta->last_assoc_req, mgmt, len);
1093
1094         /* Make sure that the previously registered inactivity timer will not
1095          * remove the STA immediately. */
1096         sta->timeout_next = STA_NULLFUNC;
1097
1098  fail:
1099         os_memset(buf, 0, sizeof(buf));
1100         reply = (struct ieee80211_mgmt *) buf;
1101         reply->frame_control =
1102                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1103                              (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1104                               (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1105                                WLAN_FC_STYPE_ASSOC_RESP)));
1106         os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1107         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1108         os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1109
1110         send_len = IEEE80211_HDRLEN;
1111         if (send_deauth) {
1112                 send_len += sizeof(reply->u.deauth);
1113                 reply->u.deauth.reason_code = host_to_le16(resp);
1114         } else {
1115                 u8 *p;
1116                 send_len += sizeof(reply->u.assoc_resp);
1117                 reply->u.assoc_resp.capab_info =
1118                         host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1119                 reply->u.assoc_resp.status_code = host_to_le16(resp);
1120                 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1121                                                        | BIT(14) | BIT(15));
1122                 /* Supported rates */
1123                 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1124                 /* Extended supported rates */
1125                 p = hostapd_eid_ext_supp_rates(hapd, p);
1126                 if (sta->flags & WLAN_STA_WME)
1127                         p = hostapd_eid_wme(hapd, p);
1128
1129                 p = hostapd_eid_ht_capabilities_info(hapd, p);
1130                 p = hostapd_eid_ht_operation(hapd, p);
1131
1132 #ifdef CONFIG_IEEE80211R
1133                 if (resp == WLAN_STATUS_SUCCESS) {
1134                         /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1135                          * Transition Information, RSN */
1136                         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1137                                                         buf + sizeof(buf) - p,
1138                                                         sta->auth_alg);
1139                 }
1140 #endif /* CONFIG_IEEE80211R */
1141
1142 #ifdef CONFIG_IEEE80211W
1143                 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1144                         p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1145 #endif /* CONFIG_IEEE80211W */
1146
1147                 send_len += p - reply->u.assoc_resp.variable;
1148         }
1149
1150         if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1151                 perror("handle_assoc: send");
1152 }
1153
1154
1155 static void handle_disassoc(struct hostapd_data *hapd,
1156                             struct ieee80211_mgmt *mgmt, size_t len)
1157 {
1158         struct sta_info *sta;
1159
1160         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1161                 printf("handle_disassoc - too short payload (len=%lu)\n",
1162                        (unsigned long) len);
1163                 return;
1164         }
1165
1166         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1167                    MAC2STR(mgmt->sa),
1168                    le_to_host16(mgmt->u.disassoc.reason_code));
1169
1170         sta = ap_get_sta(hapd, mgmt->sa);
1171         if (sta == NULL) {
1172                 printf("Station " MACSTR " trying to disassociate, but it "
1173                        "is not associated.\n", MAC2STR(mgmt->sa));
1174                 return;
1175         }
1176
1177         sta->flags &= ~WLAN_STA_ASSOC;
1178         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1179         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1180                        HOSTAPD_LEVEL_INFO, "disassociated");
1181         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1182         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1183         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1184          * authenticated. */
1185         accounting_sta_stop(hapd, sta);
1186         ieee802_1x_free_station(sta);
1187         hostapd_sta_remove(hapd, sta->addr);
1188
1189         if (sta->timeout_next == STA_NULLFUNC ||
1190             sta->timeout_next == STA_DISASSOC) {
1191                 sta->timeout_next = STA_DEAUTH;
1192                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1193                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1194                                        hapd, sta);
1195         }
1196
1197         mlme_disassociate_indication(
1198                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1199 }
1200
1201
1202 static void handle_deauth(struct hostapd_data *hapd,
1203                           struct ieee80211_mgmt *mgmt, size_t len)
1204 {
1205         struct sta_info *sta;
1206
1207         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1208                 printf("handle_deauth - too short payload (len=%lu)\n",
1209                        (unsigned long) len);
1210                 return;
1211         }
1212
1213         wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1214                    " reason_code=%d",
1215                    MAC2STR(mgmt->sa),
1216                    le_to_host16(mgmt->u.deauth.reason_code));
1217
1218         sta = ap_get_sta(hapd, mgmt->sa);
1219         if (sta == NULL) {
1220                 printf("Station " MACSTR " trying to deauthenticate, but it "
1221                        "is not authenticated.\n", MAC2STR(mgmt->sa));
1222                 return;
1223         }
1224
1225         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1226         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1227         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1228                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1229         mlme_deauthenticate_indication(
1230                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1231         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1232         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1233         ap_free_sta(hapd, sta);
1234 }
1235
1236
1237 static void handle_beacon(struct hostapd_data *hapd,
1238                           struct ieee80211_mgmt *mgmt, size_t len,
1239                           struct hostapd_frame_info *fi)
1240 {
1241         struct ieee802_11_elems elems;
1242
1243         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1244                 printf("handle_beacon - too short payload (len=%lu)\n",
1245                        (unsigned long) len);
1246                 return;
1247         }
1248
1249         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1250                                       len - (IEEE80211_HDRLEN +
1251                                              sizeof(mgmt->u.beacon)), &elems,
1252                                       0);
1253
1254         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1255 }
1256
1257
1258 #ifdef CONFIG_IEEE80211W
1259
1260 /* MLME-SAQuery.request */
1261 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1262                                   const u8 *addr, const u8 *trans_id)
1263 {
1264         struct ieee80211_mgmt mgmt;
1265         u8 *end;
1266
1267         os_memset(&mgmt, 0, sizeof(mgmt));
1268         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1269                                           WLAN_FC_STYPE_ACTION);
1270         os_memcpy(mgmt.da, addr, ETH_ALEN);
1271         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1272         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1273         mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1274         mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1275         os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1276                   WLAN_SA_QUERY_TR_ID_LEN);
1277         end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1278         if (hostapd_send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt, 0) < 0)
1279                 perror("ieee802_11_send_sa_query_req: send");
1280 }
1281
1282
1283 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1284                                     struct ieee80211_mgmt *mgmt, size_t len)
1285 {
1286         struct sta_info *sta;
1287         u8 *end;
1288         int i;
1289
1290         end = mgmt->u.action.u.sa_query_resp.trans_id +
1291                 WLAN_SA_QUERY_TR_ID_LEN;
1292         if (((u8 *) mgmt) + len < end) {
1293                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1294                            "frame (len=%lu)", (unsigned long) len);
1295                 return;
1296         }
1297
1298         if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1299                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1300                            "Action %d", mgmt->u.action.u.sa_query_resp.action);
1301                 return;
1302         }
1303
1304         /* MLME-SAQuery.confirm */
1305
1306         sta = ap_get_sta(hapd, mgmt->sa);
1307         if (sta == NULL || sta->sa_query_trans_id == NULL) {
1308                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1309                            "pending SA Query request found");
1310                 return;
1311         }
1312
1313         for (i = 0; i < sta->sa_query_count; i++) {
1314                 if (os_memcmp(sta->sa_query_trans_id +
1315                               i * WLAN_SA_QUERY_TR_ID_LEN,
1316                               mgmt->u.action.u.sa_query_resp.trans_id,
1317                               WLAN_SA_QUERY_TR_ID_LEN) == 0)
1318                         break;
1319         }
1320
1321         if (i >= sta->sa_query_count) {
1322                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1323                            "transaction identifier found");
1324                 return;
1325         }
1326
1327         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1328                        HOSTAPD_LEVEL_DEBUG,
1329                        "Reply to pending SA Query received");
1330         ap_sta_stop_sa_query(hapd, sta);
1331 }
1332 #endif /* CONFIG_IEEE80211W */
1333
1334
1335 static void handle_action(struct hostapd_data *hapd,
1336                           struct ieee80211_mgmt *mgmt, size_t len)
1337 {
1338         if (len < IEEE80211_HDRLEN + 1) {
1339                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1340                                HOSTAPD_LEVEL_DEBUG,
1341                                "handle_action - too short payload (len=%lu)",
1342                                (unsigned long) len);
1343                 return;
1344         }
1345
1346         switch (mgmt->u.action.category) {
1347 #ifdef CONFIG_IEEE80211R
1348         case WLAN_ACTION_FT:
1349         {
1350                 struct sta_info *sta;
1351
1352                 sta = ap_get_sta(hapd, mgmt->sa);
1353                 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1354                         wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1355                                    "frame from unassociated STA " MACSTR,
1356                                    MAC2STR(mgmt->sa));
1357                         return;
1358                 }
1359
1360                 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1361                                      len - IEEE80211_HDRLEN))
1362                         break;
1363
1364                 return;
1365         }
1366 #endif /* CONFIG_IEEE80211R */
1367         case WLAN_ACTION_WMM:
1368                 hostapd_wme_action(hapd, mgmt, len);
1369                 return;
1370 #ifdef CONFIG_IEEE80211W
1371         case WLAN_ACTION_SA_QUERY:
1372                 hostapd_sa_query_action(hapd, mgmt, len);
1373                 return;
1374 #endif /* CONFIG_IEEE80211W */
1375         }
1376
1377         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1378                        HOSTAPD_LEVEL_DEBUG,
1379                        "handle_action - unknown action category %d or invalid "
1380                        "frame",
1381                        mgmt->u.action.category);
1382         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1383             !(mgmt->sa[0] & 0x01)) {
1384                 /*
1385                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1386                  * Return the Action frame to the source without change
1387                  * except that MSB of the Category set to 1.
1388                  */
1389                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1390                            "frame back to sender");
1391                 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1392                 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1393                 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1394                 mgmt->u.action.category |= 0x80;
1395
1396                 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1397         }
1398 }
1399
1400
1401 /**
1402  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1403  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1404  * sent to)
1405  * @buf: management frame data (starting from IEEE 802.11 header)
1406  * @len: length of frame data in octets
1407  * @stype: management frame subtype from frame control field
1408  * @fi: meta data about received frame (signal level, etc.)
1409  *
1410  * Process all incoming IEEE 802.11 management frames. This will be called for
1411  * each frame received from the kernel driver through wlan#ap interface. In
1412  * addition, it can be called to re-inserted pending frames (e.g., when using
1413  * external RADIUS server as an MAC ACL).
1414  */
1415 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1416                      struct hostapd_frame_info *fi)
1417 {
1418         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1419         int broadcast;
1420
1421         if (stype == WLAN_FC_STYPE_BEACON) {
1422                 handle_beacon(hapd, mgmt, len, fi);
1423                 return;
1424         }
1425
1426         if (fi && fi->passive_scan)
1427                 return;
1428
1429         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1430                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1431                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1432
1433         if (!broadcast &&
1434             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1435                 printf("MGMT: BSSID=" MACSTR " not our address\n",
1436                        MAC2STR(mgmt->bssid));
1437                 return;
1438         }
1439
1440
1441         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1442                 handle_probe_req(hapd, mgmt, len);
1443                 return;
1444         }
1445
1446         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1447                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1448                                HOSTAPD_LEVEL_DEBUG,
1449                                "MGMT: DA=" MACSTR " not our address",
1450                                MAC2STR(mgmt->da));
1451                 return;
1452         }
1453
1454         switch (stype) {
1455         case WLAN_FC_STYPE_AUTH:
1456                 wpa_printf(MSG_DEBUG, "mgmt::auth");
1457                 handle_auth(hapd, mgmt, len);
1458                 break;
1459         case WLAN_FC_STYPE_ASSOC_REQ:
1460                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1461                 handle_assoc(hapd, mgmt, len, 0);
1462                 break;
1463         case WLAN_FC_STYPE_REASSOC_REQ:
1464                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1465                 handle_assoc(hapd, mgmt, len, 1);
1466                 break;
1467         case WLAN_FC_STYPE_DISASSOC:
1468                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1469                 handle_disassoc(hapd, mgmt, len);
1470                 break;
1471         case WLAN_FC_STYPE_DEAUTH:
1472                 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1473                 handle_deauth(hapd, mgmt, len);
1474                 break;
1475         case WLAN_FC_STYPE_ACTION:
1476                 wpa_printf(MSG_DEBUG, "mgmt::action");
1477                 handle_action(hapd, mgmt, len);
1478                 break;
1479         default:
1480                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1481                                HOSTAPD_LEVEL_DEBUG,
1482                                "unknown mgmt frame subtype %d", stype);
1483                 break;
1484         }
1485 }
1486
1487
1488 static void handle_auth_cb(struct hostapd_data *hapd,
1489                            struct ieee80211_mgmt *mgmt,
1490                            size_t len, int ok)
1491 {
1492         u16 auth_alg, auth_transaction, status_code;
1493         struct sta_info *sta;
1494
1495         if (!ok) {
1496                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1497                                HOSTAPD_LEVEL_NOTICE,
1498                                "did not acknowledge authentication response");
1499                 return;
1500         }
1501
1502         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1503                 printf("handle_auth_cb - too short payload (len=%lu)\n",
1504                        (unsigned long) len);
1505                 return;
1506         }
1507
1508         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1509         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1510         status_code = le_to_host16(mgmt->u.auth.status_code);
1511
1512         sta = ap_get_sta(hapd, mgmt->da);
1513         if (!sta) {
1514                 printf("handle_auth_cb: STA " MACSTR " not found\n",
1515                        MAC2STR(mgmt->da));
1516                 return;
1517         }
1518
1519         if (status_code == WLAN_STATUS_SUCCESS &&
1520             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1521              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1522                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1523                                HOSTAPD_LEVEL_INFO, "authenticated");
1524                 sta->flags |= WLAN_STA_AUTH;
1525         }
1526 }
1527
1528
1529 static void handle_assoc_cb(struct hostapd_data *hapd,
1530                             struct ieee80211_mgmt *mgmt,
1531                             size_t len, int reassoc, int ok)
1532 {
1533         u16 status;
1534         struct sta_info *sta;
1535         int new_assoc = 1;
1536         struct ht_cap_ie *ht_cap = NULL;
1537
1538         if (!ok) {
1539                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1540                                HOSTAPD_LEVEL_DEBUG,
1541                                "did not acknowledge association response");
1542                 return;
1543         }
1544
1545         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1546                                       sizeof(mgmt->u.assoc_resp))) {
1547                 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1548                        "(len=%lu)\n", reassoc, (unsigned long) len);
1549                 return;
1550         }
1551
1552         if (reassoc)
1553                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1554         else
1555                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1556
1557         sta = ap_get_sta(hapd, mgmt->da);
1558         if (!sta) {
1559                 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1560                        MAC2STR(mgmt->da));
1561                 return;
1562         }
1563
1564         if (status != WLAN_STATUS_SUCCESS)
1565                 goto fail;
1566
1567         /* Stop previous accounting session, if one is started, and allocate
1568          * new session id for the new session. */
1569         accounting_sta_stop(hapd, sta);
1570
1571         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1572                        HOSTAPD_LEVEL_INFO,
1573                        "associated (aid %d)",
1574                        sta->aid);
1575
1576         if (sta->flags & WLAN_STA_ASSOC)
1577                 new_assoc = 0;
1578         sta->flags |= WLAN_STA_ASSOC;
1579
1580         if (reassoc)
1581                 mlme_reassociate_indication(hapd, sta);
1582         else
1583                 mlme_associate_indication(hapd, sta);
1584
1585 #ifdef CONFIG_IEEE80211N
1586         if (sta->flags & WLAN_STA_HT)
1587                 ht_cap = &sta->ht_capabilities;
1588 #endif /* CONFIG_IEEE80211N */
1589
1590 #ifdef CONFIG_IEEE80211W
1591         sta->sa_query_timed_out = 0;
1592 #endif /* CONFIG_IEEE80211W */
1593
1594         if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1595                             sta->capability, sta->supported_rates,
1596                             sta->supported_rates_len, 0, sta->listen_interval,
1597                             ht_cap))
1598         {
1599                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1600                                HOSTAPD_LEVEL_NOTICE,
1601                                "Could not add STA to kernel driver");
1602         }
1603
1604         if (sta->eapol_sm == NULL) {
1605                 /*
1606                  * This STA does not use RADIUS server for EAP authentication,
1607                  * so bind it to the selected VLAN interface now, since the
1608                  * interface selection is not going to change anymore.
1609                  */
1610                 ap_sta_bind_vlan(hapd, sta, 0);
1611         } else if (sta->vlan_id) {
1612                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1613                 ap_sta_bind_vlan(hapd, sta, 0);
1614         }
1615         if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1616                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1617                                       WLAN_STA_SHORT_PREAMBLE, ~0);
1618         } else {
1619                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1620                                       0, ~WLAN_STA_SHORT_PREAMBLE);
1621         }
1622
1623         if (sta->auth_alg == WLAN_AUTH_FT)
1624                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1625         else
1626                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1627         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1628
1629         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1630
1631  fail:
1632         /* Copy of the association request is not needed anymore */
1633         if (sta->last_assoc_req) {
1634                 os_free(sta->last_assoc_req);
1635                 sta->last_assoc_req = NULL;
1636         }
1637 }
1638
1639
1640 /**
1641  * ieee802_11_mgmt_cb - Process management frame TX status callback
1642  * @hapd: hostapd BSS data structure (the BSS from which the management frame
1643  * was sent from)
1644  * @buf: management frame data (starting from IEEE 802.11 header)
1645  * @len: length of frame data in octets
1646  * @stype: management frame subtype from frame control field
1647  * @ok: Whether the frame was ACK'ed
1648  */
1649 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1650                         u16 stype, int ok)
1651 {
1652         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1653
1654         switch (stype) {
1655         case WLAN_FC_STYPE_AUTH:
1656                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1657                 handle_auth_cb(hapd, mgmt, len, ok);
1658                 break;
1659         case WLAN_FC_STYPE_ASSOC_RESP:
1660                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1661                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1662                 break;
1663         case WLAN_FC_STYPE_REASSOC_RESP:
1664                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1665                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1666                 break;
1667         case WLAN_FC_STYPE_PROBE_RESP:
1668                 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1669                 break;
1670         case WLAN_FC_STYPE_DEAUTH:
1671                 /* ignore */
1672                 break;
1673         case WLAN_FC_STYPE_ACTION:
1674                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1675                 break;
1676         default:
1677                 printf("unknown mgmt cb frame subtype %d\n", stype);
1678                 break;
1679         }
1680 }
1681
1682
1683 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1684 {
1685         /* TODO */
1686         return 0;
1687 }
1688
1689
1690 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1691                            char *buf, size_t buflen)
1692 {
1693         /* TODO */
1694         return 0;
1695 }
1696
1697 #endif /* CONFIG_NATIVE_WINDOWS */