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