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