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