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