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