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