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