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