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