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