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