VLAN: Separate station grouping and uplink configuration
[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         struct vlan_description vlan_id;
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         os_memset(&vlan_id, 0, sizeof(vlan_id));
898
899         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
900                 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
901                            (unsigned long) len);
902                 return;
903         }
904
905 #ifdef CONFIG_TESTING_OPTIONS
906         if (hapd->iconf->ignore_auth_probability > 0.0 &&
907             drand48() < hapd->iconf->ignore_auth_probability) {
908                 wpa_printf(MSG_INFO,
909                            "TESTING: ignoring auth frame from " MACSTR,
910                            MAC2STR(mgmt->sa));
911                 return;
912         }
913 #endif /* CONFIG_TESTING_OPTIONS */
914
915         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
916         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
917         status_code = le_to_host16(mgmt->u.auth.status_code);
918         fc = le_to_host16(mgmt->frame_control);
919         seq_ctrl = le_to_host16(mgmt->seq_ctrl);
920
921         if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
922             2 + WLAN_AUTH_CHALLENGE_LEN &&
923             mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
924             mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
925                 challenge = &mgmt->u.auth.variable[2];
926
927         wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
928                    "auth_transaction=%d status_code=%d wep=%d%s "
929                    "seq_ctrl=0x%x%s",
930                    MAC2STR(mgmt->sa), auth_alg, auth_transaction,
931                    status_code, !!(fc & WLAN_FC_ISWEP),
932                    challenge ? " challenge" : "",
933                    seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
934
935 #ifdef CONFIG_NO_RC4
936         if (auth_alg == WLAN_AUTH_SHARED_KEY) {
937                 wpa_printf(MSG_INFO,
938                            "Unsupported authentication algorithm (%d)",
939                            auth_alg);
940                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
941                 goto fail;
942         }
943 #endif /* CONFIG_NO_RC4 */
944
945         if (hapd->tkip_countermeasures) {
946                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
947                 goto fail;
948         }
949
950         if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
951                auth_alg == WLAN_AUTH_OPEN) ||
952 #ifdef CONFIG_IEEE80211R
953               (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
954                auth_alg == WLAN_AUTH_FT) ||
955 #endif /* CONFIG_IEEE80211R */
956 #ifdef CONFIG_SAE
957               (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
958                auth_alg == WLAN_AUTH_SAE) ||
959 #endif /* CONFIG_SAE */
960               ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
961                auth_alg == WLAN_AUTH_SHARED_KEY))) {
962                 wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
963                            auth_alg);
964                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
965                 goto fail;
966         }
967
968         if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
969               (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
970                 wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
971                            auth_transaction);
972                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
973                 goto fail;
974         }
975
976         if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
977                 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
978                            MAC2STR(mgmt->sa));
979                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
980                 goto fail;
981         }
982
983         if (hapd->conf->no_auth_if_seen_on) {
984                 struct hostapd_data *other;
985
986                 other = sta_track_seen_on(hapd->iface, mgmt->sa,
987                                           hapd->conf->no_auth_if_seen_on);
988                 if (other) {
989                         u8 *pos;
990                         u32 info;
991                         u8 op_class, channel, phytype;
992
993                         wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
994                                    MACSTR " since STA has been seen on %s",
995                                    hapd->conf->iface, MAC2STR(mgmt->sa),
996                                    hapd->conf->no_auth_if_seen_on);
997
998                         resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
999                         pos = &resp_ies[0];
1000                         *pos++ = WLAN_EID_NEIGHBOR_REPORT;
1001                         *pos++ = 13;
1002                         os_memcpy(pos, other->own_addr, ETH_ALEN);
1003                         pos += ETH_ALEN;
1004                         info = 0; /* TODO: BSSID Information */
1005                         WPA_PUT_LE32(pos, info);
1006                         pos += 4;
1007                         if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
1008                                 phytype = 8; /* dmg */
1009                         else if (other->iconf->ieee80211ac)
1010                                 phytype = 9; /* vht */
1011                         else if (other->iconf->ieee80211n)
1012                                 phytype = 7; /* ht */
1013                         else if (other->iconf->hw_mode ==
1014                                  HOSTAPD_MODE_IEEE80211A)
1015                                 phytype = 4; /* ofdm */
1016                         else if (other->iconf->hw_mode ==
1017                                  HOSTAPD_MODE_IEEE80211G)
1018                                 phytype = 6; /* erp */
1019                         else
1020                                 phytype = 5; /* hrdsss */
1021                         if (ieee80211_freq_to_channel_ext(
1022                                     hostapd_hw_get_freq(other,
1023                                                         other->iconf->channel),
1024                                     other->iconf->secondary_channel,
1025                                     other->iconf->ieee80211ac,
1026                                     &op_class, &channel) == NUM_HOSTAPD_MODES) {
1027                                 op_class = 0;
1028                                 channel = other->iconf->channel;
1029                         }
1030                         *pos++ = op_class;
1031                         *pos++ = channel;
1032                         *pos++ = phytype;
1033                         resp_ies_len = pos - &resp_ies[0];
1034                         goto fail;
1035                 }
1036         }
1037
1038         res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
1039                                       &session_timeout,
1040                                       &acct_interim_interval, &vlan_id,
1041                                       &psk, &identity, &radius_cui);
1042
1043         if (res == HOSTAPD_ACL_REJECT) {
1044                 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
1045                            MAC2STR(mgmt->sa));
1046                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1047                 goto fail;
1048         }
1049         if (res == HOSTAPD_ACL_PENDING) {
1050                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
1051                            " waiting for an external authentication",
1052                            MAC2STR(mgmt->sa));
1053                 /* Authentication code will re-send the authentication frame
1054                  * after it has received (and cached) information from the
1055                  * external source. */
1056                 return;
1057         }
1058
1059         sta = ap_get_sta(hapd, mgmt->sa);
1060         if (sta) {
1061                 if ((fc & WLAN_FC_RETRY) &&
1062                     sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1063                     sta->last_seq_ctrl == seq_ctrl &&
1064                     sta->last_subtype == WLAN_FC_STYPE_AUTH) {
1065                         hostapd_logger(hapd, sta->addr,
1066                                        HOSTAPD_MODULE_IEEE80211,
1067                                        HOSTAPD_LEVEL_DEBUG,
1068                                        "Drop repeated authentication frame seq_ctrl=0x%x",
1069                                        seq_ctrl);
1070                         return;
1071                 }
1072         } else {
1073 #ifdef CONFIG_MESH
1074                 if (hapd->conf->mesh & MESH_ENABLED) {
1075                         /* if the mesh peer is not available, we don't do auth.
1076                          */
1077                         wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
1078                                    " not yet known - drop Authentiation frame",
1079                                    MAC2STR(mgmt->sa));
1080                         /*
1081                          * Save a copy of the frame so that it can be processed
1082                          * if a new peer entry is added shortly after this.
1083                          */
1084                         wpabuf_free(hapd->mesh_pending_auth);
1085                         hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
1086                         os_get_reltime(&hapd->mesh_pending_auth_time);
1087                         return;
1088                 }
1089 #endif /* CONFIG_MESH */
1090
1091                 sta = ap_sta_add(hapd, mgmt->sa);
1092                 if (!sta) {
1093                         resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1094                         goto fail;
1095                 }
1096         }
1097         sta->last_seq_ctrl = seq_ctrl;
1098         sta->last_subtype = WLAN_FC_STYPE_AUTH;
1099
1100         if (vlan_id.notempty) {
1101                 if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_id)) {
1102                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1103                                        HOSTAPD_LEVEL_INFO,
1104                                        "Invalid VLAN %d received from RADIUS server",
1105                                        vlan_id.untagged);
1106                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1107                         goto fail;
1108                 }
1109                 if (ap_sta_set_vlan(hapd, sta, &vlan_id) < 0) {
1110                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1111                         goto fail;
1112                 }
1113                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1114                                HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
1115         }
1116
1117         hostapd_free_psk_list(sta->psk);
1118         if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
1119                 sta->psk = psk;
1120                 psk = NULL;
1121         } else {
1122                 sta->psk = NULL;
1123         }
1124
1125         sta->identity = identity;
1126         identity = NULL;
1127         sta->radius_cui = radius_cui;
1128         radius_cui = NULL;
1129
1130         sta->flags &= ~WLAN_STA_PREAUTH;
1131         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
1132
1133         if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
1134                 sta->acct_interim_interval = acct_interim_interval;
1135         if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
1136                 ap_sta_session_timeout(hapd, sta, session_timeout);
1137         else
1138                 ap_sta_no_session_timeout(hapd, sta);
1139
1140         switch (auth_alg) {
1141         case WLAN_AUTH_OPEN:
1142                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1143                                HOSTAPD_LEVEL_DEBUG,
1144                                "authentication OK (open system)");
1145                 sta->flags |= WLAN_STA_AUTH;
1146                 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1147                 sta->auth_alg = WLAN_AUTH_OPEN;
1148                 mlme_authenticate_indication(hapd, sta);
1149                 break;
1150 #ifndef CONFIG_NO_RC4
1151         case WLAN_AUTH_SHARED_KEY:
1152                 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
1153                                        fc & WLAN_FC_ISWEP);
1154                 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
1155                 mlme_authenticate_indication(hapd, sta);
1156                 if (sta->challenge && auth_transaction == 1) {
1157                         resp_ies[0] = WLAN_EID_CHALLENGE;
1158                         resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
1159                         os_memcpy(resp_ies + 2, sta->challenge,
1160                                   WLAN_AUTH_CHALLENGE_LEN);
1161                         resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
1162                 }
1163                 break;
1164 #endif /* CONFIG_NO_RC4 */
1165 #ifdef CONFIG_IEEE80211R
1166         case WLAN_AUTH_FT:
1167                 sta->auth_alg = WLAN_AUTH_FT;
1168                 if (sta->wpa_sm == NULL)
1169                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1170                                                         sta->addr, NULL);
1171                 if (sta->wpa_sm == NULL) {
1172                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
1173                                    "state machine");
1174                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1175                         goto fail;
1176                 }
1177                 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
1178                                     auth_transaction, mgmt->u.auth.variable,
1179                                     len - IEEE80211_HDRLEN -
1180                                     sizeof(mgmt->u.auth),
1181                                     handle_auth_ft_finish, hapd);
1182                 /* handle_auth_ft_finish() callback will complete auth. */
1183                 return;
1184 #endif /* CONFIG_IEEE80211R */
1185 #ifdef CONFIG_SAE
1186         case WLAN_AUTH_SAE:
1187 #ifdef CONFIG_MESH
1188                 if (status_code == WLAN_STATUS_SUCCESS &&
1189                     hapd->conf->mesh & MESH_ENABLED) {
1190                         if (sta->wpa_sm == NULL)
1191                                 sta->wpa_sm =
1192                                         wpa_auth_sta_init(hapd->wpa_auth,
1193                                                           sta->addr, NULL);
1194                         if (sta->wpa_sm == NULL) {
1195                                 wpa_printf(MSG_DEBUG,
1196                                            "SAE: Failed to initialize WPA state machine");
1197                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1198                                 goto fail;
1199                         }
1200                 }
1201 #endif /* CONFIG_MESH */
1202                 handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
1203                                 status_code);
1204                 return;
1205 #endif /* CONFIG_SAE */
1206         }
1207
1208  fail:
1209         os_free(identity);
1210         os_free(radius_cui);
1211         hostapd_free_psk_list(psk);
1212
1213         send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
1214                         auth_transaction + 1, resp, resp_ies, resp_ies_len);
1215 }
1216
1217
1218 int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
1219 {
1220         int i, j = 32, aid;
1221
1222         /* get a unique AID */
1223         if (sta->aid > 0) {
1224                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
1225                 return 0;
1226         }
1227
1228         for (i = 0; i < AID_WORDS; i++) {
1229                 if (hapd->sta_aid[i] == (u32) -1)
1230                         continue;
1231                 for (j = 0; j < 32; j++) {
1232                         if (!(hapd->sta_aid[i] & BIT(j)))
1233                                 break;
1234                 }
1235                 if (j < 32)
1236                         break;
1237         }
1238         if (j == 32)
1239                 return -1;
1240         aid = i * 32 + j + 1;
1241         if (aid > 2007)
1242                 return -1;
1243
1244         sta->aid = aid;
1245         hapd->sta_aid[i] |= BIT(j);
1246         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
1247         return 0;
1248 }
1249
1250
1251 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
1252                       const u8 *ssid_ie, size_t ssid_ie_len)
1253 {
1254         if (ssid_ie == NULL)
1255                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1256
1257         if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
1258             os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
1259                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1260                                HOSTAPD_LEVEL_INFO,
1261                                "Station tried to associate with unknown SSID "
1262                                "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
1263                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1264         }
1265
1266         return WLAN_STATUS_SUCCESS;
1267 }
1268
1269
1270 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
1271                      const u8 *wmm_ie, size_t wmm_ie_len)
1272 {
1273         sta->flags &= ~WLAN_STA_WMM;
1274         sta->qosinfo = 0;
1275         if (wmm_ie && hapd->conf->wmm_enabled) {
1276                 struct wmm_information_element *wmm;
1277
1278                 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
1279                         hostapd_logger(hapd, sta->addr,
1280                                        HOSTAPD_MODULE_WPA,
1281                                        HOSTAPD_LEVEL_DEBUG,
1282                                        "invalid WMM element in association "
1283                                        "request");
1284                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1285                 }
1286
1287                 sta->flags |= WLAN_STA_WMM;
1288                 wmm = (struct wmm_information_element *) wmm_ie;
1289                 sta->qosinfo = wmm->qos_info;
1290         }
1291         return WLAN_STATUS_SUCCESS;
1292 }
1293
1294
1295 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
1296                            struct ieee802_11_elems *elems)
1297 {
1298         if (!elems->supp_rates) {
1299                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1300                                HOSTAPD_LEVEL_DEBUG,
1301                                "No supported rates element in AssocReq");
1302                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1303         }
1304
1305         if (elems->supp_rates_len + elems->ext_supp_rates_len >
1306             sizeof(sta->supported_rates)) {
1307                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1308                                HOSTAPD_LEVEL_DEBUG,
1309                                "Invalid supported rates element length %d+%d",
1310                                elems->supp_rates_len,
1311                                elems->ext_supp_rates_len);
1312                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1313         }
1314
1315         sta->supported_rates_len = merge_byte_arrays(
1316                 sta->supported_rates, sizeof(sta->supported_rates),
1317                 elems->supp_rates, elems->supp_rates_len,
1318                 elems->ext_supp_rates, elems->ext_supp_rates_len);
1319
1320         return WLAN_STATUS_SUCCESS;
1321 }
1322
1323
1324 static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
1325                            const u8 *ext_capab_ie, size_t ext_capab_ie_len)
1326 {
1327 #ifdef CONFIG_INTERWORKING
1328         /* check for QoS Map support */
1329         if (ext_capab_ie_len >= 5) {
1330                 if (ext_capab_ie[4] & 0x01)
1331                         sta->qos_map_enabled = 1;
1332         }
1333 #endif /* CONFIG_INTERWORKING */
1334
1335         if (ext_capab_ie_len > 0)
1336                 sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
1337
1338         return WLAN_STATUS_SUCCESS;
1339 }
1340
1341
1342 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
1343                            const u8 *ies, size_t ies_len, int reassoc)
1344 {
1345         struct ieee802_11_elems elems;
1346         u16 resp;
1347         const u8 *wpa_ie;
1348         size_t wpa_ie_len;
1349         const u8 *p2p_dev_addr = NULL;
1350
1351         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
1352                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1353                                HOSTAPD_LEVEL_INFO, "Station sent an invalid "
1354                                "association request");
1355                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1356         }
1357
1358         resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
1359         if (resp != WLAN_STATUS_SUCCESS)
1360                 return resp;
1361         resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
1362         if (resp != WLAN_STATUS_SUCCESS)
1363                 return resp;
1364         resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
1365         if (resp != WLAN_STATUS_SUCCESS)
1366                 return resp;
1367         resp = copy_supp_rates(hapd, sta, &elems);
1368         if (resp != WLAN_STATUS_SUCCESS)
1369                 return resp;
1370 #ifdef CONFIG_IEEE80211N
1371         resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
1372         if (resp != WLAN_STATUS_SUCCESS)
1373                 return resp;
1374         if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
1375             !(sta->flags & WLAN_STA_HT)) {
1376                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1377                                HOSTAPD_LEVEL_INFO, "Station does not support "
1378                                "mandatory HT PHY - reject association");
1379                 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
1380         }
1381 #endif /* CONFIG_IEEE80211N */
1382
1383 #ifdef CONFIG_IEEE80211AC
1384         if (hapd->iconf->ieee80211ac) {
1385                 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
1386                 if (resp != WLAN_STATUS_SUCCESS)
1387                         return resp;
1388
1389                 resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
1390                 if (resp != WLAN_STATUS_SUCCESS)
1391                         return resp;
1392         }
1393
1394         if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
1395             !(sta->flags & WLAN_STA_VHT)) {
1396                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1397                                HOSTAPD_LEVEL_INFO, "Station does not support "
1398                                "mandatory VHT PHY - reject association");
1399                 return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
1400         }
1401
1402         if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
1403                 resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
1404                                            elems.vendor_vht_len);
1405                 if (resp != WLAN_STATUS_SUCCESS)
1406                         return resp;
1407         }
1408 #endif /* CONFIG_IEEE80211AC */
1409
1410 #ifdef CONFIG_P2P
1411         if (elems.p2p) {
1412                 wpabuf_free(sta->p2p_ie);
1413                 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1414                                                           P2P_IE_VENDOR_TYPE);
1415                 if (sta->p2p_ie)
1416                         p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
1417         } else {
1418                 wpabuf_free(sta->p2p_ie);
1419                 sta->p2p_ie = NULL;
1420         }
1421 #endif /* CONFIG_P2P */
1422
1423         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1424                 wpa_ie = elems.rsn_ie;
1425                 wpa_ie_len = elems.rsn_ie_len;
1426         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1427                    elems.wpa_ie) {
1428                 wpa_ie = elems.wpa_ie;
1429                 wpa_ie_len = elems.wpa_ie_len;
1430         } else {
1431                 wpa_ie = NULL;
1432                 wpa_ie_len = 0;
1433         }
1434
1435 #ifdef CONFIG_WPS
1436         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
1437         if (hapd->conf->wps_state && elems.wps_ie) {
1438                 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
1439                            "Request - assume WPS is used");
1440                 sta->flags |= WLAN_STA_WPS;
1441                 wpabuf_free(sta->wps_ie);
1442                 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1443                                                           WPS_IE_VENDOR_TYPE);
1444                 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
1445                         wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
1446                         sta->flags |= WLAN_STA_WPS2;
1447                 }
1448                 wpa_ie = NULL;
1449                 wpa_ie_len = 0;
1450                 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
1451                         wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
1452                                    "(Re)Association Request - reject");
1453                         return WLAN_STATUS_INVALID_IE;
1454                 }
1455         } else if (hapd->conf->wps_state && wpa_ie == NULL) {
1456                 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
1457                            "(Re)Association Request - possible WPS use");
1458                 sta->flags |= WLAN_STA_MAYBE_WPS;
1459         } else
1460 #endif /* CONFIG_WPS */
1461         if (hapd->conf->wpa && wpa_ie == NULL) {
1462                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1463                                HOSTAPD_LEVEL_INFO,
1464                                "No WPA/RSN IE in association request");
1465                 return WLAN_STATUS_INVALID_IE;
1466         }
1467
1468         if (hapd->conf->wpa && wpa_ie) {
1469                 int res;
1470                 wpa_ie -= 2;
1471                 wpa_ie_len += 2;
1472                 if (sta->wpa_sm == NULL)
1473                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1474                                                         sta->addr,
1475                                                         p2p_dev_addr);
1476                 if (sta->wpa_sm == NULL) {
1477                         wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1478                                    "state machine");
1479                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1480                 }
1481                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1482                                           wpa_ie, wpa_ie_len,
1483                                           elems.mdie, elems.mdie_len);
1484                 if (res == WPA_INVALID_GROUP)
1485                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1486                 else if (res == WPA_INVALID_PAIRWISE)
1487                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1488                 else if (res == WPA_INVALID_AKMP)
1489                         resp = WLAN_STATUS_AKMP_NOT_VALID;
1490                 else if (res == WPA_ALLOC_FAIL)
1491                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1492 #ifdef CONFIG_IEEE80211W
1493                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1494                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1495                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1496                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1497 #endif /* CONFIG_IEEE80211W */
1498                 else if (res == WPA_INVALID_MDIE)
1499                         resp = WLAN_STATUS_INVALID_MDIE;
1500                 else if (res != WPA_IE_OK)
1501                         resp = WLAN_STATUS_INVALID_IE;
1502                 if (resp != WLAN_STATUS_SUCCESS)
1503                         return resp;
1504 #ifdef CONFIG_IEEE80211W
1505                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1506                     sta->sa_query_count > 0)
1507                         ap_check_sa_query_timeout(hapd, sta);
1508                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1509                     (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
1510                         /*
1511                          * STA has already been associated with MFP and SA
1512                          * Query timeout has not been reached. Reject the
1513                          * association attempt temporarily and start SA Query,
1514                          * if one is not pending.
1515                          */
1516
1517                         if (sta->sa_query_count == 0)
1518                                 ap_sta_start_sa_query(hapd, sta);
1519
1520                         return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
1521                 }
1522
1523                 if (wpa_auth_uses_mfp(sta->wpa_sm))
1524                         sta->flags |= WLAN_STA_MFP;
1525                 else
1526                         sta->flags &= ~WLAN_STA_MFP;
1527 #endif /* CONFIG_IEEE80211W */
1528
1529 #ifdef CONFIG_IEEE80211R
1530                 if (sta->auth_alg == WLAN_AUTH_FT) {
1531                         if (!reassoc) {
1532                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1533                                            "to use association (not "
1534                                            "re-association) with FT auth_alg",
1535                                            MAC2STR(sta->addr));
1536                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1537                         }
1538
1539                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
1540                                                        ies_len);
1541                         if (resp != WLAN_STATUS_SUCCESS)
1542                                 return resp;
1543                 }
1544 #endif /* CONFIG_IEEE80211R */
1545
1546 #ifdef CONFIG_SAE
1547                 if (wpa_auth_uses_sae(sta->wpa_sm) &&
1548                     sta->auth_alg == WLAN_AUTH_OPEN) {
1549                         struct rsn_pmksa_cache_entry *sa;
1550                         sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1551                         if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
1552                                 wpa_printf(MSG_DEBUG,
1553                                            "SAE: No PMKSA cache entry found for "
1554                                            MACSTR, MAC2STR(sta->addr));
1555                                 return WLAN_STATUS_INVALID_PMKID;
1556                         }
1557                         wpa_printf(MSG_DEBUG, "SAE: " MACSTR
1558                                    " using PMKSA caching", MAC2STR(sta->addr));
1559                 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
1560                            sta->auth_alg != WLAN_AUTH_SAE &&
1561                            !(sta->auth_alg == WLAN_AUTH_FT &&
1562                              wpa_auth_uses_ft_sae(sta->wpa_sm))) {
1563                         wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
1564                                    "SAE AKM after non-SAE auth_alg %u",
1565                                    MAC2STR(sta->addr), sta->auth_alg);
1566                         return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1567                 }
1568 #endif /* CONFIG_SAE */
1569
1570 #ifdef CONFIG_IEEE80211N
1571                 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
1572                     wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
1573                         hostapd_logger(hapd, sta->addr,
1574                                        HOSTAPD_MODULE_IEEE80211,
1575                                        HOSTAPD_LEVEL_INFO,
1576                                        "Station tried to use TKIP with HT "
1577                                        "association");
1578                         return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1579                 }
1580 #endif /* CONFIG_IEEE80211N */
1581 #ifdef CONFIG_HS20
1582         } else if (hapd->conf->osen) {
1583                 if (elems.osen == NULL) {
1584                         hostapd_logger(
1585                                 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1586                                 HOSTAPD_LEVEL_INFO,
1587                                 "No HS 2.0 OSEN element in association request");
1588                         return WLAN_STATUS_INVALID_IE;
1589                 }
1590
1591                 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
1592                 if (sta->wpa_sm == NULL)
1593                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1594                                                         sta->addr, NULL);
1595                 if (sta->wpa_sm == NULL) {
1596                         wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1597                                    "state machine");
1598                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1599                 }
1600                 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
1601                                       elems.osen - 2, elems.osen_len + 2) < 0)
1602                         return WLAN_STATUS_INVALID_IE;
1603 #endif /* CONFIG_HS20 */
1604         } else
1605                 wpa_auth_sta_no_wpa(sta->wpa_sm);
1606
1607 #ifdef CONFIG_P2P
1608         p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
1609 #endif /* CONFIG_P2P */
1610
1611 #ifdef CONFIG_HS20
1612         wpabuf_free(sta->hs20_ie);
1613         if (elems.hs20 && elems.hs20_len > 4) {
1614                 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
1615                                                  elems.hs20_len - 4);
1616         } else
1617                 sta->hs20_ie = NULL;
1618 #endif /* CONFIG_HS20 */
1619
1620 #ifdef CONFIG_FST
1621         wpabuf_free(sta->mb_ies);
1622         if (hapd->iface->fst)
1623                 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
1624         else
1625                 sta->mb_ies = NULL;
1626 #endif /* CONFIG_FST */
1627
1628         return WLAN_STATUS_SUCCESS;
1629 }
1630
1631
1632 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
1633                         u16 reason_code)
1634 {
1635         int send_len;
1636         struct ieee80211_mgmt reply;
1637
1638         os_memset(&reply, 0, sizeof(reply));
1639         reply.frame_control =
1640                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
1641         os_memcpy(reply.da, addr, ETH_ALEN);
1642         os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
1643         os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
1644
1645         send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
1646         reply.u.deauth.reason_code = host_to_le16(reason_code);
1647
1648         if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
1649                 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
1650                            strerror(errno));
1651 }
1652
1653
1654 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
1655                             u16 status_code, int reassoc, const u8 *ies,
1656                             size_t ies_len)
1657 {
1658         int send_len;
1659         u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
1660         struct ieee80211_mgmt *reply;
1661         u8 *p;
1662
1663         os_memset(buf, 0, sizeof(buf));
1664         reply = (struct ieee80211_mgmt *) buf;
1665         reply->frame_control =
1666                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1667                              (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1668                               WLAN_FC_STYPE_ASSOC_RESP));
1669         os_memcpy(reply->da, sta->addr, ETH_ALEN);
1670         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1671         os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
1672
1673         send_len = IEEE80211_HDRLEN;
1674         send_len += sizeof(reply->u.assoc_resp);
1675         reply->u.assoc_resp.capab_info =
1676                 host_to_le16(hostapd_own_capab_info(hapd));
1677         reply->u.assoc_resp.status_code = host_to_le16(status_code);
1678         reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
1679         /* Supported rates */
1680         p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1681         /* Extended supported rates */
1682         p = hostapd_eid_ext_supp_rates(hapd, p);
1683
1684 #ifdef CONFIG_IEEE80211R
1685         if (status_code == WLAN_STATUS_SUCCESS) {
1686                 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1687                  * Transition Information, RSN, [RIC Response] */
1688                 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1689                                                 buf + sizeof(buf) - p,
1690                                                 sta->auth_alg, ies, ies_len);
1691         }
1692 #endif /* CONFIG_IEEE80211R */
1693
1694 #ifdef CONFIG_IEEE80211W
1695         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1696                 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1697 #endif /* CONFIG_IEEE80211W */
1698
1699 #ifdef CONFIG_IEEE80211N
1700         p = hostapd_eid_ht_capabilities(hapd, p);
1701         p = hostapd_eid_ht_operation(hapd, p);
1702 #endif /* CONFIG_IEEE80211N */
1703
1704 #ifdef CONFIG_IEEE80211AC
1705         if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
1706                 p = hostapd_eid_vht_capabilities(hapd, p);
1707                 p = hostapd_eid_vht_operation(hapd, p);
1708         }
1709 #endif /* CONFIG_IEEE80211AC */
1710
1711         p = hostapd_eid_ext_capab(hapd, p);
1712         p = hostapd_eid_bss_max_idle_period(hapd, p);
1713         if (sta->qos_map_enabled)
1714                 p = hostapd_eid_qos_map_set(hapd, p);
1715
1716 #ifdef CONFIG_FST
1717         if (hapd->iface->fst_ies) {
1718                 os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
1719                           wpabuf_len(hapd->iface->fst_ies));
1720                 p += wpabuf_len(hapd->iface->fst_ies);
1721         }
1722 #endif /* CONFIG_FST */
1723
1724 #ifdef CONFIG_IEEE80211AC
1725         if (hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
1726                 p = hostapd_eid_vendor_vht(hapd, p);
1727 #endif /* CONFIG_IEEE80211AC */
1728
1729         if (sta->flags & WLAN_STA_WMM)
1730                 p = hostapd_eid_wmm(hapd, p);
1731
1732 #ifdef CONFIG_WPS
1733         if ((sta->flags & WLAN_STA_WPS) ||
1734             ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
1735                 struct wpabuf *wps = wps_build_assoc_resp_ie();
1736                 if (wps) {
1737                         os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
1738                         p += wpabuf_len(wps);
1739                         wpabuf_free(wps);
1740                 }
1741         }
1742 #endif /* CONFIG_WPS */
1743
1744 #ifdef CONFIG_P2P
1745         if (sta->p2p_ie && hapd->p2p_group) {
1746                 struct wpabuf *p2p_resp_ie;
1747                 enum p2p_status_code status;
1748                 switch (status_code) {
1749                 case WLAN_STATUS_SUCCESS:
1750                         status = P2P_SC_SUCCESS;
1751                         break;
1752                 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
1753                         status = P2P_SC_FAIL_LIMIT_REACHED;
1754                         break;
1755                 default:
1756                         status = P2P_SC_FAIL_INVALID_PARAMS;
1757                         break;
1758                 }
1759                 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
1760                 if (p2p_resp_ie) {
1761                         os_memcpy(p, wpabuf_head(p2p_resp_ie),
1762                                   wpabuf_len(p2p_resp_ie));
1763                         p += wpabuf_len(p2p_resp_ie);
1764                         wpabuf_free(p2p_resp_ie);
1765                 }
1766         }
1767 #endif /* CONFIG_P2P */
1768
1769 #ifdef CONFIG_P2P_MANAGER
1770         if (hapd->conf->p2p & P2P_MANAGE)
1771                 p = hostapd_eid_p2p_manage(hapd, p);
1772 #endif /* CONFIG_P2P_MANAGER */
1773
1774         send_len += p - reply->u.assoc_resp.variable;
1775
1776         if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
1777                 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
1778                            strerror(errno));
1779 }
1780
1781
1782 static void handle_assoc(struct hostapd_data *hapd,
1783                          const struct ieee80211_mgmt *mgmt, size_t len,
1784                          int reassoc)
1785 {
1786         u16 capab_info, listen_interval, seq_ctrl, fc;
1787         u16 resp = WLAN_STATUS_SUCCESS;
1788         const u8 *pos;
1789         int left, i;
1790         struct sta_info *sta;
1791
1792         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
1793                                       sizeof(mgmt->u.assoc_req))) {
1794                 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
1795                            reassoc, (unsigned long) len);
1796                 return;
1797         }
1798
1799 #ifdef CONFIG_TESTING_OPTIONS
1800         if (reassoc) {
1801                 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
1802                     drand48() < hapd->iconf->ignore_reassoc_probability) {
1803                         wpa_printf(MSG_INFO,
1804                                    "TESTING: ignoring reassoc request from "
1805                                    MACSTR, MAC2STR(mgmt->sa));
1806                         return;
1807                 }
1808         } else {
1809                 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
1810                     drand48() < hapd->iconf->ignore_assoc_probability) {
1811                         wpa_printf(MSG_INFO,
1812                                    "TESTING: ignoring assoc request from "
1813                                    MACSTR, MAC2STR(mgmt->sa));
1814                         return;
1815                 }
1816         }
1817 #endif /* CONFIG_TESTING_OPTIONS */
1818
1819         fc = le_to_host16(mgmt->frame_control);
1820         seq_ctrl = le_to_host16(mgmt->seq_ctrl);
1821
1822         if (reassoc) {
1823                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
1824                 listen_interval = le_to_host16(
1825                         mgmt->u.reassoc_req.listen_interval);
1826                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
1827                            " capab_info=0x%02x listen_interval=%d current_ap="
1828                            MACSTR " seq_ctrl=0x%x%s",
1829                            MAC2STR(mgmt->sa), capab_info, listen_interval,
1830                            MAC2STR(mgmt->u.reassoc_req.current_ap),
1831                            seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
1832                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
1833                 pos = mgmt->u.reassoc_req.variable;
1834         } else {
1835                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
1836                 listen_interval = le_to_host16(
1837                         mgmt->u.assoc_req.listen_interval);
1838                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
1839                            " capab_info=0x%02x listen_interval=%d "
1840                            "seq_ctrl=0x%x%s",
1841                            MAC2STR(mgmt->sa), capab_info, listen_interval,
1842                            seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
1843                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
1844                 pos = mgmt->u.assoc_req.variable;
1845         }
1846
1847         sta = ap_get_sta(hapd, mgmt->sa);
1848 #ifdef CONFIG_IEEE80211R
1849         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
1850             (sta->flags & WLAN_STA_AUTH) == 0) {
1851                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
1852                            "prior to authentication since it is using "
1853                            "over-the-DS FT", MAC2STR(mgmt->sa));
1854         } else
1855 #endif /* CONFIG_IEEE80211R */
1856         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1857                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1858                                HOSTAPD_LEVEL_INFO, "Station tried to "
1859                                "associate before authentication "
1860                                "(aid=%d flags=0x%x)",
1861                                sta ? sta->aid : -1,
1862                                sta ? sta->flags : 0);
1863                 send_deauth(hapd, mgmt->sa,
1864                             WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
1865                 return;
1866         }
1867
1868         if ((fc & WLAN_FC_RETRY) &&
1869             sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1870             sta->last_seq_ctrl == seq_ctrl &&
1871             sta->last_subtype == reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
1872             WLAN_FC_STYPE_ASSOC_REQ) {
1873                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1874                                HOSTAPD_LEVEL_DEBUG,
1875                                "Drop repeated association frame seq_ctrl=0x%x",
1876                                seq_ctrl);
1877                 return;
1878         }
1879         sta->last_seq_ctrl = seq_ctrl;
1880         sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
1881                 WLAN_FC_STYPE_ASSOC_REQ;
1882
1883         if (hapd->tkip_countermeasures) {
1884                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1885                 goto fail;
1886         }
1887
1888         if (listen_interval > hapd->conf->max_listen_interval) {
1889                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1890                                HOSTAPD_LEVEL_DEBUG,
1891                                "Too large Listen Interval (%d)",
1892                                listen_interval);
1893                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1894                 goto fail;
1895         }
1896
1897         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1898          * is used */
1899         resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1900         if (resp != WLAN_STATUS_SUCCESS)
1901                 goto fail;
1902
1903         if (hostapd_get_aid(hapd, sta) < 0) {
1904                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1905                                HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1906                 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1907                 goto fail;
1908         }
1909
1910         sta->capability = capab_info;
1911         sta->listen_interval = listen_interval;
1912
1913         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1914                 sta->flags |= WLAN_STA_NONERP;
1915         for (i = 0; i < sta->supported_rates_len; i++) {
1916                 if ((sta->supported_rates[i] & 0x7f) > 22) {
1917                         sta->flags &= ~WLAN_STA_NONERP;
1918                         break;
1919                 }
1920         }
1921         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1922                 sta->nonerp_set = 1;
1923                 hapd->iface->num_sta_non_erp++;
1924                 if (hapd->iface->num_sta_non_erp == 1)
1925                         ieee802_11_set_beacons(hapd->iface);
1926         }
1927
1928         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1929             !sta->no_short_slot_time_set) {
1930                 sta->no_short_slot_time_set = 1;
1931                 hapd->iface->num_sta_no_short_slot_time++;
1932                 if (hapd->iface->current_mode->mode ==
1933                     HOSTAPD_MODE_IEEE80211G &&
1934                     hapd->iface->num_sta_no_short_slot_time == 1)
1935                         ieee802_11_set_beacons(hapd->iface);
1936         }
1937
1938         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1939                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1940         else
1941                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1942
1943         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1944             !sta->no_short_preamble_set) {
1945                 sta->no_short_preamble_set = 1;
1946                 hapd->iface->num_sta_no_short_preamble++;
1947                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1948                     && hapd->iface->num_sta_no_short_preamble == 1)
1949                         ieee802_11_set_beacons(hapd->iface);
1950         }
1951
1952 #ifdef CONFIG_IEEE80211N
1953         update_ht_state(hapd, sta);
1954 #endif /* CONFIG_IEEE80211N */
1955
1956         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1957                        HOSTAPD_LEVEL_DEBUG,
1958                        "association OK (aid %d)", sta->aid);
1959         /* Station will be marked associated, after it acknowledges AssocResp
1960          */
1961         sta->flags |= WLAN_STA_ASSOC_REQ_OK;
1962
1963 #ifdef CONFIG_IEEE80211W
1964         if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1965                 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1966                            "SA Query procedure", reassoc ? "re" : "");
1967                 /* TODO: Send a protected Disassociate frame to the STA using
1968                  * the old key and Reason Code "Previous Authentication no
1969                  * longer valid". Make sure this is only sent protected since
1970                  * unprotected frame would be received by the STA that is now
1971                  * trying to associate.
1972                  */
1973         }
1974 #endif /* CONFIG_IEEE80211W */
1975
1976         /* Make sure that the previously registered inactivity timer will not
1977          * remove the STA immediately. */
1978         sta->timeout_next = STA_NULLFUNC;
1979
1980  fail:
1981         send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1982 }
1983
1984
1985 static void handle_disassoc(struct hostapd_data *hapd,
1986                             const struct ieee80211_mgmt *mgmt, size_t len)
1987 {
1988         struct sta_info *sta;
1989
1990         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1991                 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
1992                            (unsigned long) len);
1993                 return;
1994         }
1995
1996         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1997                    MAC2STR(mgmt->sa),
1998                    le_to_host16(mgmt->u.disassoc.reason_code));
1999
2000         sta = ap_get_sta(hapd, mgmt->sa);
2001         if (sta == NULL) {
2002                 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
2003                            MAC2STR(mgmt->sa));
2004                 return;
2005         }
2006
2007         ap_sta_set_authorized(hapd, sta, 0);
2008         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
2009         sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
2010         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
2011         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2012                        HOSTAPD_LEVEL_INFO, "disassociated");
2013         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
2014         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
2015         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
2016          * authenticated. */
2017         accounting_sta_stop(hapd, sta);
2018         ieee802_1x_free_station(hapd, sta);
2019         if (sta->ipaddr)
2020                 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
2021         ap_sta_ip6addr_del(hapd, sta);
2022         hostapd_drv_sta_remove(hapd, sta->addr);
2023
2024         if (sta->timeout_next == STA_NULLFUNC ||
2025             sta->timeout_next == STA_DISASSOC) {
2026                 sta->timeout_next = STA_DEAUTH;
2027                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2028                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
2029                                        hapd, sta);
2030         }
2031
2032         mlme_disassociate_indication(
2033                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
2034 }
2035
2036
2037 static void handle_deauth(struct hostapd_data *hapd,
2038                           const struct ieee80211_mgmt *mgmt, size_t len)
2039 {
2040         struct sta_info *sta;
2041
2042         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
2043                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
2044                         "payload (len=%lu)", (unsigned long) len);
2045                 return;
2046         }
2047
2048         wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
2049                 " reason_code=%d",
2050                 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
2051
2052         sta = ap_get_sta(hapd, mgmt->sa);
2053         if (sta == NULL) {
2054                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
2055                         "to deauthenticate, but it is not authenticated",
2056                         MAC2STR(mgmt->sa));
2057                 return;
2058         }
2059
2060         ap_sta_set_authorized(hapd, sta, 0);
2061         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
2062         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
2063                         WLAN_STA_ASSOC_REQ_OK);
2064         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
2065         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2066                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
2067         mlme_deauthenticate_indication(
2068                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
2069         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
2070         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
2071         ap_free_sta(hapd, sta);
2072 }
2073
2074
2075 static void handle_beacon(struct hostapd_data *hapd,
2076                           const struct ieee80211_mgmt *mgmt, size_t len,
2077                           struct hostapd_frame_info *fi)
2078 {
2079         struct ieee802_11_elems elems;
2080
2081         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
2082                 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
2083                            (unsigned long) len);
2084                 return;
2085         }
2086
2087         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
2088                                       len - (IEEE80211_HDRLEN +
2089                                              sizeof(mgmt->u.beacon)), &elems,
2090                                       0);
2091
2092         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
2093 }
2094
2095
2096 #ifdef CONFIG_IEEE80211W
2097
2098 static int hostapd_sa_query_action(struct hostapd_data *hapd,
2099                                    const struct ieee80211_mgmt *mgmt,
2100                                    size_t len)
2101 {
2102         const u8 *end;
2103
2104         end = mgmt->u.action.u.sa_query_resp.trans_id +
2105                 WLAN_SA_QUERY_TR_ID_LEN;
2106         if (((u8 *) mgmt) + len < end) {
2107                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
2108                            "frame (len=%lu)", (unsigned long) len);
2109                 return 0;
2110         }
2111
2112         ieee802_11_sa_query_action(hapd, mgmt->sa,
2113                                    mgmt->u.action.u.sa_query_resp.action,
2114                                    mgmt->u.action.u.sa_query_resp.trans_id);
2115         return 1;
2116 }
2117
2118
2119 static int robust_action_frame(u8 category)
2120 {
2121         return category != WLAN_ACTION_PUBLIC &&
2122                 category != WLAN_ACTION_HT;
2123 }
2124 #endif /* CONFIG_IEEE80211W */
2125
2126
2127 static int handle_action(struct hostapd_data *hapd,
2128                          const struct ieee80211_mgmt *mgmt, size_t len)
2129 {
2130         struct sta_info *sta;
2131         sta = ap_get_sta(hapd, mgmt->sa);
2132
2133         if (len < IEEE80211_HDRLEN + 1) {
2134                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2135                                HOSTAPD_LEVEL_DEBUG,
2136                                "handle_action - too short payload (len=%lu)",
2137                                (unsigned long) len);
2138                 return 0;
2139         }
2140
2141         if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
2142             (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
2143                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
2144                            "frame (category=%u) from unassociated STA " MACSTR,
2145                            MAC2STR(mgmt->sa), mgmt->u.action.category);
2146                 return 0;
2147         }
2148
2149 #ifdef CONFIG_IEEE80211W
2150         if (sta && (sta->flags & WLAN_STA_MFP) &&
2151             !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
2152             robust_action_frame(mgmt->u.action.category)) {
2153                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2154                                HOSTAPD_LEVEL_DEBUG,
2155                                "Dropped unprotected Robust Action frame from "
2156                                "an MFP STA");
2157                 return 0;
2158         }
2159 #endif /* CONFIG_IEEE80211W */
2160
2161         if (sta) {
2162                 u16 fc = le_to_host16(mgmt->frame_control);
2163                 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
2164
2165                 if ((fc & WLAN_FC_RETRY) &&
2166                     sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2167                     sta->last_seq_ctrl == seq_ctrl &&
2168                     sta->last_subtype == WLAN_FC_STYPE_ACTION) {
2169                         hostapd_logger(hapd, sta->addr,
2170                                        HOSTAPD_MODULE_IEEE80211,
2171                                        HOSTAPD_LEVEL_DEBUG,
2172                                        "Drop repeated action frame seq_ctrl=0x%x",
2173                                        seq_ctrl);
2174                         return 1;
2175                 }
2176
2177                 sta->last_seq_ctrl = seq_ctrl;
2178                 sta->last_subtype = WLAN_FC_STYPE_ACTION;
2179         }
2180
2181         switch (mgmt->u.action.category) {
2182 #ifdef CONFIG_IEEE80211R
2183         case WLAN_ACTION_FT:
2184                 if (!sta ||
2185                     wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
2186                                      len - IEEE80211_HDRLEN))
2187                         break;
2188                 return 1;
2189 #endif /* CONFIG_IEEE80211R */
2190         case WLAN_ACTION_WMM:
2191                 hostapd_wmm_action(hapd, mgmt, len);
2192                 return 1;
2193 #ifdef CONFIG_IEEE80211W
2194         case WLAN_ACTION_SA_QUERY:
2195                 return hostapd_sa_query_action(hapd, mgmt, len);
2196 #endif /* CONFIG_IEEE80211W */
2197 #ifdef CONFIG_WNM
2198         case WLAN_ACTION_WNM:
2199                 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
2200                 return 1;
2201 #endif /* CONFIG_WNM */
2202 #ifdef CONFIG_FST
2203         case WLAN_ACTION_FST:
2204                 if (hapd->iface->fst)
2205                         fst_rx_action(hapd->iface->fst, mgmt, len);
2206                 else
2207                         wpa_printf(MSG_DEBUG,
2208                                    "FST: Ignore FST Action frame - no FST attached");
2209                 return 1;
2210 #endif /* CONFIG_FST */
2211         case WLAN_ACTION_PUBLIC:
2212         case WLAN_ACTION_PROTECTED_DUAL:
2213 #ifdef CONFIG_IEEE80211N
2214                 if (len >= IEEE80211_HDRLEN + 2 &&
2215                     mgmt->u.action.u.public_action.action ==
2216                     WLAN_PA_20_40_BSS_COEX) {
2217                         wpa_printf(MSG_DEBUG,
2218                                    "HT20/40 coex mgmt frame received from STA "
2219                                    MACSTR, MAC2STR(mgmt->sa));
2220                         hostapd_2040_coex_action(hapd, mgmt, len);
2221                 }
2222 #endif /* CONFIG_IEEE80211N */
2223                 if (hapd->public_action_cb) {
2224                         hapd->public_action_cb(hapd->public_action_cb_ctx,
2225                                                (u8 *) mgmt, len,
2226                                                hapd->iface->freq);
2227                 }
2228                 if (hapd->public_action_cb2) {
2229                         hapd->public_action_cb2(hapd->public_action_cb2_ctx,
2230                                                 (u8 *) mgmt, len,
2231                                                 hapd->iface->freq);
2232                 }
2233                 if (hapd->public_action_cb || hapd->public_action_cb2)
2234                         return 1;
2235                 break;
2236         case WLAN_ACTION_VENDOR_SPECIFIC:
2237                 if (hapd->vendor_action_cb) {
2238                         if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
2239                                                    (u8 *) mgmt, len,
2240                                                    hapd->iface->freq) == 0)
2241                                 return 1;
2242                 }
2243                 break;
2244         }
2245
2246         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2247                        HOSTAPD_LEVEL_DEBUG,
2248                        "handle_action - unknown action category %d or invalid "
2249                        "frame",
2250                        mgmt->u.action.category);
2251         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
2252             !(mgmt->sa[0] & 0x01)) {
2253                 struct ieee80211_mgmt *resp;
2254
2255                 /*
2256                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
2257                  * Return the Action frame to the source without change
2258                  * except that MSB of the Category set to 1.
2259                  */
2260                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
2261                            "frame back to sender");
2262                 resp = os_malloc(len);
2263                 if (resp == NULL)
2264                         return 0;
2265                 os_memcpy(resp, mgmt, len);
2266                 os_memcpy(resp->da, resp->sa, ETH_ALEN);
2267                 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
2268                 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
2269                 resp->u.action.category |= 0x80;
2270
2271                 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
2272                         wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
2273                                    "Action frame");
2274                 }
2275                 os_free(resp);
2276         }
2277
2278         return 1;
2279 }
2280
2281
2282 /**
2283  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
2284  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
2285  * sent to)
2286  * @buf: management frame data (starting from IEEE 802.11 header)
2287  * @len: length of frame data in octets
2288  * @fi: meta data about received frame (signal level, etc.)
2289  *
2290  * Process all incoming IEEE 802.11 management frames. This will be called for
2291  * each frame received from the kernel driver through wlan#ap interface. In
2292  * addition, it can be called to re-inserted pending frames (e.g., when using
2293  * external RADIUS server as an MAC ACL).
2294  */
2295 int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
2296                     struct hostapd_frame_info *fi)
2297 {
2298         struct ieee80211_mgmt *mgmt;
2299         int broadcast;
2300         u16 fc, stype;
2301         int ret = 0;
2302
2303         if (len < 24)
2304                 return 0;
2305
2306         mgmt = (struct ieee80211_mgmt *) buf;
2307         fc = le_to_host16(mgmt->frame_control);
2308         stype = WLAN_FC_GET_STYPE(fc);
2309
2310         if (stype == WLAN_FC_STYPE_BEACON) {
2311                 handle_beacon(hapd, mgmt, len, fi);
2312                 return 1;
2313         }
2314
2315         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
2316                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
2317                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
2318
2319         if (!broadcast &&
2320 #ifdef CONFIG_P2P
2321             /* Invitation responses can be sent with the peer MAC as BSSID */
2322             !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
2323               stype == WLAN_FC_STYPE_ACTION) &&
2324 #endif /* CONFIG_P2P */
2325 #ifdef CONFIG_MESH
2326             !(hapd->conf->mesh & MESH_ENABLED) &&
2327 #endif /* CONFIG_MESH */
2328             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
2329                 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
2330                            MAC2STR(mgmt->bssid));
2331                 return 0;
2332         }
2333
2334
2335         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
2336                 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
2337                 return 1;
2338         }
2339
2340         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
2341                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2342                                HOSTAPD_LEVEL_DEBUG,
2343                                "MGMT: DA=" MACSTR " not our address",
2344                                MAC2STR(mgmt->da));
2345                 return 0;
2346         }
2347
2348         if (hapd->iconf->track_sta_max_num)
2349                 sta_track_add(hapd->iface, mgmt->sa);
2350
2351         switch (stype) {
2352         case WLAN_FC_STYPE_AUTH:
2353                 wpa_printf(MSG_DEBUG, "mgmt::auth");
2354                 handle_auth(hapd, mgmt, len);
2355                 ret = 1;
2356                 break;
2357         case WLAN_FC_STYPE_ASSOC_REQ:
2358                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
2359                 handle_assoc(hapd, mgmt, len, 0);
2360                 ret = 1;
2361                 break;
2362         case WLAN_FC_STYPE_REASSOC_REQ:
2363                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
2364                 handle_assoc(hapd, mgmt, len, 1);
2365                 ret = 1;
2366                 break;
2367         case WLAN_FC_STYPE_DISASSOC:
2368                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
2369                 handle_disassoc(hapd, mgmt, len);
2370                 ret = 1;
2371                 break;
2372         case WLAN_FC_STYPE_DEAUTH:
2373                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
2374                 handle_deauth(hapd, mgmt, len);
2375                 ret = 1;
2376                 break;
2377         case WLAN_FC_STYPE_ACTION:
2378                 wpa_printf(MSG_DEBUG, "mgmt::action");
2379                 ret = handle_action(hapd, mgmt, len);
2380                 break;
2381         default:
2382                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2383                                HOSTAPD_LEVEL_DEBUG,
2384                                "unknown mgmt frame subtype %d", stype);
2385                 break;
2386         }
2387
2388         return ret;
2389 }
2390
2391
2392 static void handle_auth_cb(struct hostapd_data *hapd,
2393                            const struct ieee80211_mgmt *mgmt,
2394                            size_t len, int ok)
2395 {
2396         u16 auth_alg, auth_transaction, status_code;
2397         struct sta_info *sta;
2398
2399         if (!ok) {
2400                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2401                                HOSTAPD_LEVEL_NOTICE,
2402                                "did not acknowledge authentication response");
2403                 return;
2404         }
2405
2406         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
2407                 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
2408                            (unsigned long) len);
2409                 return;
2410         }
2411
2412         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2413         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2414         status_code = le_to_host16(mgmt->u.auth.status_code);
2415
2416         sta = ap_get_sta(hapd, mgmt->da);
2417         if (!sta) {
2418                 wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
2419                            MAC2STR(mgmt->da));
2420                 return;
2421         }
2422
2423         if (status_code == WLAN_STATUS_SUCCESS &&
2424             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
2425              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
2426                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2427                                HOSTAPD_LEVEL_INFO, "authenticated");
2428                 sta->flags |= WLAN_STA_AUTH;
2429         }
2430 }
2431
2432
2433 static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
2434                                        struct sta_info *sta,
2435                                        char *ifname_wds)
2436 {
2437         int i;
2438         struct hostapd_ssid *ssid = &hapd->conf->ssid;
2439
2440         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
2441                 return;
2442
2443         for (i = 0; i < 4; i++) {
2444                 if (ssid->wep.key[i] &&
2445                     hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
2446                                         i == ssid->wep.idx, NULL, 0,
2447                                         ssid->wep.key[i], ssid->wep.len[i])) {
2448                         wpa_printf(MSG_WARNING,
2449                                    "Could not set WEP keys for WDS interface; %s",
2450                                    ifname_wds);
2451                         break;
2452                 }
2453         }
2454 }
2455
2456
2457 static void handle_assoc_cb(struct hostapd_data *hapd,
2458                             const struct ieee80211_mgmt *mgmt,
2459                             size_t len, int reassoc, int ok)
2460 {
2461         u16 status;
2462         struct sta_info *sta;
2463         int new_assoc = 1;
2464         struct ieee80211_ht_capabilities ht_cap;
2465         struct ieee80211_vht_capabilities vht_cap;
2466
2467         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
2468                                       sizeof(mgmt->u.assoc_resp))) {
2469                 wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
2470                            reassoc, (unsigned long) len);
2471                 return;
2472         }
2473
2474         sta = ap_get_sta(hapd, mgmt->da);
2475         if (!sta) {
2476                 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
2477                            MAC2STR(mgmt->da));
2478                 return;
2479         }
2480
2481         if (!ok) {
2482                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2483                                HOSTAPD_LEVEL_DEBUG,
2484                                "did not acknowledge association response");
2485                 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
2486                 return;
2487         }
2488
2489         if (reassoc)
2490                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
2491         else
2492                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
2493
2494         if (status != WLAN_STATUS_SUCCESS)
2495                 return;
2496
2497         /* Stop previous accounting session, if one is started, and allocate
2498          * new session id for the new session. */
2499         accounting_sta_stop(hapd, sta);
2500
2501         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2502                        HOSTAPD_LEVEL_INFO,
2503                        "associated (aid %d)",
2504                        sta->aid);
2505
2506         if (sta->flags & WLAN_STA_ASSOC)
2507                 new_assoc = 0;
2508         sta->flags |= WLAN_STA_ASSOC;
2509         sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
2510         if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
2511             sta->auth_alg == WLAN_AUTH_FT) {
2512                 /*
2513                  * Open, static WEP, or FT protocol; no separate authorization
2514                  * step.
2515                  */
2516                 ap_sta_set_authorized(hapd, sta, 1);
2517         }
2518
2519         if (reassoc)
2520                 mlme_reassociate_indication(hapd, sta);
2521         else
2522                 mlme_associate_indication(hapd, sta);
2523
2524 #ifdef CONFIG_IEEE80211W
2525         sta->sa_query_timed_out = 0;
2526 #endif /* CONFIG_IEEE80211W */
2527
2528         /*
2529          * Remove the STA entry in order to make sure the STA PS state gets
2530          * cleared and configuration gets updated in case of reassociation back
2531          * to the same AP.
2532          */
2533         hostapd_drv_sta_remove(hapd, sta->addr);
2534
2535 #ifdef CONFIG_IEEE80211N
2536         if (sta->flags & WLAN_STA_HT)
2537                 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
2538 #endif /* CONFIG_IEEE80211N */
2539 #ifdef CONFIG_IEEE80211AC
2540         if (sta->flags & WLAN_STA_VHT)
2541                 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
2542 #endif /* CONFIG_IEEE80211AC */
2543
2544         if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
2545                             sta->supported_rates, sta->supported_rates_len,
2546                             sta->listen_interval,
2547                             sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
2548                             sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
2549                             sta->flags, sta->qosinfo, sta->vht_opmode)) {
2550                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2551                                HOSTAPD_LEVEL_NOTICE,
2552                                "Could not add STA to kernel driver");
2553
2554                 ap_sta_disconnect(hapd, sta, sta->addr,
2555                                   WLAN_REASON_DISASSOC_AP_BUSY);
2556
2557                 return;
2558         }
2559
2560         if (sta->flags & WLAN_STA_WDS) {
2561                 int ret;
2562                 char ifname_wds[IFNAMSIZ + 1];
2563
2564                 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
2565                                           sta->aid, 1);
2566                 if (!ret)
2567                         hostapd_set_wds_encryption(hapd, sta, ifname_wds);
2568         }
2569
2570         if (sta->eapol_sm == NULL) {
2571                 /*
2572                  * This STA does not use RADIUS server for EAP authentication,
2573                  * so bind it to the selected VLAN interface now, since the
2574                  * interface selection is not going to change anymore.
2575                  */
2576                 if (ap_sta_bind_vlan(hapd, sta) < 0)
2577                         return;
2578         } else if (sta->vlan_id) {
2579                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
2580                 if (ap_sta_bind_vlan(hapd, sta) < 0)
2581                         return;
2582         }
2583
2584         hostapd_set_sta_flags(hapd, sta);
2585
2586         if (sta->auth_alg == WLAN_AUTH_FT)
2587                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
2588         else
2589                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
2590         hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
2591
2592         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
2593 }
2594
2595
2596 static void handle_deauth_cb(struct hostapd_data *hapd,
2597                              const struct ieee80211_mgmt *mgmt,
2598                              size_t len, int ok)
2599 {
2600         struct sta_info *sta;
2601         if (mgmt->da[0] & 0x01)
2602                 return;
2603         sta = ap_get_sta(hapd, mgmt->da);
2604         if (!sta) {
2605                 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2606                            " not found", MAC2STR(mgmt->da));
2607                 return;
2608         }
2609         if (ok)
2610                 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2611                            MAC2STR(sta->addr));
2612         else
2613                 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2614                            "deauth", MAC2STR(sta->addr));
2615
2616         ap_sta_deauth_cb(hapd, sta);
2617 }
2618
2619
2620 static void handle_disassoc_cb(struct hostapd_data *hapd,
2621                                const struct ieee80211_mgmt *mgmt,
2622                                size_t len, int ok)
2623 {
2624         struct sta_info *sta;
2625         if (mgmt->da[0] & 0x01)
2626                 return;
2627         sta = ap_get_sta(hapd, mgmt->da);
2628         if (!sta) {
2629                 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2630                            " not found", MAC2STR(mgmt->da));
2631                 return;
2632         }
2633         if (ok)
2634                 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2635                            MAC2STR(sta->addr));
2636         else
2637                 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2638                            "disassoc", MAC2STR(sta->addr));
2639
2640         ap_sta_disassoc_cb(hapd, sta);
2641 }
2642
2643
2644 /**
2645  * ieee802_11_mgmt_cb - Process management frame TX status callback
2646  * @hapd: hostapd BSS data structure (the BSS from which the management frame
2647  * was sent from)
2648  * @buf: management frame data (starting from IEEE 802.11 header)
2649  * @len: length of frame data in octets
2650  * @stype: management frame subtype from frame control field
2651  * @ok: Whether the frame was ACK'ed
2652  */
2653 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
2654                         u16 stype, int ok)
2655 {
2656         const struct ieee80211_mgmt *mgmt;
2657         mgmt = (const struct ieee80211_mgmt *) buf;
2658
2659 #ifdef CONFIG_TESTING_OPTIONS
2660         if (hapd->ext_mgmt_frame_handling) {
2661                 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2662                         stype, ok);
2663                 return;
2664         }
2665 #endif /* CONFIG_TESTING_OPTIONS */
2666
2667         switch (stype) {
2668         case WLAN_FC_STYPE_AUTH:
2669                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2670                 handle_auth_cb(hapd, mgmt, len, ok);
2671                 break;
2672         case WLAN_FC_STYPE_ASSOC_RESP:
2673                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2674                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
2675                 break;
2676         case WLAN_FC_STYPE_REASSOC_RESP:
2677                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2678                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
2679                 break;
2680         case WLAN_FC_STYPE_PROBE_RESP:
2681                 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
2682                 break;
2683         case WLAN_FC_STYPE_DEAUTH:
2684                 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2685                 handle_deauth_cb(hapd, mgmt, len, ok);
2686                 break;
2687         case WLAN_FC_STYPE_DISASSOC:
2688                 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2689                 handle_disassoc_cb(hapd, mgmt, len, ok);
2690                 break;
2691         case WLAN_FC_STYPE_ACTION:
2692                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
2693                 break;
2694         default:
2695                 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
2696                 break;
2697         }
2698 }
2699
2700
2701 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2702 {
2703         /* TODO */
2704         return 0;
2705 }
2706
2707
2708 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2709                            char *buf, size_t buflen)
2710 {
2711         /* TODO */
2712         return 0;
2713 }
2714
2715
2716 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
2717                        const u8 *buf, size_t len, int ack)
2718 {
2719         struct sta_info *sta;
2720         struct hostapd_iface *iface = hapd->iface;
2721
2722         sta = ap_get_sta(hapd, addr);
2723         if (sta == NULL && iface->num_bss > 1) {
2724                 size_t j;
2725                 for (j = 0; j < iface->num_bss; j++) {
2726                         hapd = iface->bss[j];
2727                         sta = ap_get_sta(hapd, addr);
2728                         if (sta)
2729                                 break;
2730                 }
2731         }
2732         if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
2733                 return;
2734         if (sta->flags & WLAN_STA_PENDING_POLL) {
2735                 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
2736                            "activity poll", MAC2STR(sta->addr),
2737                            ack ? "ACKed" : "did not ACK");
2738                 if (ack)
2739                         sta->flags &= ~WLAN_STA_PENDING_POLL;
2740         }
2741
2742         ieee802_1x_tx_status(hapd, sta, buf, len, ack);
2743 }
2744
2745
2746 void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
2747                              const u8 *data, size_t len, int ack)
2748 {
2749         struct sta_info *sta;
2750         struct hostapd_iface *iface = hapd->iface;
2751
2752         sta = ap_get_sta(hapd, dst);
2753         if (sta == NULL && iface->num_bss > 1) {
2754                 size_t j;
2755                 for (j = 0; j < iface->num_bss; j++) {
2756                         hapd = iface->bss[j];
2757                         sta = ap_get_sta(hapd, dst);
2758                         if (sta)
2759                                 break;
2760                 }
2761         }
2762         if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
2763                 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
2764                            MACSTR " that is not currently associated",
2765                            MAC2STR(dst));
2766                 return;
2767         }
2768
2769         ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
2770 }
2771
2772
2773 void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
2774 {
2775         struct sta_info *sta;
2776         struct hostapd_iface *iface = hapd->iface;
2777
2778         sta = ap_get_sta(hapd, addr);
2779         if (sta == NULL && iface->num_bss > 1) {
2780                 size_t j;
2781                 for (j = 0; j < iface->num_bss; j++) {
2782                         hapd = iface->bss[j];
2783                         sta = ap_get_sta(hapd, addr);
2784                         if (sta)
2785                                 break;
2786                 }
2787         }
2788         if (sta == NULL)
2789                 return;
2790         if (!(sta->flags & WLAN_STA_PENDING_POLL))
2791                 return;
2792
2793         wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
2794                    "activity poll", MAC2STR(sta->addr));
2795         sta->flags &= ~WLAN_STA_PENDING_POLL;
2796 }
2797
2798
2799 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
2800                                 int wds)
2801 {
2802         struct sta_info *sta;
2803
2804         sta = ap_get_sta(hapd, src);
2805         if (sta && (sta->flags & WLAN_STA_ASSOC)) {
2806                 if (!hapd->conf->wds_sta)
2807                         return;
2808
2809                 if (wds && !(sta->flags & WLAN_STA_WDS)) {
2810                         int ret;
2811                         char ifname_wds[IFNAMSIZ + 1];
2812
2813                         wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
2814                                    "STA " MACSTR " (aid %u)",
2815                                    MAC2STR(sta->addr), sta->aid);
2816                         sta->flags |= WLAN_STA_WDS;
2817                         ret = hostapd_set_wds_sta(hapd, ifname_wds,
2818                                                   sta->addr, sta->aid, 1);
2819                         if (!ret)
2820                                 hostapd_set_wds_encryption(hapd, sta,
2821                                                            ifname_wds);
2822                 }
2823                 return;
2824         }
2825
2826         wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
2827                    MACSTR, MAC2STR(src));
2828         if (src[0] & 0x01) {
2829                 /* Broadcast bit set in SA?! Ignore the frame silently. */
2830                 return;
2831         }
2832
2833         if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
2834                 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
2835                            "already been sent, but no TX status yet known - "
2836                            "ignore Class 3 frame issue with " MACSTR,
2837                            MAC2STR(src));
2838                 return;
2839         }
2840
2841         if (sta && (sta->flags & WLAN_STA_AUTH))
2842                 hostapd_drv_sta_disassoc(
2843                         hapd, src,
2844                         WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2845         else
2846                 hostapd_drv_sta_deauth(
2847                         hapd, src,
2848                         WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2849 }
2850
2851
2852 #endif /* CONFIG_NATIVE_WINDOWS */