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