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