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