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