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