Use os_get_random() for Shared Key authentication challenge
[mech_eap.git] / src / ap / ieee802_11.c
1 /*
2  * hostapd / IEEE 802.11 Management
3  * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #ifndef CONFIG_NATIVE_WINDOWS
12
13 #include "utils/common.h"
14 #include "utils/eloop.h"
15 #include "crypto/crypto.h"
16 #include "crypto/sha256.h"
17 #include "crypto/random.h"
18 #include "common/ieee802_11_defs.h"
19 #include "common/ieee802_11_common.h"
20 #include "common/wpa_ctrl.h"
21 #include "common/sae.h"
22 #include "radius/radius.h"
23 #include "radius/radius_client.h"
24 #include "p2p/p2p.h"
25 #include "wps/wps.h"
26 #include "fst/fst.h"
27 #include "hostapd.h"
28 #include "beacon.h"
29 #include "ieee802_11_auth.h"
30 #include "sta_info.h"
31 #include "ieee802_1x.h"
32 #include "wpa_auth.h"
33 #include "pmksa_cache_auth.h"
34 #include "wmm.h"
35 #include "ap_list.h"
36 #include "accounting.h"
37 #include "ap_config.h"
38 #include "ap_mlme.h"
39 #include "p2p_hostapd.h"
40 #include "ap_drv_ops.h"
41 #include "wnm_ap.h"
42 #include "hw_features.h"
43 #include "ieee802_11.h"
44 #include "dfs.h"
45
46
47 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
48 {
49         u8 *pos = eid;
50         int i, num, count;
51
52         if (hapd->iface->current_rates == NULL)
53                 return eid;
54
55         *pos++ = WLAN_EID_SUPP_RATES;
56         num = hapd->iface->num_rates;
57         if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
58                 num++;
59         if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
60                 num++;
61         if (num > 8) {
62                 /* rest of the rates are encoded in Extended supported
63                  * rates element */
64                 num = 8;
65         }
66
67         *pos++ = num;
68         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
69              i++) {
70                 count++;
71                 *pos = hapd->iface->current_rates[i].rate / 5;
72                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
73                         *pos |= 0x80;
74                 pos++;
75         }
76
77         if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
78                 count++;
79                 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
80         }
81
82         if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
83                 count++;
84                 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
85         }
86
87         return pos;
88 }
89
90
91 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
92 {
93         u8 *pos = eid;
94         int i, num, count;
95
96         if (hapd->iface->current_rates == NULL)
97                 return eid;
98
99         num = hapd->iface->num_rates;
100         if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
101                 num++;
102         if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
103                 num++;
104         if (num <= 8)
105                 return eid;
106         num -= 8;
107
108         *pos++ = WLAN_EID_EXT_SUPP_RATES;
109         *pos++ = num;
110         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
111              i++) {
112                 count++;
113                 if (count <= 8)
114                         continue; /* already in SuppRates IE */
115                 *pos = hapd->iface->current_rates[i].rate / 5;
116                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
117                         *pos |= 0x80;
118                 pos++;
119         }
120
121         if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
122                 count++;
123                 if (count > 8)
124                         *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
125         }
126
127         if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
128                 count++;
129                 if (count > 8)
130                         *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
131         }
132
133         return pos;
134 }
135
136
137 u16 hostapd_own_capab_info(struct hostapd_data *hapd)
138 {
139         int capab = WLAN_CAPABILITY_ESS;
140         int privacy;
141         int dfs;
142
143         /* Check if any of configured channels require DFS */
144         dfs = hostapd_is_dfs_required(hapd->iface);
145         if (dfs < 0) {
146                 wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
147                            dfs);
148                 dfs = 0;
149         }
150
151         if (hapd->iface->num_sta_no_short_preamble == 0 &&
152             hapd->iconf->preamble == SHORT_PREAMBLE)
153                 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
154
155         privacy = hapd->conf->ssid.wep.keys_set;
156
157         if (hapd->conf->ieee802_1x &&
158             (hapd->conf->default_wep_key_len ||
159              hapd->conf->individual_wep_key_len))
160                 privacy = 1;
161
162         if (hapd->conf->wpa)
163                 privacy = 1;
164
165 #ifdef CONFIG_HS20
166         if (hapd->conf->osen)
167                 privacy = 1;
168 #endif /* CONFIG_HS20 */
169
170         if (privacy)
171                 capab |= WLAN_CAPABILITY_PRIVACY;
172
173         if (hapd->iface->current_mode &&
174             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
175             hapd->iface->num_sta_no_short_slot_time == 0)
176                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
177
178         /*
179          * Currently, Spectrum Management capability bit is set when directly
180          * requested in configuration by spectrum_mgmt_required or when AP is
181          * running on DFS channel.
182          * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
183          */
184         if (hapd->iface->current_mode &&
185             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
186             (hapd->iconf->spectrum_mgmt_required || dfs))
187                 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
188
189         if (hapd->conf->radio_measurements)
190                 capab |= IEEE80211_CAP_RRM;
191
192         return capab;
193 }
194
195
196 #ifndef CONFIG_NO_RC4
197 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
198                            u16 auth_transaction, const u8 *challenge,
199                            int iswep)
200 {
201         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
202                        HOSTAPD_LEVEL_DEBUG,
203                        "authentication (shared key, transaction %d)",
204                        auth_transaction);
205
206         if (auth_transaction == 1) {
207                 if (!sta->challenge) {
208                         /* Generate a pseudo-random challenge */
209                         u8 key[8];
210
211                         sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
212                         if (sta->challenge == NULL)
213                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
214
215                         if (os_get_random(key, sizeof(key)) < 0) {
216                                 os_free(sta->challenge);
217                                 sta->challenge = NULL;
218                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
219                         }
220
221                         rc4_skip(key, sizeof(key), 0,
222                                  sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
223                 }
224                 return 0;
225         }
226
227         if (auth_transaction != 3)
228                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
229
230         /* Transaction 3 */
231         if (!iswep || !sta->challenge || !challenge ||
232             os_memcmp_const(sta->challenge, challenge,
233                             WLAN_AUTH_CHALLENGE_LEN)) {
234                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
235                                HOSTAPD_LEVEL_INFO,
236                                "shared key authentication - invalid "
237                                "challenge-response");
238                 return WLAN_STATUS_CHALLENGE_FAIL;
239         }
240
241         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
242                        HOSTAPD_LEVEL_DEBUG,
243                        "authentication OK (shared key)");
244         sta->flags |= WLAN_STA_AUTH;
245         wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
246         os_free(sta->challenge);
247         sta->challenge = NULL;
248
249         return 0;
250 }
251 #endif /* CONFIG_NO_RC4 */
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 (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
287                 wpa_printf(MSG_INFO, "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 #ifdef CONFIG_SAE
320
321 #define dot11RSNASAESync 5              /* attempts */
322
323
324 static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
325                                              struct sta_info *sta, int update)
326 {
327         struct wpabuf *buf;
328
329         if (hapd->conf->ssid.wpa_passphrase == NULL) {
330                 wpa_printf(MSG_DEBUG, "SAE: No password available");
331                 return NULL;
332         }
333
334         if (update &&
335             sae_prepare_commit(hapd->own_addr, sta->addr,
336                                (u8 *) hapd->conf->ssid.wpa_passphrase,
337                                os_strlen(hapd->conf->ssid.wpa_passphrase),
338                                sta->sae) < 0) {
339                 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
340                 return NULL;
341         }
342
343         buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
344         if (buf == NULL)
345                 return NULL;
346         sae_write_commit(sta->sae, buf, sta->sae->tmp ?
347                          sta->sae->tmp->anti_clogging_token : NULL);
348
349         return buf;
350 }
351
352
353 static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
354                                               struct sta_info *sta)
355 {
356         struct wpabuf *buf;
357
358         buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
359         if (buf == NULL)
360                 return NULL;
361
362         sae_write_confirm(sta->sae, buf);
363
364         return buf;
365 }
366
367
368 static int auth_sae_send_commit(struct hostapd_data *hapd,
369                                 struct sta_info *sta,
370                                 const u8 *bssid, int update)
371 {
372         struct wpabuf *data;
373
374         data = auth_build_sae_commit(hapd, sta, update);
375         if (data == NULL)
376                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
377
378         send_auth_reply(hapd, sta->addr, bssid,
379                         WLAN_AUTH_SAE, 1, WLAN_STATUS_SUCCESS,
380                         wpabuf_head(data), wpabuf_len(data));
381
382         wpabuf_free(data);
383
384         return WLAN_STATUS_SUCCESS;
385 }
386
387
388 static int auth_sae_send_confirm(struct hostapd_data *hapd,
389                                  struct sta_info *sta,
390                                  const u8 *bssid)
391 {
392         struct wpabuf *data;
393
394         data = auth_build_sae_confirm(hapd, sta);
395         if (data == NULL)
396                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
397
398         send_auth_reply(hapd, sta->addr, bssid,
399                         WLAN_AUTH_SAE, 2, WLAN_STATUS_SUCCESS,
400                         wpabuf_head(data), wpabuf_len(data));
401
402         wpabuf_free(data);
403
404         return WLAN_STATUS_SUCCESS;
405 }
406
407
408 static int use_sae_anti_clogging(struct hostapd_data *hapd)
409 {
410         struct sta_info *sta;
411         unsigned int open = 0;
412
413         if (hapd->conf->sae_anti_clogging_threshold == 0)
414                 return 1;
415
416         for (sta = hapd->sta_list; sta; sta = sta->next) {
417                 if (!sta->sae)
418                         continue;
419                 if (sta->sae->state != SAE_COMMITTED &&
420                     sta->sae->state != SAE_CONFIRMED)
421                         continue;
422                 open++;
423                 if (open >= hapd->conf->sae_anti_clogging_threshold)
424                         return 1;
425         }
426
427         return 0;
428 }
429
430
431 static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
432                            const u8 *token, size_t token_len)
433 {
434         u8 mac[SHA256_MAC_LEN];
435
436         if (token_len != SHA256_MAC_LEN)
437                 return -1;
438         if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
439                         addr, ETH_ALEN, mac) < 0 ||
440             os_memcmp_const(token, mac, SHA256_MAC_LEN) != 0)
441                 return -1;
442
443         return 0;
444 }
445
446
447 static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
448                                             int group, const u8 *addr)
449 {
450         struct wpabuf *buf;
451         u8 *token;
452         struct os_reltime now;
453
454         os_get_reltime(&now);
455         if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
456             os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60)) {
457                 if (random_get_bytes(hapd->sae_token_key,
458                                      sizeof(hapd->sae_token_key)) < 0)
459                         return NULL;
460                 wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
461                             hapd->sae_token_key, sizeof(hapd->sae_token_key));
462                 hapd->last_sae_token_key_update = now;
463         }
464
465         buf = wpabuf_alloc(sizeof(le16) + SHA256_MAC_LEN);
466         if (buf == NULL)
467                 return NULL;
468
469         wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
470
471         token = wpabuf_put(buf, SHA256_MAC_LEN);
472         hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
473                     addr, ETH_ALEN, token);
474
475         return buf;
476 }
477
478
479 static int sae_check_big_sync(struct sta_info *sta)
480 {
481         if (sta->sae->sync > dot11RSNASAESync) {
482                 sta->sae->state = SAE_NOTHING;
483                 sta->sae->sync = 0;
484                 return -1;
485         }
486         return 0;
487 }
488
489
490 static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
491 {
492         struct hostapd_data *hapd = eloop_ctx;
493         struct sta_info *sta = eloop_data;
494         int ret;
495
496         if (sae_check_big_sync(sta))
497                 return;
498         sta->sae->sync++;
499
500         switch (sta->sae->state) {
501         case SAE_COMMITTED:
502                 ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
503                 eloop_register_timeout(0,
504                                        hapd->dot11RSNASAERetransPeriod * 1000,
505                                        auth_sae_retransmit_timer, hapd, sta);
506                 break;
507         case SAE_CONFIRMED:
508                 ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
509                 eloop_register_timeout(0,
510                                        hapd->dot11RSNASAERetransPeriod * 1000,
511                                        auth_sae_retransmit_timer, hapd, sta);
512                 break;
513         default:
514                 ret = -1;
515                 break;
516         }
517
518         if (ret != WLAN_STATUS_SUCCESS)
519                 wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
520 }
521
522
523 void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
524 {
525         eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
526 }
527
528
529 static void sae_set_retransmit_timer(struct hostapd_data *hapd,
530                                      struct sta_info *sta)
531 {
532         if (!(hapd->conf->mesh & MESH_ENABLED))
533                 return;
534
535         eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
536         eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
537                                auth_sae_retransmit_timer, hapd, sta);
538 }
539
540
541 static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
542                        const u8 *bssid, u8 auth_transaction)
543 {
544         int ret;
545
546         if (auth_transaction != 1 && auth_transaction != 2)
547                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
548
549         switch (sta->sae->state) {
550         case SAE_NOTHING:
551                 if (auth_transaction == 1) {
552                         ret = auth_sae_send_commit(hapd, sta, bssid, 1);
553                         if (ret)
554                                 return ret;
555                         sta->sae->state = SAE_COMMITTED;
556
557                         if (sae_process_commit(sta->sae) < 0)
558                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
559
560                         /*
561                          * In mesh case, both Commit and Confirm can be sent
562                          * immediately. In infrastructure BSS, only a single
563                          * Authentication frame (Commit) is expected from the AP
564                          * here and the second one (Confirm) will be sent once
565                          * the STA has sent its second Authentication frame
566                          * (Confirm).
567                          */
568                         if (hapd->conf->mesh & MESH_ENABLED) {
569                                 /*
570                                  * Send both Commit and Confirm immediately
571                                  * based on SAE finite state machine
572                                  * Nothing -> Confirm transition.
573                                  */
574                                 ret = auth_sae_send_confirm(hapd, sta, bssid);
575                                 if (ret)
576                                         return ret;
577                                 sta->sae->state = SAE_CONFIRMED;
578                         } else {
579                                 /*
580                                  * For infrastructure BSS, send only the Commit
581                                  * message now to get alternating sequence of
582                                  * Authentication frames between the AP and STA.
583                                  * Confirm will be sent in
584                                  * Commited -> Confirmed/Accepted transition
585                                  * when receiving Confirm from STA.
586                                  */
587                         }
588                         sta->sae->sync = 0;
589                         sae_set_retransmit_timer(hapd, sta);
590                 } else {
591                         hostapd_logger(hapd, sta->addr,
592                                        HOSTAPD_MODULE_IEEE80211,
593                                        HOSTAPD_LEVEL_DEBUG,
594                                        "SAE confirm before commit");
595                 }
596                 break;
597         case SAE_COMMITTED:
598                 sae_clear_retransmit_timer(hapd, sta);
599                 if (auth_transaction == 1) {
600                         if (sae_process_commit(sta->sae) < 0)
601                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
602
603                         ret = auth_sae_send_confirm(hapd, sta, bssid);
604                         if (ret)
605                                 return ret;
606                         sta->sae->state = SAE_CONFIRMED;
607                         sta->sae->sync = 0;
608                         sae_set_retransmit_timer(hapd, sta);
609                 } else if (hapd->conf->mesh & MESH_ENABLED) {
610                         /*
611                          * In mesh case, follow SAE finite state machine and
612                          * send Commit now, if sync count allows.
613                          */
614                         if (sae_check_big_sync(sta))
615                                 return WLAN_STATUS_SUCCESS;
616                         sta->sae->sync++;
617
618                         ret = auth_sae_send_commit(hapd, sta, bssid, 0);
619                         if (ret)
620                                 return ret;
621
622                         sae_set_retransmit_timer(hapd, sta);
623                 } else {
624                         /*
625                          * For instructure BSS, send the postponed Confirm from
626                          * Nothing -> Confirmed transition that was reduced to
627                          * Nothing -> Committed above.
628                          */
629                         ret = auth_sae_send_confirm(hapd, sta, bssid);
630                         if (ret)
631                                 return ret;
632
633                         sta->sae->state = SAE_CONFIRMED;
634
635                         /*
636                          * Since this was triggered on Confirm RX, run another
637                          * step to get to Accepted without waiting for
638                          * additional events.
639                          */
640                         return sae_sm_step(hapd, sta, bssid, auth_transaction);
641                 }
642                 break;
643         case SAE_CONFIRMED:
644                 sae_clear_retransmit_timer(hapd, sta);
645                 if (auth_transaction == 1) {
646                         if (sae_check_big_sync(sta))
647                                 return WLAN_STATUS_SUCCESS;
648                         sta->sae->sync++;
649
650                         ret = auth_sae_send_commit(hapd, sta, bssid, 1);
651                         if (ret)
652                                 return ret;
653
654                         if (sae_process_commit(sta->sae) < 0)
655                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
656
657                         ret = auth_sae_send_confirm(hapd, sta, bssid);
658                         if (ret)
659                                 return ret;
660
661                         sae_set_retransmit_timer(hapd, sta);
662                 } else {
663                         sta->flags |= WLAN_STA_AUTH;
664                         sta->auth_alg = WLAN_AUTH_SAE;
665                         mlme_authenticate_indication(hapd, sta);
666                         wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
667                         sta->sae->state = SAE_ACCEPTED;
668                         wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
669                                                sta->sae->pmk, sta->sae->pmkid);
670                 }
671                 break;
672         case SAE_ACCEPTED:
673                 if (auth_transaction == 1) {
674                         wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
675                                    ") doing reauthentication",
676                                    MAC2STR(sta->addr));
677                         ap_free_sta(hapd, sta);
678                 } else {
679                         if (sae_check_big_sync(sta))
680                                 return WLAN_STATUS_SUCCESS;
681                         sta->sae->sync++;
682
683                         ret = auth_sae_send_confirm(hapd, sta, bssid);
684                         sae_clear_temp_data(sta->sae);
685                         if (ret)
686                                 return ret;
687                 }
688                 break;
689         default:
690                 wpa_printf(MSG_ERROR, "SAE: invalid state %d",
691                            sta->sae->state);
692                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
693         }
694         return WLAN_STATUS_SUCCESS;
695 }
696
697
698 static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
699                             const struct ieee80211_mgmt *mgmt, size_t len,
700                             u16 auth_transaction, u16 status_code)
701 {
702         u16 resp = WLAN_STATUS_SUCCESS;
703         struct wpabuf *data = NULL;
704
705         if (!sta->sae) {
706                 if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
707                         return;
708                 sta->sae = os_zalloc(sizeof(*sta->sae));
709                 if (sta->sae == NULL)
710                         return;
711                 sta->sae->state = SAE_NOTHING;
712                 sta->sae->sync = 0;
713         }
714
715         if (auth_transaction == 1) {
716                 const u8 *token = NULL, *pos, *end;
717                 size_t token_len = 0;
718                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
719                                HOSTAPD_LEVEL_DEBUG,
720                                "start SAE authentication (RX commit, status=%u)",
721                                status_code);
722
723                 if ((hapd->conf->mesh & MESH_ENABLED) &&
724                     status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
725                     sta->sae->tmp) {
726                         pos = mgmt->u.auth.variable;
727                         end = ((const u8 *) mgmt) + len;
728                         if (pos + sizeof(le16) > end) {
729                                 wpa_printf(MSG_ERROR,
730                                            "SAE: Too short anti-clogging token request");
731                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
732                                 goto reply;
733                         }
734                         resp = sae_group_allowed(sta->sae,
735                                                  hapd->conf->sae_groups,
736                                                  WPA_GET_LE16(pos));
737                         if (resp != WLAN_STATUS_SUCCESS) {
738                                 wpa_printf(MSG_ERROR,
739                                            "SAE: Invalid group in anti-clogging token request");
740                                 goto reply;
741                         }
742                         pos += sizeof(le16);
743
744                         wpabuf_free(sta->sae->tmp->anti_clogging_token);
745                         sta->sae->tmp->anti_clogging_token =
746                                 wpabuf_alloc_copy(pos, end - pos);
747                         if (sta->sae->tmp->anti_clogging_token == NULL) {
748                                 wpa_printf(MSG_ERROR,
749                                            "SAE: Failed to alloc for anti-clogging token");
750                                 return;
751                         }
752
753                         /*
754                          * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
755                          * is 76, a new Commit Message shall be constructed
756                          * with the Anti-Clogging Token from the received
757                          * Authentication frame, and the commit-scalar and
758                          * COMMIT-ELEMENT previously sent.
759                          */
760                         if (auth_sae_send_commit(hapd, sta, mgmt->bssid, 0)) {
761                                 wpa_printf(MSG_ERROR,
762                                            "SAE: Failed to send commit message");
763                                 return;
764                         }
765                         sta->sae->state = SAE_COMMITTED;
766                         sta->sae->sync = 0;
767                         sae_set_retransmit_timer(hapd, sta);
768                         return;
769                 }
770
771                 if (status_code != WLAN_STATUS_SUCCESS)
772                         return;
773
774                 resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
775                                         ((const u8 *) mgmt) + len -
776                                         mgmt->u.auth.variable, &token,
777                                         &token_len, hapd->conf->sae_groups);
778                 if (resp == SAE_SILENTLY_DISCARD) {
779                         wpa_printf(MSG_DEBUG,
780                                    "SAE: Drop commit message from " MACSTR " due to reflection attack",
781                                    MAC2STR(sta->addr));
782                         return;
783                 }
784                 if (token && check_sae_token(hapd, sta->addr, token, token_len)
785                     < 0) {
786                         wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
787                                    "incorrect token from " MACSTR,
788                                    MAC2STR(sta->addr));
789                         return;
790                 }
791
792                 if (resp != WLAN_STATUS_SUCCESS)
793                         goto reply;
794
795                 if (!token && use_sae_anti_clogging(hapd)) {
796                         wpa_printf(MSG_DEBUG,
797                                    "SAE: Request anti-clogging token from "
798                                    MACSTR, MAC2STR(sta->addr));
799                         data = auth_build_token_req(hapd, sta->sae->group,
800                                                     sta->addr);
801                         resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
802                         if (hapd->conf->mesh & MESH_ENABLED)
803                                 sta->sae->state = SAE_NOTHING;
804                         goto reply;
805                 }
806
807                 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
808         } else if (auth_transaction == 2) {
809                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
810                                HOSTAPD_LEVEL_DEBUG,
811                                "SAE authentication (RX confirm, status=%u)",
812                                status_code);
813                 if (status_code != WLAN_STATUS_SUCCESS)
814                         return;
815                 if (sta->sae->state >= SAE_CONFIRMED ||
816                     !(hapd->conf->mesh & MESH_ENABLED)) {
817                         if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
818                                               ((u8 *) mgmt) + len -
819                                               mgmt->u.auth.variable) < 0) {
820                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
821                                 goto reply;
822                         }
823                 }
824                 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
825         } else {
826                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
827                                HOSTAPD_LEVEL_DEBUG,
828                                "unexpected SAE authentication transaction %u (status=%u)",
829                                auth_transaction, status_code);
830                 if (status_code != WLAN_STATUS_SUCCESS)
831                         return;
832                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
833         }
834
835 reply:
836         if (resp != WLAN_STATUS_SUCCESS) {
837                 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
838                                 auth_transaction, resp,
839                                 data ? wpabuf_head(data) : (u8 *) "",
840                                 data ? wpabuf_len(data) : 0);
841         }
842         wpabuf_free(data);
843 }
844
845
846 /**
847  * auth_sae_init_committed - Send COMMIT and start SAE in committed state
848  * @hapd: BSS data for the device initiating the authentication
849  * @sta: the peer to which commit authentication frame is sent
850  *
851  * This function implements Init event handling (IEEE Std 802.11-2012,
852  * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
853  * sta->sae structure should be initialized appropriately via a call to
854  * sae_prepare_commit().
855  */
856 int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
857 {
858         int ret;
859
860         if (!sta->sae || !sta->sae->tmp)
861                 return -1;
862
863         if (sta->sae->state != SAE_NOTHING)
864                 return -1;
865
866         ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
867         if (ret)
868                 return -1;
869
870         sta->sae->state = SAE_COMMITTED;
871         sta->sae->sync = 0;
872         sae_set_retransmit_timer(hapd, sta);
873
874         return 0;
875 }
876
877 #endif /* CONFIG_SAE */
878
879
880 static void handle_auth(struct hostapd_data *hapd,
881                         const struct ieee80211_mgmt *mgmt, size_t len)
882 {
883         u16 auth_alg, auth_transaction, status_code;
884         u16 resp = WLAN_STATUS_SUCCESS;
885         struct sta_info *sta = NULL;
886         int res;
887         u16 fc;
888         const u8 *challenge = NULL;
889         u32 session_timeout, acct_interim_interval;
890         struct vlan_description vlan_id;
891         struct hostapd_sta_wpa_psk_short *psk = NULL;
892         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
893         size_t resp_ies_len = 0;
894         char *identity = NULL;
895         char *radius_cui = NULL;
896         u16 seq_ctrl;
897
898         os_memset(&vlan_id, 0, sizeof(vlan_id));
899
900         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
901                 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
902                            (unsigned long) len);
903                 return;
904         }
905
906 #ifdef CONFIG_TESTING_OPTIONS
907         if (hapd->iconf->ignore_auth_probability > 0.0 &&
908             drand48() < hapd->iconf->ignore_auth_probability) {
909                 wpa_printf(MSG_INFO,
910                            "TESTING: ignoring auth frame from " MACSTR,
911                            MAC2STR(mgmt->sa));
912                 return;
913         }
914 #endif /* CONFIG_TESTING_OPTIONS */
915
916         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
917         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
918         status_code = le_to_host16(mgmt->u.auth.status_code);
919         fc = le_to_host16(mgmt->frame_control);
920         seq_ctrl = le_to_host16(mgmt->seq_ctrl);
921
922         if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
923             2 + WLAN_AUTH_CHALLENGE_LEN &&
924             mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
925             mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
926                 challenge = &mgmt->u.auth.variable[2];
927
928         wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
929                    "auth_transaction=%d status_code=%d wep=%d%s "
930                    "seq_ctrl=0x%x%s",
931                    MAC2STR(mgmt->sa), auth_alg, auth_transaction,
932                    status_code, !!(fc & WLAN_FC_ISWEP),
933                    challenge ? " challenge" : "",
934                    seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
935
936 #ifdef CONFIG_NO_RC4
937         if (auth_alg == WLAN_AUTH_SHARED_KEY) {
938                 wpa_printf(MSG_INFO,
939                            "Unsupported authentication algorithm (%d)",
940                            auth_alg);
941                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
942                 goto fail;
943         }
944 #endif /* CONFIG_NO_RC4 */
945
946         if (hapd->tkip_countermeasures) {
947                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
948                 goto fail;
949         }
950
951         if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
952                auth_alg == WLAN_AUTH_OPEN) ||
953 #ifdef CONFIG_IEEE80211R
954               (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
955                auth_alg == WLAN_AUTH_FT) ||
956 #endif /* CONFIG_IEEE80211R */
957 #ifdef CONFIG_SAE
958               (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
959                auth_alg == WLAN_AUTH_SAE) ||
960 #endif /* CONFIG_SAE */
961               ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
962                auth_alg == WLAN_AUTH_SHARED_KEY))) {
963                 wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
964                            auth_alg);
965                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
966                 goto fail;
967         }
968
969         if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
970               (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
971                 wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
972                            auth_transaction);
973                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
974                 goto fail;
975         }
976
977         if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
978                 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
979                            MAC2STR(mgmt->sa));
980                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
981                 goto fail;
982         }
983
984         if (hapd->conf->no_auth_if_seen_on) {
985                 struct hostapd_data *other;
986
987                 other = sta_track_seen_on(hapd->iface, mgmt->sa,
988                                           hapd->conf->no_auth_if_seen_on);
989                 if (other) {
990                         u8 *pos;
991                         u32 info;
992                         u8 op_class, channel, phytype;
993
994                         wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
995                                    MACSTR " since STA has been seen on %s",
996                                    hapd->conf->iface, MAC2STR(mgmt->sa),
997                                    hapd->conf->no_auth_if_seen_on);
998
999                         resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
1000                         pos = &resp_ies[0];
1001                         *pos++ = WLAN_EID_NEIGHBOR_REPORT;
1002                         *pos++ = 13;
1003                         os_memcpy(pos, other->own_addr, ETH_ALEN);
1004                         pos += ETH_ALEN;
1005                         info = 0; /* TODO: BSSID Information */
1006                         WPA_PUT_LE32(pos, info);
1007                         pos += 4;
1008                         if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
1009                                 phytype = 8; /* dmg */
1010                         else if (other->iconf->ieee80211ac)
1011                                 phytype = 9; /* vht */
1012                         else if (other->iconf->ieee80211n)
1013                                 phytype = 7; /* ht */
1014                         else if (other->iconf->hw_mode ==
1015                                  HOSTAPD_MODE_IEEE80211A)
1016                                 phytype = 4; /* ofdm */
1017                         else if (other->iconf->hw_mode ==
1018                                  HOSTAPD_MODE_IEEE80211G)
1019                                 phytype = 6; /* erp */
1020                         else
1021                                 phytype = 5; /* hrdsss */
1022                         if (ieee80211_freq_to_channel_ext(
1023                                     hostapd_hw_get_freq(other,
1024                                                         other->iconf->channel),
1025                                     other->iconf->secondary_channel,
1026                                     other->iconf->ieee80211ac,
1027                                     &op_class, &channel) == NUM_HOSTAPD_MODES) {
1028                                 op_class = 0;
1029                                 channel = other->iconf->channel;
1030                         }
1031                         *pos++ = op_class;
1032                         *pos++ = channel;
1033                         *pos++ = phytype;
1034                         resp_ies_len = pos - &resp_ies[0];
1035                         goto fail;
1036                 }
1037         }
1038
1039         res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
1040                                       &session_timeout,
1041                                       &acct_interim_interval, &vlan_id,
1042                                       &psk, &identity, &radius_cui);
1043
1044         if (res == HOSTAPD_ACL_REJECT) {
1045                 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
1046                            MAC2STR(mgmt->sa));
1047                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1048                 goto fail;
1049         }
1050         if (res == HOSTAPD_ACL_PENDING) {
1051                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
1052                            " waiting for an external authentication",
1053                            MAC2STR(mgmt->sa));
1054                 /* Authentication code will re-send the authentication frame
1055                  * after it has received (and cached) information from the
1056                  * external source. */
1057                 return;
1058         }
1059
1060         sta = ap_get_sta(hapd, mgmt->sa);
1061         if (sta) {
1062                 if ((fc & WLAN_FC_RETRY) &&
1063                     sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1064                     sta->last_seq_ctrl == seq_ctrl &&
1065                     sta->last_subtype == WLAN_FC_STYPE_AUTH) {
1066                         hostapd_logger(hapd, sta->addr,
1067                                        HOSTAPD_MODULE_IEEE80211,
1068                                        HOSTAPD_LEVEL_DEBUG,
1069                                        "Drop repeated authentication frame seq_ctrl=0x%x",
1070                                        seq_ctrl);
1071                         return;
1072                 }
1073 #ifdef CONFIG_MESH
1074                 if ((hapd->conf->mesh & MESH_ENABLED) &&
1075                     sta->plink_state == PLINK_BLOCKED) {
1076                         wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
1077                                    " is blocked - drop Authentication frame",
1078                                    MAC2STR(mgmt->sa));
1079                         return;
1080                 }
1081 #endif /* CONFIG_MESH */
1082         } else {
1083 #ifdef CONFIG_MESH
1084                 if (hapd->conf->mesh & MESH_ENABLED) {
1085                         /* if the mesh peer is not available, we don't do auth.
1086                          */
1087                         wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
1088                                    " not yet known - drop Authentication frame",
1089                                    MAC2STR(mgmt->sa));
1090                         /*
1091                          * Save a copy of the frame so that it can be processed
1092                          * if a new peer entry is added shortly after this.
1093                          */
1094                         wpabuf_free(hapd->mesh_pending_auth);
1095                         hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
1096                         os_get_reltime(&hapd->mesh_pending_auth_time);
1097                         return;
1098                 }
1099 #endif /* CONFIG_MESH */
1100
1101                 sta = ap_sta_add(hapd, mgmt->sa);
1102                 if (!sta) {
1103                         resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1104                         goto fail;
1105                 }
1106         }
1107         sta->last_seq_ctrl = seq_ctrl;
1108         sta->last_subtype = WLAN_FC_STYPE_AUTH;
1109
1110         if (vlan_id.notempty &&
1111             !hostapd_vlan_valid(hapd->conf->vlan, &vlan_id)) {
1112                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1113                                HOSTAPD_LEVEL_INFO,
1114                                "Invalid VLAN %d%s received from RADIUS server",
1115                                vlan_id.untagged,
1116                                vlan_id.tagged[0] ? "+" : "");
1117                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1118                 goto fail;
1119         }
1120         if (ap_sta_set_vlan(hapd, sta, &vlan_id) < 0) {
1121                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1122                 goto fail;
1123         }
1124         if (sta->vlan_id)
1125                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1126                                HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
1127
1128         hostapd_free_psk_list(sta->psk);
1129         if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
1130                 sta->psk = psk;
1131                 psk = NULL;
1132         } else {
1133                 sta->psk = NULL;
1134         }
1135
1136         sta->identity = identity;
1137         identity = NULL;
1138         sta->radius_cui = radius_cui;
1139         radius_cui = NULL;
1140
1141         sta->flags &= ~WLAN_STA_PREAUTH;
1142         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
1143
1144         if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
1145                 sta->acct_interim_interval = acct_interim_interval;
1146         if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
1147                 ap_sta_session_timeout(hapd, sta, session_timeout);
1148         else
1149                 ap_sta_no_session_timeout(hapd, sta);
1150
1151         switch (auth_alg) {
1152         case WLAN_AUTH_OPEN:
1153                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1154                                HOSTAPD_LEVEL_DEBUG,
1155                                "authentication OK (open system)");
1156                 sta->flags |= WLAN_STA_AUTH;
1157                 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1158                 sta->auth_alg = WLAN_AUTH_OPEN;
1159                 mlme_authenticate_indication(hapd, sta);
1160                 break;
1161 #ifndef CONFIG_NO_RC4
1162         case WLAN_AUTH_SHARED_KEY:
1163                 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
1164                                        fc & WLAN_FC_ISWEP);
1165                 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
1166                 mlme_authenticate_indication(hapd, sta);
1167                 if (sta->challenge && auth_transaction == 1) {
1168                         resp_ies[0] = WLAN_EID_CHALLENGE;
1169                         resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
1170                         os_memcpy(resp_ies + 2, sta->challenge,
1171                                   WLAN_AUTH_CHALLENGE_LEN);
1172                         resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
1173                 }
1174                 break;
1175 #endif /* CONFIG_NO_RC4 */
1176 #ifdef CONFIG_IEEE80211R
1177         case WLAN_AUTH_FT:
1178                 sta->auth_alg = WLAN_AUTH_FT;
1179                 if (sta->wpa_sm == NULL)
1180                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1181                                                         sta->addr, NULL);
1182                 if (sta->wpa_sm == NULL) {
1183                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
1184                                    "state machine");
1185                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1186                         goto fail;
1187                 }
1188                 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
1189                                     auth_transaction, mgmt->u.auth.variable,
1190                                     len - IEEE80211_HDRLEN -
1191                                     sizeof(mgmt->u.auth),
1192                                     handle_auth_ft_finish, hapd);
1193                 /* handle_auth_ft_finish() callback will complete auth. */
1194                 return;
1195 #endif /* CONFIG_IEEE80211R */
1196 #ifdef CONFIG_SAE
1197         case WLAN_AUTH_SAE:
1198 #ifdef CONFIG_MESH
1199                 if (status_code == WLAN_STATUS_SUCCESS &&
1200                     hapd->conf->mesh & MESH_ENABLED) {
1201                         if (sta->wpa_sm == NULL)
1202                                 sta->wpa_sm =
1203                                         wpa_auth_sta_init(hapd->wpa_auth,
1204                                                           sta->addr, NULL);
1205                         if (sta->wpa_sm == NULL) {
1206                                 wpa_printf(MSG_DEBUG,
1207                                            "SAE: Failed to initialize WPA state machine");
1208                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1209                                 goto fail;
1210                         }
1211                 }
1212 #endif /* CONFIG_MESH */
1213                 handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
1214                                 status_code);
1215                 return;
1216 #endif /* CONFIG_SAE */
1217         }
1218
1219  fail:
1220         os_free(identity);
1221         os_free(radius_cui);
1222         hostapd_free_psk_list(psk);
1223
1224         send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
1225                         auth_transaction + 1, resp, resp_ies, resp_ies_len);
1226 }
1227
1228
1229 int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
1230 {
1231         int i, j = 32, aid;
1232
1233         /* get a unique AID */
1234         if (sta->aid > 0) {
1235                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
1236                 return 0;
1237         }
1238
1239         for (i = 0; i < AID_WORDS; i++) {
1240                 if (hapd->sta_aid[i] == (u32) -1)
1241                         continue;
1242                 for (j = 0; j < 32; j++) {
1243                         if (!(hapd->sta_aid[i] & BIT(j)))
1244                                 break;
1245                 }
1246                 if (j < 32)
1247                         break;
1248         }
1249         if (j == 32)
1250                 return -1;
1251         aid = i * 32 + j + 1;
1252         if (aid > 2007)
1253                 return -1;
1254
1255         sta->aid = aid;
1256         hapd->sta_aid[i] |= BIT(j);
1257         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
1258         return 0;
1259 }
1260
1261
1262 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
1263                       const u8 *ssid_ie, size_t ssid_ie_len)
1264 {
1265         if (ssid_ie == NULL)
1266                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1267
1268         if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
1269             os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
1270                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1271                                HOSTAPD_LEVEL_INFO,
1272                                "Station tried to associate with unknown SSID "
1273                                "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
1274                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1275         }
1276
1277         return WLAN_STATUS_SUCCESS;
1278 }
1279
1280
1281 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
1282                      const u8 *wmm_ie, size_t wmm_ie_len)
1283 {
1284         sta->flags &= ~WLAN_STA_WMM;
1285         sta->qosinfo = 0;
1286         if (wmm_ie && hapd->conf->wmm_enabled) {
1287                 struct wmm_information_element *wmm;
1288
1289                 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
1290                         hostapd_logger(hapd, sta->addr,
1291                                        HOSTAPD_MODULE_WPA,
1292                                        HOSTAPD_LEVEL_DEBUG,
1293                                        "invalid WMM element in association "
1294                                        "request");
1295                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1296                 }
1297
1298                 sta->flags |= WLAN_STA_WMM;
1299                 wmm = (struct wmm_information_element *) wmm_ie;
1300                 sta->qosinfo = wmm->qos_info;
1301         }
1302         return WLAN_STATUS_SUCCESS;
1303 }
1304
1305
1306 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
1307                            struct ieee802_11_elems *elems)
1308 {
1309         if (!elems->supp_rates) {
1310                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1311                                HOSTAPD_LEVEL_DEBUG,
1312                                "No supported rates element in AssocReq");
1313                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1314         }
1315
1316         if (elems->supp_rates_len + elems->ext_supp_rates_len >
1317             sizeof(sta->supported_rates)) {
1318                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1319                                HOSTAPD_LEVEL_DEBUG,
1320                                "Invalid supported rates element length %d+%d",
1321                                elems->supp_rates_len,
1322                                elems->ext_supp_rates_len);
1323                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1324         }
1325
1326         sta->supported_rates_len = merge_byte_arrays(
1327                 sta->supported_rates, sizeof(sta->supported_rates),
1328                 elems->supp_rates, elems->supp_rates_len,
1329                 elems->ext_supp_rates, elems->ext_supp_rates_len);
1330
1331         return WLAN_STATUS_SUCCESS;
1332 }
1333
1334
1335 static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
1336                            const u8 *ext_capab_ie, size_t ext_capab_ie_len)
1337 {
1338 #ifdef CONFIG_INTERWORKING
1339         /* check for QoS Map support */
1340         if (ext_capab_ie_len >= 5) {
1341                 if (ext_capab_ie[4] & 0x01)
1342                         sta->qos_map_enabled = 1;
1343         }
1344 #endif /* CONFIG_INTERWORKING */
1345
1346         if (ext_capab_ie_len > 0)
1347                 sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
1348
1349         return WLAN_STATUS_SUCCESS;
1350 }
1351
1352
1353 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
1354                            const u8 *ies, size_t ies_len, int reassoc)
1355 {
1356         struct ieee802_11_elems elems;
1357         u16 resp;
1358         const u8 *wpa_ie;
1359         size_t wpa_ie_len;
1360         const u8 *p2p_dev_addr = NULL;
1361
1362         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
1363                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1364                                HOSTAPD_LEVEL_INFO, "Station sent an invalid "
1365                                "association request");
1366                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1367         }
1368
1369         resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
1370         if (resp != WLAN_STATUS_SUCCESS)
1371                 return resp;
1372         resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
1373         if (resp != WLAN_STATUS_SUCCESS)
1374                 return resp;
1375         resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
1376         if (resp != WLAN_STATUS_SUCCESS)
1377                 return resp;
1378         resp = copy_supp_rates(hapd, sta, &elems);
1379         if (resp != WLAN_STATUS_SUCCESS)
1380                 return resp;
1381 #ifdef CONFIG_IEEE80211N
1382         resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
1383         if (resp != WLAN_STATUS_SUCCESS)
1384                 return resp;
1385         if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
1386             !(sta->flags & WLAN_STA_HT)) {
1387                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1388                                HOSTAPD_LEVEL_INFO, "Station does not support "
1389                                "mandatory HT PHY - reject association");
1390                 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
1391         }
1392 #endif /* CONFIG_IEEE80211N */
1393
1394 #ifdef CONFIG_IEEE80211AC
1395         if (hapd->iconf->ieee80211ac) {
1396                 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
1397                 if (resp != WLAN_STATUS_SUCCESS)
1398                         return resp;
1399
1400                 resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
1401                 if (resp != WLAN_STATUS_SUCCESS)
1402                         return resp;
1403         }
1404
1405         if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
1406             !(sta->flags & WLAN_STA_VHT)) {
1407                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1408                                HOSTAPD_LEVEL_INFO, "Station does not support "
1409                                "mandatory VHT PHY - reject association");
1410                 return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
1411         }
1412
1413         if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
1414                 resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
1415                                            elems.vendor_vht_len);
1416                 if (resp != WLAN_STATUS_SUCCESS)
1417                         return resp;
1418         }
1419 #endif /* CONFIG_IEEE80211AC */
1420
1421 #ifdef CONFIG_P2P
1422         if (elems.p2p) {
1423                 wpabuf_free(sta->p2p_ie);
1424                 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1425                                                           P2P_IE_VENDOR_TYPE);
1426                 if (sta->p2p_ie)
1427                         p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
1428         } else {
1429                 wpabuf_free(sta->p2p_ie);
1430                 sta->p2p_ie = NULL;
1431         }
1432 #endif /* CONFIG_P2P */
1433
1434         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1435                 wpa_ie = elems.rsn_ie;
1436                 wpa_ie_len = elems.rsn_ie_len;
1437         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1438                    elems.wpa_ie) {
1439                 wpa_ie = elems.wpa_ie;
1440                 wpa_ie_len = elems.wpa_ie_len;
1441         } else {
1442                 wpa_ie = NULL;
1443                 wpa_ie_len = 0;
1444         }
1445
1446 #ifdef CONFIG_WPS
1447         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
1448         if (hapd->conf->wps_state && elems.wps_ie) {
1449                 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
1450                            "Request - assume WPS is used");
1451                 sta->flags |= WLAN_STA_WPS;
1452                 wpabuf_free(sta->wps_ie);
1453                 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1454                                                           WPS_IE_VENDOR_TYPE);
1455                 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
1456                         wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
1457                         sta->flags |= WLAN_STA_WPS2;
1458                 }
1459                 wpa_ie = NULL;
1460                 wpa_ie_len = 0;
1461                 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
1462                         wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
1463                                    "(Re)Association Request - reject");
1464                         return WLAN_STATUS_INVALID_IE;
1465                 }
1466         } else if (hapd->conf->wps_state && wpa_ie == NULL) {
1467                 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
1468                            "(Re)Association Request - possible WPS use");
1469                 sta->flags |= WLAN_STA_MAYBE_WPS;
1470         } else
1471 #endif /* CONFIG_WPS */
1472         if (hapd->conf->wpa && wpa_ie == NULL) {
1473                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1474                                HOSTAPD_LEVEL_INFO,
1475                                "No WPA/RSN IE in association request");
1476                 return WLAN_STATUS_INVALID_IE;
1477         }
1478
1479         if (hapd->conf->wpa && wpa_ie) {
1480                 int res;
1481                 wpa_ie -= 2;
1482                 wpa_ie_len += 2;
1483                 if (sta->wpa_sm == NULL)
1484                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1485                                                         sta->addr,
1486                                                         p2p_dev_addr);
1487                 if (sta->wpa_sm == NULL) {
1488                         wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1489                                    "state machine");
1490                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1491                 }
1492                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1493                                           wpa_ie, wpa_ie_len,
1494                                           elems.mdie, elems.mdie_len);
1495                 if (res == WPA_INVALID_GROUP)
1496                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1497                 else if (res == WPA_INVALID_PAIRWISE)
1498                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1499                 else if (res == WPA_INVALID_AKMP)
1500                         resp = WLAN_STATUS_AKMP_NOT_VALID;
1501                 else if (res == WPA_ALLOC_FAIL)
1502                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1503 #ifdef CONFIG_IEEE80211W
1504                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1505                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1506                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1507                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1508 #endif /* CONFIG_IEEE80211W */
1509                 else if (res == WPA_INVALID_MDIE)
1510                         resp = WLAN_STATUS_INVALID_MDIE;
1511                 else if (res != WPA_IE_OK)
1512                         resp = WLAN_STATUS_INVALID_IE;
1513                 if (resp != WLAN_STATUS_SUCCESS)
1514                         return resp;
1515 #ifdef CONFIG_IEEE80211W
1516                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1517                     sta->sa_query_count > 0)
1518                         ap_check_sa_query_timeout(hapd, sta);
1519                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1520                     (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
1521                         /*
1522                          * STA has already been associated with MFP and SA
1523                          * Query timeout has not been reached. Reject the
1524                          * association attempt temporarily and start SA Query,
1525                          * if one is not pending.
1526                          */
1527
1528                         if (sta->sa_query_count == 0)
1529                                 ap_sta_start_sa_query(hapd, sta);
1530
1531                         return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
1532                 }
1533
1534                 if (wpa_auth_uses_mfp(sta->wpa_sm))
1535                         sta->flags |= WLAN_STA_MFP;
1536                 else
1537                         sta->flags &= ~WLAN_STA_MFP;
1538 #endif /* CONFIG_IEEE80211W */
1539
1540 #ifdef CONFIG_IEEE80211R
1541                 if (sta->auth_alg == WLAN_AUTH_FT) {
1542                         if (!reassoc) {
1543                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1544                                            "to use association (not "
1545                                            "re-association) with FT auth_alg",
1546                                            MAC2STR(sta->addr));
1547                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1548                         }
1549
1550                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
1551                                                        ies_len);
1552                         if (resp != WLAN_STATUS_SUCCESS)
1553                                 return resp;
1554                 }
1555 #endif /* CONFIG_IEEE80211R */
1556
1557 #ifdef CONFIG_SAE
1558                 if (wpa_auth_uses_sae(sta->wpa_sm) &&
1559                     sta->auth_alg == WLAN_AUTH_OPEN) {
1560                         struct rsn_pmksa_cache_entry *sa;
1561                         sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1562                         if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
1563                                 wpa_printf(MSG_DEBUG,
1564                                            "SAE: No PMKSA cache entry found for "
1565                                            MACSTR, MAC2STR(sta->addr));
1566                                 return WLAN_STATUS_INVALID_PMKID;
1567                         }
1568                         wpa_printf(MSG_DEBUG, "SAE: " MACSTR
1569                                    " using PMKSA caching", MAC2STR(sta->addr));
1570                 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
1571                            sta->auth_alg != WLAN_AUTH_SAE &&
1572                            !(sta->auth_alg == WLAN_AUTH_FT &&
1573                              wpa_auth_uses_ft_sae(sta->wpa_sm))) {
1574                         wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
1575                                    "SAE AKM after non-SAE auth_alg %u",
1576                                    MAC2STR(sta->addr), sta->auth_alg);
1577                         return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1578                 }
1579 #endif /* CONFIG_SAE */
1580
1581 #ifdef CONFIG_IEEE80211N
1582                 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
1583                     wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
1584                         hostapd_logger(hapd, sta->addr,
1585                                        HOSTAPD_MODULE_IEEE80211,
1586                                        HOSTAPD_LEVEL_INFO,
1587                                        "Station tried to use TKIP with HT "
1588                                        "association");
1589                         return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1590                 }
1591 #endif /* CONFIG_IEEE80211N */
1592 #ifdef CONFIG_HS20
1593         } else if (hapd->conf->osen) {
1594                 if (elems.osen == NULL) {
1595                         hostapd_logger(
1596                                 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1597                                 HOSTAPD_LEVEL_INFO,
1598                                 "No HS 2.0 OSEN element in association request");
1599                         return WLAN_STATUS_INVALID_IE;
1600                 }
1601
1602                 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
1603                 if (sta->wpa_sm == NULL)
1604                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1605                                                         sta->addr, NULL);
1606                 if (sta->wpa_sm == NULL) {
1607                         wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1608                                    "state machine");
1609                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1610                 }
1611                 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
1612                                       elems.osen - 2, elems.osen_len + 2) < 0)
1613                         return WLAN_STATUS_INVALID_IE;
1614 #endif /* CONFIG_HS20 */
1615         } else
1616                 wpa_auth_sta_no_wpa(sta->wpa_sm);
1617
1618 #ifdef CONFIG_P2P
1619         p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
1620 #endif /* CONFIG_P2P */
1621
1622 #ifdef CONFIG_HS20
1623         wpabuf_free(sta->hs20_ie);
1624         if (elems.hs20 && elems.hs20_len > 4) {
1625                 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
1626                                                  elems.hs20_len - 4);
1627         } else
1628                 sta->hs20_ie = NULL;
1629 #endif /* CONFIG_HS20 */
1630
1631 #ifdef CONFIG_FST
1632         wpabuf_free(sta->mb_ies);
1633         if (hapd->iface->fst)
1634                 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
1635         else
1636                 sta->mb_ies = NULL;
1637 #endif /* CONFIG_FST */
1638
1639         return WLAN_STATUS_SUCCESS;
1640 }
1641
1642
1643 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
1644                         u16 reason_code)
1645 {
1646         int send_len;
1647         struct ieee80211_mgmt reply;
1648
1649         os_memset(&reply, 0, sizeof(reply));
1650         reply.frame_control =
1651                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
1652         os_memcpy(reply.da, addr, ETH_ALEN);
1653         os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
1654         os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
1655
1656         send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
1657         reply.u.deauth.reason_code = host_to_le16(reason_code);
1658
1659         if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
1660                 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
1661                            strerror(errno));
1662 }
1663
1664
1665 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
1666                             u16 status_code, int reassoc, const u8 *ies,
1667                             size_t ies_len)
1668 {
1669         int send_len;
1670         u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
1671         struct ieee80211_mgmt *reply;
1672         u8 *p;
1673
1674         os_memset(buf, 0, sizeof(buf));
1675         reply = (struct ieee80211_mgmt *) buf;
1676         reply->frame_control =
1677                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1678                              (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1679                               WLAN_FC_STYPE_ASSOC_RESP));
1680         os_memcpy(reply->da, sta->addr, ETH_ALEN);
1681         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1682         os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
1683
1684         send_len = IEEE80211_HDRLEN;
1685         send_len += sizeof(reply->u.assoc_resp);
1686         reply->u.assoc_resp.capab_info =
1687                 host_to_le16(hostapd_own_capab_info(hapd));
1688         reply->u.assoc_resp.status_code = host_to_le16(status_code);
1689         reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
1690         /* Supported rates */
1691         p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1692         /* Extended supported rates */
1693         p = hostapd_eid_ext_supp_rates(hapd, p);
1694
1695 #ifdef CONFIG_IEEE80211R
1696         if (status_code == WLAN_STATUS_SUCCESS) {
1697                 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1698                  * Transition Information, RSN, [RIC Response] */
1699                 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1700                                                 buf + sizeof(buf) - p,
1701                                                 sta->auth_alg, ies, ies_len);
1702         }
1703 #endif /* CONFIG_IEEE80211R */
1704
1705 #ifdef CONFIG_IEEE80211W
1706         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1707                 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1708 #endif /* CONFIG_IEEE80211W */
1709
1710 #ifdef CONFIG_IEEE80211N
1711         p = hostapd_eid_ht_capabilities(hapd, p);
1712         p = hostapd_eid_ht_operation(hapd, p);
1713 #endif /* CONFIG_IEEE80211N */
1714
1715 #ifdef CONFIG_IEEE80211AC
1716         if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
1717                 p = hostapd_eid_vht_capabilities(hapd, p);
1718                 p = hostapd_eid_vht_operation(hapd, p);
1719         }
1720 #endif /* CONFIG_IEEE80211AC */
1721
1722         p = hostapd_eid_ext_capab(hapd, p);
1723         p = hostapd_eid_bss_max_idle_period(hapd, p);
1724         if (sta->qos_map_enabled)
1725                 p = hostapd_eid_qos_map_set(hapd, p);
1726
1727 #ifdef CONFIG_FST
1728         if (hapd->iface->fst_ies) {
1729                 os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
1730                           wpabuf_len(hapd->iface->fst_ies));
1731                 p += wpabuf_len(hapd->iface->fst_ies);
1732         }
1733 #endif /* CONFIG_FST */
1734
1735 #ifdef CONFIG_IEEE80211AC
1736         if (hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
1737                 p = hostapd_eid_vendor_vht(hapd, p);
1738 #endif /* CONFIG_IEEE80211AC */
1739
1740         if (sta->flags & WLAN_STA_WMM)
1741                 p = hostapd_eid_wmm(hapd, p);
1742
1743 #ifdef CONFIG_WPS
1744         if ((sta->flags & WLAN_STA_WPS) ||
1745             ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
1746                 struct wpabuf *wps = wps_build_assoc_resp_ie();
1747                 if (wps) {
1748                         os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
1749                         p += wpabuf_len(wps);
1750                         wpabuf_free(wps);
1751                 }
1752         }
1753 #endif /* CONFIG_WPS */
1754
1755 #ifdef CONFIG_P2P
1756         if (sta->p2p_ie && hapd->p2p_group) {
1757                 struct wpabuf *p2p_resp_ie;
1758                 enum p2p_status_code status;
1759                 switch (status_code) {
1760                 case WLAN_STATUS_SUCCESS:
1761                         status = P2P_SC_SUCCESS;
1762                         break;
1763                 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
1764                         status = P2P_SC_FAIL_LIMIT_REACHED;
1765                         break;
1766                 default:
1767                         status = P2P_SC_FAIL_INVALID_PARAMS;
1768                         break;
1769                 }
1770                 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
1771                 if (p2p_resp_ie) {
1772                         os_memcpy(p, wpabuf_head(p2p_resp_ie),
1773                                   wpabuf_len(p2p_resp_ie));
1774                         p += wpabuf_len(p2p_resp_ie);
1775                         wpabuf_free(p2p_resp_ie);
1776                 }
1777         }
1778 #endif /* CONFIG_P2P */
1779
1780 #ifdef CONFIG_P2P_MANAGER
1781         if (hapd->conf->p2p & P2P_MANAGE)
1782                 p = hostapd_eid_p2p_manage(hapd, p);
1783 #endif /* CONFIG_P2P_MANAGER */
1784
1785         send_len += p - reply->u.assoc_resp.variable;
1786
1787         if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
1788                 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
1789                            strerror(errno));
1790 }
1791
1792
1793 static void handle_assoc(struct hostapd_data *hapd,
1794                          const struct ieee80211_mgmt *mgmt, size_t len,
1795                          int reassoc)
1796 {
1797         u16 capab_info, listen_interval, seq_ctrl, fc;
1798         u16 resp = WLAN_STATUS_SUCCESS;
1799         const u8 *pos;
1800         int left, i;
1801         struct sta_info *sta;
1802
1803         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
1804                                       sizeof(mgmt->u.assoc_req))) {
1805                 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
1806                            reassoc, (unsigned long) len);
1807                 return;
1808         }
1809
1810 #ifdef CONFIG_TESTING_OPTIONS
1811         if (reassoc) {
1812                 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
1813                     drand48() < hapd->iconf->ignore_reassoc_probability) {
1814                         wpa_printf(MSG_INFO,
1815                                    "TESTING: ignoring reassoc request from "
1816                                    MACSTR, MAC2STR(mgmt->sa));
1817                         return;
1818                 }
1819         } else {
1820                 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
1821                     drand48() < hapd->iconf->ignore_assoc_probability) {
1822                         wpa_printf(MSG_INFO,
1823                                    "TESTING: ignoring assoc request from "
1824                                    MACSTR, MAC2STR(mgmt->sa));
1825                         return;
1826                 }
1827         }
1828 #endif /* CONFIG_TESTING_OPTIONS */
1829
1830         fc = le_to_host16(mgmt->frame_control);
1831         seq_ctrl = le_to_host16(mgmt->seq_ctrl);
1832
1833         if (reassoc) {
1834                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
1835                 listen_interval = le_to_host16(
1836                         mgmt->u.reassoc_req.listen_interval);
1837                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
1838                            " capab_info=0x%02x listen_interval=%d current_ap="
1839                            MACSTR " seq_ctrl=0x%x%s",
1840                            MAC2STR(mgmt->sa), capab_info, listen_interval,
1841                            MAC2STR(mgmt->u.reassoc_req.current_ap),
1842                            seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
1843                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
1844                 pos = mgmt->u.reassoc_req.variable;
1845         } else {
1846                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
1847                 listen_interval = le_to_host16(
1848                         mgmt->u.assoc_req.listen_interval);
1849                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
1850                            " capab_info=0x%02x listen_interval=%d "
1851                            "seq_ctrl=0x%x%s",
1852                            MAC2STR(mgmt->sa), capab_info, listen_interval,
1853                            seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
1854                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
1855                 pos = mgmt->u.assoc_req.variable;
1856         }
1857
1858         sta = ap_get_sta(hapd, mgmt->sa);
1859 #ifdef CONFIG_IEEE80211R
1860         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
1861             (sta->flags & WLAN_STA_AUTH) == 0) {
1862                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
1863                            "prior to authentication since it is using "
1864                            "over-the-DS FT", MAC2STR(mgmt->sa));
1865         } else
1866 #endif /* CONFIG_IEEE80211R */
1867         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1868                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1869                                HOSTAPD_LEVEL_INFO, "Station tried to "
1870                                "associate before authentication "
1871                                "(aid=%d flags=0x%x)",
1872                                sta ? sta->aid : -1,
1873                                sta ? sta->flags : 0);
1874                 send_deauth(hapd, mgmt->sa,
1875                             WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
1876                 return;
1877         }
1878
1879         if ((fc & WLAN_FC_RETRY) &&
1880             sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1881             sta->last_seq_ctrl == seq_ctrl &&
1882             sta->last_subtype == reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
1883             WLAN_FC_STYPE_ASSOC_REQ) {
1884                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1885                                HOSTAPD_LEVEL_DEBUG,
1886                                "Drop repeated association frame seq_ctrl=0x%x",
1887                                seq_ctrl);
1888                 return;
1889         }
1890         sta->last_seq_ctrl = seq_ctrl;
1891         sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
1892                 WLAN_FC_STYPE_ASSOC_REQ;
1893
1894         if (hapd->tkip_countermeasures) {
1895                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1896                 goto fail;
1897         }
1898
1899         if (listen_interval > hapd->conf->max_listen_interval) {
1900                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1901                                HOSTAPD_LEVEL_DEBUG,
1902                                "Too large Listen Interval (%d)",
1903                                listen_interval);
1904                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1905                 goto fail;
1906         }
1907
1908         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1909          * is used */
1910         resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1911         if (resp != WLAN_STATUS_SUCCESS)
1912                 goto fail;
1913
1914         if (hostapd_get_aid(hapd, sta) < 0) {
1915                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1916                                HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1917                 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1918                 goto fail;
1919         }
1920
1921         sta->capability = capab_info;
1922         sta->listen_interval = listen_interval;
1923
1924         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1925                 sta->flags |= WLAN_STA_NONERP;
1926         for (i = 0; i < sta->supported_rates_len; i++) {
1927                 if ((sta->supported_rates[i] & 0x7f) > 22) {
1928                         sta->flags &= ~WLAN_STA_NONERP;
1929                         break;
1930                 }
1931         }
1932         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1933                 sta->nonerp_set = 1;
1934                 hapd->iface->num_sta_non_erp++;
1935                 if (hapd->iface->num_sta_non_erp == 1)
1936                         ieee802_11_set_beacons(hapd->iface);
1937         }
1938
1939         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1940             !sta->no_short_slot_time_set) {
1941                 sta->no_short_slot_time_set = 1;
1942                 hapd->iface->num_sta_no_short_slot_time++;
1943                 if (hapd->iface->current_mode->mode ==
1944                     HOSTAPD_MODE_IEEE80211G &&
1945                     hapd->iface->num_sta_no_short_slot_time == 1)
1946                         ieee802_11_set_beacons(hapd->iface);
1947         }
1948
1949         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1950                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1951         else
1952                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1953
1954         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1955             !sta->no_short_preamble_set) {
1956                 sta->no_short_preamble_set = 1;
1957                 hapd->iface->num_sta_no_short_preamble++;
1958                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1959                     && hapd->iface->num_sta_no_short_preamble == 1)
1960                         ieee802_11_set_beacons(hapd->iface);
1961         }
1962
1963 #ifdef CONFIG_IEEE80211N
1964         update_ht_state(hapd, sta);
1965 #endif /* CONFIG_IEEE80211N */
1966
1967         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1968                        HOSTAPD_LEVEL_DEBUG,
1969                        "association OK (aid %d)", sta->aid);
1970         /* Station will be marked associated, after it acknowledges AssocResp
1971          */
1972         sta->flags |= WLAN_STA_ASSOC_REQ_OK;
1973
1974 #ifdef CONFIG_IEEE80211W
1975         if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1976                 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1977                            "SA Query procedure", reassoc ? "re" : "");
1978                 /* TODO: Send a protected Disassociate frame to the STA using
1979                  * the old key and Reason Code "Previous Authentication no
1980                  * longer valid". Make sure this is only sent protected since
1981                  * unprotected frame would be received by the STA that is now
1982                  * trying to associate.
1983                  */
1984         }
1985 #endif /* CONFIG_IEEE80211W */
1986
1987         /* Make sure that the previously registered inactivity timer will not
1988          * remove the STA immediately. */
1989         sta->timeout_next = STA_NULLFUNC;
1990
1991  fail:
1992         send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1993 }
1994
1995
1996 static void handle_disassoc(struct hostapd_data *hapd,
1997                             const struct ieee80211_mgmt *mgmt, size_t len)
1998 {
1999         struct sta_info *sta;
2000
2001         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
2002                 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
2003                            (unsigned long) len);
2004                 return;
2005         }
2006
2007         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
2008                    MAC2STR(mgmt->sa),
2009                    le_to_host16(mgmt->u.disassoc.reason_code));
2010
2011         sta = ap_get_sta(hapd, mgmt->sa);
2012         if (sta == NULL) {
2013                 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
2014                            MAC2STR(mgmt->sa));
2015                 return;
2016         }
2017
2018         ap_sta_set_authorized(hapd, sta, 0);
2019         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
2020         sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
2021         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
2022         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2023                        HOSTAPD_LEVEL_INFO, "disassociated");
2024         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
2025         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
2026         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
2027          * authenticated. */
2028         accounting_sta_stop(hapd, sta);
2029         ieee802_1x_free_station(hapd, sta);
2030         if (sta->ipaddr)
2031                 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
2032         ap_sta_ip6addr_del(hapd, sta);
2033         hostapd_drv_sta_remove(hapd, sta->addr);
2034
2035         if (sta->timeout_next == STA_NULLFUNC ||
2036             sta->timeout_next == STA_DISASSOC) {
2037                 sta->timeout_next = STA_DEAUTH;
2038                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2039                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
2040                                        hapd, sta);
2041         }
2042
2043         mlme_disassociate_indication(
2044                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
2045 }
2046
2047
2048 static void handle_deauth(struct hostapd_data *hapd,
2049                           const struct ieee80211_mgmt *mgmt, size_t len)
2050 {
2051         struct sta_info *sta;
2052
2053         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
2054                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
2055                         "payload (len=%lu)", (unsigned long) len);
2056                 return;
2057         }
2058
2059         wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
2060                 " reason_code=%d",
2061                 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
2062
2063         sta = ap_get_sta(hapd, mgmt->sa);
2064         if (sta == NULL) {
2065                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
2066                         "to deauthenticate, but it is not authenticated",
2067                         MAC2STR(mgmt->sa));
2068                 return;
2069         }
2070
2071         ap_sta_set_authorized(hapd, sta, 0);
2072         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
2073         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
2074                         WLAN_STA_ASSOC_REQ_OK);
2075         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
2076         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2077                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
2078         mlme_deauthenticate_indication(
2079                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
2080         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
2081         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
2082         ap_free_sta(hapd, sta);
2083 }
2084
2085
2086 static void handle_beacon(struct hostapd_data *hapd,
2087                           const struct ieee80211_mgmt *mgmt, size_t len,
2088                           struct hostapd_frame_info *fi)
2089 {
2090         struct ieee802_11_elems elems;
2091
2092         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
2093                 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
2094                            (unsigned long) len);
2095                 return;
2096         }
2097
2098         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
2099                                       len - (IEEE80211_HDRLEN +
2100                                              sizeof(mgmt->u.beacon)), &elems,
2101                                       0);
2102
2103         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
2104 }
2105
2106
2107 #ifdef CONFIG_IEEE80211W
2108
2109 static int hostapd_sa_query_action(struct hostapd_data *hapd,
2110                                    const struct ieee80211_mgmt *mgmt,
2111                                    size_t len)
2112 {
2113         const u8 *end;
2114
2115         end = mgmt->u.action.u.sa_query_resp.trans_id +
2116                 WLAN_SA_QUERY_TR_ID_LEN;
2117         if (((u8 *) mgmt) + len < end) {
2118                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
2119                            "frame (len=%lu)", (unsigned long) len);
2120                 return 0;
2121         }
2122
2123         ieee802_11_sa_query_action(hapd, mgmt->sa,
2124                                    mgmt->u.action.u.sa_query_resp.action,
2125                                    mgmt->u.action.u.sa_query_resp.trans_id);
2126         return 1;
2127 }
2128
2129
2130 static int robust_action_frame(u8 category)
2131 {
2132         return category != WLAN_ACTION_PUBLIC &&
2133                 category != WLAN_ACTION_HT;
2134 }
2135 #endif /* CONFIG_IEEE80211W */
2136
2137
2138 static int handle_action(struct hostapd_data *hapd,
2139                          const struct ieee80211_mgmt *mgmt, size_t len)
2140 {
2141         struct sta_info *sta;
2142         sta = ap_get_sta(hapd, mgmt->sa);
2143
2144         if (len < IEEE80211_HDRLEN + 1) {
2145                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2146                                HOSTAPD_LEVEL_DEBUG,
2147                                "handle_action - too short payload (len=%lu)",
2148                                (unsigned long) len);
2149                 return 0;
2150         }
2151
2152         if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
2153             (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
2154                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
2155                            "frame (category=%u) from unassociated STA " MACSTR,
2156                            MAC2STR(mgmt->sa), mgmt->u.action.category);
2157                 return 0;
2158         }
2159
2160 #ifdef CONFIG_IEEE80211W
2161         if (sta && (sta->flags & WLAN_STA_MFP) &&
2162             !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
2163             robust_action_frame(mgmt->u.action.category)) {
2164                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2165                                HOSTAPD_LEVEL_DEBUG,
2166                                "Dropped unprotected Robust Action frame from "
2167                                "an MFP STA");
2168                 return 0;
2169         }
2170 #endif /* CONFIG_IEEE80211W */
2171
2172         if (sta) {
2173                 u16 fc = le_to_host16(mgmt->frame_control);
2174                 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
2175
2176                 if ((fc & WLAN_FC_RETRY) &&
2177                     sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2178                     sta->last_seq_ctrl == seq_ctrl &&
2179                     sta->last_subtype == WLAN_FC_STYPE_ACTION) {
2180                         hostapd_logger(hapd, sta->addr,
2181                                        HOSTAPD_MODULE_IEEE80211,
2182                                        HOSTAPD_LEVEL_DEBUG,
2183                                        "Drop repeated action frame seq_ctrl=0x%x",
2184                                        seq_ctrl);
2185                         return 1;
2186                 }
2187
2188                 sta->last_seq_ctrl = seq_ctrl;
2189                 sta->last_subtype = WLAN_FC_STYPE_ACTION;
2190         }
2191
2192         switch (mgmt->u.action.category) {
2193 #ifdef CONFIG_IEEE80211R
2194         case WLAN_ACTION_FT:
2195                 if (!sta ||
2196                     wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
2197                                      len - IEEE80211_HDRLEN))
2198                         break;
2199                 return 1;
2200 #endif /* CONFIG_IEEE80211R */
2201         case WLAN_ACTION_WMM:
2202                 hostapd_wmm_action(hapd, mgmt, len);
2203                 return 1;
2204 #ifdef CONFIG_IEEE80211W
2205         case WLAN_ACTION_SA_QUERY:
2206                 return hostapd_sa_query_action(hapd, mgmt, len);
2207 #endif /* CONFIG_IEEE80211W */
2208 #ifdef CONFIG_WNM
2209         case WLAN_ACTION_WNM:
2210                 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
2211                 return 1;
2212 #endif /* CONFIG_WNM */
2213 #ifdef CONFIG_FST
2214         case WLAN_ACTION_FST:
2215                 if (hapd->iface->fst)
2216                         fst_rx_action(hapd->iface->fst, mgmt, len);
2217                 else
2218                         wpa_printf(MSG_DEBUG,
2219                                    "FST: Ignore FST Action frame - no FST attached");
2220                 return 1;
2221 #endif /* CONFIG_FST */
2222         case WLAN_ACTION_PUBLIC:
2223         case WLAN_ACTION_PROTECTED_DUAL:
2224 #ifdef CONFIG_IEEE80211N
2225                 if (len >= IEEE80211_HDRLEN + 2 &&
2226                     mgmt->u.action.u.public_action.action ==
2227                     WLAN_PA_20_40_BSS_COEX) {
2228                         wpa_printf(MSG_DEBUG,
2229                                    "HT20/40 coex mgmt frame received from STA "
2230                                    MACSTR, MAC2STR(mgmt->sa));
2231                         hostapd_2040_coex_action(hapd, mgmt, len);
2232                 }
2233 #endif /* CONFIG_IEEE80211N */
2234                 if (hapd->public_action_cb) {
2235                         hapd->public_action_cb(hapd->public_action_cb_ctx,
2236                                                (u8 *) mgmt, len,
2237                                                hapd->iface->freq);
2238                 }
2239                 if (hapd->public_action_cb2) {
2240                         hapd->public_action_cb2(hapd->public_action_cb2_ctx,
2241                                                 (u8 *) mgmt, len,
2242                                                 hapd->iface->freq);
2243                 }
2244                 if (hapd->public_action_cb || hapd->public_action_cb2)
2245                         return 1;
2246                 break;
2247         case WLAN_ACTION_VENDOR_SPECIFIC:
2248                 if (hapd->vendor_action_cb) {
2249                         if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
2250                                                    (u8 *) mgmt, len,
2251                                                    hapd->iface->freq) == 0)
2252                                 return 1;
2253                 }
2254                 break;
2255         }
2256
2257         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2258                        HOSTAPD_LEVEL_DEBUG,
2259                        "handle_action - unknown action category %d or invalid "
2260                        "frame",
2261                        mgmt->u.action.category);
2262         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
2263             !(mgmt->sa[0] & 0x01)) {
2264                 struct ieee80211_mgmt *resp;
2265
2266                 /*
2267                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
2268                  * Return the Action frame to the source without change
2269                  * except that MSB of the Category set to 1.
2270                  */
2271                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
2272                            "frame back to sender");
2273                 resp = os_malloc(len);
2274                 if (resp == NULL)
2275                         return 0;
2276                 os_memcpy(resp, mgmt, len);
2277                 os_memcpy(resp->da, resp->sa, ETH_ALEN);
2278                 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
2279                 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
2280                 resp->u.action.category |= 0x80;
2281
2282                 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
2283                         wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
2284                                    "Action frame");
2285                 }
2286                 os_free(resp);
2287         }
2288
2289         return 1;
2290 }
2291
2292
2293 /**
2294  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
2295  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
2296  * sent to)
2297  * @buf: management frame data (starting from IEEE 802.11 header)
2298  * @len: length of frame data in octets
2299  * @fi: meta data about received frame (signal level, etc.)
2300  *
2301  * Process all incoming IEEE 802.11 management frames. This will be called for
2302  * each frame received from the kernel driver through wlan#ap interface. In
2303  * addition, it can be called to re-inserted pending frames (e.g., when using
2304  * external RADIUS server as an MAC ACL).
2305  */
2306 int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
2307                     struct hostapd_frame_info *fi)
2308 {
2309         struct ieee80211_mgmt *mgmt;
2310         int broadcast;
2311         u16 fc, stype;
2312         int ret = 0;
2313
2314         if (len < 24)
2315                 return 0;
2316
2317         mgmt = (struct ieee80211_mgmt *) buf;
2318         fc = le_to_host16(mgmt->frame_control);
2319         stype = WLAN_FC_GET_STYPE(fc);
2320
2321         if (stype == WLAN_FC_STYPE_BEACON) {
2322                 handle_beacon(hapd, mgmt, len, fi);
2323                 return 1;
2324         }
2325
2326         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
2327                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
2328                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
2329
2330         if (!broadcast &&
2331 #ifdef CONFIG_P2P
2332             /* Invitation responses can be sent with the peer MAC as BSSID */
2333             !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
2334               stype == WLAN_FC_STYPE_ACTION) &&
2335 #endif /* CONFIG_P2P */
2336 #ifdef CONFIG_MESH
2337             !(hapd->conf->mesh & MESH_ENABLED) &&
2338 #endif /* CONFIG_MESH */
2339             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
2340                 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
2341                            MAC2STR(mgmt->bssid));
2342                 return 0;
2343         }
2344
2345
2346         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
2347                 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
2348                 return 1;
2349         }
2350
2351         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
2352                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2353                                HOSTAPD_LEVEL_DEBUG,
2354                                "MGMT: DA=" MACSTR " not our address",
2355                                MAC2STR(mgmt->da));
2356                 return 0;
2357         }
2358
2359         if (hapd->iconf->track_sta_max_num)
2360                 sta_track_add(hapd->iface, mgmt->sa);
2361
2362         switch (stype) {
2363         case WLAN_FC_STYPE_AUTH:
2364                 wpa_printf(MSG_DEBUG, "mgmt::auth");
2365                 handle_auth(hapd, mgmt, len);
2366                 ret = 1;
2367                 break;
2368         case WLAN_FC_STYPE_ASSOC_REQ:
2369                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
2370                 handle_assoc(hapd, mgmt, len, 0);
2371                 ret = 1;
2372                 break;
2373         case WLAN_FC_STYPE_REASSOC_REQ:
2374                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
2375                 handle_assoc(hapd, mgmt, len, 1);
2376                 ret = 1;
2377                 break;
2378         case WLAN_FC_STYPE_DISASSOC:
2379                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
2380                 handle_disassoc(hapd, mgmt, len);
2381                 ret = 1;
2382                 break;
2383         case WLAN_FC_STYPE_DEAUTH:
2384                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
2385                 handle_deauth(hapd, mgmt, len);
2386                 ret = 1;
2387                 break;
2388         case WLAN_FC_STYPE_ACTION:
2389                 wpa_printf(MSG_DEBUG, "mgmt::action");
2390                 ret = handle_action(hapd, mgmt, len);
2391                 break;
2392         default:
2393                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2394                                HOSTAPD_LEVEL_DEBUG,
2395                                "unknown mgmt frame subtype %d", stype);
2396                 break;
2397         }
2398
2399         return ret;
2400 }
2401
2402
2403 static void handle_auth_cb(struct hostapd_data *hapd,
2404                            const struct ieee80211_mgmt *mgmt,
2405                            size_t len, int ok)
2406 {
2407         u16 auth_alg, auth_transaction, status_code;
2408         struct sta_info *sta;
2409
2410         if (!ok) {
2411                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2412                                HOSTAPD_LEVEL_NOTICE,
2413                                "did not acknowledge authentication response");
2414                 return;
2415         }
2416
2417         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
2418                 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
2419                            (unsigned long) len);
2420                 return;
2421         }
2422
2423         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2424         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2425         status_code = le_to_host16(mgmt->u.auth.status_code);
2426
2427         sta = ap_get_sta(hapd, mgmt->da);
2428         if (!sta) {
2429                 wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
2430                            MAC2STR(mgmt->da));
2431                 return;
2432         }
2433
2434         if (status_code == WLAN_STATUS_SUCCESS &&
2435             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
2436              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
2437                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2438                                HOSTAPD_LEVEL_INFO, "authenticated");
2439                 sta->flags |= WLAN_STA_AUTH;
2440         }
2441 }
2442
2443
2444 static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
2445                                        struct sta_info *sta,
2446                                        char *ifname_wds)
2447 {
2448         int i;
2449         struct hostapd_ssid *ssid = &hapd->conf->ssid;
2450
2451         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
2452                 return;
2453
2454         for (i = 0; i < 4; i++) {
2455                 if (ssid->wep.key[i] &&
2456                     hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
2457                                         i == ssid->wep.idx, NULL, 0,
2458                                         ssid->wep.key[i], ssid->wep.len[i])) {
2459                         wpa_printf(MSG_WARNING,
2460                                    "Could not set WEP keys for WDS interface; %s",
2461                                    ifname_wds);
2462                         break;
2463                 }
2464         }
2465 }
2466
2467
2468 static void handle_assoc_cb(struct hostapd_data *hapd,
2469                             const struct ieee80211_mgmt *mgmt,
2470                             size_t len, int reassoc, int ok)
2471 {
2472         u16 status;
2473         struct sta_info *sta;
2474         int new_assoc = 1;
2475         struct ieee80211_ht_capabilities ht_cap;
2476         struct ieee80211_vht_capabilities vht_cap;
2477
2478         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
2479                                       sizeof(mgmt->u.assoc_resp))) {
2480                 wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
2481                            reassoc, (unsigned long) len);
2482                 return;
2483         }
2484
2485         sta = ap_get_sta(hapd, mgmt->da);
2486         if (!sta) {
2487                 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
2488                            MAC2STR(mgmt->da));
2489                 return;
2490         }
2491
2492         if (!ok) {
2493                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2494                                HOSTAPD_LEVEL_DEBUG,
2495                                "did not acknowledge association response");
2496                 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
2497                 return;
2498         }
2499
2500         if (reassoc)
2501                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
2502         else
2503                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
2504
2505         if (status != WLAN_STATUS_SUCCESS)
2506                 return;
2507
2508         /* Stop previous accounting session, if one is started, and allocate
2509          * new session id for the new session. */
2510         accounting_sta_stop(hapd, sta);
2511
2512         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2513                        HOSTAPD_LEVEL_INFO,
2514                        "associated (aid %d)",
2515                        sta->aid);
2516
2517         if (sta->flags & WLAN_STA_ASSOC)
2518                 new_assoc = 0;
2519         sta->flags |= WLAN_STA_ASSOC;
2520         sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
2521         if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
2522             sta->auth_alg == WLAN_AUTH_FT) {
2523                 /*
2524                  * Open, static WEP, or FT protocol; no separate authorization
2525                  * step.
2526                  */
2527                 ap_sta_set_authorized(hapd, sta, 1);
2528         }
2529
2530         if (reassoc)
2531                 mlme_reassociate_indication(hapd, sta);
2532         else
2533                 mlme_associate_indication(hapd, sta);
2534
2535 #ifdef CONFIG_IEEE80211W
2536         sta->sa_query_timed_out = 0;
2537 #endif /* CONFIG_IEEE80211W */
2538
2539         /*
2540          * Remove the STA entry in order to make sure the STA PS state gets
2541          * cleared and configuration gets updated in case of reassociation back
2542          * to the same AP.
2543          */
2544         hostapd_drv_sta_remove(hapd, sta->addr);
2545
2546 #ifdef CONFIG_IEEE80211N
2547         if (sta->flags & WLAN_STA_HT)
2548                 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
2549 #endif /* CONFIG_IEEE80211N */
2550 #ifdef CONFIG_IEEE80211AC
2551         if (sta->flags & WLAN_STA_VHT)
2552                 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
2553 #endif /* CONFIG_IEEE80211AC */
2554
2555         if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
2556                             sta->supported_rates, sta->supported_rates_len,
2557                             sta->listen_interval,
2558                             sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
2559                             sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
2560                             sta->flags, sta->qosinfo, sta->vht_opmode)) {
2561                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2562                                HOSTAPD_LEVEL_NOTICE,
2563                                "Could not add STA to kernel driver");
2564
2565                 ap_sta_disconnect(hapd, sta, sta->addr,
2566                                   WLAN_REASON_DISASSOC_AP_BUSY);
2567
2568                 return;
2569         }
2570
2571         if (sta->flags & WLAN_STA_WDS) {
2572                 int ret;
2573                 char ifname_wds[IFNAMSIZ + 1];
2574
2575                 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
2576                                           sta->aid, 1);
2577                 if (!ret)
2578                         hostapd_set_wds_encryption(hapd, sta, ifname_wds);
2579         }
2580
2581         if (sta->eapol_sm == NULL) {
2582                 /*
2583                  * This STA does not use RADIUS server for EAP authentication,
2584                  * so bind it to the selected VLAN interface now, since the
2585                  * interface selection is not going to change anymore.
2586                  */
2587                 if (ap_sta_bind_vlan(hapd, sta) < 0)
2588                         return;
2589         } else if (sta->vlan_id) {
2590                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
2591                 if (ap_sta_bind_vlan(hapd, sta) < 0)
2592                         return;
2593         }
2594
2595         hostapd_set_sta_flags(hapd, sta);
2596
2597         if (sta->auth_alg == WLAN_AUTH_FT)
2598                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
2599         else
2600                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
2601         hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
2602
2603         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
2604 }
2605
2606
2607 static void handle_deauth_cb(struct hostapd_data *hapd,
2608                              const struct ieee80211_mgmt *mgmt,
2609                              size_t len, int ok)
2610 {
2611         struct sta_info *sta;
2612         if (mgmt->da[0] & 0x01)
2613                 return;
2614         sta = ap_get_sta(hapd, mgmt->da);
2615         if (!sta) {
2616                 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2617                            " not found", MAC2STR(mgmt->da));
2618                 return;
2619         }
2620         if (ok)
2621                 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2622                            MAC2STR(sta->addr));
2623         else
2624                 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2625                            "deauth", MAC2STR(sta->addr));
2626
2627         ap_sta_deauth_cb(hapd, sta);
2628 }
2629
2630
2631 static void handle_disassoc_cb(struct hostapd_data *hapd,
2632                                const struct ieee80211_mgmt *mgmt,
2633                                size_t len, int ok)
2634 {
2635         struct sta_info *sta;
2636         if (mgmt->da[0] & 0x01)
2637                 return;
2638         sta = ap_get_sta(hapd, mgmt->da);
2639         if (!sta) {
2640                 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2641                            " not found", MAC2STR(mgmt->da));
2642                 return;
2643         }
2644         if (ok)
2645                 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2646                            MAC2STR(sta->addr));
2647         else
2648                 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2649                            "disassoc", MAC2STR(sta->addr));
2650
2651         ap_sta_disassoc_cb(hapd, sta);
2652 }
2653
2654
2655 /**
2656  * ieee802_11_mgmt_cb - Process management frame TX status callback
2657  * @hapd: hostapd BSS data structure (the BSS from which the management frame
2658  * was sent from)
2659  * @buf: management frame data (starting from IEEE 802.11 header)
2660  * @len: length of frame data in octets
2661  * @stype: management frame subtype from frame control field
2662  * @ok: Whether the frame was ACK'ed
2663  */
2664 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
2665                         u16 stype, int ok)
2666 {
2667         const struct ieee80211_mgmt *mgmt;
2668         mgmt = (const struct ieee80211_mgmt *) buf;
2669
2670 #ifdef CONFIG_TESTING_OPTIONS
2671         if (hapd->ext_mgmt_frame_handling) {
2672                 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2673                         stype, ok);
2674                 return;
2675         }
2676 #endif /* CONFIG_TESTING_OPTIONS */
2677
2678         switch (stype) {
2679         case WLAN_FC_STYPE_AUTH:
2680                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2681                 handle_auth_cb(hapd, mgmt, len, ok);
2682                 break;
2683         case WLAN_FC_STYPE_ASSOC_RESP:
2684                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2685                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
2686                 break;
2687         case WLAN_FC_STYPE_REASSOC_RESP:
2688                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2689                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
2690                 break;
2691         case WLAN_FC_STYPE_PROBE_RESP:
2692                 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
2693                 break;
2694         case WLAN_FC_STYPE_DEAUTH:
2695                 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2696                 handle_deauth_cb(hapd, mgmt, len, ok);
2697                 break;
2698         case WLAN_FC_STYPE_DISASSOC:
2699                 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2700                 handle_disassoc_cb(hapd, mgmt, len, ok);
2701                 break;
2702         case WLAN_FC_STYPE_ACTION:
2703                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
2704                 break;
2705         default:
2706                 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
2707                 break;
2708         }
2709 }
2710
2711
2712 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2713 {
2714         /* TODO */
2715         return 0;
2716 }
2717
2718
2719 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2720                            char *buf, size_t buflen)
2721 {
2722         /* TODO */
2723         return 0;
2724 }
2725
2726
2727 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
2728                        const u8 *buf, size_t len, int ack)
2729 {
2730         struct sta_info *sta;
2731         struct hostapd_iface *iface = hapd->iface;
2732
2733         sta = ap_get_sta(hapd, addr);
2734         if (sta == NULL && iface->num_bss > 1) {
2735                 size_t j;
2736                 for (j = 0; j < iface->num_bss; j++) {
2737                         hapd = iface->bss[j];
2738                         sta = ap_get_sta(hapd, addr);
2739                         if (sta)
2740                                 break;
2741                 }
2742         }
2743         if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
2744                 return;
2745         if (sta->flags & WLAN_STA_PENDING_POLL) {
2746                 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
2747                            "activity poll", MAC2STR(sta->addr),
2748                            ack ? "ACKed" : "did not ACK");
2749                 if (ack)
2750                         sta->flags &= ~WLAN_STA_PENDING_POLL;
2751         }
2752
2753         ieee802_1x_tx_status(hapd, sta, buf, len, ack);
2754 }
2755
2756
2757 void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
2758                              const u8 *data, size_t len, int ack)
2759 {
2760         struct sta_info *sta;
2761         struct hostapd_iface *iface = hapd->iface;
2762
2763         sta = ap_get_sta(hapd, dst);
2764         if (sta == NULL && iface->num_bss > 1) {
2765                 size_t j;
2766                 for (j = 0; j < iface->num_bss; j++) {
2767                         hapd = iface->bss[j];
2768                         sta = ap_get_sta(hapd, dst);
2769                         if (sta)
2770                                 break;
2771                 }
2772         }
2773         if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
2774                 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
2775                            MACSTR " that is not currently associated",
2776                            MAC2STR(dst));
2777                 return;
2778         }
2779
2780         ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
2781 }
2782
2783
2784 void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
2785 {
2786         struct sta_info *sta;
2787         struct hostapd_iface *iface = hapd->iface;
2788
2789         sta = ap_get_sta(hapd, addr);
2790         if (sta == NULL && iface->num_bss > 1) {
2791                 size_t j;
2792                 for (j = 0; j < iface->num_bss; j++) {
2793                         hapd = iface->bss[j];
2794                         sta = ap_get_sta(hapd, addr);
2795                         if (sta)
2796                                 break;
2797                 }
2798         }
2799         if (sta == NULL)
2800                 return;
2801         if (!(sta->flags & WLAN_STA_PENDING_POLL))
2802                 return;
2803
2804         wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
2805                    "activity poll", MAC2STR(sta->addr));
2806         sta->flags &= ~WLAN_STA_PENDING_POLL;
2807 }
2808
2809
2810 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
2811                                 int wds)
2812 {
2813         struct sta_info *sta;
2814
2815         sta = ap_get_sta(hapd, src);
2816         if (sta && (sta->flags & WLAN_STA_ASSOC)) {
2817                 if (!hapd->conf->wds_sta)
2818                         return;
2819
2820                 if (wds && !(sta->flags & WLAN_STA_WDS)) {
2821                         int ret;
2822                         char ifname_wds[IFNAMSIZ + 1];
2823
2824                         wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
2825                                    "STA " MACSTR " (aid %u)",
2826                                    MAC2STR(sta->addr), sta->aid);
2827                         sta->flags |= WLAN_STA_WDS;
2828                         ret = hostapd_set_wds_sta(hapd, ifname_wds,
2829                                                   sta->addr, sta->aid, 1);
2830                         if (!ret)
2831                                 hostapd_set_wds_encryption(hapd, sta,
2832                                                            ifname_wds);
2833                 }
2834                 return;
2835         }
2836
2837         wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
2838                    MACSTR, MAC2STR(src));
2839         if (src[0] & 0x01) {
2840                 /* Broadcast bit set in SA?! Ignore the frame silently. */
2841                 return;
2842         }
2843
2844         if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
2845                 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
2846                            "already been sent, but no TX status yet known - "
2847                            "ignore Class 3 frame issue with " MACSTR,
2848                            MAC2STR(src));
2849                 return;
2850         }
2851
2852         if (sta && (sta->flags & WLAN_STA_AUTH))
2853                 hostapd_drv_sta_disassoc(
2854                         hapd, src,
2855                         WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2856         else
2857                 hostapd_drv_sta_deauth(
2858                         hapd, src,
2859                         WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2860 }
2861
2862
2863 #endif /* CONFIG_NATIVE_WINDOWS */