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