AP: Extend the BSS bridge neighbor entry management to support IPv6
[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         hostapd_drv_sta_remove(hapd, sta->addr);
1722
1723         if (sta->timeout_next == STA_NULLFUNC ||
1724             sta->timeout_next == STA_DISASSOC) {
1725                 sta->timeout_next = STA_DEAUTH;
1726                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1727                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1728                                        hapd, sta);
1729         }
1730
1731         mlme_disassociate_indication(
1732                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1733 }
1734
1735
1736 static void handle_deauth(struct hostapd_data *hapd,
1737                           const struct ieee80211_mgmt *mgmt, size_t len)
1738 {
1739         struct sta_info *sta;
1740
1741         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1742                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
1743                         "payload (len=%lu)", (unsigned long) len);
1744                 return;
1745         }
1746
1747         wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
1748                 " reason_code=%d",
1749                 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
1750
1751         sta = ap_get_sta(hapd, mgmt->sa);
1752         if (sta == NULL) {
1753                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
1754                         "to deauthenticate, but it is not authenticated",
1755                         MAC2STR(mgmt->sa));
1756                 return;
1757         }
1758
1759         ap_sta_set_authorized(hapd, sta, 0);
1760         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
1761         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1762                         WLAN_STA_ASSOC_REQ_OK);
1763         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1764         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1765                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1766         mlme_deauthenticate_indication(
1767                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1768         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1769         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1770         ap_free_sta(hapd, sta);
1771 }
1772
1773
1774 static void handle_beacon(struct hostapd_data *hapd,
1775                           const struct ieee80211_mgmt *mgmt, size_t len,
1776                           struct hostapd_frame_info *fi)
1777 {
1778         struct ieee802_11_elems elems;
1779
1780         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1781                 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
1782                            (unsigned long) len);
1783                 return;
1784         }
1785
1786         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1787                                       len - (IEEE80211_HDRLEN +
1788                                              sizeof(mgmt->u.beacon)), &elems,
1789                                       0);
1790
1791         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1792 }
1793
1794
1795 #ifdef CONFIG_IEEE80211W
1796
1797 static int hostapd_sa_query_action(struct hostapd_data *hapd,
1798                                    const struct ieee80211_mgmt *mgmt,
1799                                    size_t len)
1800 {
1801         const u8 *end;
1802
1803         end = mgmt->u.action.u.sa_query_resp.trans_id +
1804                 WLAN_SA_QUERY_TR_ID_LEN;
1805         if (((u8 *) mgmt) + len < end) {
1806                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1807                            "frame (len=%lu)", (unsigned long) len);
1808                 return 0;
1809         }
1810
1811         ieee802_11_sa_query_action(hapd, mgmt->sa,
1812                                    mgmt->u.action.u.sa_query_resp.action,
1813                                    mgmt->u.action.u.sa_query_resp.trans_id);
1814         return 1;
1815 }
1816
1817
1818 static int robust_action_frame(u8 category)
1819 {
1820         return category != WLAN_ACTION_PUBLIC &&
1821                 category != WLAN_ACTION_HT;
1822 }
1823 #endif /* CONFIG_IEEE80211W */
1824
1825
1826 static int handle_action(struct hostapd_data *hapd,
1827                          const struct ieee80211_mgmt *mgmt, size_t len)
1828 {
1829         struct sta_info *sta;
1830         sta = ap_get_sta(hapd, mgmt->sa);
1831
1832         if (len < IEEE80211_HDRLEN + 1) {
1833                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1834                                HOSTAPD_LEVEL_DEBUG,
1835                                "handle_action - too short payload (len=%lu)",
1836                                (unsigned long) len);
1837                 return 0;
1838         }
1839
1840         if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
1841             (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
1842                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
1843                            "frame (category=%u) from unassociated STA " MACSTR,
1844                            MAC2STR(mgmt->sa), mgmt->u.action.category);
1845                 return 0;
1846         }
1847
1848 #ifdef CONFIG_IEEE80211W
1849         if (sta && (sta->flags & WLAN_STA_MFP) &&
1850             !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
1851             robust_action_frame(mgmt->u.action.category)) {
1852                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1853                                HOSTAPD_LEVEL_DEBUG,
1854                                "Dropped unprotected Robust Action frame from "
1855                                "an MFP STA");
1856                 return 0;
1857         }
1858 #endif /* CONFIG_IEEE80211W */
1859
1860         if (sta) {
1861                 u16 fc = le_to_host16(mgmt->frame_control);
1862                 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
1863
1864                 if ((fc & WLAN_FC_RETRY) &&
1865                     sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1866                     sta->last_seq_ctrl == seq_ctrl &&
1867                     sta->last_subtype == WLAN_FC_STYPE_ACTION) {
1868                         hostapd_logger(hapd, sta->addr,
1869                                        HOSTAPD_MODULE_IEEE80211,
1870                                        HOSTAPD_LEVEL_DEBUG,
1871                                        "Drop repeated action frame seq_ctrl=0x%x",
1872                                        seq_ctrl);
1873                         return 1;
1874                 }
1875
1876                 sta->last_seq_ctrl = seq_ctrl;
1877                 sta->last_subtype = WLAN_FC_STYPE_ACTION;
1878         }
1879
1880         switch (mgmt->u.action.category) {
1881 #ifdef CONFIG_IEEE80211R
1882         case WLAN_ACTION_FT:
1883                 if (!sta ||
1884                     wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1885                                      len - IEEE80211_HDRLEN))
1886                         break;
1887                 return 1;
1888 #endif /* CONFIG_IEEE80211R */
1889         case WLAN_ACTION_WMM:
1890                 hostapd_wmm_action(hapd, mgmt, len);
1891                 return 1;
1892 #ifdef CONFIG_IEEE80211W
1893         case WLAN_ACTION_SA_QUERY:
1894                 return hostapd_sa_query_action(hapd, mgmt, len);
1895 #endif /* CONFIG_IEEE80211W */
1896 #ifdef CONFIG_WNM
1897         case WLAN_ACTION_WNM:
1898                 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
1899                 return 1;
1900 #endif /* CONFIG_WNM */
1901         case WLAN_ACTION_PUBLIC:
1902         case WLAN_ACTION_PROTECTED_DUAL:
1903 #ifdef CONFIG_IEEE80211N
1904                 if (mgmt->u.action.u.public_action.action ==
1905                     WLAN_PA_20_40_BSS_COEX) {
1906                         wpa_printf(MSG_DEBUG,
1907                                    "HT20/40 coex mgmt frame received from STA "
1908                                    MACSTR, MAC2STR(mgmt->sa));
1909                         hostapd_2040_coex_action(hapd, mgmt, len);
1910                 }
1911 #endif /* CONFIG_IEEE80211N */
1912                 if (hapd->public_action_cb) {
1913                         hapd->public_action_cb(hapd->public_action_cb_ctx,
1914                                                (u8 *) mgmt, len,
1915                                                hapd->iface->freq);
1916                 }
1917                 if (hapd->public_action_cb2) {
1918                         hapd->public_action_cb2(hapd->public_action_cb2_ctx,
1919                                                 (u8 *) mgmt, len,
1920                                                 hapd->iface->freq);
1921                 }
1922                 if (hapd->public_action_cb || hapd->public_action_cb2)
1923                         return 1;
1924                 break;
1925         case WLAN_ACTION_VENDOR_SPECIFIC:
1926                 if (hapd->vendor_action_cb) {
1927                         if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1928                                                    (u8 *) mgmt, len,
1929                                                    hapd->iface->freq) == 0)
1930                                 return 1;
1931                 }
1932                 break;
1933         }
1934
1935         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1936                        HOSTAPD_LEVEL_DEBUG,
1937                        "handle_action - unknown action category %d or invalid "
1938                        "frame",
1939                        mgmt->u.action.category);
1940         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1941             !(mgmt->sa[0] & 0x01)) {
1942                 struct ieee80211_mgmt *resp;
1943
1944                 /*
1945                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1946                  * Return the Action frame to the source without change
1947                  * except that MSB of the Category set to 1.
1948                  */
1949                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1950                            "frame back to sender");
1951                 resp = os_malloc(len);
1952                 if (resp == NULL)
1953                         return 0;
1954                 os_memcpy(resp, mgmt, len);
1955                 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1956                 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1957                 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1958                 resp->u.action.category |= 0x80;
1959
1960                 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
1961                         wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
1962                                    "Action frame");
1963                 }
1964                 os_free(resp);
1965         }
1966
1967         return 1;
1968 }
1969
1970
1971 /**
1972  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1973  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1974  * sent to)
1975  * @buf: management frame data (starting from IEEE 802.11 header)
1976  * @len: length of frame data in octets
1977  * @fi: meta data about received frame (signal level, etc.)
1978  *
1979  * Process all incoming IEEE 802.11 management frames. This will be called for
1980  * each frame received from the kernel driver through wlan#ap interface. In
1981  * addition, it can be called to re-inserted pending frames (e.g., when using
1982  * external RADIUS server as an MAC ACL).
1983  */
1984 int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1985                     struct hostapd_frame_info *fi)
1986 {
1987         struct ieee80211_mgmt *mgmt;
1988         int broadcast;
1989         u16 fc, stype;
1990         int ret = 0;
1991
1992         if (len < 24)
1993                 return 0;
1994
1995         mgmt = (struct ieee80211_mgmt *) buf;
1996         fc = le_to_host16(mgmt->frame_control);
1997         stype = WLAN_FC_GET_STYPE(fc);
1998
1999         if (stype == WLAN_FC_STYPE_BEACON) {
2000                 handle_beacon(hapd, mgmt, len, fi);
2001                 return 1;
2002         }
2003
2004         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
2005                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
2006                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
2007
2008         if (!broadcast &&
2009 #ifdef CONFIG_P2P
2010             /* Invitation responses can be sent with the peer MAC as BSSID */
2011             !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
2012               stype == WLAN_FC_STYPE_ACTION) &&
2013 #endif /* CONFIG_P2P */
2014 #ifdef CONFIG_MESH
2015             !(hapd->conf->mesh & MESH_ENABLED) &&
2016 #endif /* CONFIG_MESH */
2017             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
2018                 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
2019                            MAC2STR(mgmt->bssid));
2020                 return 0;
2021         }
2022
2023
2024         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
2025                 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
2026                 return 1;
2027         }
2028
2029         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
2030                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2031                                HOSTAPD_LEVEL_DEBUG,
2032                                "MGMT: DA=" MACSTR " not our address",
2033                                MAC2STR(mgmt->da));
2034                 return 0;
2035         }
2036
2037         switch (stype) {
2038         case WLAN_FC_STYPE_AUTH:
2039                 wpa_printf(MSG_DEBUG, "mgmt::auth");
2040                 handle_auth(hapd, mgmt, len);
2041                 ret = 1;
2042                 break;
2043         case WLAN_FC_STYPE_ASSOC_REQ:
2044                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
2045                 handle_assoc(hapd, mgmt, len, 0);
2046                 ret = 1;
2047                 break;
2048         case WLAN_FC_STYPE_REASSOC_REQ:
2049                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
2050                 handle_assoc(hapd, mgmt, len, 1);
2051                 ret = 1;
2052                 break;
2053         case WLAN_FC_STYPE_DISASSOC:
2054                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
2055                 handle_disassoc(hapd, mgmt, len);
2056                 ret = 1;
2057                 break;
2058         case WLAN_FC_STYPE_DEAUTH:
2059                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
2060                 handle_deauth(hapd, mgmt, len);
2061                 ret = 1;
2062                 break;
2063         case WLAN_FC_STYPE_ACTION:
2064                 wpa_printf(MSG_DEBUG, "mgmt::action");
2065                 ret = handle_action(hapd, mgmt, len);
2066                 break;
2067         default:
2068                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2069                                HOSTAPD_LEVEL_DEBUG,
2070                                "unknown mgmt frame subtype %d", stype);
2071                 break;
2072         }
2073
2074         return ret;
2075 }
2076
2077
2078 static void handle_auth_cb(struct hostapd_data *hapd,
2079                            const struct ieee80211_mgmt *mgmt,
2080                            size_t len, int ok)
2081 {
2082         u16 auth_alg, auth_transaction, status_code;
2083         struct sta_info *sta;
2084
2085         if (!ok) {
2086                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2087                                HOSTAPD_LEVEL_NOTICE,
2088                                "did not acknowledge authentication response");
2089                 return;
2090         }
2091
2092         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
2093                 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
2094                            (unsigned long) len);
2095                 return;
2096         }
2097
2098         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2099         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2100         status_code = le_to_host16(mgmt->u.auth.status_code);
2101
2102         sta = ap_get_sta(hapd, mgmt->da);
2103         if (!sta) {
2104                 wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
2105                            MAC2STR(mgmt->da));
2106                 return;
2107         }
2108
2109         if (status_code == WLAN_STATUS_SUCCESS &&
2110             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
2111              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
2112                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2113                                HOSTAPD_LEVEL_INFO, "authenticated");
2114                 sta->flags |= WLAN_STA_AUTH;
2115         }
2116 }
2117
2118
2119 static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
2120                                        struct sta_info *sta,
2121                                        char *ifname_wds)
2122 {
2123         int i;
2124         struct hostapd_ssid *ssid = sta->ssid;
2125
2126         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
2127                 return;
2128
2129         for (i = 0; i < 4; i++) {
2130                 if (ssid->wep.key[i] &&
2131                     hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
2132                                         i == ssid->wep.idx, NULL, 0,
2133                                         ssid->wep.key[i], ssid->wep.len[i])) {
2134                         wpa_printf(MSG_WARNING,
2135                                    "Could not set WEP keys for WDS interface; %s",
2136                                    ifname_wds);
2137                         break;
2138                 }
2139         }
2140 }
2141
2142
2143 static void handle_assoc_cb(struct hostapd_data *hapd,
2144                             const struct ieee80211_mgmt *mgmt,
2145                             size_t len, int reassoc, int ok)
2146 {
2147         u16 status;
2148         struct sta_info *sta;
2149         int new_assoc = 1;
2150         struct ieee80211_ht_capabilities ht_cap;
2151         struct ieee80211_vht_capabilities vht_cap;
2152
2153         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
2154                                       sizeof(mgmt->u.assoc_resp))) {
2155                 wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
2156                            reassoc, (unsigned long) len);
2157                 return;
2158         }
2159
2160         sta = ap_get_sta(hapd, mgmt->da);
2161         if (!sta) {
2162                 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
2163                            MAC2STR(mgmt->da));
2164                 return;
2165         }
2166
2167         if (!ok) {
2168                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2169                                HOSTAPD_LEVEL_DEBUG,
2170                                "did not acknowledge association response");
2171                 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
2172                 return;
2173         }
2174
2175         if (reassoc)
2176                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
2177         else
2178                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
2179
2180         if (status != WLAN_STATUS_SUCCESS)
2181                 return;
2182
2183         /* Stop previous accounting session, if one is started, and allocate
2184          * new session id for the new session. */
2185         accounting_sta_stop(hapd, sta);
2186
2187         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2188                        HOSTAPD_LEVEL_INFO,
2189                        "associated (aid %d)",
2190                        sta->aid);
2191
2192         if (sta->flags & WLAN_STA_ASSOC)
2193                 new_assoc = 0;
2194         sta->flags |= WLAN_STA_ASSOC;
2195         sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
2196         if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
2197             sta->auth_alg == WLAN_AUTH_FT) {
2198                 /*
2199                  * Open, static WEP, or FT protocol; no separate authorization
2200                  * step.
2201                  */
2202                 ap_sta_set_authorized(hapd, sta, 1);
2203         }
2204
2205         if (reassoc)
2206                 mlme_reassociate_indication(hapd, sta);
2207         else
2208                 mlme_associate_indication(hapd, sta);
2209
2210 #ifdef CONFIG_IEEE80211W
2211         sta->sa_query_timed_out = 0;
2212 #endif /* CONFIG_IEEE80211W */
2213
2214         /*
2215          * Remove the STA entry in order to make sure the STA PS state gets
2216          * cleared and configuration gets updated in case of reassociation back
2217          * to the same AP.
2218          */
2219         hostapd_drv_sta_remove(hapd, sta->addr);
2220
2221 #ifdef CONFIG_IEEE80211N
2222         if (sta->flags & WLAN_STA_HT)
2223                 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
2224 #endif /* CONFIG_IEEE80211N */
2225 #ifdef CONFIG_IEEE80211AC
2226         if (sta->flags & WLAN_STA_VHT)
2227                 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
2228 #endif /* CONFIG_IEEE80211AC */
2229
2230         if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
2231                             sta->supported_rates, sta->supported_rates_len,
2232                             sta->listen_interval,
2233                             sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
2234                             sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
2235                             sta->flags, sta->qosinfo, sta->vht_opmode)) {
2236                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2237                                HOSTAPD_LEVEL_NOTICE,
2238                                "Could not add STA to kernel driver");
2239
2240                 ap_sta_disconnect(hapd, sta, sta->addr,
2241                                   WLAN_REASON_DISASSOC_AP_BUSY);
2242
2243                 return;
2244         }
2245
2246         if (sta->flags & WLAN_STA_WDS) {
2247                 int ret;
2248                 char ifname_wds[IFNAMSIZ + 1];
2249
2250                 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
2251                                           sta->aid, 1);
2252                 if (!ret)
2253                         hostapd_set_wds_encryption(hapd, sta, ifname_wds);
2254         }
2255
2256         if (sta->eapol_sm == NULL) {
2257                 /*
2258                  * This STA does not use RADIUS server for EAP authentication,
2259                  * so bind it to the selected VLAN interface now, since the
2260                  * interface selection is not going to change anymore.
2261                  */
2262                 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
2263                         return;
2264         } else if (sta->vlan_id) {
2265                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
2266                 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
2267                         return;
2268         }
2269
2270         hostapd_set_sta_flags(hapd, sta);
2271
2272         if (sta->auth_alg == WLAN_AUTH_FT)
2273                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
2274         else
2275                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
2276         hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
2277
2278         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
2279 }
2280
2281
2282 static void handle_deauth_cb(struct hostapd_data *hapd,
2283                              const struct ieee80211_mgmt *mgmt,
2284                              size_t len, int ok)
2285 {
2286         struct sta_info *sta;
2287         if (mgmt->da[0] & 0x01)
2288                 return;
2289         sta = ap_get_sta(hapd, mgmt->da);
2290         if (!sta) {
2291                 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2292                            " not found", MAC2STR(mgmt->da));
2293                 return;
2294         }
2295         if (ok)
2296                 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2297                            MAC2STR(sta->addr));
2298         else
2299                 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2300                            "deauth", MAC2STR(sta->addr));
2301
2302         ap_sta_deauth_cb(hapd, sta);
2303 }
2304
2305
2306 static void handle_disassoc_cb(struct hostapd_data *hapd,
2307                                const struct ieee80211_mgmt *mgmt,
2308                                size_t len, int ok)
2309 {
2310         struct sta_info *sta;
2311         if (mgmt->da[0] & 0x01)
2312                 return;
2313         sta = ap_get_sta(hapd, mgmt->da);
2314         if (!sta) {
2315                 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2316                            " not found", MAC2STR(mgmt->da));
2317                 return;
2318         }
2319         if (ok)
2320                 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2321                            MAC2STR(sta->addr));
2322         else
2323                 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2324                            "disassoc", MAC2STR(sta->addr));
2325
2326         ap_sta_disassoc_cb(hapd, sta);
2327 }
2328
2329
2330 /**
2331  * ieee802_11_mgmt_cb - Process management frame TX status callback
2332  * @hapd: hostapd BSS data structure (the BSS from which the management frame
2333  * was sent from)
2334  * @buf: management frame data (starting from IEEE 802.11 header)
2335  * @len: length of frame data in octets
2336  * @stype: management frame subtype from frame control field
2337  * @ok: Whether the frame was ACK'ed
2338  */
2339 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
2340                         u16 stype, int ok)
2341 {
2342         const struct ieee80211_mgmt *mgmt;
2343         mgmt = (const struct ieee80211_mgmt *) buf;
2344
2345 #ifdef CONFIG_TESTING_OPTIONS
2346         if (hapd->ext_mgmt_frame_handling) {
2347                 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2348                         stype, ok);
2349                 return;
2350         }
2351 #endif /* CONFIG_TESTING_OPTIONS */
2352
2353         switch (stype) {
2354         case WLAN_FC_STYPE_AUTH:
2355                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2356                 handle_auth_cb(hapd, mgmt, len, ok);
2357                 break;
2358         case WLAN_FC_STYPE_ASSOC_RESP:
2359                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2360                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
2361                 break;
2362         case WLAN_FC_STYPE_REASSOC_RESP:
2363                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2364                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
2365                 break;
2366         case WLAN_FC_STYPE_PROBE_RESP:
2367                 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
2368                 break;
2369         case WLAN_FC_STYPE_DEAUTH:
2370                 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2371                 handle_deauth_cb(hapd, mgmt, len, ok);
2372                 break;
2373         case WLAN_FC_STYPE_DISASSOC:
2374                 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2375                 handle_disassoc_cb(hapd, mgmt, len, ok);
2376                 break;
2377         case WLAN_FC_STYPE_ACTION:
2378                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
2379                 break;
2380         default:
2381                 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
2382                 break;
2383         }
2384 }
2385
2386
2387 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2388 {
2389         /* TODO */
2390         return 0;
2391 }
2392
2393
2394 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2395                            char *buf, size_t buflen)
2396 {
2397         /* TODO */
2398         return 0;
2399 }
2400
2401
2402 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
2403                        const u8 *buf, size_t len, int ack)
2404 {
2405         struct sta_info *sta;
2406         struct hostapd_iface *iface = hapd->iface;
2407
2408         sta = ap_get_sta(hapd, addr);
2409         if (sta == NULL && iface->num_bss > 1) {
2410                 size_t j;
2411                 for (j = 0; j < iface->num_bss; j++) {
2412                         hapd = iface->bss[j];
2413                         sta = ap_get_sta(hapd, addr);
2414                         if (sta)
2415                                 break;
2416                 }
2417         }
2418         if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
2419                 return;
2420         if (sta->flags & WLAN_STA_PENDING_POLL) {
2421                 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
2422                            "activity poll", MAC2STR(sta->addr),
2423                            ack ? "ACKed" : "did not ACK");
2424                 if (ack)
2425                         sta->flags &= ~WLAN_STA_PENDING_POLL;
2426         }
2427
2428         ieee802_1x_tx_status(hapd, sta, buf, len, ack);
2429 }
2430
2431
2432 void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
2433                              const u8 *data, size_t len, int ack)
2434 {
2435         struct sta_info *sta;
2436         struct hostapd_iface *iface = hapd->iface;
2437
2438         sta = ap_get_sta(hapd, dst);
2439         if (sta == NULL && iface->num_bss > 1) {
2440                 size_t j;
2441                 for (j = 0; j < iface->num_bss; j++) {
2442                         hapd = iface->bss[j];
2443                         sta = ap_get_sta(hapd, dst);
2444                         if (sta)
2445                                 break;
2446                 }
2447         }
2448         if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
2449                 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
2450                            MACSTR " that is not currently associated",
2451                            MAC2STR(dst));
2452                 return;
2453         }
2454
2455         ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
2456 }
2457
2458
2459 void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
2460 {
2461         struct sta_info *sta;
2462         struct hostapd_iface *iface = hapd->iface;
2463
2464         sta = ap_get_sta(hapd, addr);
2465         if (sta == NULL && iface->num_bss > 1) {
2466                 size_t j;
2467                 for (j = 0; j < iface->num_bss; j++) {
2468                         hapd = iface->bss[j];
2469                         sta = ap_get_sta(hapd, addr);
2470                         if (sta)
2471                                 break;
2472                 }
2473         }
2474         if (sta == NULL)
2475                 return;
2476         if (!(sta->flags & WLAN_STA_PENDING_POLL))
2477                 return;
2478
2479         wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
2480                    "activity poll", MAC2STR(sta->addr));
2481         sta->flags &= ~WLAN_STA_PENDING_POLL;
2482 }
2483
2484
2485 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
2486                                 int wds)
2487 {
2488         struct sta_info *sta;
2489
2490         sta = ap_get_sta(hapd, src);
2491         if (sta && (sta->flags & WLAN_STA_ASSOC)) {
2492                 if (!hapd->conf->wds_sta)
2493                         return;
2494
2495                 if (wds && !(sta->flags & WLAN_STA_WDS)) {
2496                         int ret;
2497                         char ifname_wds[IFNAMSIZ + 1];
2498
2499                         wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
2500                                    "STA " MACSTR " (aid %u)",
2501                                    MAC2STR(sta->addr), sta->aid);
2502                         sta->flags |= WLAN_STA_WDS;
2503                         ret = hostapd_set_wds_sta(hapd, ifname_wds,
2504                                                   sta->addr, sta->aid, 1);
2505                         if (!ret)
2506                                 hostapd_set_wds_encryption(hapd, sta,
2507                                                            ifname_wds);
2508                 }
2509                 return;
2510         }
2511
2512         wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
2513                    MACSTR, MAC2STR(src));
2514         if (src[0] & 0x01) {
2515                 /* Broadcast bit set in SA?! Ignore the frame silently. */
2516                 return;
2517         }
2518
2519         if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
2520                 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
2521                            "already been sent, but no TX status yet known - "
2522                            "ignore Class 3 frame issue with " MACSTR,
2523                            MAC2STR(src));
2524                 return;
2525         }
2526
2527         if (sta && (sta->flags & WLAN_STA_AUTH))
2528                 hostapd_drv_sta_disassoc(
2529                         hapd, src,
2530                         WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2531         else
2532                 hostapd_drv_sta_deauth(
2533                         hapd, src,
2534                         WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2535 }
2536
2537
2538 #endif /* CONFIG_NATIVE_WINDOWS */