f6fca67100f6570cdf2d47f509ea98015d2c4d80
[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, 0, 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         for (i = 0; i < AID_WORDS; i++) {
1389                 if (hapd->sta_aid[i] == (u32) -1)
1390                         continue;
1391                 for (j = 0; j < 32; j++) {
1392                         if (!(hapd->sta_aid[i] & BIT(j)))
1393                                 break;
1394                 }
1395                 if (j < 32)
1396                         break;
1397         }
1398         if (j == 32)
1399                 return -1;
1400         aid = i * 32 + j + 1;
1401         if (aid > 2007)
1402                 return -1;
1403
1404         sta->aid = aid;
1405         hapd->sta_aid[i] |= BIT(j);
1406         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
1407         return 0;
1408 }
1409
1410
1411 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
1412                       const u8 *ssid_ie, size_t ssid_ie_len)
1413 {
1414         if (ssid_ie == NULL)
1415                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1416
1417         if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
1418             os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
1419                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1420                                HOSTAPD_LEVEL_INFO,
1421                                "Station tried to associate with unknown SSID "
1422                                "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
1423                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1424         }
1425
1426         return WLAN_STATUS_SUCCESS;
1427 }
1428
1429
1430 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
1431                      const u8 *wmm_ie, size_t wmm_ie_len)
1432 {
1433         sta->flags &= ~WLAN_STA_WMM;
1434         sta->qosinfo = 0;
1435         if (wmm_ie && hapd->conf->wmm_enabled) {
1436                 struct wmm_information_element *wmm;
1437
1438                 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
1439                         hostapd_logger(hapd, sta->addr,
1440                                        HOSTAPD_MODULE_WPA,
1441                                        HOSTAPD_LEVEL_DEBUG,
1442                                        "invalid WMM element in association "
1443                                        "request");
1444                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1445                 }
1446
1447                 sta->flags |= WLAN_STA_WMM;
1448                 wmm = (struct wmm_information_element *) wmm_ie;
1449                 sta->qosinfo = wmm->qos_info;
1450         }
1451         return WLAN_STATUS_SUCCESS;
1452 }
1453
1454
1455 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
1456                            struct ieee802_11_elems *elems)
1457 {
1458         if (!elems->supp_rates) {
1459                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1460                                HOSTAPD_LEVEL_DEBUG,
1461                                "No supported rates element in AssocReq");
1462                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1463         }
1464
1465         if (elems->supp_rates_len + elems->ext_supp_rates_len >
1466             sizeof(sta->supported_rates)) {
1467                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1468                                HOSTAPD_LEVEL_DEBUG,
1469                                "Invalid supported rates element length %d+%d",
1470                                elems->supp_rates_len,
1471                                elems->ext_supp_rates_len);
1472                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1473         }
1474
1475         sta->supported_rates_len = merge_byte_arrays(
1476                 sta->supported_rates, sizeof(sta->supported_rates),
1477                 elems->supp_rates, elems->supp_rates_len,
1478                 elems->ext_supp_rates, elems->ext_supp_rates_len);
1479
1480         return WLAN_STATUS_SUCCESS;
1481 }
1482
1483
1484 static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
1485                            const u8 *ext_capab_ie, size_t ext_capab_ie_len)
1486 {
1487 #ifdef CONFIG_INTERWORKING
1488         /* check for QoS Map support */
1489         if (ext_capab_ie_len >= 5) {
1490                 if (ext_capab_ie[4] & 0x01)
1491                         sta->qos_map_enabled = 1;
1492         }
1493 #endif /* CONFIG_INTERWORKING */
1494
1495         if (ext_capab_ie_len > 0)
1496                 sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
1497
1498         return WLAN_STATUS_SUCCESS;
1499 }
1500
1501
1502 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
1503                            const u8 *ies, size_t ies_len, int reassoc)
1504 {
1505         struct ieee802_11_elems elems;
1506         u16 resp;
1507         const u8 *wpa_ie;
1508         size_t wpa_ie_len;
1509         const u8 *p2p_dev_addr = NULL;
1510
1511         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
1512                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1513                                HOSTAPD_LEVEL_INFO, "Station sent an invalid "
1514                                "association request");
1515                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1516         }
1517
1518         resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
1519         if (resp != WLAN_STATUS_SUCCESS)
1520                 return resp;
1521         resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
1522         if (resp != WLAN_STATUS_SUCCESS)
1523                 return resp;
1524         resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
1525         if (resp != WLAN_STATUS_SUCCESS)
1526                 return resp;
1527         resp = copy_supp_rates(hapd, sta, &elems);
1528         if (resp != WLAN_STATUS_SUCCESS)
1529                 return resp;
1530 #ifdef CONFIG_IEEE80211N
1531         resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
1532         if (resp != WLAN_STATUS_SUCCESS)
1533                 return resp;
1534         if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
1535             !(sta->flags & WLAN_STA_HT)) {
1536                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1537                                HOSTAPD_LEVEL_INFO, "Station does not support "
1538                                "mandatory HT PHY - reject association");
1539                 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
1540         }
1541 #endif /* CONFIG_IEEE80211N */
1542
1543 #ifdef CONFIG_IEEE80211AC
1544         if (hapd->iconf->ieee80211ac) {
1545                 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
1546                 if (resp != WLAN_STATUS_SUCCESS)
1547                         return resp;
1548
1549                 resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
1550                 if (resp != WLAN_STATUS_SUCCESS)
1551                         return resp;
1552         }
1553
1554         if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
1555             !(sta->flags & WLAN_STA_VHT)) {
1556                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1557                                HOSTAPD_LEVEL_INFO, "Station does not support "
1558                                "mandatory VHT PHY - reject association");
1559                 return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
1560         }
1561
1562         if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
1563                 resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
1564                                            elems.vendor_vht_len);
1565                 if (resp != WLAN_STATUS_SUCCESS)
1566                         return resp;
1567         }
1568 #endif /* CONFIG_IEEE80211AC */
1569
1570 #ifdef CONFIG_P2P
1571         if (elems.p2p) {
1572                 wpabuf_free(sta->p2p_ie);
1573                 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1574                                                           P2P_IE_VENDOR_TYPE);
1575                 if (sta->p2p_ie)
1576                         p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
1577         } else {
1578                 wpabuf_free(sta->p2p_ie);
1579                 sta->p2p_ie = NULL;
1580         }
1581 #endif /* CONFIG_P2P */
1582
1583         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1584                 wpa_ie = elems.rsn_ie;
1585                 wpa_ie_len = elems.rsn_ie_len;
1586         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1587                    elems.wpa_ie) {
1588                 wpa_ie = elems.wpa_ie;
1589                 wpa_ie_len = elems.wpa_ie_len;
1590         } else {
1591                 wpa_ie = NULL;
1592                 wpa_ie_len = 0;
1593         }
1594
1595 #ifdef CONFIG_WPS
1596         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
1597         if (hapd->conf->wps_state && elems.wps_ie) {
1598                 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
1599                            "Request - assume WPS is used");
1600                 sta->flags |= WLAN_STA_WPS;
1601                 wpabuf_free(sta->wps_ie);
1602                 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1603                                                           WPS_IE_VENDOR_TYPE);
1604                 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
1605                         wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
1606                         sta->flags |= WLAN_STA_WPS2;
1607                 }
1608                 wpa_ie = NULL;
1609                 wpa_ie_len = 0;
1610                 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
1611                         wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
1612                                    "(Re)Association Request - reject");
1613                         return WLAN_STATUS_INVALID_IE;
1614                 }
1615         } else if (hapd->conf->wps_state && wpa_ie == NULL) {
1616                 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
1617                            "(Re)Association Request - possible WPS use");
1618                 sta->flags |= WLAN_STA_MAYBE_WPS;
1619         } else
1620 #endif /* CONFIG_WPS */
1621         if (hapd->conf->wpa && wpa_ie == NULL) {
1622                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1623                                HOSTAPD_LEVEL_INFO,
1624                                "No WPA/RSN IE in association request");
1625                 return WLAN_STATUS_INVALID_IE;
1626         }
1627
1628         if (hapd->conf->wpa && wpa_ie) {
1629                 int res;
1630                 wpa_ie -= 2;
1631                 wpa_ie_len += 2;
1632                 if (sta->wpa_sm == NULL)
1633                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1634                                                         sta->addr,
1635                                                         p2p_dev_addr);
1636                 if (sta->wpa_sm == NULL) {
1637                         wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1638                                    "state machine");
1639                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1640                 }
1641                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1642                                           wpa_ie, wpa_ie_len,
1643                                           elems.mdie, elems.mdie_len);
1644                 if (res == WPA_INVALID_GROUP)
1645                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1646                 else if (res == WPA_INVALID_PAIRWISE)
1647                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1648                 else if (res == WPA_INVALID_AKMP)
1649                         resp = WLAN_STATUS_AKMP_NOT_VALID;
1650                 else if (res == WPA_ALLOC_FAIL)
1651                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1652 #ifdef CONFIG_IEEE80211W
1653                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1654                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1655                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1656                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1657 #endif /* CONFIG_IEEE80211W */
1658                 else if (res == WPA_INVALID_MDIE)
1659                         resp = WLAN_STATUS_INVALID_MDIE;
1660                 else if (res != WPA_IE_OK)
1661                         resp = WLAN_STATUS_INVALID_IE;
1662                 if (resp != WLAN_STATUS_SUCCESS)
1663                         return resp;
1664 #ifdef CONFIG_IEEE80211W
1665                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1666                     sta->sa_query_count > 0)
1667                         ap_check_sa_query_timeout(hapd, sta);
1668                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1669                     (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
1670                         /*
1671                          * STA has already been associated with MFP and SA
1672                          * Query timeout has not been reached. Reject the
1673                          * association attempt temporarily and start SA Query,
1674                          * if one is not pending.
1675                          */
1676
1677                         if (sta->sa_query_count == 0)
1678                                 ap_sta_start_sa_query(hapd, sta);
1679
1680                         return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
1681                 }
1682
1683                 if (wpa_auth_uses_mfp(sta->wpa_sm))
1684                         sta->flags |= WLAN_STA_MFP;
1685                 else
1686                         sta->flags &= ~WLAN_STA_MFP;
1687 #endif /* CONFIG_IEEE80211W */
1688
1689 #ifdef CONFIG_IEEE80211R
1690                 if (sta->auth_alg == WLAN_AUTH_FT) {
1691                         if (!reassoc) {
1692                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1693                                            "to use association (not "
1694                                            "re-association) with FT auth_alg",
1695                                            MAC2STR(sta->addr));
1696                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1697                         }
1698
1699                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
1700                                                        ies_len);
1701                         if (resp != WLAN_STATUS_SUCCESS)
1702                                 return resp;
1703                 }
1704 #endif /* CONFIG_IEEE80211R */
1705
1706 #ifdef CONFIG_SAE
1707                 if (wpa_auth_uses_sae(sta->wpa_sm) &&
1708                     sta->auth_alg == WLAN_AUTH_OPEN) {
1709                         struct rsn_pmksa_cache_entry *sa;
1710                         sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1711                         if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
1712                                 wpa_printf(MSG_DEBUG,
1713                                            "SAE: No PMKSA cache entry found for "
1714                                            MACSTR, MAC2STR(sta->addr));
1715                                 return WLAN_STATUS_INVALID_PMKID;
1716                         }
1717                         wpa_printf(MSG_DEBUG, "SAE: " MACSTR
1718                                    " using PMKSA caching", MAC2STR(sta->addr));
1719                 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
1720                            sta->auth_alg != WLAN_AUTH_SAE &&
1721                            !(sta->auth_alg == WLAN_AUTH_FT &&
1722                              wpa_auth_uses_ft_sae(sta->wpa_sm))) {
1723                         wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
1724                                    "SAE AKM after non-SAE auth_alg %u",
1725                                    MAC2STR(sta->addr), sta->auth_alg);
1726                         return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1727                 }
1728 #endif /* CONFIG_SAE */
1729
1730 #ifdef CONFIG_IEEE80211N
1731                 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
1732                     wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
1733                         hostapd_logger(hapd, sta->addr,
1734                                        HOSTAPD_MODULE_IEEE80211,
1735                                        HOSTAPD_LEVEL_INFO,
1736                                        "Station tried to use TKIP with HT "
1737                                        "association");
1738                         return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1739                 }
1740 #endif /* CONFIG_IEEE80211N */
1741 #ifdef CONFIG_HS20
1742         } else if (hapd->conf->osen) {
1743                 if (elems.osen == NULL) {
1744                         hostapd_logger(
1745                                 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1746                                 HOSTAPD_LEVEL_INFO,
1747                                 "No HS 2.0 OSEN element in association request");
1748                         return WLAN_STATUS_INVALID_IE;
1749                 }
1750
1751                 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
1752                 if (sta->wpa_sm == NULL)
1753                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1754                                                         sta->addr, NULL);
1755                 if (sta->wpa_sm == NULL) {
1756                         wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1757                                    "state machine");
1758                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
1759                 }
1760                 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
1761                                       elems.osen - 2, elems.osen_len + 2) < 0)
1762                         return WLAN_STATUS_INVALID_IE;
1763 #endif /* CONFIG_HS20 */
1764         } else
1765                 wpa_auth_sta_no_wpa(sta->wpa_sm);
1766
1767 #ifdef CONFIG_P2P
1768         p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
1769 #endif /* CONFIG_P2P */
1770
1771 #ifdef CONFIG_HS20
1772         wpabuf_free(sta->hs20_ie);
1773         if (elems.hs20 && elems.hs20_len > 4) {
1774                 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
1775                                                  elems.hs20_len - 4);
1776         } else
1777                 sta->hs20_ie = NULL;
1778 #endif /* CONFIG_HS20 */
1779
1780 #ifdef CONFIG_FST
1781         wpabuf_free(sta->mb_ies);
1782         if (hapd->iface->fst)
1783                 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
1784         else
1785                 sta->mb_ies = NULL;
1786 #endif /* CONFIG_FST */
1787
1788 #ifdef CONFIG_MBO
1789         mbo_ap_check_sta_assoc(hapd, sta, &elems);
1790
1791         if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
1792             elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
1793             hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1794                 wpa_printf(MSG_INFO,
1795                            "MBO: Reject WPA2 association without PMF");
1796                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1797         }
1798 #endif /* CONFIG_MBO */
1799
1800         ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
1801                                     elems.supp_op_classes_len);
1802
1803         if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
1804             elems.rrm_enabled &&
1805             elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
1806                 os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
1807                           sizeof(sta->rrm_enabled_capa));
1808
1809         return WLAN_STATUS_SUCCESS;
1810 }
1811
1812
1813 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
1814                         u16 reason_code)
1815 {
1816         int send_len;
1817         struct ieee80211_mgmt reply;
1818
1819         os_memset(&reply, 0, sizeof(reply));
1820         reply.frame_control =
1821                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
1822         os_memcpy(reply.da, addr, ETH_ALEN);
1823         os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
1824         os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
1825
1826         send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
1827         reply.u.deauth.reason_code = host_to_le16(reason_code);
1828
1829         if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
1830                 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
1831                            strerror(errno));
1832 }
1833
1834
1835 static int add_associated_sta(struct hostapd_data *hapd,
1836                               struct sta_info *sta)
1837 {
1838         struct ieee80211_ht_capabilities ht_cap;
1839         struct ieee80211_vht_capabilities vht_cap;
1840
1841         /*
1842          * Remove the STA entry to ensure the STA PS state gets cleared and
1843          * configuration gets updated. This is relevant for cases, such as
1844          * FT-over-the-DS, where a station re-associates back to the same AP but
1845          * skips the authentication flow, or if working with a driver that
1846          * does not support full AP client state.
1847          */
1848         if (!sta->added_unassoc)
1849                 hostapd_drv_sta_remove(hapd, sta->addr);
1850
1851 #ifdef CONFIG_IEEE80211N
1852         if (sta->flags & WLAN_STA_HT)
1853                 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1854 #endif /* CONFIG_IEEE80211N */
1855 #ifdef CONFIG_IEEE80211AC
1856         if (sta->flags & WLAN_STA_VHT)
1857                 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
1858 #endif /* CONFIG_IEEE80211AC */
1859
1860         /*
1861          * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
1862          * will be set when the ACK frame for the (Re)Association Response frame
1863          * is processed (TX status driver event).
1864          */
1865         if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
1866                             sta->supported_rates, sta->supported_rates_len,
1867                             sta->listen_interval,
1868                             sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
1869                             sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
1870                             sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
1871                             sta->vht_opmode, sta->p2p_ie ? 1 : 0,
1872                             sta->added_unassoc)) {
1873                 hostapd_logger(hapd, sta->addr,
1874                                HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
1875                                "Could not %s STA to kernel driver",
1876                                sta->added_unassoc ? "set" : "add");
1877
1878                 if (sta->added_unassoc) {
1879                         hostapd_drv_sta_remove(hapd, sta->addr);
1880                         sta->added_unassoc = 0;
1881                 }
1882
1883                 return -1;
1884         }
1885
1886         sta->added_unassoc = 0;
1887
1888         return 0;
1889 }
1890
1891
1892 static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
1893                            u16 status_code, int reassoc, const u8 *ies,
1894                            size_t ies_len)
1895 {
1896         int send_len;
1897         u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
1898         struct ieee80211_mgmt *reply;
1899         u8 *p;
1900
1901         os_memset(buf, 0, sizeof(buf));
1902         reply = (struct ieee80211_mgmt *) buf;
1903         reply->frame_control =
1904                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1905                              (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1906                               WLAN_FC_STYPE_ASSOC_RESP));
1907         os_memcpy(reply->da, sta->addr, ETH_ALEN);
1908         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1909         os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
1910
1911         send_len = IEEE80211_HDRLEN;
1912         send_len += sizeof(reply->u.assoc_resp);
1913         reply->u.assoc_resp.capab_info =
1914                 host_to_le16(hostapd_own_capab_info(hapd));
1915         reply->u.assoc_resp.status_code = host_to_le16(status_code);
1916         reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
1917         /* Supported rates */
1918         p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1919         /* Extended supported rates */
1920         p = hostapd_eid_ext_supp_rates(hapd, p);
1921
1922 #ifdef CONFIG_IEEE80211R
1923         if (status_code == WLAN_STATUS_SUCCESS) {
1924                 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1925                  * Transition Information, RSN, [RIC Response] */
1926                 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1927                                                 buf + sizeof(buf) - p,
1928                                                 sta->auth_alg, ies, ies_len);
1929         }
1930 #endif /* CONFIG_IEEE80211R */
1931
1932 #ifdef CONFIG_IEEE80211W
1933         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1934                 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1935 #endif /* CONFIG_IEEE80211W */
1936
1937 #ifdef CONFIG_IEEE80211N
1938         p = hostapd_eid_ht_capabilities(hapd, p);
1939         p = hostapd_eid_ht_operation(hapd, p);
1940 #endif /* CONFIG_IEEE80211N */
1941
1942 #ifdef CONFIG_IEEE80211AC
1943         if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
1944                 p = hostapd_eid_vht_capabilities(hapd, p);
1945                 p = hostapd_eid_vht_operation(hapd, p);
1946         }
1947 #endif /* CONFIG_IEEE80211AC */
1948
1949         p = hostapd_eid_ext_capab(hapd, p);
1950         p = hostapd_eid_bss_max_idle_period(hapd, p);
1951         if (sta->qos_map_enabled)
1952                 p = hostapd_eid_qos_map_set(hapd, p);
1953
1954 #ifdef CONFIG_FST
1955         if (hapd->iface->fst_ies) {
1956                 os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
1957                           wpabuf_len(hapd->iface->fst_ies));
1958                 p += wpabuf_len(hapd->iface->fst_ies);
1959         }
1960 #endif /* CONFIG_FST */
1961
1962 #ifdef CONFIG_IEEE80211AC
1963         if (hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
1964                 p = hostapd_eid_vendor_vht(hapd, p);
1965 #endif /* CONFIG_IEEE80211AC */
1966
1967         if (sta->flags & WLAN_STA_WMM)
1968                 p = hostapd_eid_wmm(hapd, p);
1969
1970 #ifdef CONFIG_WPS
1971         if ((sta->flags & WLAN_STA_WPS) ||
1972             ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
1973                 struct wpabuf *wps = wps_build_assoc_resp_ie();
1974                 if (wps) {
1975                         os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
1976                         p += wpabuf_len(wps);
1977                         wpabuf_free(wps);
1978                 }
1979         }
1980 #endif /* CONFIG_WPS */
1981
1982 #ifdef CONFIG_P2P
1983         if (sta->p2p_ie && hapd->p2p_group) {
1984                 struct wpabuf *p2p_resp_ie;
1985                 enum p2p_status_code status;
1986                 switch (status_code) {
1987                 case WLAN_STATUS_SUCCESS:
1988                         status = P2P_SC_SUCCESS;
1989                         break;
1990                 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
1991                         status = P2P_SC_FAIL_LIMIT_REACHED;
1992                         break;
1993                 default:
1994                         status = P2P_SC_FAIL_INVALID_PARAMS;
1995                         break;
1996                 }
1997                 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
1998                 if (p2p_resp_ie) {
1999                         os_memcpy(p, wpabuf_head(p2p_resp_ie),
2000                                   wpabuf_len(p2p_resp_ie));
2001                         p += wpabuf_len(p2p_resp_ie);
2002                         wpabuf_free(p2p_resp_ie);
2003                 }
2004         }
2005 #endif /* CONFIG_P2P */
2006
2007 #ifdef CONFIG_P2P_MANAGER
2008         if (hapd->conf->p2p & P2P_MANAGE)
2009                 p = hostapd_eid_p2p_manage(hapd, p);
2010 #endif /* CONFIG_P2P_MANAGER */
2011
2012         p = hostapd_eid_mbo(hapd, p, buf + sizeof(buf) - p);
2013
2014         if (hapd->conf->assocresp_elements &&
2015             (size_t) (buf + sizeof(buf) - p) >=
2016             wpabuf_len(hapd->conf->assocresp_elements)) {
2017                 os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
2018                           wpabuf_len(hapd->conf->assocresp_elements));
2019                 p += wpabuf_len(hapd->conf->assocresp_elements);
2020         }
2021
2022         send_len += p - reply->u.assoc_resp.variable;
2023
2024         if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0) {
2025                 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
2026                            strerror(errno));
2027                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2028         }
2029
2030         return WLAN_STATUS_SUCCESS;
2031 }
2032
2033
2034 static void handle_assoc(struct hostapd_data *hapd,
2035                          const struct ieee80211_mgmt *mgmt, size_t len,
2036                          int reassoc)
2037 {
2038         u16 capab_info, listen_interval, seq_ctrl, fc;
2039         u16 resp = WLAN_STATUS_SUCCESS, reply_res;
2040         const u8 *pos;
2041         int left, i;
2042         struct sta_info *sta;
2043
2044         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
2045                                       sizeof(mgmt->u.assoc_req))) {
2046                 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
2047                            reassoc, (unsigned long) len);
2048                 return;
2049         }
2050
2051 #ifdef CONFIG_TESTING_OPTIONS
2052         if (reassoc) {
2053                 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
2054                     drand48() < hapd->iconf->ignore_reassoc_probability) {
2055                         wpa_printf(MSG_INFO,
2056                                    "TESTING: ignoring reassoc request from "
2057                                    MACSTR, MAC2STR(mgmt->sa));
2058                         return;
2059                 }
2060         } else {
2061                 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
2062                     drand48() < hapd->iconf->ignore_assoc_probability) {
2063                         wpa_printf(MSG_INFO,
2064                                    "TESTING: ignoring assoc request from "
2065                                    MACSTR, MAC2STR(mgmt->sa));
2066                         return;
2067                 }
2068         }
2069 #endif /* CONFIG_TESTING_OPTIONS */
2070
2071         fc = le_to_host16(mgmt->frame_control);
2072         seq_ctrl = le_to_host16(mgmt->seq_ctrl);
2073
2074         if (reassoc) {
2075                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
2076                 listen_interval = le_to_host16(
2077                         mgmt->u.reassoc_req.listen_interval);
2078                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
2079                            " capab_info=0x%02x listen_interval=%d current_ap="
2080                            MACSTR " seq_ctrl=0x%x%s",
2081                            MAC2STR(mgmt->sa), capab_info, listen_interval,
2082                            MAC2STR(mgmt->u.reassoc_req.current_ap),
2083                            seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
2084                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
2085                 pos = mgmt->u.reassoc_req.variable;
2086         } else {
2087                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
2088                 listen_interval = le_to_host16(
2089                         mgmt->u.assoc_req.listen_interval);
2090                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
2091                            " capab_info=0x%02x listen_interval=%d "
2092                            "seq_ctrl=0x%x%s",
2093                            MAC2STR(mgmt->sa), capab_info, listen_interval,
2094                            seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
2095                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
2096                 pos = mgmt->u.assoc_req.variable;
2097         }
2098
2099         sta = ap_get_sta(hapd, mgmt->sa);
2100 #ifdef CONFIG_IEEE80211R
2101         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
2102             (sta->flags & WLAN_STA_AUTH) == 0) {
2103                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
2104                            "prior to authentication since it is using "
2105                            "over-the-DS FT", MAC2STR(mgmt->sa));
2106
2107                 /*
2108                  * Mark station as authenticated, to avoid adding station
2109                  * entry in the driver as associated and not authenticated
2110                  */
2111                 sta->flags |= WLAN_STA_AUTH;
2112         } else
2113 #endif /* CONFIG_IEEE80211R */
2114         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
2115                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2116                                HOSTAPD_LEVEL_INFO, "Station tried to "
2117                                "associate before authentication "
2118                                "(aid=%d flags=0x%x)",
2119                                sta ? sta->aid : -1,
2120                                sta ? sta->flags : 0);
2121                 send_deauth(hapd, mgmt->sa,
2122                             WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
2123                 return;
2124         }
2125
2126         if ((fc & WLAN_FC_RETRY) &&
2127             sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2128             sta->last_seq_ctrl == seq_ctrl &&
2129             sta->last_subtype == reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
2130             WLAN_FC_STYPE_ASSOC_REQ) {
2131                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2132                                HOSTAPD_LEVEL_DEBUG,
2133                                "Drop repeated association frame seq_ctrl=0x%x",
2134                                seq_ctrl);
2135                 return;
2136         }
2137         sta->last_seq_ctrl = seq_ctrl;
2138         sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
2139                 WLAN_FC_STYPE_ASSOC_REQ;
2140
2141         if (hapd->tkip_countermeasures) {
2142                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
2143                 goto fail;
2144         }
2145
2146         if (listen_interval > hapd->conf->max_listen_interval) {
2147                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2148                                HOSTAPD_LEVEL_DEBUG,
2149                                "Too large Listen Interval (%d)",
2150                                listen_interval);
2151                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
2152                 goto fail;
2153         }
2154
2155 #ifdef CONFIG_MBO
2156         if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
2157                 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2158                 goto fail;
2159         }
2160 #endif /* CONFIG_MBO */
2161
2162         /*
2163          * sta->capability is used in check_assoc_ies() for RRM enabled
2164          * capability element.
2165          */
2166         sta->capability = capab_info;
2167
2168         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
2169          * is used */
2170         resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
2171         if (resp != WLAN_STATUS_SUCCESS)
2172                 goto fail;
2173
2174         if (hostapd_get_aid(hapd, sta) < 0) {
2175                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2176                                HOSTAPD_LEVEL_INFO, "No room for more AIDs");
2177                 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2178                 goto fail;
2179         }
2180
2181         sta->listen_interval = listen_interval;
2182
2183         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
2184                 sta->flags |= WLAN_STA_NONERP;
2185         for (i = 0; i < sta->supported_rates_len; i++) {
2186                 if ((sta->supported_rates[i] & 0x7f) > 22) {
2187                         sta->flags &= ~WLAN_STA_NONERP;
2188                         break;
2189                 }
2190         }
2191         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
2192                 sta->nonerp_set = 1;
2193                 hapd->iface->num_sta_non_erp++;
2194                 if (hapd->iface->num_sta_non_erp == 1)
2195                         ieee802_11_set_beacons(hapd->iface);
2196         }
2197
2198         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
2199             !sta->no_short_slot_time_set) {
2200                 sta->no_short_slot_time_set = 1;
2201                 hapd->iface->num_sta_no_short_slot_time++;
2202                 if (hapd->iface->current_mode->mode ==
2203                     HOSTAPD_MODE_IEEE80211G &&
2204                     hapd->iface->num_sta_no_short_slot_time == 1)
2205                         ieee802_11_set_beacons(hapd->iface);
2206         }
2207
2208         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2209                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
2210         else
2211                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
2212
2213         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
2214             !sta->no_short_preamble_set) {
2215                 sta->no_short_preamble_set = 1;
2216                 hapd->iface->num_sta_no_short_preamble++;
2217                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
2218                     && hapd->iface->num_sta_no_short_preamble == 1)
2219                         ieee802_11_set_beacons(hapd->iface);
2220         }
2221
2222 #ifdef CONFIG_IEEE80211N
2223         update_ht_state(hapd, sta);
2224 #endif /* CONFIG_IEEE80211N */
2225
2226         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2227                        HOSTAPD_LEVEL_DEBUG,
2228                        "association OK (aid %d)", sta->aid);
2229         /* Station will be marked associated, after it acknowledges AssocResp
2230          */
2231         sta->flags |= WLAN_STA_ASSOC_REQ_OK;
2232
2233 #ifdef CONFIG_IEEE80211W
2234         if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
2235                 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
2236                            "SA Query procedure", reassoc ? "re" : "");
2237                 /* TODO: Send a protected Disassociate frame to the STA using
2238                  * the old key and Reason Code "Previous Authentication no
2239                  * longer valid". Make sure this is only sent protected since
2240                  * unprotected frame would be received by the STA that is now
2241                  * trying to associate.
2242                  */
2243         }
2244 #endif /* CONFIG_IEEE80211W */
2245
2246         /* Make sure that the previously registered inactivity timer will not
2247          * remove the STA immediately. */
2248         sta->timeout_next = STA_NULLFUNC;
2249
2250  fail:
2251         /*
2252          * In case of a successful response, add the station to the driver.
2253          * Otherwise, the kernel may ignore Data frames before we process the
2254          * ACK frame (TX status). In case of a failure, this station will be
2255          * removed.
2256          *
2257          * Note that this is not compliant with the IEEE 802.11 standard that
2258          * states that a non-AP station should transition into the
2259          * authenticated/associated state only after the station acknowledges
2260          * the (Re)Association Response frame. However, still do this as:
2261          *
2262          * 1. In case the station does not acknowledge the (Re)Association
2263          *    Response frame, it will be removed.
2264          * 2. Data frames will be dropped in the kernel until the station is
2265          *    set into authorized state, and there are no significant known
2266          *    issues with processing other non-Data Class 3 frames during this
2267          *    window.
2268          */
2269         if (resp == WLAN_STATUS_SUCCESS && add_associated_sta(hapd, sta))
2270                 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2271
2272         reply_res = send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
2273
2274         /*
2275          * Remove the station in case tranmission of a success response fails
2276          * (the STA was added associated to the driver) or if the station was
2277          * previously added unassociated.
2278          */
2279         if ((reply_res != WLAN_STATUS_SUCCESS &&
2280              resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc) {
2281                 hostapd_drv_sta_remove(hapd, sta->addr);
2282                 sta->added_unassoc = 0;
2283         }
2284 }
2285
2286
2287 static void handle_disassoc(struct hostapd_data *hapd,
2288                             const struct ieee80211_mgmt *mgmt, size_t len)
2289 {
2290         struct sta_info *sta;
2291
2292         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
2293                 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
2294                            (unsigned long) len);
2295                 return;
2296         }
2297
2298         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
2299                    MAC2STR(mgmt->sa),
2300                    le_to_host16(mgmt->u.disassoc.reason_code));
2301
2302         sta = ap_get_sta(hapd, mgmt->sa);
2303         if (sta == NULL) {
2304                 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
2305                            MAC2STR(mgmt->sa));
2306                 return;
2307         }
2308
2309         ap_sta_set_authorized(hapd, sta, 0);
2310         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
2311         sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
2312         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
2313         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2314                        HOSTAPD_LEVEL_INFO, "disassociated");
2315         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
2316         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
2317         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
2318          * authenticated. */
2319         accounting_sta_stop(hapd, sta);
2320         ieee802_1x_free_station(hapd, sta);
2321         if (sta->ipaddr)
2322                 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
2323         ap_sta_ip6addr_del(hapd, sta);
2324         hostapd_drv_sta_remove(hapd, sta->addr);
2325         sta->added_unassoc = 0;
2326
2327         if (sta->timeout_next == STA_NULLFUNC ||
2328             sta->timeout_next == STA_DISASSOC) {
2329                 sta->timeout_next = STA_DEAUTH;
2330                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2331                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
2332                                        hapd, sta);
2333         }
2334
2335         mlme_disassociate_indication(
2336                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
2337 }
2338
2339
2340 static void handle_deauth(struct hostapd_data *hapd,
2341                           const struct ieee80211_mgmt *mgmt, size_t len)
2342 {
2343         struct sta_info *sta;
2344
2345         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
2346                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
2347                         "payload (len=%lu)", (unsigned long) len);
2348                 return;
2349         }
2350
2351         wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
2352                 " reason_code=%d",
2353                 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
2354
2355         sta = ap_get_sta(hapd, mgmt->sa);
2356         if (sta == NULL) {
2357                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
2358                         "to deauthenticate, but it is not authenticated",
2359                         MAC2STR(mgmt->sa));
2360                 return;
2361         }
2362
2363         ap_sta_set_authorized(hapd, sta, 0);
2364         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
2365         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
2366                         WLAN_STA_ASSOC_REQ_OK);
2367         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
2368         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2369                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
2370         mlme_deauthenticate_indication(
2371                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
2372         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
2373         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
2374         ap_free_sta(hapd, sta);
2375 }
2376
2377
2378 static void handle_beacon(struct hostapd_data *hapd,
2379                           const struct ieee80211_mgmt *mgmt, size_t len,
2380                           struct hostapd_frame_info *fi)
2381 {
2382         struct ieee802_11_elems elems;
2383
2384         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
2385                 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
2386                            (unsigned long) len);
2387                 return;
2388         }
2389
2390         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
2391                                       len - (IEEE80211_HDRLEN +
2392                                              sizeof(mgmt->u.beacon)), &elems,
2393                                       0);
2394
2395         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
2396 }
2397
2398
2399 #ifdef CONFIG_IEEE80211W
2400
2401 static int hostapd_sa_query_action(struct hostapd_data *hapd,
2402                                    const struct ieee80211_mgmt *mgmt,
2403                                    size_t len)
2404 {
2405         const u8 *end;
2406
2407         end = mgmt->u.action.u.sa_query_resp.trans_id +
2408                 WLAN_SA_QUERY_TR_ID_LEN;
2409         if (((u8 *) mgmt) + len < end) {
2410                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
2411                            "frame (len=%lu)", (unsigned long) len);
2412                 return 0;
2413         }
2414
2415         ieee802_11_sa_query_action(hapd, mgmt->sa,
2416                                    mgmt->u.action.u.sa_query_resp.action,
2417                                    mgmt->u.action.u.sa_query_resp.trans_id);
2418         return 1;
2419 }
2420
2421
2422 static int robust_action_frame(u8 category)
2423 {
2424         return category != WLAN_ACTION_PUBLIC &&
2425                 category != WLAN_ACTION_HT;
2426 }
2427 #endif /* CONFIG_IEEE80211W */
2428
2429
2430 static int handle_action(struct hostapd_data *hapd,
2431                          const struct ieee80211_mgmt *mgmt, size_t len)
2432 {
2433         struct sta_info *sta;
2434         sta = ap_get_sta(hapd, mgmt->sa);
2435
2436         if (len < IEEE80211_HDRLEN + 1) {
2437                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2438                                HOSTAPD_LEVEL_DEBUG,
2439                                "handle_action - too short payload (len=%lu)",
2440                                (unsigned long) len);
2441                 return 0;
2442         }
2443
2444         if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
2445             (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
2446                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
2447                            "frame (category=%u) from unassociated STA " MACSTR,
2448                            MAC2STR(mgmt->sa), mgmt->u.action.category);
2449                 return 0;
2450         }
2451
2452 #ifdef CONFIG_IEEE80211W
2453         if (sta && (sta->flags & WLAN_STA_MFP) &&
2454             !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
2455             robust_action_frame(mgmt->u.action.category)) {
2456                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2457                                HOSTAPD_LEVEL_DEBUG,
2458                                "Dropped unprotected Robust Action frame from "
2459                                "an MFP STA");
2460                 return 0;
2461         }
2462 #endif /* CONFIG_IEEE80211W */
2463
2464         if (sta) {
2465                 u16 fc = le_to_host16(mgmt->frame_control);
2466                 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
2467
2468                 if ((fc & WLAN_FC_RETRY) &&
2469                     sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2470                     sta->last_seq_ctrl == seq_ctrl &&
2471                     sta->last_subtype == WLAN_FC_STYPE_ACTION) {
2472                         hostapd_logger(hapd, sta->addr,
2473                                        HOSTAPD_MODULE_IEEE80211,
2474                                        HOSTAPD_LEVEL_DEBUG,
2475                                        "Drop repeated action frame seq_ctrl=0x%x",
2476                                        seq_ctrl);
2477                         return 1;
2478                 }
2479
2480                 sta->last_seq_ctrl = seq_ctrl;
2481                 sta->last_subtype = WLAN_FC_STYPE_ACTION;
2482         }
2483
2484         switch (mgmt->u.action.category) {
2485 #ifdef CONFIG_IEEE80211R
2486         case WLAN_ACTION_FT:
2487                 if (!sta ||
2488                     wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
2489                                      len - IEEE80211_HDRLEN))
2490                         break;
2491                 return 1;
2492 #endif /* CONFIG_IEEE80211R */
2493         case WLAN_ACTION_WMM:
2494                 hostapd_wmm_action(hapd, mgmt, len);
2495                 return 1;
2496 #ifdef CONFIG_IEEE80211W
2497         case WLAN_ACTION_SA_QUERY:
2498                 return hostapd_sa_query_action(hapd, mgmt, len);
2499 #endif /* CONFIG_IEEE80211W */
2500 #ifdef CONFIG_WNM
2501         case WLAN_ACTION_WNM:
2502                 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
2503                 return 1;
2504 #endif /* CONFIG_WNM */
2505 #ifdef CONFIG_FST
2506         case WLAN_ACTION_FST:
2507                 if (hapd->iface->fst)
2508                         fst_rx_action(hapd->iface->fst, mgmt, len);
2509                 else
2510                         wpa_printf(MSG_DEBUG,
2511                                    "FST: Ignore FST Action frame - no FST attached");
2512                 return 1;
2513 #endif /* CONFIG_FST */
2514         case WLAN_ACTION_PUBLIC:
2515         case WLAN_ACTION_PROTECTED_DUAL:
2516 #ifdef CONFIG_IEEE80211N
2517                 if (len >= IEEE80211_HDRLEN + 2 &&
2518                     mgmt->u.action.u.public_action.action ==
2519                     WLAN_PA_20_40_BSS_COEX) {
2520                         wpa_printf(MSG_DEBUG,
2521                                    "HT20/40 coex mgmt frame received from STA "
2522                                    MACSTR, MAC2STR(mgmt->sa));
2523                         hostapd_2040_coex_action(hapd, mgmt, len);
2524                 }
2525 #endif /* CONFIG_IEEE80211N */
2526                 if (hapd->public_action_cb) {
2527                         hapd->public_action_cb(hapd->public_action_cb_ctx,
2528                                                (u8 *) mgmt, len,
2529                                                hapd->iface->freq);
2530                 }
2531                 if (hapd->public_action_cb2) {
2532                         hapd->public_action_cb2(hapd->public_action_cb2_ctx,
2533                                                 (u8 *) mgmt, len,
2534                                                 hapd->iface->freq);
2535                 }
2536                 if (hapd->public_action_cb || hapd->public_action_cb2)
2537                         return 1;
2538                 break;
2539         case WLAN_ACTION_VENDOR_SPECIFIC:
2540                 if (hapd->vendor_action_cb) {
2541                         if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
2542                                                    (u8 *) mgmt, len,
2543                                                    hapd->iface->freq) == 0)
2544                                 return 1;
2545                 }
2546                 break;
2547         case WLAN_ACTION_RADIO_MEASUREMENT:
2548                 hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
2549                 return 1;
2550         }
2551
2552         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2553                        HOSTAPD_LEVEL_DEBUG,
2554                        "handle_action - unknown action category %d or invalid "
2555                        "frame",
2556                        mgmt->u.action.category);
2557         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
2558             !(mgmt->sa[0] & 0x01)) {
2559                 struct ieee80211_mgmt *resp;
2560
2561                 /*
2562                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
2563                  * Return the Action frame to the source without change
2564                  * except that MSB of the Category set to 1.
2565                  */
2566                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
2567                            "frame back to sender");
2568                 resp = os_malloc(len);
2569                 if (resp == NULL)
2570                         return 0;
2571                 os_memcpy(resp, mgmt, len);
2572                 os_memcpy(resp->da, resp->sa, ETH_ALEN);
2573                 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
2574                 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
2575                 resp->u.action.category |= 0x80;
2576
2577                 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
2578                         wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
2579                                    "Action frame");
2580                 }
2581                 os_free(resp);
2582         }
2583
2584         return 1;
2585 }
2586
2587
2588 /**
2589  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
2590  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
2591  * sent to)
2592  * @buf: management frame data (starting from IEEE 802.11 header)
2593  * @len: length of frame data in octets
2594  * @fi: meta data about received frame (signal level, etc.)
2595  *
2596  * Process all incoming IEEE 802.11 management frames. This will be called for
2597  * each frame received from the kernel driver through wlan#ap interface. In
2598  * addition, it can be called to re-inserted pending frames (e.g., when using
2599  * external RADIUS server as an MAC ACL).
2600  */
2601 int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
2602                     struct hostapd_frame_info *fi)
2603 {
2604         struct ieee80211_mgmt *mgmt;
2605         int broadcast;
2606         u16 fc, stype;
2607         int ret = 0;
2608
2609         if (len < 24)
2610                 return 0;
2611
2612         mgmt = (struct ieee80211_mgmt *) buf;
2613         fc = le_to_host16(mgmt->frame_control);
2614         stype = WLAN_FC_GET_STYPE(fc);
2615
2616         if (stype == WLAN_FC_STYPE_BEACON) {
2617                 handle_beacon(hapd, mgmt, len, fi);
2618                 return 1;
2619         }
2620
2621         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
2622                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
2623                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
2624
2625         if (!broadcast &&
2626 #ifdef CONFIG_P2P
2627             /* Invitation responses can be sent with the peer MAC as BSSID */
2628             !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
2629               stype == WLAN_FC_STYPE_ACTION) &&
2630 #endif /* CONFIG_P2P */
2631 #ifdef CONFIG_MESH
2632             !(hapd->conf->mesh & MESH_ENABLED) &&
2633 #endif /* CONFIG_MESH */
2634             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
2635                 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
2636                            MAC2STR(mgmt->bssid));
2637                 return 0;
2638         }
2639
2640
2641         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
2642                 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
2643                 return 1;
2644         }
2645
2646         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
2647                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2648                                HOSTAPD_LEVEL_DEBUG,
2649                                "MGMT: DA=" MACSTR " not our address",
2650                                MAC2STR(mgmt->da));
2651                 return 0;
2652         }
2653
2654         if (hapd->iconf->track_sta_max_num)
2655                 sta_track_add(hapd->iface, mgmt->sa);
2656
2657         switch (stype) {
2658         case WLAN_FC_STYPE_AUTH:
2659                 wpa_printf(MSG_DEBUG, "mgmt::auth");
2660                 handle_auth(hapd, mgmt, len);
2661                 ret = 1;
2662                 break;
2663         case WLAN_FC_STYPE_ASSOC_REQ:
2664                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
2665                 handle_assoc(hapd, mgmt, len, 0);
2666                 ret = 1;
2667                 break;
2668         case WLAN_FC_STYPE_REASSOC_REQ:
2669                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
2670                 handle_assoc(hapd, mgmt, len, 1);
2671                 ret = 1;
2672                 break;
2673         case WLAN_FC_STYPE_DISASSOC:
2674                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
2675                 handle_disassoc(hapd, mgmt, len);
2676                 ret = 1;
2677                 break;
2678         case WLAN_FC_STYPE_DEAUTH:
2679                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
2680                 handle_deauth(hapd, mgmt, len);
2681                 ret = 1;
2682                 break;
2683         case WLAN_FC_STYPE_ACTION:
2684                 wpa_printf(MSG_DEBUG, "mgmt::action");
2685                 ret = handle_action(hapd, mgmt, len);
2686                 break;
2687         default:
2688                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2689                                HOSTAPD_LEVEL_DEBUG,
2690                                "unknown mgmt frame subtype %d", stype);
2691                 break;
2692         }
2693
2694         return ret;
2695 }
2696
2697
2698 static void handle_auth_cb(struct hostapd_data *hapd,
2699                            const struct ieee80211_mgmt *mgmt,
2700                            size_t len, int ok)
2701 {
2702         u16 auth_alg, auth_transaction, status_code;
2703         struct sta_info *sta;
2704
2705         sta = ap_get_sta(hapd, mgmt->da);
2706         if (!sta) {
2707                 wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
2708                            MAC2STR(mgmt->da));
2709                 return;
2710         }
2711
2712         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2713         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2714         status_code = le_to_host16(mgmt->u.auth.status_code);
2715
2716         if (!ok) {
2717                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2718                                HOSTAPD_LEVEL_NOTICE,
2719                                "did not acknowledge authentication response");
2720                 goto fail;
2721         }
2722
2723         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
2724                 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
2725                            (unsigned long) len);
2726                 goto fail;
2727         }
2728
2729         if (status_code == WLAN_STATUS_SUCCESS &&
2730             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
2731              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
2732                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2733                                HOSTAPD_LEVEL_INFO, "authenticated");
2734                 sta->flags |= WLAN_STA_AUTH;
2735                 if (sta->added_unassoc)
2736                         hostapd_set_sta_flags(hapd, sta);
2737                 return;
2738         }
2739
2740 fail:
2741         if (status_code != WLAN_STATUS_SUCCESS && sta->added_unassoc) {
2742                 hostapd_drv_sta_remove(hapd, sta->addr);
2743                 sta->added_unassoc = 0;
2744         }
2745 }
2746
2747
2748 static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
2749                                        struct sta_info *sta,
2750                                        char *ifname_wds)
2751 {
2752         int i;
2753         struct hostapd_ssid *ssid = &hapd->conf->ssid;
2754
2755         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
2756                 return;
2757
2758         for (i = 0; i < 4; i++) {
2759                 if (ssid->wep.key[i] &&
2760                     hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
2761                                         i == ssid->wep.idx, NULL, 0,
2762                                         ssid->wep.key[i], ssid->wep.len[i])) {
2763                         wpa_printf(MSG_WARNING,
2764                                    "Could not set WEP keys for WDS interface; %s",
2765                                    ifname_wds);
2766                         break;
2767                 }
2768         }
2769 }
2770
2771
2772 static void handle_assoc_cb(struct hostapd_data *hapd,
2773                             const struct ieee80211_mgmt *mgmt,
2774                             size_t len, int reassoc, int ok)
2775 {
2776         u16 status;
2777         struct sta_info *sta;
2778         int new_assoc = 1;
2779
2780         sta = ap_get_sta(hapd, mgmt->da);
2781         if (!sta) {
2782                 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
2783                            MAC2STR(mgmt->da));
2784                 return;
2785         }
2786
2787         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
2788                                       sizeof(mgmt->u.assoc_resp))) {
2789                 wpa_printf(MSG_INFO,
2790                            "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
2791                            reassoc, (unsigned long) len);
2792                 hostapd_drv_sta_remove(hapd, sta->addr);
2793                 return;
2794         }
2795
2796         if (reassoc)
2797                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
2798         else
2799                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
2800
2801         if (!ok) {
2802                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2803                                HOSTAPD_LEVEL_DEBUG,
2804                                "did not acknowledge association response");
2805                 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
2806                 /* The STA is added only in case of SUCCESS */
2807                 if (status == WLAN_STATUS_SUCCESS)
2808                         hostapd_drv_sta_remove(hapd, sta->addr);
2809
2810                 return;
2811         }
2812
2813         if (status != WLAN_STATUS_SUCCESS)
2814                 return;
2815
2816         /* Stop previous accounting session, if one is started, and allocate
2817          * new session id for the new session. */
2818         accounting_sta_stop(hapd, sta);
2819
2820         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2821                        HOSTAPD_LEVEL_INFO,
2822                        "associated (aid %d)",
2823                        sta->aid);
2824
2825         if (sta->flags & WLAN_STA_ASSOC)
2826                 new_assoc = 0;
2827         sta->flags |= WLAN_STA_ASSOC;
2828         sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
2829         if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
2830             sta->auth_alg == WLAN_AUTH_FT) {
2831                 /*
2832                  * Open, static WEP, or FT protocol; no separate authorization
2833                  * step.
2834                  */
2835                 ap_sta_set_authorized(hapd, sta, 1);
2836         }
2837
2838         if (reassoc)
2839                 mlme_reassociate_indication(hapd, sta);
2840         else
2841                 mlme_associate_indication(hapd, sta);
2842
2843 #ifdef CONFIG_IEEE80211W
2844         sta->sa_query_timed_out = 0;
2845 #endif /* CONFIG_IEEE80211W */
2846
2847         if (sta->flags & WLAN_STA_WDS) {
2848                 int ret;
2849                 char ifname_wds[IFNAMSIZ + 1];
2850
2851                 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
2852                                           sta->aid, 1);
2853                 if (!ret)
2854                         hostapd_set_wds_encryption(hapd, sta, ifname_wds);
2855         }
2856
2857         if (sta->eapol_sm == NULL) {
2858                 /*
2859                  * This STA does not use RADIUS server for EAP authentication,
2860                  * so bind it to the selected VLAN interface now, since the
2861                  * interface selection is not going to change anymore.
2862                  */
2863                 if (ap_sta_bind_vlan(hapd, sta) < 0)
2864                         return;
2865         } else if (sta->vlan_id) {
2866                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
2867                 if (ap_sta_bind_vlan(hapd, sta) < 0)
2868                         return;
2869         }
2870
2871         hostapd_set_sta_flags(hapd, sta);
2872
2873         if (sta->auth_alg == WLAN_AUTH_FT)
2874                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
2875         else
2876                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
2877         hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
2878         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
2879
2880         if (sta->pending_eapol_rx) {
2881                 struct os_reltime now, age;
2882
2883                 os_get_reltime(&now);
2884                 os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
2885                 if (age.sec == 0 && age.usec < 200000) {
2886                         wpa_printf(MSG_DEBUG,
2887                                    "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
2888                                    MAC2STR(sta->addr));
2889                         ieee802_1x_receive(
2890                                 hapd, mgmt->da,
2891                                 wpabuf_head(sta->pending_eapol_rx->buf),
2892                                 wpabuf_len(sta->pending_eapol_rx->buf));
2893                 }
2894                 wpabuf_free(sta->pending_eapol_rx->buf);
2895                 os_free(sta->pending_eapol_rx);
2896                 sta->pending_eapol_rx = NULL;
2897         }
2898 }
2899
2900
2901 static void handle_deauth_cb(struct hostapd_data *hapd,
2902                              const struct ieee80211_mgmt *mgmt,
2903                              size_t len, int ok)
2904 {
2905         struct sta_info *sta;
2906         if (mgmt->da[0] & 0x01)
2907                 return;
2908         sta = ap_get_sta(hapd, mgmt->da);
2909         if (!sta) {
2910                 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2911                            " not found", MAC2STR(mgmt->da));
2912                 return;
2913         }
2914         if (ok)
2915                 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2916                            MAC2STR(sta->addr));
2917         else
2918                 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2919                            "deauth", MAC2STR(sta->addr));
2920
2921         ap_sta_deauth_cb(hapd, sta);
2922 }
2923
2924
2925 static void handle_disassoc_cb(struct hostapd_data *hapd,
2926                                const struct ieee80211_mgmt *mgmt,
2927                                size_t len, int ok)
2928 {
2929         struct sta_info *sta;
2930         if (mgmt->da[0] & 0x01)
2931                 return;
2932         sta = ap_get_sta(hapd, mgmt->da);
2933         if (!sta) {
2934                 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2935                            " not found", MAC2STR(mgmt->da));
2936                 return;
2937         }
2938         if (ok)
2939                 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2940                            MAC2STR(sta->addr));
2941         else
2942                 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2943                            "disassoc", MAC2STR(sta->addr));
2944
2945         ap_sta_disassoc_cb(hapd, sta);
2946 }
2947
2948
2949 /**
2950  * ieee802_11_mgmt_cb - Process management frame TX status callback
2951  * @hapd: hostapd BSS data structure (the BSS from which the management frame
2952  * was sent from)
2953  * @buf: management frame data (starting from IEEE 802.11 header)
2954  * @len: length of frame data in octets
2955  * @stype: management frame subtype from frame control field
2956  * @ok: Whether the frame was ACK'ed
2957  */
2958 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
2959                         u16 stype, int ok)
2960 {
2961         const struct ieee80211_mgmt *mgmt;
2962         mgmt = (const struct ieee80211_mgmt *) buf;
2963
2964 #ifdef CONFIG_TESTING_OPTIONS
2965         if (hapd->ext_mgmt_frame_handling) {
2966                 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2967                         stype, ok);
2968                 return;
2969         }
2970 #endif /* CONFIG_TESTING_OPTIONS */
2971
2972         switch (stype) {
2973         case WLAN_FC_STYPE_AUTH:
2974                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2975                 handle_auth_cb(hapd, mgmt, len, ok);
2976                 break;
2977         case WLAN_FC_STYPE_ASSOC_RESP:
2978                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2979                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
2980                 break;
2981         case WLAN_FC_STYPE_REASSOC_RESP:
2982                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2983                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
2984                 break;
2985         case WLAN_FC_STYPE_PROBE_RESP:
2986                 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
2987                 break;
2988         case WLAN_FC_STYPE_DEAUTH:
2989                 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2990                 handle_deauth_cb(hapd, mgmt, len, ok);
2991                 break;
2992         case WLAN_FC_STYPE_DISASSOC:
2993                 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2994                 handle_disassoc_cb(hapd, mgmt, len, ok);
2995                 break;
2996         case WLAN_FC_STYPE_ACTION:
2997                 wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
2998                 break;
2999         default:
3000                 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
3001                 break;
3002         }
3003 }
3004
3005
3006 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
3007 {
3008         /* TODO */
3009         return 0;
3010 }
3011
3012
3013 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
3014                            char *buf, size_t buflen)
3015 {
3016         /* TODO */
3017         return 0;
3018 }
3019
3020
3021 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
3022                        const u8 *buf, size_t len, int ack)
3023 {
3024         struct sta_info *sta;
3025         struct hostapd_iface *iface = hapd->iface;
3026
3027         sta = ap_get_sta(hapd, addr);
3028         if (sta == NULL && iface->num_bss > 1) {
3029                 size_t j;
3030                 for (j = 0; j < iface->num_bss; j++) {
3031                         hapd = iface->bss[j];
3032                         sta = ap_get_sta(hapd, addr);
3033                         if (sta)
3034                                 break;
3035                 }
3036         }
3037         if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
3038                 return;
3039         if (sta->flags & WLAN_STA_PENDING_POLL) {
3040                 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
3041                            "activity poll", MAC2STR(sta->addr),
3042                            ack ? "ACKed" : "did not ACK");
3043                 if (ack)
3044                         sta->flags &= ~WLAN_STA_PENDING_POLL;
3045         }
3046
3047         ieee802_1x_tx_status(hapd, sta, buf, len, ack);
3048 }
3049
3050
3051 void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
3052                              const u8 *data, size_t len, int ack)
3053 {
3054         struct sta_info *sta;
3055         struct hostapd_iface *iface = hapd->iface;
3056
3057         sta = ap_get_sta(hapd, dst);
3058         if (sta == NULL && iface->num_bss > 1) {
3059                 size_t j;
3060                 for (j = 0; j < iface->num_bss; j++) {
3061                         hapd = iface->bss[j];
3062                         sta = ap_get_sta(hapd, dst);
3063                         if (sta)
3064                                 break;
3065                 }
3066         }
3067         if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
3068                 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
3069                            MACSTR " that is not currently associated",
3070                            MAC2STR(dst));
3071                 return;
3072         }
3073
3074         ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
3075 }
3076
3077
3078 void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
3079 {
3080         struct sta_info *sta;
3081         struct hostapd_iface *iface = hapd->iface;
3082
3083         sta = ap_get_sta(hapd, addr);
3084         if (sta == NULL && iface->num_bss > 1) {
3085                 size_t j;
3086                 for (j = 0; j < iface->num_bss; j++) {
3087                         hapd = iface->bss[j];
3088                         sta = ap_get_sta(hapd, addr);
3089                         if (sta)
3090                                 break;
3091                 }
3092         }
3093         if (sta == NULL)
3094                 return;
3095         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
3096                 MAC2STR(sta->addr));
3097         if (!(sta->flags & WLAN_STA_PENDING_POLL))
3098                 return;
3099
3100         wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
3101                    "activity poll", MAC2STR(sta->addr));
3102         sta->flags &= ~WLAN_STA_PENDING_POLL;
3103 }
3104
3105
3106 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
3107                                 int wds)
3108 {
3109         struct sta_info *sta;
3110
3111         sta = ap_get_sta(hapd, src);
3112         if (sta && (sta->flags & WLAN_STA_ASSOC)) {
3113                 if (!hapd->conf->wds_sta)
3114                         return;
3115
3116                 if (wds && !(sta->flags & WLAN_STA_WDS)) {
3117                         int ret;
3118                         char ifname_wds[IFNAMSIZ + 1];
3119
3120                         wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
3121                                    "STA " MACSTR " (aid %u)",
3122                                    MAC2STR(sta->addr), sta->aid);
3123                         sta->flags |= WLAN_STA_WDS;
3124                         ret = hostapd_set_wds_sta(hapd, ifname_wds,
3125                                                   sta->addr, sta->aid, 1);
3126                         if (!ret)
3127                                 hostapd_set_wds_encryption(hapd, sta,
3128                                                            ifname_wds);
3129                 }
3130                 return;
3131         }
3132
3133         wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
3134                    MACSTR, MAC2STR(src));
3135         if (src[0] & 0x01) {
3136                 /* Broadcast bit set in SA?! Ignore the frame silently. */
3137                 return;
3138         }
3139
3140         if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
3141                 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
3142                            "already been sent, but no TX status yet known - "
3143                            "ignore Class 3 frame issue with " MACSTR,
3144                            MAC2STR(src));
3145                 return;
3146         }
3147
3148         if (sta && (sta->flags & WLAN_STA_AUTH))
3149                 hostapd_drv_sta_disassoc(
3150                         hapd, src,
3151                         WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
3152         else
3153                 hostapd_drv_sta_deauth(
3154                         hapd, src,
3155                         WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
3156 }
3157
3158
3159 #endif /* CONFIG_NATIVE_WINDOWS */