]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/ieee802_11.c
HE: Add AP mode MLME/SME handling for HE stations
[thirdparty/hostap.git] / src / ap / ieee802_11.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / IEEE 802.11 Management
f3383366 3 * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
1057d78e 9#include "utils/includes.h"
6fc6879b
JM
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
1057d78e
JM
13#include "utils/common.h"
14#include "utils/eloop.h"
03da66bd 15#include "crypto/crypto.h"
d136c376 16#include "crypto/sha256.h"
7a12edd1
JM
17#include "crypto/sha384.h"
18#include "crypto/sha512.h"
d136c376 19#include "crypto/random.h"
81f4f619
JM
20#include "common/ieee802_11_defs.h"
21#include "common/ieee802_11_common.h"
03da66bd 22#include "common/wpa_ctrl.h"
98efcc41 23#include "common/sae.h"
10ec6a5f 24#include "common/dpp.h"
99621dc1 25#include "common/ocv.h"
03da66bd
JM
26#include "radius/radius.h"
27#include "radius/radius_client.h"
e44f8bf2 28#include "p2p/p2p.h"
ed7a09f9 29#include "wps/wps.h"
037378ff 30#include "fst/fst.h"
6fc6879b 31#include "hostapd.h"
6fc6879b 32#include "beacon.h"
6fc6879b
JM
33#include "ieee802_11_auth.h"
34#include "sta_info.h"
6fc6879b 35#include "ieee802_1x.h"
6226e38d 36#include "wpa_auth.h"
f2991170 37#include "pmksa_cache_auth.h"
1057d78e 38#include "wmm.h"
6fc6879b
JM
39#include "ap_list.h"
40#include "accounting.h"
6226e38d
JM
41#include "ap_config.h"
42#include "ap_mlme.h"
dce044cc 43#include "p2p_hostapd.h"
cee7d66b 44#include "ap_drv_ops.h"
c79938a5 45#include "wnm_ap.h"
0e2412d0 46#include "hw_features.h"
6226e38d 47#include "ieee802_11.h"
3d7ad2f6 48#include "dfs.h"
6332aaf3 49#include "mbo_ap.h"
2572df34 50#include "rrm.h"
04059ab8 51#include "taxonomy.h"
54b04d6f 52#include "fils_hlp.h"
9c2b8204
JM
53#include "dpp_hostapd.h"
54#include "gas_query_ap.h"
6fc6879b
JM
55
56
5e5f8c81
JM
57#ifdef CONFIG_FILS
58static struct wpabuf *
59prepare_auth_resp_fils(struct hostapd_data *hapd,
60 struct sta_info *sta, u16 *resp,
61 struct rsn_pmksa_cache_entry *pmksa,
62 struct wpabuf *erp_resp,
63 const u8 *msk, size_t msk_len,
64 int *is_pub);
65#endif /* CONFIG_FILS */
ff9f40ae
JM
66static void handle_auth(struct hostapd_data *hapd,
67 const struct ieee80211_mgmt *mgmt, size_t len,
68 int rssi, int from_queue);
5e5f8c81 69
9c06f0f6
VN
70
71u8 * hostapd_eid_multi_ap(struct hostapd_data *hapd, u8 *eid)
72{
73 u8 multi_ap_val = 0;
74
75 if (!hapd->conf->multi_ap)
76 return eid;
77 if (hapd->conf->multi_ap & BACKHAUL_BSS)
78 multi_ap_val |= MULTI_AP_BACKHAUL_BSS;
79 if (hapd->conf->multi_ap & FRONTHAUL_BSS)
80 multi_ap_val |= MULTI_AP_FRONTHAUL_BSS;
81
82 return eid + add_multi_ap_ie(eid, 9, multi_ap_val);
83}
84
85
6fc6879b
JM
86u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
87{
88 u8 *pos = eid;
89 int i, num, count;
90
91 if (hapd->iface->current_rates == NULL)
92 return eid;
93
94 *pos++ = WLAN_EID_SUPP_RATES;
95 num = hapd->iface->num_rates;
29448243
JM
96 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
97 num++;
202d97d4
JB
98 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
99 num++;
6fc6879b
JM
100 if (num > 8) {
101 /* rest of the rates are encoded in Extended supported
102 * rates element */
103 num = 8;
104 }
105
106 *pos++ = num;
6fc6879b
JM
107 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
108 i++) {
109 count++;
110 *pos = hapd->iface->current_rates[i].rate / 5;
111 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
112 *pos |= 0x80;
113 pos++;
114 }
115
202d97d4
JB
116 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
117 count++;
29448243 118 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
202d97d4
JB
119 }
120
121 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
122 count++;
123 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
124 }
29448243 125
6fc6879b
JM
126 return pos;
127}
128
129
130u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
131{
132 u8 *pos = eid;
133 int i, num, count;
134
135 if (hapd->iface->current_rates == NULL)
136 return eid;
137
138 num = hapd->iface->num_rates;
29448243
JM
139 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
140 num++;
202d97d4
JB
141 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
142 num++;
6fc6879b
JM
143 if (num <= 8)
144 return eid;
145 num -= 8;
146
147 *pos++ = WLAN_EID_EXT_SUPP_RATES;
148 *pos++ = num;
6fc6879b
JM
149 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
150 i++) {
151 count++;
152 if (count <= 8)
153 continue; /* already in SuppRates IE */
154 *pos = hapd->iface->current_rates[i].rate / 5;
155 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
156 *pos |= 0x80;
157 pos++;
158 }
159
202d97d4
JB
160 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
161 count++;
162 if (count > 8)
163 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
164 }
165
166 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
167 count++;
168 if (count > 8)
169 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
170 }
29448243 171
6fc6879b
JM
172 return pos;
173}
174
175
f41ded6f 176u16 hostapd_own_capab_info(struct hostapd_data *hapd)
6fc6879b
JM
177{
178 int capab = WLAN_CAPABILITY_ESS;
179 int privacy;
3d7ad2f6 180 int dfs;
01018212 181 int i;
3d7ad2f6
C
182
183 /* Check if any of configured channels require DFS */
184 dfs = hostapd_is_dfs_required(hapd->iface);
185 if (dfs < 0) {
186 wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
187 dfs);
188 dfs = 0;
189 }
6fc6879b
JM
190
191 if (hapd->iface->num_sta_no_short_preamble == 0 &&
192 hapd->iconf->preamble == SHORT_PREAMBLE)
193 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
194
195 privacy = hapd->conf->ssid.wep.keys_set;
196
197 if (hapd->conf->ieee802_1x &&
198 (hapd->conf->default_wep_key_len ||
199 hapd->conf->individual_wep_key_len))
200 privacy = 1;
201
202 if (hapd->conf->wpa)
203 privacy = 1;
204
a14896e8
JM
205#ifdef CONFIG_HS20
206 if (hapd->conf->osen)
207 privacy = 1;
208#endif /* CONFIG_HS20 */
209
6fc6879b
JM
210 if (privacy)
211 capab |= WLAN_CAPABILITY_PRIVACY;
212
213 if (hapd->iface->current_mode &&
214 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
215 hapd->iface->num_sta_no_short_slot_time == 0)
216 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
217
3d7ad2f6
C
218 /*
219 * Currently, Spectrum Management capability bit is set when directly
220 * requested in configuration by spectrum_mgmt_required or when AP is
221 * running on DFS channel.
222 * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
223 */
224 if (hapd->iface->current_mode &&
225 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
226 (hapd->iconf->spectrum_mgmt_required || dfs))
227 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
228
01018212
DS
229 for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
230 if (hapd->conf->radio_measurements[i]) {
231 capab |= IEEE80211_CAP_RRM;
232 break;
233 }
234 }
0629eeb4 235
6fc6879b
JM
236 return capab;
237}
238
239
7cb53ded 240#ifndef CONFIG_NO_RC4
6fc6879b 241static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
b57e086c
JM
242 u16 auth_transaction, const u8 *challenge,
243 int iswep)
6fc6879b
JM
244{
245 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
246 HOSTAPD_LEVEL_DEBUG,
247 "authentication (shared key, transaction %d)",
248 auth_transaction);
249
250 if (auth_transaction == 1) {
251 if (!sta->challenge) {
252 /* Generate a pseudo-random challenge */
253 u8 key[8];
f441e5af 254
6fc6879b
JM
255 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
256 if (sta->challenge == NULL)
257 return WLAN_STATUS_UNSPECIFIED_FAILURE;
258
f441e5af
NL
259 if (os_get_random(key, sizeof(key)) < 0) {
260 os_free(sta->challenge);
261 sta->challenge = NULL;
262 return WLAN_STATUS_UNSPECIFIED_FAILURE;
263 }
264
8ef16831
JM
265 rc4_skip(key, sizeof(key), 0,
266 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
6fc6879b
JM
267 }
268 return 0;
269 }
270
271 if (auth_transaction != 3)
272 return WLAN_STATUS_UNSPECIFIED_FAILURE;
273
274 /* Transaction 3 */
275 if (!iswep || !sta->challenge || !challenge ||
34ef46ce
JM
276 os_memcmp_const(sta->challenge, challenge,
277 WLAN_AUTH_CHALLENGE_LEN)) {
6fc6879b
JM
278 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
279 HOSTAPD_LEVEL_INFO,
280 "shared key authentication - invalid "
281 "challenge-response");
282 return WLAN_STATUS_CHALLENGE_FAIL;
283 }
284
285 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
286 HOSTAPD_LEVEL_DEBUG,
287 "authentication OK (shared key)");
6fc6879b
JM
288 sta->flags |= WLAN_STA_AUTH;
289 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
6fc6879b
JM
290 os_free(sta->challenge);
291 sta->challenge = NULL;
292
293 return 0;
294}
7cb53ded 295#endif /* CONFIG_NO_RC4 */
6fc6879b
JM
296
297
bb598c3b
AB
298static int send_auth_reply(struct hostapd_data *hapd,
299 const u8 *dst, const u8 *bssid,
300 u16 auth_alg, u16 auth_transaction, u16 resp,
e7525a29 301 const u8 *ies, size_t ies_len, const char *dbg)
6fc6879b
JM
302{
303 struct ieee80211_mgmt *reply;
304 u8 *buf;
305 size_t rlen;
bb598c3b 306 int reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
307
308 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
309 buf = os_zalloc(rlen);
310 if (buf == NULL)
bb598c3b 311 return -1;
6fc6879b
JM
312
313 reply = (struct ieee80211_mgmt *) buf;
314 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
315 WLAN_FC_STYPE_AUTH);
6fc6879b
JM
316 os_memcpy(reply->da, dst, ETH_ALEN);
317 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
318 os_memcpy(reply->bssid, bssid, ETH_ALEN);
319
320 reply->u.auth.auth_alg = host_to_le16(auth_alg);
321 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
322 reply->u.auth.status_code = host_to_le16(resp);
323
324 if (ies && ies_len)
325 os_memcpy(reply->u.auth.variable, ies, ies_len);
326
327 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
e7525a29 328 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu) (dbg=%s)",
6fc6879b 329 MAC2STR(dst), auth_alg, auth_transaction,
e7525a29 330 resp, (unsigned long) ies_len, dbg);
8cfa3527 331 if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
bb598c3b
AB
332 wpa_printf(MSG_INFO, "send_auth_reply: send failed");
333 else
334 reply_res = WLAN_STATUS_SUCCESS;
6fc6879b
JM
335
336 os_free(buf);
bb598c3b
AB
337
338 return reply_res;
6fc6879b
JM
339}
340
341
4ec1fd8e 342#ifdef CONFIG_IEEE80211R_AP
6fc6879b
JM
343static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
344 u16 auth_transaction, u16 status,
345 const u8 *ies, size_t ies_len)
346{
347 struct hostapd_data *hapd = ctx;
348 struct sta_info *sta;
bb598c3b 349 int reply_res;
6fc6879b 350
bb598c3b 351 reply_res = send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT,
e7525a29
BG
352 auth_transaction, status, ies, ies_len,
353 "auth-ft-finish");
6fc6879b
JM
354
355 sta = ap_get_sta(hapd, dst);
356 if (sta == NULL)
357 return;
358
bb598c3b
AB
359 if (sta->added_unassoc && (reply_res != WLAN_STATUS_SUCCESS ||
360 status != WLAN_STATUS_SUCCESS)) {
361 hostapd_drv_sta_remove(hapd, sta->addr);
362 sta->added_unassoc = 0;
363 return;
364 }
365
366 if (status != WLAN_STATUS_SUCCESS)
367 return;
368
6fc6879b
JM
369 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
370 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
371 sta->flags |= WLAN_STA_AUTH;
372 mlme_authenticate_indication(hapd, sta);
373}
4ec1fd8e 374#endif /* CONFIG_IEEE80211R_AP */
6fc6879b
JM
375
376
c10347f2 377#ifdef CONFIG_SAE
21af6d15 378
9249afc8
JM
379static void sae_set_state(struct sta_info *sta, enum sae_state state,
380 const char *reason)
381{
382 wpa_printf(MSG_DEBUG, "SAE: State %s -> %s for peer " MACSTR " (%s)",
383 sae_state_txt(sta->sae->state), sae_state_txt(state),
384 MAC2STR(sta->addr), reason);
385 sta->sae->state = state;
386}
387
388
e96da42b 389static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
872b7545 390 struct sta_info *sta, int update)
21af6d15
JM
391{
392 struct wpabuf *buf;
9be19d0b
JM
393 const char *password = NULL;
394 struct sae_password_entry *pw;
395 const char *rx_id = NULL;
21af6d15 396
9be19d0b
JM
397 if (sta->sae->tmp)
398 rx_id = sta->sae->tmp->pw_id;
399
400 for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
401 if (!is_broadcast_ether_addr(pw->peer_addr) &&
402 os_memcmp(pw->peer_addr, sta->addr, ETH_ALEN) != 0)
403 continue;
404 if ((rx_id && !pw->identifier) || (!rx_id && pw->identifier))
405 continue;
406 if (rx_id && pw->identifier &&
407 os_strcmp(rx_id, pw->identifier) != 0)
408 continue;
409 password = pw->password;
410 break;
411 }
2377c1ca
JM
412 if (!password)
413 password = hapd->conf->ssid.wpa_passphrase;
414 if (!password) {
146f6c9a
JM
415 wpa_printf(MSG_DEBUG, "SAE: No password available");
416 return NULL;
417 }
418
872b7545
MH
419 if (update &&
420 sae_prepare_commit(hapd->own_addr, sta->addr,
9be19d0b 421 (u8 *) password, os_strlen(password), rx_id,
8e31e955
JM
422 sta->sae) < 0) {
423 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
21af6d15 424 return NULL;
8e31e955 425 }
21af6d15 426
b0e91e38
JM
427 if (pw && pw->vlan_id) {
428 if (!sta->sae->tmp) {
429 wpa_printf(MSG_INFO,
430 "SAE: No temporary data allocated - cannot store VLAN ID");
431 return NULL;
432 }
433 sta->sae->tmp->vlan_id = pw->vlan_id;
434 }
435
9be19d0b
JM
436 buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN +
437 (rx_id ? 3 + os_strlen(rx_id) : 0));
8e31e955
JM
438 if (buf == NULL)
439 return NULL;
872b7545 440 sae_write_commit(sta->sae, buf, sta->sae->tmp ?
9be19d0b 441 sta->sae->tmp->anti_clogging_token : NULL, rx_id);
21af6d15
JM
442
443 return buf;
444}
445
446
447static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
448 struct sta_info *sta)
449{
450 struct wpabuf *buf;
451
fb8fcc29 452 buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
21af6d15
JM
453 if (buf == NULL)
454 return NULL;
455
fb8fcc29 456 sae_write_confirm(sta->sae, buf);
21af6d15
JM
457
458 return buf;
459}
460
461
e96da42b
BC
462static int auth_sae_send_commit(struct hostapd_data *hapd,
463 struct sta_info *sta,
872b7545 464 const u8 *bssid, int update)
e96da42b
BC
465{
466 struct wpabuf *data;
bb598c3b 467 int reply_res;
e96da42b 468
872b7545 469 data = auth_build_sae_commit(hapd, sta, update);
9be19d0b
JM
470 if (!data && sta->sae->tmp && sta->sae->tmp->pw_id)
471 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
e96da42b
BC
472 if (data == NULL)
473 return WLAN_STATUS_UNSPECIFIED_FAILURE;
474
bb598c3b
AB
475 reply_res = send_auth_reply(hapd, sta->addr, bssid, WLAN_AUTH_SAE, 1,
476 WLAN_STATUS_SUCCESS, wpabuf_head(data),
e7525a29 477 wpabuf_len(data), "sae-send-commit");
e96da42b
BC
478
479 wpabuf_free(data);
480
bb598c3b 481 return reply_res;
e96da42b
BC
482}
483
484
485static int auth_sae_send_confirm(struct hostapd_data *hapd,
486 struct sta_info *sta,
487 const u8 *bssid)
488{
489 struct wpabuf *data;
bb598c3b 490 int reply_res;
e96da42b
BC
491
492 data = auth_build_sae_confirm(hapd, sta);
493 if (data == NULL)
494 return WLAN_STATUS_UNSPECIFIED_FAILURE;
495
bb598c3b
AB
496 reply_res = send_auth_reply(hapd, sta->addr, bssid, WLAN_AUTH_SAE, 2,
497 WLAN_STATUS_SUCCESS, wpabuf_head(data),
e7525a29 498 wpabuf_len(data), "sae-send-confirm");
e96da42b
BC
499
500 wpabuf_free(data);
501
bb598c3b 502 return reply_res;
e96da42b
BC
503}
504
505
d136c376
JM
506static int use_sae_anti_clogging(struct hostapd_data *hapd)
507{
508 struct sta_info *sta;
509 unsigned int open = 0;
510
511 if (hapd->conf->sae_anti_clogging_threshold == 0)
512 return 1;
513
514 for (sta = hapd->sta_list; sta; sta = sta->next) {
515 if (!sta->sae)
516 continue;
517 if (sta->sae->state != SAE_COMMITTED &&
518 sta->sae->state != SAE_CONFIRMED)
519 continue;
520 open++;
521 if (open >= hapd->conf->sae_anti_clogging_threshold)
522 return 1;
523 }
524
ff9f40ae
JM
525 /* In addition to already existing open SAE sessions, check whether
526 * there are enough pending commit messages in the processing queue to
527 * potentially result in too many open sessions. */
528 if (open + dl_list_len(&hapd->sae_commit_queue) >=
529 hapd->conf->sae_anti_clogging_threshold)
530 return 1;
531
d136c376
JM
532 return 0;
533}
534
535
a9af1da0
JM
536static u8 sae_token_hash(struct hostapd_data *hapd, const u8 *addr)
537{
538 u8 hash[SHA256_MAC_LEN];
539
540 hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
541 addr, ETH_ALEN, hash);
542 return hash[0];
543}
544
545
d136c376
JM
546static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
547 const u8 *token, size_t token_len)
548{
549 u8 mac[SHA256_MAC_LEN];
a9af1da0
JM
550 const u8 *addrs[2];
551 size_t len[2];
552 u16 token_idx;
553 u8 idx;
d136c376
JM
554
555 if (token_len != SHA256_MAC_LEN)
556 return -1;
a9af1da0
JM
557 idx = sae_token_hash(hapd, addr);
558 token_idx = hapd->sae_pending_token_idx[idx];
559 if (token_idx == 0 || token_idx != WPA_GET_BE16(token)) {
560 wpa_printf(MSG_DEBUG, "SAE: Invalid anti-clogging token from "
561 MACSTR " - token_idx 0x%04x, expected 0x%04x",
562 MAC2STR(addr), WPA_GET_BE16(token), token_idx);
563 return -1;
564 }
565
566 addrs[0] = addr;
567 len[0] = ETH_ALEN;
568 addrs[1] = token;
569 len[1] = 2;
570 if (hmac_sha256_vector(hapd->sae_token_key, sizeof(hapd->sae_token_key),
571 2, addrs, len, mac) < 0 ||
572 os_memcmp_const(token + 2, &mac[2], SHA256_MAC_LEN - 2) != 0)
d136c376
JM
573 return -1;
574
a9af1da0
JM
575 hapd->sae_pending_token_idx[idx] = 0; /* invalidate used token */
576
d136c376
JM
577 return 0;
578}
579
580
581static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
a959a3b6 582 int group, const u8 *addr)
d136c376
JM
583{
584 struct wpabuf *buf;
585 u8 *token;
fe52c210 586 struct os_reltime now;
a9af1da0
JM
587 u8 idx[2];
588 const u8 *addrs[2];
589 size_t len[2];
590 u8 p_idx;
591 u16 token_idx;
d136c376 592
fe52c210
JB
593 os_get_reltime(&now);
594 if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
a9af1da0
JM
595 os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60) ||
596 hapd->sae_token_idx == 0xffff) {
a50414c3
JM
597 if (random_get_bytes(hapd->sae_token_key,
598 sizeof(hapd->sae_token_key)) < 0)
599 return NULL;
d136c376
JM
600 wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
601 hapd->sae_token_key, sizeof(hapd->sae_token_key));
fe52c210 602 hapd->last_sae_token_key_update = now;
a9af1da0
JM
603 hapd->sae_token_idx = 0;
604 os_memset(hapd->sae_pending_token_idx, 0,
605 sizeof(hapd->sae_pending_token_idx));
d136c376
JM
606 }
607
a959a3b6 608 buf = wpabuf_alloc(sizeof(le16) + SHA256_MAC_LEN);
d136c376
JM
609 if (buf == NULL)
610 return NULL;
611
a959a3b6
MH
612 wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
613
a9af1da0
JM
614 p_idx = sae_token_hash(hapd, addr);
615 token_idx = hapd->sae_pending_token_idx[p_idx];
616 if (!token_idx) {
617 hapd->sae_token_idx++;
618 token_idx = hapd->sae_token_idx;
619 hapd->sae_pending_token_idx[p_idx] = token_idx;
620 }
621 WPA_PUT_BE16(idx, token_idx);
d136c376 622 token = wpabuf_put(buf, SHA256_MAC_LEN);
a9af1da0
JM
623 addrs[0] = addr;
624 len[0] = ETH_ALEN;
625 addrs[1] = idx;
626 len[1] = sizeof(idx);
627 if (hmac_sha256_vector(hapd->sae_token_key, sizeof(hapd->sae_token_key),
628 2, addrs, len, token) < 0) {
629 wpabuf_free(buf);
630 return NULL;
631 }
632 WPA_PUT_BE16(token, token_idx);
d136c376
JM
633
634 return buf;
635}
636
637
d8b841eb 638static int sae_check_big_sync(struct hostapd_data *hapd, struct sta_info *sta)
f3b8ad4d 639{
d8b841eb 640 if (sta->sae->sync > hapd->conf->sae_sync) {
9249afc8 641 sae_set_state(sta, SAE_NOTHING, "Sync > dot11RSNASAESync");
f3b8ad4d
BC
642 sta->sae->sync = 0;
643 return -1;
644 }
645 return 0;
646}
647
648
649static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
650{
651 struct hostapd_data *hapd = eloop_ctx;
652 struct sta_info *sta = eloop_data;
653 int ret;
654
d8b841eb 655 if (sae_check_big_sync(hapd, sta))
f3b8ad4d
BC
656 return;
657 sta->sae->sync++;
dad01292 658 wpa_printf(MSG_DEBUG, "SAE: Auth SAE retransmit timer for " MACSTR
9249afc8
JM
659 " (sync=%d state=%s)",
660 MAC2STR(sta->addr), sta->sae->sync,
661 sae_state_txt(sta->sae->state));
f3b8ad4d
BC
662
663 switch (sta->sae->state) {
664 case SAE_COMMITTED:
665 ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
ecd40fef
MH
666 eloop_register_timeout(0,
667 hapd->dot11RSNASAERetransPeriod * 1000,
f3b8ad4d
BC
668 auth_sae_retransmit_timer, hapd, sta);
669 break;
670 case SAE_CONFIRMED:
671 ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
ecd40fef
MH
672 eloop_register_timeout(0,
673 hapd->dot11RSNASAERetransPeriod * 1000,
f3b8ad4d
BC
674 auth_sae_retransmit_timer, hapd, sta);
675 break;
676 default:
677 ret = -1;
678 break;
679 }
680
681 if (ret != WLAN_STATUS_SUCCESS)
682 wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
683}
684
685
686void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
687{
688 eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
689}
690
691
692static void sae_set_retransmit_timer(struct hostapd_data *hapd,
693 struct sta_info *sta)
694{
695 if (!(hapd->conf->mesh & MESH_ENABLED))
696 return;
697
698 eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
ecd40fef 699 eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
f3b8ad4d
BC
700 auth_sae_retransmit_timer, hapd, sta);
701}
702
703
4ffb0fef
SD
704static void sae_sme_send_external_auth_status(struct hostapd_data *hapd,
705 struct sta_info *sta, u16 status)
706{
707 struct external_auth params;
708
709 os_memset(&params, 0, sizeof(params));
710 params.status = status;
dd1a8cef 711 params.bssid = sta->addr;
4ffb0fef
SD
712 if (status == WLAN_STATUS_SUCCESS && sta->sae)
713 params.pmkid = sta->sae->pmkid;
714
715 hostapd_drv_send_external_auth_status(hapd, &params);
716}
717
718
9f2cf23e
MH
719void sae_accept_sta(struct hostapd_data *hapd, struct sta_info *sta)
720{
b0e91e38
JM
721#ifndef CONFIG_NO_VLAN
722 struct vlan_description vlan_desc;
723
724 if (sta->sae->tmp && sta->sae->tmp->vlan_id > 0) {
725 wpa_printf(MSG_DEBUG, "SAE: Assign STA " MACSTR
726 " to VLAN ID %d",
727 MAC2STR(sta->addr), sta->sae->tmp->vlan_id);
728
729 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
730 vlan_desc.notempty = 1;
731 vlan_desc.untagged = sta->sae->tmp->vlan_id;
732 if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
733 wpa_printf(MSG_INFO,
734 "Invalid VLAN ID %d in sae_password",
735 sta->sae->tmp->vlan_id);
736 return;
737 }
738
739 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0 ||
740 ap_sta_bind_vlan(hapd, sta) < 0) {
741 wpa_printf(MSG_INFO,
742 "Failed to assign VLAN ID %d from sae_password to "
743 MACSTR, sta->sae->tmp->vlan_id,
744 MAC2STR(sta->addr));
745 return;
746 }
747 }
748#endif /* CONFIG_NO_VLAN */
749
9f2cf23e
MH
750 sta->flags |= WLAN_STA_AUTH;
751 sta->auth_alg = WLAN_AUTH_SAE;
752 mlme_authenticate_indication(hapd, sta);
753 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
9249afc8 754 sae_set_state(sta, SAE_ACCEPTED, "Accept Confirm");
9f2cf23e
MH
755 wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
756 sta->sae->pmk, sta->sae->pmkid);
4ffb0fef 757 sae_sme_send_external_auth_status(hapd, sta, WLAN_STATUS_SUCCESS);
9f2cf23e
MH
758}
759
760
e96da42b 761static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
10cf866b
JM
762 const u8 *bssid, u8 auth_transaction, int allow_reuse,
763 int *sta_removed)
e96da42b
BC
764{
765 int ret;
766
10cf866b
JM
767 *sta_removed = 0;
768
e96da42b
BC
769 if (auth_transaction != 1 && auth_transaction != 2)
770 return WLAN_STATUS_UNSPECIFIED_FAILURE;
771
9249afc8
JM
772 wpa_printf(MSG_DEBUG, "SAE: Peer " MACSTR " state=%s auth_trans=%u",
773 MAC2STR(sta->addr), sae_state_txt(sta->sae->state),
774 auth_transaction);
e96da42b
BC
775 switch (sta->sae->state) {
776 case SAE_NOTHING:
777 if (auth_transaction == 1) {
f2143615
JM
778 ret = auth_sae_send_commit(hapd, sta, bssid,
779 !allow_reuse);
e96da42b
BC
780 if (ret)
781 return ret;
9249afc8 782 sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
e96da42b
BC
783
784 if (sae_process_commit(sta->sae) < 0)
785 return WLAN_STATUS_UNSPECIFIED_FAILURE;
786
787 /*
788 * In mesh case, both Commit and Confirm can be sent
789 * immediately. In infrastructure BSS, only a single
790 * Authentication frame (Commit) is expected from the AP
791 * here and the second one (Confirm) will be sent once
792 * the STA has sent its second Authentication frame
793 * (Confirm).
794 */
795 if (hapd->conf->mesh & MESH_ENABLED) {
796 /*
797 * Send both Commit and Confirm immediately
798 * based on SAE finite state machine
799 * Nothing -> Confirm transition.
800 */
801 ret = auth_sae_send_confirm(hapd, sta, bssid);
802 if (ret)
803 return ret;
9249afc8
JM
804 sae_set_state(sta, SAE_CONFIRMED,
805 "Sent Confirm (mesh)");
e96da42b
BC
806 } else {
807 /*
808 * For infrastructure BSS, send only the Commit
809 * message now to get alternating sequence of
810 * Authentication frames between the AP and STA.
811 * Confirm will be sent in
746e5c25 812 * Committed -> Confirmed/Accepted transition
e96da42b
BC
813 * when receiving Confirm from STA.
814 */
815 }
f3b8ad4d
BC
816 sta->sae->sync = 0;
817 sae_set_retransmit_timer(hapd, sta);
e96da42b
BC
818 } else {
819 hostapd_logger(hapd, sta->addr,
820 HOSTAPD_MODULE_IEEE80211,
821 HOSTAPD_LEVEL_DEBUG,
822 "SAE confirm before commit");
823 }
824 break;
825 case SAE_COMMITTED:
f3b8ad4d 826 sae_clear_retransmit_timer(hapd, sta);
e96da42b
BC
827 if (auth_transaction == 1) {
828 if (sae_process_commit(sta->sae) < 0)
829 return WLAN_STATUS_UNSPECIFIED_FAILURE;
830
831 ret = auth_sae_send_confirm(hapd, sta, bssid);
832 if (ret)
833 return ret;
9249afc8 834 sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
f3b8ad4d
BC
835 sta->sae->sync = 0;
836 sae_set_retransmit_timer(hapd, sta);
e96da42b
BC
837 } else if (hapd->conf->mesh & MESH_ENABLED) {
838 /*
839 * In mesh case, follow SAE finite state machine and
f3b8ad4d 840 * send Commit now, if sync count allows.
e96da42b 841 */
d8b841eb 842 if (sae_check_big_sync(hapd, sta))
f3b8ad4d
BC
843 return WLAN_STATUS_SUCCESS;
844 sta->sae->sync++;
845
fabc6dd8 846 ret = auth_sae_send_commit(hapd, sta, bssid, 0);
e96da42b
BC
847 if (ret)
848 return ret;
f3b8ad4d
BC
849
850 sae_set_retransmit_timer(hapd, sta);
e96da42b
BC
851 } else {
852 /*
853 * For instructure BSS, send the postponed Confirm from
854 * Nothing -> Confirmed transition that was reduced to
855 * Nothing -> Committed above.
856 */
857 ret = auth_sae_send_confirm(hapd, sta, bssid);
858 if (ret)
859 return ret;
860
9249afc8 861 sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
e96da42b
BC
862
863 /*
864 * Since this was triggered on Confirm RX, run another
865 * step to get to Accepted without waiting for
866 * additional events.
867 */
f2143615 868 return sae_sm_step(hapd, sta, bssid, auth_transaction,
10cf866b 869 0, sta_removed);
e96da42b
BC
870 }
871 break;
872 case SAE_CONFIRMED:
f3b8ad4d 873 sae_clear_retransmit_timer(hapd, sta);
e96da42b 874 if (auth_transaction == 1) {
d8b841eb 875 if (sae_check_big_sync(hapd, sta))
f3b8ad4d
BC
876 return WLAN_STATUS_SUCCESS;
877 sta->sae->sync++;
878
872b7545 879 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
e96da42b
BC
880 if (ret)
881 return ret;
882
883 if (sae_process_commit(sta->sae) < 0)
884 return WLAN_STATUS_UNSPECIFIED_FAILURE;
885
886 ret = auth_sae_send_confirm(hapd, sta, bssid);
887 if (ret)
888 return ret;
f3b8ad4d
BC
889
890 sae_set_retransmit_timer(hapd, sta);
e96da42b 891 } else {
c4fc7e31 892 sta->sae->send_confirm = 0xffff;
9f2cf23e 893 sae_accept_sta(hapd, sta);
e96da42b
BC
894 }
895 break;
896 case SAE_ACCEPTED:
e8d08cf3
JM
897 if (auth_transaction == 1 &&
898 (hapd->conf->mesh & MESH_ENABLED)) {
e96da42b
BC
899 wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
900 ") doing reauthentication",
901 MAC2STR(sta->addr));
9f2cf23e 902 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
153d4c50 903 ap_free_sta(hapd, sta);
10cf866b 904 *sta_removed = 1;
e8d08cf3
JM
905 } else if (auth_transaction == 1) {
906 wpa_printf(MSG_DEBUG, "SAE: Start reauthentication");
907 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
908 if (ret)
909 return ret;
910 sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
911
912 if (sae_process_commit(sta->sae) < 0)
913 return WLAN_STATUS_UNSPECIFIED_FAILURE;
914 sta->sae->sync = 0;
915 sae_set_retransmit_timer(hapd, sta);
e96da42b 916 } else {
d8b841eb 917 if (sae_check_big_sync(hapd, sta))
f3b8ad4d
BC
918 return WLAN_STATUS_SUCCESS;
919 sta->sae->sync++;
920
e96da42b
BC
921 ret = auth_sae_send_confirm(hapd, sta, bssid);
922 sae_clear_temp_data(sta->sae);
923 if (ret)
924 return ret;
925 }
926 break;
927 default:
928 wpa_printf(MSG_ERROR, "SAE: invalid state %d",
929 sta->sae->state);
930 return WLAN_STATUS_UNSPECIFIED_FAILURE;
931 }
932 return WLAN_STATUS_SUCCESS;
933}
934
935
dad01292
JM
936static void sae_pick_next_group(struct hostapd_data *hapd, struct sta_info *sta)
937{
938 struct sae_data *sae = sta->sae;
939 int i, *groups = hapd->conf->sae_groups;
941bad5e 940 int default_groups[] = { 19, 0 };
dad01292
JM
941
942 if (sae->state != SAE_COMMITTED)
943 return;
944
945 wpa_printf(MSG_DEBUG, "SAE: Previously selected group: %d", sae->group);
946
941bad5e
JM
947 if (!groups)
948 groups = default_groups;
949 for (i = 0; groups[i] > 0; i++) {
dad01292
JM
950 if (sae->group == groups[i])
951 break;
952 }
953
941bad5e 954 if (groups[i] <= 0) {
dad01292
JM
955 wpa_printf(MSG_DEBUG,
956 "SAE: Previously selected group not found from the current configuration");
957 return;
958 }
959
960 for (;;) {
961 i++;
962 if (groups[i] <= 0) {
963 wpa_printf(MSG_DEBUG,
964 "SAE: No alternative group enabled");
965 return;
966 }
967
968 if (sae_set_group(sae, groups[i]) < 0)
969 continue;
970
971 break;
972 }
973 wpa_printf(MSG_DEBUG, "SAE: Selected new group: %d", groups[i]);
974}
975
976
c10347f2
JM
977static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
978 const struct ieee80211_mgmt *mgmt, size_t len,
afa2ffb4 979 u16 auth_transaction, u16 status_code)
c10347f2 980{
bb598c3b 981 int resp = WLAN_STATUS_SUCCESS;
750efe6e 982 struct wpabuf *data = NULL;
941bad5e
JM
983 int *groups = hapd->conf->sae_groups;
984 int default_groups[] = { 19, 0 };
96d6dfa8 985 const u8 *pos, *end;
10cf866b 986 int sta_removed = 0;
941bad5e
JM
987
988 if (!groups)
989 groups = default_groups;
c10347f2 990
e7533538
JM
991#ifdef CONFIG_TESTING_OPTIONS
992 if (hapd->conf->sae_reflection_attack && auth_transaction == 1) {
e7533538
JM
993 wpa_printf(MSG_DEBUG, "SAE: TESTING - reflection attack");
994 pos = mgmt->u.auth.variable;
995 end = ((const u8 *) mgmt) + len;
996 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
e7525a29
BG
997 auth_transaction, resp, pos, end - pos,
998 "auth-sae-reflection-attack");
e7533538
JM
999 goto remove_sta;
1000 }
3648d8a1
JM
1001
1002 if (hapd->conf->sae_commit_override && auth_transaction == 1) {
1003 wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
1004 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
1005 auth_transaction, resp,
1006 wpabuf_head(hapd->conf->sae_commit_override),
e7525a29
BG
1007 wpabuf_len(hapd->conf->sae_commit_override),
1008 "sae-commit-override");
3648d8a1
JM
1009 goto remove_sta;
1010 }
e7533538 1011#endif /* CONFIG_TESTING_OPTIONS */
98efcc41 1012 if (!sta->sae) {
bb598c3b
AB
1013 if (auth_transaction != 1 ||
1014 status_code != WLAN_STATUS_SUCCESS) {
1015 resp = -1;
1016 goto remove_sta;
1017 }
98efcc41 1018 sta->sae = os_zalloc(sizeof(*sta->sae));
bb598c3b
AB
1019 if (!sta->sae) {
1020 resp = -1;
1021 goto remove_sta;
1022 }
9249afc8 1023 sae_set_state(sta, SAE_NOTHING, "Init");
f3b8ad4d 1024 sta->sae->sync = 0;
98efcc41
JM
1025 }
1026
9f2cf23e
MH
1027 if (sta->mesh_sae_pmksa_caching) {
1028 wpa_printf(MSG_DEBUG,
1029 "SAE: Cancel use of mesh PMKSA caching because peer starts SAE authentication");
1030 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1031 sta->mesh_sae_pmksa_caching = 0;
1032 }
1033
c10347f2 1034 if (auth_transaction == 1) {
96d6dfa8 1035 const u8 *token = NULL;
d136c376 1036 size_t token_len = 0;
f2143615
JM
1037 int allow_reuse = 0;
1038
c10347f2
JM
1039 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1040 HOSTAPD_LEVEL_DEBUG,
9c951244
AK
1041 "start SAE authentication (RX commit, status=%u (%s))",
1042 status_code, status2str(status_code));
872b7545
MH
1043
1044 if ((hapd->conf->mesh & MESH_ENABLED) &&
afa2ffb4
JM
1045 status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
1046 sta->sae->tmp) {
a959a3b6
MH
1047 pos = mgmt->u.auth.variable;
1048 end = ((const u8 *) mgmt) + len;
1049 if (pos + sizeof(le16) > end) {
1050 wpa_printf(MSG_ERROR,
1051 "SAE: Too short anti-clogging token request");
1052 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1053 goto reply;
1054 }
941bad5e 1055 resp = sae_group_allowed(sta->sae, groups,
a959a3b6
MH
1056 WPA_GET_LE16(pos));
1057 if (resp != WLAN_STATUS_SUCCESS) {
1058 wpa_printf(MSG_ERROR,
1059 "SAE: Invalid group in anti-clogging token request");
1060 goto reply;
1061 }
1062 pos += sizeof(le16);
1063
872b7545
MH
1064 wpabuf_free(sta->sae->tmp->anti_clogging_token);
1065 sta->sae->tmp->anti_clogging_token =
a959a3b6 1066 wpabuf_alloc_copy(pos, end - pos);
872b7545
MH
1067 if (sta->sae->tmp->anti_clogging_token == NULL) {
1068 wpa_printf(MSG_ERROR,
1069 "SAE: Failed to alloc for anti-clogging token");
bb598c3b
AB
1070 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1071 goto remove_sta;
872b7545
MH
1072 }
1073
1074 /*
1075 * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
1076 * is 76, a new Commit Message shall be constructed
1077 * with the Anti-Clogging Token from the received
1078 * Authentication frame, and the commit-scalar and
1079 * COMMIT-ELEMENT previously sent.
1080 */
bb598c3b
AB
1081 resp = auth_sae_send_commit(hapd, sta, mgmt->bssid, 0);
1082 if (resp != WLAN_STATUS_SUCCESS) {
872b7545
MH
1083 wpa_printf(MSG_ERROR,
1084 "SAE: Failed to send commit message");
bb598c3b 1085 goto remove_sta;
872b7545 1086 }
9249afc8
JM
1087 sae_set_state(sta, SAE_COMMITTED,
1088 "Sent Commit (anti-clogging token case in mesh)");
f3b8ad4d
BC
1089 sta->sae->sync = 0;
1090 sae_set_retransmit_timer(hapd, sta);
872b7545
MH
1091 return;
1092 }
1093
dad01292
JM
1094 if ((hapd->conf->mesh & MESH_ENABLED) &&
1095 status_code ==
1096 WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1097 sta->sae->tmp) {
1098 wpa_printf(MSG_DEBUG,
1099 "SAE: Peer did not accept our SAE group");
1100 sae_pick_next_group(hapd, sta);
1101 goto remove_sta;
1102 }
1103
afa2ffb4 1104 if (status_code != WLAN_STATUS_SUCCESS)
bb598c3b 1105 goto remove_sta;
afa2ffb4 1106
3a8dd390
JM
1107 if (!(hapd->conf->mesh & MESH_ENABLED) &&
1108 sta->sae->state == SAE_COMMITTED) {
1109 /* This is needed in the infrastructure BSS case to
1110 * address a sequence where a STA entry may remain in
1111 * hostapd across two attempts to do SAE authentication
1112 * by the same STA. The second attempt may end up trying
1113 * to use a different group and that would not be
1114 * allowed if we remain in Committed state with the
1115 * previously set parameters. */
f2143615
JM
1116 pos = mgmt->u.auth.variable;
1117 end = ((const u8 *) mgmt) + len;
1118 if (end - pos >= (int) sizeof(le16) &&
1119 sae_group_allowed(sta->sae, groups,
1120 WPA_GET_LE16(pos)) ==
1121 WLAN_STATUS_SUCCESS) {
1122 /* Do not waste resources deriving the same PWE
1123 * again since the same group is reused. */
1124 sae_set_state(sta, SAE_NOTHING,
1125 "Allow previous PWE to be reused");
1126 allow_reuse = 1;
1127 } else {
1128 sae_set_state(sta, SAE_NOTHING,
1129 "Clear existing state to allow restart");
1130 sae_clear_data(sta->sae);
1131 }
3a8dd390
JM
1132 }
1133
146f6c9a
JM
1134 resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
1135 ((const u8 *) mgmt) + len -
d136c376 1136 mgmt->u.auth.variable, &token,
941bad5e 1137 &token_len, groups);
6a58444d
JM
1138 if (resp == SAE_SILENTLY_DISCARD) {
1139 wpa_printf(MSG_DEBUG,
1140 "SAE: Drop commit message from " MACSTR " due to reflection attack",
1141 MAC2STR(sta->addr));
bb598c3b 1142 goto remove_sta;
6a58444d 1143 }
9be19d0b
JM
1144
1145 if (resp == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER) {
1146 wpa_msg(hapd->msg_ctx, MSG_INFO,
1147 WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER
1148 MACSTR, MAC2STR(sta->addr));
1149 sae_clear_retransmit_timer(hapd, sta);
1150 sae_set_state(sta, SAE_NOTHING,
1151 "Unknown Password Identifier");
1152 goto remove_sta;
1153 }
1154
d136c376
JM
1155 if (token && check_sae_token(hapd, sta->addr, token, token_len)
1156 < 0) {
1157 wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
1158 "incorrect token from " MACSTR,
1159 MAC2STR(sta->addr));
bb598c3b
AB
1160 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1161 goto remove_sta;
d136c376
JM
1162 }
1163
e96da42b
BC
1164 if (resp != WLAN_STATUS_SUCCESS)
1165 goto reply;
1166
f2143615 1167 if (!token && use_sae_anti_clogging(hapd) && !allow_reuse) {
e96da42b
BC
1168 wpa_printf(MSG_DEBUG,
1169 "SAE: Request anti-clogging token from "
1170 MACSTR, MAC2STR(sta->addr));
a959a3b6
MH
1171 data = auth_build_token_req(hapd, sta->sae->group,
1172 sta->addr);
e96da42b 1173 resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
872b7545 1174 if (hapd->conf->mesh & MESH_ENABLED)
9249afc8
JM
1175 sae_set_state(sta, SAE_NOTHING,
1176 "Request anti-clogging token case in mesh");
e96da42b 1177 goto reply;
750efe6e 1178 }
e96da42b 1179
f2143615 1180 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction,
10cf866b 1181 allow_reuse, &sta_removed);
c10347f2 1182 } else if (auth_transaction == 2) {
c10347f2
JM
1183 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1184 HOSTAPD_LEVEL_DEBUG,
9c951244
AK
1185 "SAE authentication (RX confirm, status=%u (%s))",
1186 status_code, status2str(status_code));
afa2ffb4 1187 if (status_code != WLAN_STATUS_SUCCESS)
bb598c3b 1188 goto remove_sta;
e96da42b
BC
1189 if (sta->sae->state >= SAE_CONFIRMED ||
1190 !(hapd->conf->mesh & MESH_ENABLED)) {
abcbd060
JM
1191 const u8 *var;
1192 size_t var_len;
1193 u16 peer_send_confirm;
1194
1195 var = mgmt->u.auth.variable;
1196 var_len = ((u8 *) mgmt) + len - mgmt->u.auth.variable;
1197 if (var_len < 2) {
1198 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1199 goto reply;
1200 }
1201
1202 peer_send_confirm = WPA_GET_LE16(var);
1203
1204 if (sta->sae->state == SAE_ACCEPTED &&
1205 (peer_send_confirm <= sta->sae->rc ||
1206 peer_send_confirm == 0xffff)) {
1207 wpa_printf(MSG_DEBUG,
1208 "SAE: Silently ignore unexpected Confirm from peer "
1209 MACSTR
1210 " (peer-send-confirm=%u Rc=%u)",
1211 MAC2STR(sta->addr),
1212 peer_send_confirm, sta->sae->rc);
1213 return;
1214 }
1215
1216 if (sae_check_confirm(sta->sae, var, var_len) < 0) {
750efe6e 1217 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
e96da42b 1218 goto reply;
b4fd3613 1219 }
abcbd060 1220 sta->sae->rc = peer_send_confirm;
21af6d15 1221 }
10cf866b
JM
1222 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction, 0,
1223 &sta_removed);
c10347f2
JM
1224 } else {
1225 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1226 HOSTAPD_LEVEL_DEBUG,
9c951244
AK
1227 "unexpected SAE authentication transaction %u (status=%u (%s))",
1228 auth_transaction, status_code,
1229 status2str(status_code));
afa2ffb4 1230 if (status_code != WLAN_STATUS_SUCCESS)
bb598c3b 1231 goto remove_sta;
c10347f2
JM
1232 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1233 }
1234
e96da42b 1235reply:
10cf866b 1236 if (!sta_removed && resp != WLAN_STATUS_SUCCESS) {
96d6dfa8
JM
1237 pos = mgmt->u.auth.variable;
1238 end = ((const u8 *) mgmt) + len;
1239
1240 /* Copy the Finite Cyclic Group field from the request if we
1241 * rejected it as unsupported group. */
1242 if (resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1243 !data && end - pos >= 2)
1244 data = wpabuf_alloc_copy(pos, 2);
1245
4ffb0fef 1246 sae_sme_send_external_auth_status(hapd, sta, resp);
e96da42b
BC
1247 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
1248 auth_transaction, resp,
1249 data ? wpabuf_head(data) : (u8 *) "",
e7525a29 1250 data ? wpabuf_len(data) : 0, "auth-sae");
e96da42b 1251 }
bb598c3b
AB
1252
1253remove_sta:
10cf866b
JM
1254 if (!sta_removed && sta->added_unassoc &&
1255 (resp != WLAN_STATUS_SUCCESS ||
1256 status_code != WLAN_STATUS_SUCCESS)) {
bb598c3b
AB
1257 hostapd_drv_sta_remove(hapd, sta->addr);
1258 sta->added_unassoc = 0;
1259 }
21af6d15 1260 wpabuf_free(data);
c10347f2 1261}
a206e2a1
BC
1262
1263
1264/**
1265 * auth_sae_init_committed - Send COMMIT and start SAE in committed state
1266 * @hapd: BSS data for the device initiating the authentication
1267 * @sta: the peer to which commit authentication frame is sent
1268 *
1269 * This function implements Init event handling (IEEE Std 802.11-2012,
1270 * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
1271 * sta->sae structure should be initialized appropriately via a call to
1272 * sae_prepare_commit().
1273 */
1274int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
1275{
1276 int ret;
1277
1278 if (!sta->sae || !sta->sae->tmp)
1279 return -1;
1280
1281 if (sta->sae->state != SAE_NOTHING)
1282 return -1;
1283
1284 ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
1285 if (ret)
1286 return -1;
1287
9249afc8 1288 sae_set_state(sta, SAE_COMMITTED, "Init and sent commit");
f3b8ad4d
BC
1289 sta->sae->sync = 0;
1290 sae_set_retransmit_timer(hapd, sta);
a206e2a1
BC
1291
1292 return 0;
1293}
1294
ff9f40ae
JM
1295
1296void auth_sae_process_commit(void *eloop_ctx, void *user_ctx)
1297{
1298 struct hostapd_data *hapd = eloop_ctx;
1299 struct hostapd_sae_commit_queue *q;
1300 unsigned int queue_len;
1301
1302 q = dl_list_first(&hapd->sae_commit_queue,
1303 struct hostapd_sae_commit_queue, list);
1304 if (!q)
1305 return;
1306 wpa_printf(MSG_DEBUG,
5e3a759c 1307 "SAE: Process next available message from queue");
ff9f40ae
JM
1308 dl_list_del(&q->list);
1309 handle_auth(hapd, (const struct ieee80211_mgmt *) q->msg, q->len,
1310 q->rssi, 1);
1311 os_free(q);
1312
1313 if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
1314 return;
1315 queue_len = dl_list_len(&hapd->sae_commit_queue);
ff5f54e1 1316 eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
ff9f40ae
JM
1317 hapd, NULL);
1318}
1319
1320
5e3a759c
JM
1321static void auth_sae_queue(struct hostapd_data *hapd,
1322 const struct ieee80211_mgmt *mgmt, size_t len,
1323 int rssi)
ff9f40ae 1324{
5e3a759c 1325 struct hostapd_sae_commit_queue *q, *q2;
ff9f40ae 1326 unsigned int queue_len;
5e3a759c 1327 const struct ieee80211_mgmt *mgmt2;
ff9f40ae
JM
1328
1329 queue_len = dl_list_len(&hapd->sae_commit_queue);
1330 if (queue_len >= 15) {
1331 wpa_printf(MSG_DEBUG,
5e3a759c 1332 "SAE: No more room in message queue - drop the new frame from "
ff9f40ae
JM
1333 MACSTR, MAC2STR(mgmt->sa));
1334 return;
1335 }
1336
5e3a759c
JM
1337 wpa_printf(MSG_DEBUG, "SAE: Queue Authentication message from "
1338 MACSTR " for processing (queue_len %u)", MAC2STR(mgmt->sa),
1339 queue_len);
ff9f40ae
JM
1340 q = os_zalloc(sizeof(*q) + len);
1341 if (!q)
1342 return;
1343 q->rssi = rssi;
1344 q->len = len;
1345 os_memcpy(q->msg, mgmt, len);
5e3a759c
JM
1346
1347 /* Check whether there is already a queued Authentication frame from the
1348 * same station with the same transaction number and if so, replace that
1349 * queue entry with the new one. This avoids issues with a peer that
1350 * sends multiple times (e.g., due to frequent SAE retries). There is no
1351 * point in us trying to process the old attempts after a new one has
1352 * obsoleted them. */
1353 dl_list_for_each(q2, &hapd->sae_commit_queue,
1354 struct hostapd_sae_commit_queue, list) {
1355 mgmt2 = (const struct ieee80211_mgmt *) q2->msg;
1356 if (os_memcmp(mgmt->sa, mgmt2->sa, ETH_ALEN) == 0 &&
1357 mgmt->u.auth.auth_transaction ==
1358 mgmt2->u.auth.auth_transaction) {
1359 wpa_printf(MSG_DEBUG,
1360 "SAE: Replace queued message from same STA with same transaction number");
1361 dl_list_add(&q2->list, &q->list);
1362 dl_list_del(&q2->list);
1363 os_free(q2);
1364 goto queued;
1365 }
1366 }
1367
1368 /* No pending identical entry, so add to the end of the queue */
ff9f40ae 1369 dl_list_add_tail(&hapd->sae_commit_queue, &q->list);
5e3a759c
JM
1370
1371queued:
ff9f40ae
JM
1372 if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
1373 return;
ff5f54e1 1374 eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
ff9f40ae
JM
1375 hapd, NULL);
1376}
1377
5e3a759c
JM
1378
1379static int auth_sae_queued_addr(struct hostapd_data *hapd, const u8 *addr)
1380{
1381 struct hostapd_sae_commit_queue *q;
1382 const struct ieee80211_mgmt *mgmt;
1383
1384 dl_list_for_each(q, &hapd->sae_commit_queue,
1385 struct hostapd_sae_commit_queue, list) {
1386 mgmt = (const struct ieee80211_mgmt *) q->msg;
1387 if (os_memcmp(addr, mgmt->sa, ETH_ALEN) == 0)
1388 return 1;
1389 }
1390
1391 return 0;
1392}
1393
c10347f2
JM
1394#endif /* CONFIG_SAE */
1395
1396
ffb62f22
JM
1397static u16 wpa_res_to_status_code(int res)
1398{
1399 if (res == WPA_INVALID_GROUP)
1400 return WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1401 if (res == WPA_INVALID_PAIRWISE)
1402 return WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1403 if (res == WPA_INVALID_AKMP)
1404 return WLAN_STATUS_AKMP_NOT_VALID;
1405 if (res == WPA_ALLOC_FAIL)
1406 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1407#ifdef CONFIG_IEEE80211W
1408 if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1409 return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1410 if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
feba5848 1411 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
ffb62f22
JM
1412#endif /* CONFIG_IEEE80211W */
1413 if (res == WPA_INVALID_MDIE)
1414 return WLAN_STATUS_INVALID_MDIE;
567da5bb
JM
1415 if (res == WPA_INVALID_PMKID)
1416 return WLAN_STATUS_INVALID_PMKID;
ffb62f22
JM
1417 if (res != WPA_IE_OK)
1418 return WLAN_STATUS_INVALID_IE;
1419 return WLAN_STATUS_SUCCESS;
1420}
1421
1422
c4fd6d8a
JM
1423#ifdef CONFIG_FILS
1424
1425static void handle_auth_fils_finish(struct hostapd_data *hapd,
1426 struct sta_info *sta, u16 resp,
5cee22ca
JM
1427 struct wpabuf *data, int pub);
1428
1429void handle_auth_fils(struct hostapd_data *hapd, struct sta_info *sta,
1430 const u8 *pos, size_t len, u16 auth_alg,
1431 u16 auth_transaction, u16 status_code,
1432 void (*cb)(struct hostapd_data *hapd,
1433 struct sta_info *sta, u16 resp,
1434 struct wpabuf *data, int pub))
c4fd6d8a
JM
1435{
1436 u16 resp = WLAN_STATUS_SUCCESS;
b8a3453a 1437 const u8 *end;
c4fd6d8a
JM
1438 struct ieee802_11_elems elems;
1439 int res;
1440 struct wpa_ie_data rsn;
1441 struct rsn_pmksa_cache_entry *pmksa = NULL;
1442
1443 if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
1444 return;
1445
b8a3453a 1446 end = pos + len;
c4fd6d8a
JM
1447
1448 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
1449 pos, end - pos);
1450
1764559e
JM
1451 /* TODO: FILS PK */
1452#ifdef CONFIG_FILS_SK_PFS
1453 if (auth_alg == WLAN_AUTH_FILS_SK_PFS) {
1454 u16 group;
1455 struct wpabuf *pub;
1456 size_t elem_len;
1457
1458 /* Using FILS PFS */
1459
1460 /* Finite Cyclic Group */
1461 if (end - pos < 2) {
1462 wpa_printf(MSG_DEBUG,
1463 "FILS: No room for Finite Cyclic Group");
1464 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1465 goto fail;
1466 }
1467 group = WPA_GET_LE16(pos);
1468 pos += 2;
1469 if (group != hapd->conf->fils_dh_group) {
1470 wpa_printf(MSG_DEBUG,
1471 "FILS: Unsupported Finite Cyclic Group: %u (expected %u)",
1472 group, hapd->conf->fils_dh_group);
1473 resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1474 goto fail;
1475 }
1476
1477 crypto_ecdh_deinit(sta->fils_ecdh);
1478 sta->fils_ecdh = crypto_ecdh_init(group);
1479 if (!sta->fils_ecdh) {
1480 wpa_printf(MSG_INFO,
1481 "FILS: Could not initialize ECDH with group %d",
1482 group);
1483 resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1484 goto fail;
1485 }
1486
1487 pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
1488 if (!pub) {
1489 wpa_printf(MSG_DEBUG,
1490 "FILS: Failed to derive ECDH public key");
1491 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1492 goto fail;
1493 }
1494 elem_len = wpabuf_len(pub);
1495 wpabuf_free(pub);
1496
1497 /* Element */
1498 if ((size_t) (end - pos) < elem_len) {
1499 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
1500 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1501 goto fail;
1502 }
1503
80ddf5d9
JM
1504 wpabuf_free(sta->fils_g_sta);
1505 sta->fils_g_sta = wpabuf_alloc_copy(pos, elem_len);
1764559e
JM
1506 wpabuf_clear_free(sta->fils_dh_ss);
1507 sta->fils_dh_ss = crypto_ecdh_set_peerkey(sta->fils_ecdh, 1,
1508 pos, elem_len);
1509 if (!sta->fils_dh_ss) {
1510 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
1511 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1512 goto fail;
1513 }
1514 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", sta->fils_dh_ss);
1515 pos += elem_len;
1516 } else {
1517 crypto_ecdh_deinit(sta->fils_ecdh);
1518 sta->fils_ecdh = NULL;
1519 wpabuf_clear_free(sta->fils_dh_ss);
1520 sta->fils_dh_ss = NULL;
1521 }
1522#endif /* CONFIG_FILS_SK_PFS */
c4fd6d8a
JM
1523
1524 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
1525 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
1526 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
1527 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1528 goto fail;
1529 }
1530
1531 /* RSNE */
1532 wpa_hexdump(MSG_DEBUG, "FILS: RSN element",
1533 elems.rsn_ie, elems.rsn_ie_len);
1534 if (!elems.rsn_ie ||
1535 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
1536 &rsn) < 0) {
1537 wpa_printf(MSG_DEBUG, "FILS: No valid RSN element");
1538 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1539 goto fail;
1540 }
1541
1542 if (!sta->wpa_sm)
1543 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
1544 NULL);
1545 if (!sta->wpa_sm) {
1546 wpa_printf(MSG_DEBUG,
1547 "FILS: Failed to initialize RSN state machine");
1548 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1549 goto fail;
1550 }
1551
1552 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
2c129a1b 1553 hapd->iface->freq,
c4fd6d8a 1554 elems.rsn_ie - 2, elems.rsn_ie_len + 2,
09368515 1555 elems.mdie, elems.mdie_len, NULL, 0);
c4fd6d8a
JM
1556 resp = wpa_res_to_status_code(res);
1557 if (resp != WLAN_STATUS_SUCCESS)
1558 goto fail;
1559
c4fd6d8a
JM
1560 if (!elems.fils_nonce) {
1561 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
1562 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1563 goto fail;
1564 }
1565 wpa_hexdump(MSG_DEBUG, "FILS: SNonce", elems.fils_nonce,
1566 FILS_NONCE_LEN);
1567 os_memcpy(sta->fils_snonce, elems.fils_nonce, FILS_NONCE_LEN);
1568
1569 /* PMKID List */
1570 if (rsn.pmkid && rsn.num_pmkid > 0) {
1571 u8 num;
1572 const u8 *pmkid;
1573
1574 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
1575 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
1576
1577 pmkid = rsn.pmkid;
1578 num = rsn.num_pmkid;
1579 while (num) {
1580 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
1581 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr,
1582 pmkid);
1583 if (pmksa)
1584 break;
7eace378
JM
1585 pmksa = wpa_auth_pmksa_get_fils_cache_id(hapd->wpa_auth,
1586 sta->addr,
1587 pmkid);
1588 if (pmksa)
1589 break;
c4fd6d8a
JM
1590 pmkid += PMKID_LEN;
1591 num--;
1592 }
1593 }
1594 if (pmksa && wpa_auth_sta_key_mgmt(sta->wpa_sm) != pmksa->akmp) {
1595 wpa_printf(MSG_DEBUG,
1596 "FILS: Matching PMKSA cache entry has different AKMP (0x%x != 0x%x) - ignore",
1597 wpa_auth_sta_key_mgmt(sta->wpa_sm), pmksa->akmp);
1598 pmksa = NULL;
1599 }
1600 if (pmksa)
1601 wpa_printf(MSG_DEBUG, "FILS: Found matching PMKSA cache entry");
1602
1603 /* FILS Session */
1604 if (!elems.fils_session) {
1605 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
1606 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1607 goto fail;
1608 }
1609 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
1610 FILS_SESSION_LEN);
1611 os_memcpy(sta->fils_session, elems.fils_session, FILS_SESSION_LEN);
1612
1613 /* FILS Wrapped Data */
1614 if (elems.fils_wrapped_data) {
1615 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
1616 elems.fils_wrapped_data,
1617 elems.fils_wrapped_data_len);
1618 if (!pmksa) {
1619#ifndef CONFIG_NO_RADIUS
1620 if (!sta->eapol_sm) {
1621 sta->eapol_sm =
1622 ieee802_1x_alloc_eapol_sm(hapd, sta);
1623 }
1624 wpa_printf(MSG_DEBUG,
bfe44833 1625 "FILS: Forward EAP-Initiate/Re-auth to authentication server");
c4fd6d8a
JM
1626 ieee802_1x_encapsulate_radius(
1627 hapd, sta, elems.fils_wrapped_data,
1628 elems.fils_wrapped_data_len);
5cee22ca 1629 sta->fils_pending_cb = cb;
c4fd6d8a
JM
1630 wpa_printf(MSG_DEBUG,
1631 "FILS: Will send Authentication frame once the response from authentication server is available");
1632 sta->flags |= WLAN_STA_PENDING_FILS_ERP;
b3e567c8
JM
1633 /* Calculate pending PMKID here so that we do not need
1634 * to maintain a copy of the EAP-Initiate/Reauth
1635 * message. */
1636 if (fils_pmkid_erp(wpa_auth_sta_key_mgmt(sta->wpa_sm),
1637 elems.fils_wrapped_data,
1638 elems.fils_wrapped_data_len,
1639 sta->fils_erp_pmkid) == 0)
1640 sta->fils_erp_pmkid_set = 1;
c4fd6d8a
JM
1641 return;
1642#else /* CONFIG_NO_RADIUS */
1643 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1644 goto fail;
1645#endif /* CONFIG_NO_RADIUS */
1646 }
1647 }
1648
1649fail:
5cee22ca
JM
1650 if (cb) {
1651 struct wpabuf *data;
1652 int pub = 0;
1653
1654 data = prepare_auth_resp_fils(hapd, sta, &resp, pmksa, NULL,
1655 NULL, 0, &pub);
1656 if (!data) {
1657 wpa_printf(MSG_DEBUG,
1658 "%s: prepare_auth_resp_fils() returned failure",
1659 __func__);
1660 }
1661
1662 cb(hapd, sta, resp, data, pub);
1663 }
c4fd6d8a
JM
1664}
1665
1666
5e5f8c81
JM
1667static struct wpabuf *
1668prepare_auth_resp_fils(struct hostapd_data *hapd,
1669 struct sta_info *sta, u16 *resp,
1670 struct rsn_pmksa_cache_entry *pmksa,
1671 struct wpabuf *erp_resp,
1672 const u8 *msk, size_t msk_len,
1673 int *is_pub)
c4fd6d8a
JM
1674{
1675 u8 fils_nonce[FILS_NONCE_LEN];
1676 size_t ielen;
1677 struct wpabuf *data = NULL;
1678 const u8 *ie;
1679 u8 *ie_buf = NULL;
1680 const u8 *pmk = NULL;
1681 size_t pmk_len = 0;
fcd3d6ce 1682 u8 pmk_buf[PMK_LEN_MAX];
1764559e 1683 struct wpabuf *pub = NULL;
c4fd6d8a 1684
5e5f8c81 1685 if (*resp != WLAN_STATUS_SUCCESS)
c4fd6d8a
JM
1686 goto fail;
1687
1688 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
1689 if (!ie) {
5e5f8c81 1690 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
c4fd6d8a
JM
1691 goto fail;
1692 }
5e5f8c81 1693
c4fd6d8a
JM
1694 if (pmksa) {
1695 /* Add PMKID of the selected PMKSA into RSNE */
1696 ie_buf = os_malloc(ielen + 2 + 2 + PMKID_LEN);
1697 if (!ie_buf) {
5e5f8c81 1698 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
c4fd6d8a
JM
1699 goto fail;
1700 }
5e5f8c81 1701
c4fd6d8a
JM
1702 os_memcpy(ie_buf, ie, ielen);
1703 if (wpa_insert_pmkid(ie_buf, &ielen, pmksa->pmkid) < 0) {
5e5f8c81 1704 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
c4fd6d8a
JM
1705 goto fail;
1706 }
1707 ie = ie_buf;
1708 }
1709
1710 if (random_get_bytes(fils_nonce, FILS_NONCE_LEN) < 0) {
5e5f8c81 1711 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
c4fd6d8a
JM
1712 goto fail;
1713 }
1714 wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS Nonce",
1715 fils_nonce, FILS_NONCE_LEN);
1716
1764559e
JM
1717#ifdef CONFIG_FILS_SK_PFS
1718 if (sta->fils_dh_ss && sta->fils_ecdh) {
1719 pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
1720 if (!pub) {
5e5f8c81 1721 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1764559e
JM
1722 goto fail;
1723 }
1724 }
1725#endif /* CONFIG_FILS_SK_PFS */
1726
1727 data = wpabuf_alloc(1000 + ielen + (pub ? wpabuf_len(pub) : 0));
c4fd6d8a 1728 if (!data) {
5e5f8c81 1729 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
c4fd6d8a
JM
1730 goto fail;
1731 }
1732
1764559e
JM
1733 /* TODO: FILS PK */
1734#ifdef CONFIG_FILS_SK_PFS
1735 if (pub) {
1736 /* Finite Cyclic Group */
1737 wpabuf_put_le16(data, hapd->conf->fils_dh_group);
1738
1739 /* Element */
1740 wpabuf_put_buf(data, pub);
1741 }
1742#endif /* CONFIG_FILS_SK_PFS */
c4fd6d8a
JM
1743
1744 /* RSNE */
1745 wpabuf_put_data(data, ie, ielen);
1746
5db997e3
JM
1747 /* MDE when using FILS+FT (already included in ie,ielen with RSNE) */
1748
1749#ifdef CONFIG_IEEE80211R_AP
1750 if (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm))) {
1751 /* FTE[R1KH-ID,R0KH-ID] when using FILS+FT */
1752 int res;
a7968ea5
JM
1753 int use_sha384 = wpa_key_mgmt_sha384(
1754 wpa_auth_sta_key_mgmt(sta->wpa_sm));
5db997e3 1755
a7968ea5
JM
1756 res = wpa_auth_write_fte(hapd->wpa_auth, use_sha384,
1757 wpabuf_put(data, 0),
5db997e3
JM
1758 wpabuf_tailroom(data));
1759 if (res < 0) {
5e5f8c81 1760 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5db997e3
JM
1761 goto fail;
1762 }
1763 wpabuf_put(data, res);
1764 }
1765#endif /* CONFIG_IEEE80211R_AP */
c4fd6d8a
JM
1766
1767 /* FILS Nonce */
1768 wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
1769 wpabuf_put_u8(data, 1 + FILS_NONCE_LEN); /* Length */
1770 /* Element ID Extension */
1771 wpabuf_put_u8(data, WLAN_EID_EXT_FILS_NONCE);
1772 wpabuf_put_data(data, fils_nonce, FILS_NONCE_LEN);
1773
1774 /* FILS Session */
1775 wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
1776 wpabuf_put_u8(data, 1 + FILS_SESSION_LEN); /* Length */
1777 /* Element ID Extension */
1778 wpabuf_put_u8(data, WLAN_EID_EXT_FILS_SESSION);
1779 wpabuf_put_data(data, sta->fils_session, FILS_SESSION_LEN);
1780
1781 /* FILS Wrapped Data */
1782 if (!pmksa && erp_resp) {
1783 wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
1784 wpabuf_put_u8(data, 1 + wpabuf_len(erp_resp)); /* Length */
1785 /* Element ID Extension */
1786 wpabuf_put_u8(data, WLAN_EID_EXT_FILS_WRAPPED_DATA);
1787 wpabuf_put_buf(data, erp_resp);
1788
fcd3d6ce
JM
1789 if (fils_rmsk_to_pmk(wpa_auth_sta_key_mgmt(sta->wpa_sm),
1790 msk, msk_len, sta->fils_snonce, fils_nonce,
1764559e
JM
1791 sta->fils_dh_ss ?
1792 wpabuf_head(sta->fils_dh_ss) : NULL,
1793 sta->fils_dh_ss ?
1794 wpabuf_len(sta->fils_dh_ss) : 0,
1795 pmk_buf, &pmk_len)) {
fcd3d6ce 1796 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
5e5f8c81 1797 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
fcd3d6ce
JM
1798 wpabuf_free(data);
1799 data = NULL;
1800 goto fail;
1801 }
1802 pmk = pmk_buf;
b3e567c8 1803
4cada9dc
JM
1804 /* Don't use DHss in PTK derivation if PMKSA caching is not
1805 * used. */
1806 wpabuf_clear_free(sta->fils_dh_ss);
1807 sta->fils_dh_ss = NULL;
1808
b3e567c8
JM
1809 if (sta->fils_erp_pmkid_set) {
1810 /* TODO: get PMKLifetime from WPA parameters */
1811 unsigned int dot11RSNAConfigPMKLifetime = 43200;
13f118dc
MB
1812 int session_timeout;
1813
1814 session_timeout = dot11RSNAConfigPMKLifetime;
1815 if (sta->session_timeout_set) {
1816 struct os_reltime now, diff;
1817
1818 os_get_reltime(&now);
1819 os_reltime_sub(&sta->session_timeout, &now,
1820 &diff);
1821 session_timeout = diff.sec;
1822 }
b3e567c8
JM
1823
1824 sta->fils_erp_pmkid_set = 0;
a40bd06e
JM
1825 wpa_auth_add_fils_pmk_pmkid(sta->wpa_sm, pmk, pmk_len,
1826 sta->fils_erp_pmkid);
aabbdb81
JM
1827 if (!hapd->conf->disable_pmksa_caching &&
1828 wpa_auth_pmksa_add2(
b3e567c8
JM
1829 hapd->wpa_auth, sta->addr,
1830 pmk, pmk_len,
1831 sta->fils_erp_pmkid,
13f118dc 1832 session_timeout,
b3e567c8
JM
1833 wpa_auth_sta_key_mgmt(sta->wpa_sm)) < 0) {
1834 wpa_printf(MSG_ERROR,
1835 "FILS: Failed to add PMKSA cache entry based on ERP");
1836 }
1837 }
c4fd6d8a
JM
1838 } else if (pmksa) {
1839 pmk = pmksa->pmk;
1840 pmk_len = pmksa->pmk_len;
1841 }
1842
1843 if (!pmk) {
1844 wpa_printf(MSG_DEBUG, "FILS: No PMK available");
5e5f8c81 1845 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
c4fd6d8a
JM
1846 wpabuf_free(data);
1847 data = NULL;
1848 goto fail;
1849 }
1850
1851 if (fils_auth_pmk_to_ptk(sta->wpa_sm, pmk, pmk_len,
80ddf5d9 1852 sta->fils_snonce, fils_nonce,
4cada9dc
JM
1853 sta->fils_dh_ss ?
1854 wpabuf_head(sta->fils_dh_ss) : NULL,
1855 sta->fils_dh_ss ?
1856 wpabuf_len(sta->fils_dh_ss) : 0,
80ddf5d9 1857 sta->fils_g_sta, pub) < 0) {
5e5f8c81 1858 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
c4fd6d8a
JM
1859 wpabuf_free(data);
1860 data = NULL;
1861 goto fail;
1862 }
1863
1864fail:
5e5f8c81
JM
1865 if (is_pub)
1866 *is_pub = pub != NULL;
1867 os_free(ie_buf);
1868 wpabuf_free(pub);
1869 wpabuf_clear_free(sta->fils_dh_ss);
1870 sta->fils_dh_ss = NULL;
1871#ifdef CONFIG_FILS_SK_PFS
1872 crypto_ecdh_deinit(sta->fils_ecdh);
1873 sta->fils_ecdh = NULL;
1874#endif /* CONFIG_FILS_SK_PFS */
1875 return data;
1876}
1877
1878
1879static void handle_auth_fils_finish(struct hostapd_data *hapd,
1880 struct sta_info *sta, u16 resp,
5cee22ca 1881 struct wpabuf *data, int pub)
5e5f8c81 1882{
5e5f8c81 1883 u16 auth_alg;
5e5f8c81 1884
1764559e
JM
1885 auth_alg = (pub ||
1886 resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) ?
1887 WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
1888 send_auth_reply(hapd, sta->addr, hapd->own_addr, auth_alg, 2, resp,
c4fd6d8a 1889 data ? wpabuf_head(data) : (u8 *) "",
e7525a29 1890 data ? wpabuf_len(data) : 0, "auth-fils-finish");
c4fd6d8a
JM
1891 wpabuf_free(data);
1892
1893 if (resp == WLAN_STATUS_SUCCESS) {
1894 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1895 HOSTAPD_LEVEL_DEBUG,
1896 "authentication OK (FILS)");
1897 sta->flags |= WLAN_STA_AUTH;
1898 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1764559e 1899 sta->auth_alg = pub ? WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
c4fd6d8a
JM
1900 mlme_authenticate_indication(hapd, sta);
1901 }
c4fd6d8a
JM
1902}
1903
1904
1905void ieee802_11_finish_fils_auth(struct hostapd_data *hapd,
1906 struct sta_info *sta, int success,
1907 struct wpabuf *erp_resp,
1908 const u8 *msk, size_t msk_len)
1909{
5cee22ca
JM
1910 struct wpabuf *data;
1911 int pub = 0;
1912 u16 resp;
1913
c4fd6d8a 1914 sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
5cee22ca
JM
1915
1916 if (!sta->fils_pending_cb)
1917 return;
1918 resp = success ? WLAN_STATUS_SUCCESS : WLAN_STATUS_UNSPECIFIED_FAILURE;
1919 data = prepare_auth_resp_fils(hapd, sta, &resp, NULL, erp_resp,
1920 msk, msk_len, &pub);
1921 if (!data) {
1922 wpa_printf(MSG_DEBUG,
1923 "%s: prepare_auth_resp_fils() returned failure",
1924 __func__);
1925 }
1926 sta->fils_pending_cb(hapd, sta, resp, data, pub);
c4fd6d8a
JM
1927}
1928
1929#endif /* CONFIG_FILS */
1930
1931
92eb00ae 1932int
53d17144
DL
1933ieee802_11_allowed_address(struct hostapd_data *hapd, const u8 *addr,
1934 const u8 *msg, size_t len, u32 *session_timeout,
1935 u32 *acct_interim_interval,
1936 struct vlan_description *vlan_id,
1937 struct hostapd_sta_wpa_psk_short **psk,
92eb00ae 1938 char **identity, char **radius_cui, int is_probe_req)
53d17144
DL
1939{
1940 int res;
1941
1942 os_memset(vlan_id, 0, sizeof(*vlan_id));
1943 res = hostapd_allowed_address(hapd, addr, msg, len,
1944 session_timeout, acct_interim_interval,
92eb00ae
T
1945 vlan_id, psk, identity, radius_cui,
1946 is_probe_req);
53d17144
DL
1947
1948 if (res == HOSTAPD_ACL_REJECT) {
6588f712
JM
1949 if (!is_probe_req)
1950 wpa_printf(MSG_DEBUG,
1951 "Station " MACSTR
1952 " not allowed to authenticate",
1953 MAC2STR(addr));
53d17144
DL
1954 return HOSTAPD_ACL_REJECT;
1955 }
1956
1957 if (res == HOSTAPD_ACL_PENDING) {
1958 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
1959 " waiting for an external authentication",
1960 MAC2STR(addr));
1961 /* Authentication code will re-send the authentication frame
1962 * after it has received (and cached) information from the
1963 * external source. */
1964 return HOSTAPD_ACL_PENDING;
1965 }
1966
1967 return res;
1968}
1969
1970
1971static int
1972ieee802_11_set_radius_info(struct hostapd_data *hapd, struct sta_info *sta,
1973 int res, u32 session_timeout,
1974 u32 acct_interim_interval,
1975 struct vlan_description *vlan_id,
1976 struct hostapd_sta_wpa_psk_short **psk,
1977 char **identity, char **radius_cui)
1978{
1979 if (vlan_id->notempty &&
1980 !hostapd_vlan_valid(hapd->conf->vlan, vlan_id)) {
1981 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1982 HOSTAPD_LEVEL_INFO,
1983 "Invalid VLAN %d%s received from RADIUS server",
1984 vlan_id->untagged,
1985 vlan_id->tagged[0] ? "+" : "");
1986 return -1;
1987 }
1988 if (ap_sta_set_vlan(hapd, sta, vlan_id) < 0)
1989 return -1;
1990 if (sta->vlan_id)
1991 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1992 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
1993
1994 hostapd_free_psk_list(sta->psk);
1995 if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
1996 sta->psk = *psk;
1997 *psk = NULL;
1998 } else {
1999 sta->psk = NULL;
2000 }
2001
17a8a989 2002 os_free(sta->identity);
53d17144
DL
2003 sta->identity = *identity;
2004 *identity = NULL;
17a8a989
MB
2005
2006 os_free(sta->radius_cui);
53d17144
DL
2007 sta->radius_cui = *radius_cui;
2008 *radius_cui = NULL;
2009
2010 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
2011 sta->acct_interim_interval = acct_interim_interval;
13f118dc
MB
2012 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT) {
2013 sta->session_timeout_set = 1;
2014 os_get_reltime(&sta->session_timeout);
2015 sta->session_timeout.sec += session_timeout;
53d17144 2016 ap_sta_session_timeout(hapd, sta, session_timeout);
13f118dc
MB
2017 } else {
2018 sta->session_timeout_set = 0;
53d17144 2019 ap_sta_no_session_timeout(hapd, sta);
13f118dc 2020 }
53d17144
DL
2021
2022 return 0;
2023}
2024
2025
b57e086c 2026static void handle_auth(struct hostapd_data *hapd,
2ea1fce3 2027 const struct ieee80211_mgmt *mgmt, size_t len,
ff9f40ae 2028 int rssi, int from_queue)
6fc6879b
JM
2029{
2030 u16 auth_alg, auth_transaction, status_code;
2031 u16 resp = WLAN_STATUS_SUCCESS;
2032 struct sta_info *sta = NULL;
bb598c3b 2033 int res, reply_res;
6fc6879b 2034 u16 fc;
b57e086c 2035 const u8 *challenge = NULL;
6fc6879b 2036 u32 session_timeout, acct_interim_interval;
1889af2e 2037 struct vlan_description vlan_id;
2ad3e6c8 2038 struct hostapd_sta_wpa_psk_short *psk = NULL;
6fc6879b
JM
2039 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
2040 size_t resp_ies_len = 0;
2092597f
MB
2041 char *identity = NULL;
2042 char *radius_cui = NULL;
38cb0a2d 2043 u16 seq_ctrl;
6fc6879b
JM
2044
2045 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
61323e70
JM
2046 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
2047 (unsigned long) len);
6fc6879b
JM
2048 return;
2049 }
2050
c2aff6b1 2051#ifdef CONFIG_TESTING_OPTIONS
06df2aa6 2052 if (hapd->iconf->ignore_auth_probability > 0.0 &&
c2aff6b1
JB
2053 drand48() < hapd->iconf->ignore_auth_probability) {
2054 wpa_printf(MSG_INFO,
2055 "TESTING: ignoring auth frame from " MACSTR,
2056 MAC2STR(mgmt->sa));
2057 return;
2058 }
2059#endif /* CONFIG_TESTING_OPTIONS */
2060
6fc6879b
JM
2061 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2062 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2063 status_code = le_to_host16(mgmt->u.auth.status_code);
2064 fc = le_to_host16(mgmt->frame_control);
38cb0a2d 2065 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
6fc6879b
JM
2066
2067 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
2068 2 + WLAN_AUTH_CHALLENGE_LEN &&
2069 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
2070 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
2071 challenge = &mgmt->u.auth.variable[2];
2072
2073 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
38cb0a2d 2074 "auth_transaction=%d status_code=%d wep=%d%s "
ff9f40ae 2075 "seq_ctrl=0x%x%s%s",
6fc6879b
JM
2076 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
2077 status_code, !!(fc & WLAN_FC_ISWEP),
38cb0a2d 2078 challenge ? " challenge" : "",
ff9f40ae
JM
2079 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "",
2080 from_queue ? " (from queue)" : "");
6fc6879b 2081
7cb53ded
JM
2082#ifdef CONFIG_NO_RC4
2083 if (auth_alg == WLAN_AUTH_SHARED_KEY) {
2084 wpa_printf(MSG_INFO,
2085 "Unsupported authentication algorithm (%d)",
2086 auth_alg);
2087 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
2088 goto fail;
2089 }
2090#endif /* CONFIG_NO_RC4 */
2091
6fc6879b 2092 if (hapd->tkip_countermeasures) {
e2fc13d0
BG
2093 wpa_printf(MSG_DEBUG,
2094 "Ongoing TKIP countermeasures (Michael MIC failure) - reject authentication");
fe12ae77 2095 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
2096 goto fail;
2097 }
2098
2099 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
2100 auth_alg == WLAN_AUTH_OPEN) ||
4ec1fd8e 2101#ifdef CONFIG_IEEE80211R_AP
0bf927a0 2102 (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
6fc6879b 2103 auth_alg == WLAN_AUTH_FT) ||
4ec1fd8e 2104#endif /* CONFIG_IEEE80211R_AP */
c10347f2
JM
2105#ifdef CONFIG_SAE
2106 (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
2107 auth_alg == WLAN_AUTH_SAE) ||
2108#endif /* CONFIG_SAE */
c4fd6d8a
JM
2109#ifdef CONFIG_FILS
2110 (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
2111 auth_alg == WLAN_AUTH_FILS_SK) ||
1764559e
JM
2112 (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
2113 hapd->conf->fils_dh_group &&
2114 auth_alg == WLAN_AUTH_FILS_SK_PFS) ||
c4fd6d8a 2115#endif /* CONFIG_FILS */
6fc6879b
JM
2116 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
2117 auth_alg == WLAN_AUTH_SHARED_KEY))) {
61323e70
JM
2118 wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
2119 auth_alg);
6fc6879b
JM
2120 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
2121 goto fail;
2122 }
2123
c10347f2 2124 if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
6fc6879b 2125 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
61323e70
JM
2126 wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
2127 auth_transaction);
6fc6879b
JM
2128 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
2129 goto fail;
2130 }
2131
2132 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
61323e70
JM
2133 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
2134 MAC2STR(mgmt->sa));
6fc6879b
JM
2135 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2136 goto fail;
2137 }
2138
0e2412d0
JM
2139 if (hapd->conf->no_auth_if_seen_on) {
2140 struct hostapd_data *other;
2141
2142 other = sta_track_seen_on(hapd->iface, mgmt->sa,
2143 hapd->conf->no_auth_if_seen_on);
2144 if (other) {
2145 u8 *pos;
2146 u32 info;
2147 u8 op_class, channel, phytype;
2148
2149 wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
2150 MACSTR " since STA has been seen on %s",
2151 hapd->conf->iface, MAC2STR(mgmt->sa),
2152 hapd->conf->no_auth_if_seen_on);
2153
2154 resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
2155 pos = &resp_ies[0];
2156 *pos++ = WLAN_EID_NEIGHBOR_REPORT;
2157 *pos++ = 13;
2158 os_memcpy(pos, other->own_addr, ETH_ALEN);
2159 pos += ETH_ALEN;
2160 info = 0; /* TODO: BSSID Information */
2161 WPA_PUT_LE32(pos, info);
2162 pos += 4;
2163 if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
2164 phytype = 8; /* dmg */
2165 else if (other->iconf->ieee80211ac)
2166 phytype = 9; /* vht */
2167 else if (other->iconf->ieee80211n)
2168 phytype = 7; /* ht */
2169 else if (other->iconf->hw_mode ==
2170 HOSTAPD_MODE_IEEE80211A)
2171 phytype = 4; /* ofdm */
2172 else if (other->iconf->hw_mode ==
2173 HOSTAPD_MODE_IEEE80211G)
2174 phytype = 6; /* erp */
2175 else
2176 phytype = 5; /* hrdsss */
2177 if (ieee80211_freq_to_channel_ext(
2178 hostapd_hw_get_freq(other,
2179 other->iconf->channel),
2180 other->iconf->secondary_channel,
2181 other->iconf->ieee80211ac,
2182 &op_class, &channel) == NUM_HOSTAPD_MODES) {
2183 op_class = 0;
2184 channel = other->iconf->channel;
2185 }
2186 *pos++ = op_class;
2187 *pos++ = channel;
2188 *pos++ = phytype;
2189 resp_ies_len = pos - &resp_ies[0];
2190 goto fail;
2191 }
2192 }
2193
53d17144
DL
2194 res = ieee802_11_allowed_address(
2195 hapd, mgmt->sa, (const u8 *) mgmt, len, &session_timeout,
92eb00ae
T
2196 &acct_interim_interval, &vlan_id, &psk, &identity, &radius_cui,
2197 0);
6fc6879b 2198 if (res == HOSTAPD_ACL_REJECT) {
92eb00ae
T
2199 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
2200 "Ignore Authentication frame from " MACSTR
2201 " due to ACL reject", MAC2STR(mgmt->sa));
6fc6879b
JM
2202 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2203 goto fail;
2204 }
53d17144 2205 if (res == HOSTAPD_ACL_PENDING)
6fc6879b 2206 return;
6fc6879b 2207
ff9f40ae 2208#ifdef CONFIG_SAE
5e3a759c
JM
2209 if (auth_alg == WLAN_AUTH_SAE && !from_queue &&
2210 (auth_transaction == 1 ||
2211 (auth_transaction == 2 && auth_sae_queued_addr(hapd, mgmt->sa)))) {
ff9f40ae
JM
2212 /* Handle SAE Authentication commit message through a queue to
2213 * provide more control for postponing the needed heavy
5e3a759c
JM
2214 * processing under a possible DoS attack scenario. In addition,
2215 * queue SAE Authentication confirm message if there happens to
2216 * be a queued commit message from the same peer. This is needed
2217 * to avoid reordering Authentication frames within the same
2218 * SAE exchange. */
2219 auth_sae_queue(hapd, mgmt, len, rssi);
ff9f40ae
JM
2220 return;
2221 }
2222#endif /* CONFIG_SAE */
2223
38cb0a2d
IP
2224 sta = ap_get_sta(hapd, mgmt->sa);
2225 if (sta) {
c4fd6d8a 2226 sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
4cf5efec 2227 sta->ft_over_ds = 0;
38cb0a2d
IP
2228 if ((fc & WLAN_FC_RETRY) &&
2229 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2230 sta->last_seq_ctrl == seq_ctrl &&
2231 sta->last_subtype == WLAN_FC_STYPE_AUTH) {
2232 hostapd_logger(hapd, sta->addr,
2233 HOSTAPD_MODULE_IEEE80211,
2234 HOSTAPD_LEVEL_DEBUG,
2235 "Drop repeated authentication frame seq_ctrl=0x%x",
2236 seq_ctrl);
2237 return;
2238 }
09d96de0
MH
2239#ifdef CONFIG_MESH
2240 if ((hapd->conf->mesh & MESH_ENABLED) &&
2241 sta->plink_state == PLINK_BLOCKED) {
2242 wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
2243 " is blocked - drop Authentication frame",
2244 MAC2STR(mgmt->sa));
2245 return;
2246 }
2247#endif /* CONFIG_MESH */
38cb0a2d 2248 } else {
c50d94f1 2249#ifdef CONFIG_MESH
38cb0a2d
IP
2250 if (hapd->conf->mesh & MESH_ENABLED) {
2251 /* if the mesh peer is not available, we don't do auth.
2252 */
3a322496 2253 wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
09d96de0 2254 " not yet known - drop Authentication frame",
3a322496
JM
2255 MAC2STR(mgmt->sa));
2256 /*
2257 * Save a copy of the frame so that it can be processed
2258 * if a new peer entry is added shortly after this.
2259 */
2260 wpabuf_free(hapd->mesh_pending_auth);
2261 hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
2262 os_get_reltime(&hapd->mesh_pending_auth_time);
c50d94f1 2263 return;
38cb0a2d 2264 }
c50d94f1 2265#endif /* CONFIG_MESH */
38cb0a2d 2266
c50d94f1
BC
2267 sta = ap_sta_add(hapd, mgmt->sa);
2268 if (!sta) {
c6b5b9a3 2269 wpa_printf(MSG_DEBUG, "ap_sta_add() failed");
c50d94f1
BC
2270 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2271 goto fail;
2272 }
6fc6879b 2273 }
38cb0a2d
IP
2274 sta->last_seq_ctrl = seq_ctrl;
2275 sta->last_subtype = WLAN_FC_STYPE_AUTH;
2ea1fce3
JM
2276#ifdef CONFIG_MBO
2277 sta->auth_rssi = rssi;
2278#endif /* CONFIG_MBO */
6fc6879b 2279
53d17144
DL
2280 res = ieee802_11_set_radius_info(
2281 hapd, sta, res, session_timeout, acct_interim_interval,
2282 &vlan_id, &psk, &identity, &radius_cui);
2283 if (res) {
c6b5b9a3 2284 wpa_printf(MSG_DEBUG, "ieee802_11_set_radius_info() failed");
8be640b7
MB
2285 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2286 goto fail;
6fc6879b 2287 }
2092597f 2288
6fc6879b
JM
2289 sta->flags &= ~WLAN_STA_PREAUTH;
2290 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
2291
bb598c3b
AB
2292 /*
2293 * If the driver supports full AP client state, add a station to the
2294 * driver before sending authentication reply to make sure the driver
2295 * has resources, and not to go through the entire authentication and
2296 * association handshake, and fail it at the end.
2297 *
2298 * If this is not the first transaction, in a multi-step authentication
2299 * algorithm, the station already exists in the driver
2300 * (sta->added_unassoc = 1) so skip it.
2301 *
2302 * In mesh mode, the station was already added to the driver when the
2303 * NEW_PEER_CANDIDATE event is received.
2ab09656
JM
2304 *
2305 * If PMF was negotiated for the existing association, skip this to
2306 * avoid dropping the STA entry and the associated keys. This is needed
2307 * to allow the original connection work until the attempt can complete
2308 * (re)association, so that unprotected Authentication frame cannot be
2309 * used to bypass PMF protection.
bb598c3b
AB
2310 */
2311 if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
2ab09656 2312 (!(sta->flags & WLAN_STA_MFP) || !ap_sta_is_authorized(sta)) &&
bb598c3b
AB
2313 !(hapd->conf->mesh & MESH_ENABLED) &&
2314 !(sta->added_unassoc)) {
2315 /*
2316 * If a station that is already associated to the AP, is trying
2317 * to authenticate again, remove the STA entry, in order to make
2318 * sure the STA PS state gets cleared and configuration gets
2319 * updated. To handle this, station's added_unassoc flag is
2320 * cleared once the station has completed association.
2321 */
b5ebe5dd 2322 ap_sta_set_authorized(hapd, sta, 0);
bb598c3b
AB
2323 hostapd_drv_sta_remove(hapd, sta->addr);
2324 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH |
2325 WLAN_STA_AUTHORIZED);
2326
468b7b12 2327 if (hostapd_sta_add(hapd, sta->addr, 0, 0, NULL, 0, 0,
78d35b16
JC
2328 NULL, NULL, NULL, 0,
2329 sta->flags, 0, 0, 0, 0)) {
bb598c3b
AB
2330 hostapd_logger(hapd, sta->addr,
2331 HOSTAPD_MODULE_IEEE80211,
2332 HOSTAPD_LEVEL_NOTICE,
2333 "Could not add STA to kernel driver");
2334 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2335 goto fail;
2336 }
2337
2338 sta->added_unassoc = 1;
2339 }
2340
6fc6879b
JM
2341 switch (auth_alg) {
2342 case WLAN_AUTH_OPEN:
2343 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2344 HOSTAPD_LEVEL_DEBUG,
2345 "authentication OK (open system)");
6fc6879b
JM
2346 sta->flags |= WLAN_STA_AUTH;
2347 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
2348 sta->auth_alg = WLAN_AUTH_OPEN;
2349 mlme_authenticate_indication(hapd, sta);
6fc6879b 2350 break;
7cb53ded 2351#ifndef CONFIG_NO_RC4
6fc6879b
JM
2352 case WLAN_AUTH_SHARED_KEY:
2353 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
2354 fc & WLAN_FC_ISWEP);
c6b5b9a3
BG
2355 if (resp != 0)
2356 wpa_printf(MSG_DEBUG,
2357 "auth_shared_key() failed: status=%d", resp);
6fc6879b
JM
2358 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
2359 mlme_authenticate_indication(hapd, sta);
2360 if (sta->challenge && auth_transaction == 1) {
2361 resp_ies[0] = WLAN_EID_CHALLENGE;
2362 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
2363 os_memcpy(resp_ies + 2, sta->challenge,
2364 WLAN_AUTH_CHALLENGE_LEN);
2365 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
2366 }
2367 break;
7cb53ded 2368#endif /* CONFIG_NO_RC4 */
4ec1fd8e 2369#ifdef CONFIG_IEEE80211R_AP
6fc6879b
JM
2370 case WLAN_AUTH_FT:
2371 sta->auth_alg = WLAN_AUTH_FT;
2372 if (sta->wpa_sm == NULL)
2373 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e 2374 sta->addr, NULL);
6fc6879b
JM
2375 if (sta->wpa_sm == NULL) {
2376 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
2377 "state machine");
2378 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2379 goto fail;
2380 }
2381 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
2382 auth_transaction, mgmt->u.auth.variable,
2383 len - IEEE80211_HDRLEN -
2384 sizeof(mgmt->u.auth),
2385 handle_auth_ft_finish, hapd);
2386 /* handle_auth_ft_finish() callback will complete auth. */
2387 return;
4ec1fd8e 2388#endif /* CONFIG_IEEE80211R_AP */
c10347f2
JM
2389#ifdef CONFIG_SAE
2390 case WLAN_AUTH_SAE:
c50d94f1 2391#ifdef CONFIG_MESH
afa2ffb4
JM
2392 if (status_code == WLAN_STATUS_SUCCESS &&
2393 hapd->conf->mesh & MESH_ENABLED) {
c50d94f1
BC
2394 if (sta->wpa_sm == NULL)
2395 sta->wpa_sm =
2396 wpa_auth_sta_init(hapd->wpa_auth,
2397 sta->addr, NULL);
2398 if (sta->wpa_sm == NULL) {
2399 wpa_printf(MSG_DEBUG,
2400 "SAE: Failed to initialize WPA state machine");
2401 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2402 goto fail;
2403 }
2404 }
2405#endif /* CONFIG_MESH */
afa2ffb4
JM
2406 handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
2407 status_code);
c10347f2
JM
2408 return;
2409#endif /* CONFIG_SAE */
c4fd6d8a
JM
2410#ifdef CONFIG_FILS
2411 case WLAN_AUTH_FILS_SK:
1764559e 2412 case WLAN_AUTH_FILS_SK_PFS:
b8a3453a
JM
2413 handle_auth_fils(hapd, sta, mgmt->u.auth.variable,
2414 len - IEEE80211_HDRLEN - sizeof(mgmt->u.auth),
5cee22ca
JM
2415 auth_alg, auth_transaction, status_code,
2416 handle_auth_fils_finish);
c4fd6d8a
JM
2417 return;
2418#endif /* CONFIG_FILS */
6fc6879b
JM
2419 }
2420
2421 fail:
2092597f
MB
2422 os_free(identity);
2423 os_free(radius_cui);
f2a14be7 2424 hostapd_free_psk_list(psk);
2092597f 2425
bb598c3b
AB
2426 reply_res = send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
2427 auth_transaction + 1, resp, resp_ies,
e7525a29 2428 resp_ies_len, "handle-auth");
bb598c3b
AB
2429
2430 if (sta && sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
2431 reply_res != WLAN_STATUS_SUCCESS)) {
2432 hostapd_drv_sta_remove(hapd, sta->addr);
2433 sta->added_unassoc = 0;
2434 }
6fc6879b
JM
2435}
2436
2437
681753f2 2438int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
d42a62b3 2439{
2991469c
JM
2440 int i, j = 32, aid;
2441
d42a62b3
JM
2442 /* get a unique AID */
2443 if (sta->aid > 0) {
2444 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
2445 return 0;
2446 }
2447
d7358113
JM
2448 if (TEST_FAIL())
2449 return -1;
2450
2991469c
JM
2451 for (i = 0; i < AID_WORDS; i++) {
2452 if (hapd->sta_aid[i] == (u32) -1)
2453 continue;
2454 for (j = 0; j < 32; j++) {
2455 if (!(hapd->sta_aid[i] & BIT(j)))
2456 break;
2457 }
2458 if (j < 32)
d42a62b3 2459 break;
d42a62b3 2460 }
2991469c
JM
2461 if (j == 32)
2462 return -1;
2463 aid = i * 32 + j + 1;
2464 if (aid > 2007)
2465 return -1;
d42a62b3 2466
2991469c
JM
2467 sta->aid = aid;
2468 hapd->sta_aid[i] |= BIT(j);
d42a62b3
JM
2469 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
2470 return 0;
2471}
2472
2473
5586f500
JM
2474static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
2475 const u8 *ssid_ie, size_t ssid_ie_len)
6fc6879b 2476{
5586f500
JM
2477 if (ssid_ie == NULL)
2478 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b 2479
5586f500
JM
2480 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
2481 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
5586f500
JM
2482 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2483 HOSTAPD_LEVEL_INFO,
2484 "Station tried to associate with unknown SSID "
b37d582e 2485 "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
5586f500 2486 return WLAN_STATUS_UNSPECIFIED_FAILURE;
b0194fe0
JM
2487 }
2488
5586f500
JM
2489 return WLAN_STATUS_SUCCESS;
2490}
6fc6879b 2491
6fc6879b 2492
5586f500
JM
2493static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
2494 const u8 *wmm_ie, size_t wmm_ie_len)
2495{
3ae0800c 2496 sta->flags &= ~WLAN_STA_WMM;
5d061637 2497 sta->qosinfo = 0;
5586f500 2498 if (wmm_ie && hapd->conf->wmm_enabled) {
5f32f79c 2499 struct wmm_information_element *wmm;
5f32f79c 2500
c84b868a 2501 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
6fc6879b
JM
2502 hostapd_logger(hapd, sta->addr,
2503 HOSTAPD_MODULE_WPA,
2504 HOSTAPD_LEVEL_DEBUG,
3ae0800c 2505 "invalid WMM element in association "
6fc6879b 2506 "request");
5f32f79c
EP
2507 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2508 }
2509
2510 sta->flags |= WLAN_STA_WMM;
2511 wmm = (struct wmm_information_element *) wmm_ie;
5d061637 2512 sta->qosinfo = wmm->qos_info;
6fc6879b 2513 }
5586f500
JM
2514 return WLAN_STATUS_SUCCESS;
2515}
6fc6879b 2516
9c06f0f6
VN
2517static u16 check_multi_ap(struct hostapd_data *hapd, struct sta_info *sta,
2518 const u8 *multi_ap_ie, size_t multi_ap_len)
2519{
2520 u8 multi_ap_value = 0;
2521
2522 sta->flags &= ~WLAN_STA_MULTI_AP;
2523
2524 if (!hapd->conf->multi_ap)
2525 return WLAN_STATUS_SUCCESS;
2526
2527 if (multi_ap_ie) {
2528 const u8 *multi_ap_subelem;
2529
2530 multi_ap_subelem = get_ie(multi_ap_ie + 4,
2531 multi_ap_len - 4,
2532 MULTI_AP_SUB_ELEM_TYPE);
2533 if (multi_ap_subelem && multi_ap_subelem[1] == 1) {
2534 multi_ap_value = multi_ap_subelem[2];
2535 } else {
2536 hostapd_logger(hapd, sta->addr,
2537 HOSTAPD_MODULE_IEEE80211,
2538 HOSTAPD_LEVEL_INFO,
2539 "Multi-AP IE has missing or invalid Multi-AP subelement");
2540 return WLAN_STATUS_INVALID_IE;
2541 }
2542 }
2543
bfcdac1c
AVEM
2544 if (multi_ap_value && multi_ap_value != MULTI_AP_BACKHAUL_STA)
2545 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2546 HOSTAPD_LEVEL_INFO,
2547 "Multi-AP IE with unexpected value 0x%02x",
2548 multi_ap_value);
9c06f0f6 2549
bfcdac1c
AVEM
2550 if (!(multi_ap_value & MULTI_AP_BACKHAUL_STA)) {
2551 if (hapd->conf->multi_ap & FRONTHAUL_BSS)
2552 return WLAN_STATUS_SUCCESS;
9c06f0f6 2553
bfcdac1c
AVEM
2554 hostapd_logger(hapd, sta->addr,
2555 HOSTAPD_MODULE_IEEE80211,
2556 HOSTAPD_LEVEL_INFO,
2557 "Non-Multi-AP STA tries to associate with backhaul-only BSS");
2558 return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
9c06f0f6
VN
2559 }
2560
bfcdac1c
AVEM
2561 if (!(hapd->conf->multi_ap & BACKHAUL_BSS))
2562 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2563 HOSTAPD_LEVEL_DEBUG,
2564 "Backhaul STA tries to associate with fronthaul-only BSS");
2565
2566 sta->flags |= WLAN_STA_MULTI_AP;
2567 return WLAN_STATUS_SUCCESS;
9c06f0f6
VN
2568}
2569
5586f500
JM
2570
2571static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
2572 struct ieee802_11_elems *elems)
2573{
0c4b9025
DL
2574 /* Supported rates not used in IEEE 802.11ad/DMG */
2575 if (hapd->iface->current_mode &&
2576 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD)
2577 return WLAN_STATUS_SUCCESS;
2578
5586f500
JM
2579 if (!elems->supp_rates) {
2580 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6fc6879b
JM
2581 HOSTAPD_LEVEL_DEBUG,
2582 "No supported rates element in AssocReq");
5586f500 2583 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
2584 }
2585
3489cfb0
JM
2586 if (elems->supp_rates_len + elems->ext_supp_rates_len >
2587 sizeof(sta->supported_rates)) {
5586f500 2588 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6fc6879b 2589 HOSTAPD_LEVEL_DEBUG,
3489cfb0
JM
2590 "Invalid supported rates element length %d+%d",
2591 elems->supp_rates_len,
2592 elems->ext_supp_rates_len);
5586f500 2593 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
2594 }
2595
3489cfb0
JM
2596 sta->supported_rates_len = merge_byte_arrays(
2597 sta->supported_rates, sizeof(sta->supported_rates),
2598 elems->supp_rates, elems->supp_rates_len,
2599 elems->ext_supp_rates, elems->ext_supp_rates_len);
6fc6879b 2600
5586f500
JM
2601 return WLAN_STATUS_SUCCESS;
2602}
2603
2604
c551700f
KP
2605static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
2606 const u8 *ext_capab_ie, size_t ext_capab_ie_len)
2607{
2608#ifdef CONFIG_INTERWORKING
2609 /* check for QoS Map support */
2610 if (ext_capab_ie_len >= 5) {
2611 if (ext_capab_ie[4] & 0x01)
2612 sta->qos_map_enabled = 1;
2613 }
2614#endif /* CONFIG_INTERWORKING */
2615
65f9db6b 2616 if (ext_capab_ie_len > 0) {
98b05081 2617 sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
65f9db6b 2618 os_free(sta->ext_capability);
2619 sta->ext_capability = os_malloc(1 + ext_capab_ie_len);
2620 if (sta->ext_capability) {
2621 sta->ext_capability[0] = ext_capab_ie_len;
2622 os_memcpy(sta->ext_capability + 1, ext_capab_ie,
2623 ext_capab_ie_len);
2624 }
2625 }
98b05081 2626
c551700f
KP
2627 return WLAN_STATUS_SUCCESS;
2628}
2629
2630
09368515 2631#ifdef CONFIG_OWE
91cc34bf
JM
2632
2633static int owe_group_supported(struct hostapd_data *hapd, u16 group)
2634{
2635 int i;
2636 int *groups = hapd->conf->owe_groups;
2637
2638 if (group != 19 && group != 20 && group != 21)
2639 return 0;
2640
2641 if (!groups)
2642 return 1;
2643
2644 for (i = 0; groups[i] > 0; i++) {
2645 if (groups[i] == group)
2646 return 1;
2647 }
2648
2649 return 0;
2650}
2651
2652
d90f10fa
JM
2653static u16 owe_process_assoc_req(struct hostapd_data *hapd,
2654 struct sta_info *sta, const u8 *owe_dh,
09368515
JM
2655 u8 owe_dh_len)
2656{
2657 struct wpabuf *secret, *pub, *hkey;
2658 int res;
7a12edd1 2659 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
09368515
JM
2660 const char *info = "OWE Key Generation";
2661 const u8 *addr[2];
2662 size_t len[2];
7a12edd1
JM
2663 u16 group;
2664 size_t hash_len, prime_len;
2665
d90f10fa
JM
2666 if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
2667 wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
2668 return WLAN_STATUS_SUCCESS;
2669 }
2670
7a12edd1 2671 group = WPA_GET_LE16(owe_dh);
91cc34bf
JM
2672 if (!owe_group_supported(hapd, group)) {
2673 wpa_printf(MSG_DEBUG, "OWE: Unsupported DH group %u", group);
2674 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2675 }
7a12edd1
JM
2676 if (group == 19)
2677 prime_len = 32;
2678 else if (group == 20)
2679 prime_len = 48;
2680 else if (group == 21)
2681 prime_len = 66;
2682 else
09368515
JM
2683 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2684
2685 crypto_ecdh_deinit(sta->owe_ecdh);
7a12edd1 2686 sta->owe_ecdh = crypto_ecdh_init(group);
09368515
JM
2687 if (!sta->owe_ecdh)
2688 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
7a12edd1 2689 sta->owe_group = group;
09368515
JM
2690
2691 secret = crypto_ecdh_set_peerkey(sta->owe_ecdh, 0, owe_dh + 2,
2692 owe_dh_len - 2);
7a12edd1 2693 secret = wpabuf_zeropad(secret, prime_len);
09368515
JM
2694 if (!secret) {
2695 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
2696 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2697 }
2698 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
2699
2700 /* prk = HKDF-extract(C | A | group, z) */
2701
2702 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
2703 if (!pub) {
2704 wpabuf_clear_free(secret);
2705 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2706 }
2707
2708 /* PMKID = Truncate-128(Hash(C | A)) */
2709 addr[0] = owe_dh + 2;
2710 len[0] = owe_dh_len - 2;
2711 addr[1] = wpabuf_head(pub);
2712 len[1] = wpabuf_len(pub);
7a12edd1
JM
2713 if (group == 19) {
2714 res = sha256_vector(2, addr, len, pmkid);
2715 hash_len = SHA256_MAC_LEN;
2716 } else if (group == 20) {
2717 res = sha384_vector(2, addr, len, pmkid);
2718 hash_len = SHA384_MAC_LEN;
2719 } else if (group == 21) {
2720 res = sha512_vector(2, addr, len, pmkid);
2721 hash_len = SHA512_MAC_LEN;
2722 } else {
2723 wpabuf_free(pub);
2724 wpabuf_clear_free(secret);
2725 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2726 }
2727 pub = wpabuf_zeropad(pub, prime_len);
2728 if (res < 0 || !pub) {
09368515
JM
2729 wpabuf_free(pub);
2730 wpabuf_clear_free(secret);
2731 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2732 }
2733
2734 hkey = wpabuf_alloc(owe_dh_len - 2 + wpabuf_len(pub) + 2);
2735 if (!hkey) {
2736 wpabuf_free(pub);
2737 wpabuf_clear_free(secret);
2738 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2739 }
2740
2741 wpabuf_put_data(hkey, owe_dh + 2, owe_dh_len - 2); /* C */
2742 wpabuf_put_buf(hkey, pub); /* A */
2743 wpabuf_free(pub);
7a12edd1
JM
2744 wpabuf_put_le16(hkey, group); /* group */
2745 if (group == 19)
2746 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
2747 wpabuf_head(secret), wpabuf_len(secret), prk);
2748 else if (group == 20)
2749 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
2750 wpabuf_head(secret), wpabuf_len(secret), prk);
2751 else if (group == 21)
2752 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
2753 wpabuf_head(secret), wpabuf_len(secret), prk);
09368515
JM
2754 wpabuf_clear_free(hkey);
2755 wpabuf_clear_free(secret);
2756 if (res < 0)
2757 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2758
7a12edd1 2759 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
09368515
JM
2760
2761 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
2762
2763 os_free(sta->owe_pmk);
7a12edd1 2764 sta->owe_pmk = os_malloc(hash_len);
09368515 2765 if (!sta->owe_pmk) {
7a12edd1 2766 os_memset(prk, 0, SHA512_MAC_LEN);
09368515
JM
2767 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2768 }
2769
7a12edd1
JM
2770 if (group == 19)
2771 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
2772 os_strlen(info), sta->owe_pmk, hash_len);
2773 else if (group == 20)
2774 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
2775 os_strlen(info), sta->owe_pmk, hash_len);
2776 else if (group == 21)
2777 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
2778 os_strlen(info), sta->owe_pmk, hash_len);
2779 os_memset(prk, 0, SHA512_MAC_LEN);
09368515
JM
2780 if (res < 0) {
2781 os_free(sta->owe_pmk);
2782 sta->owe_pmk = NULL;
2783 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2784 }
7a12edd1 2785 sta->owe_pmk_len = hash_len;
09368515 2786
7a12edd1 2787 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sta->owe_pmk, sta->owe_pmk_len);
09368515 2788 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
d90f10fa
JM
2789 wpa_auth_pmksa_add2(hapd->wpa_auth, sta->addr, sta->owe_pmk,
2790 sta->owe_pmk_len, pmkid, 0, WPA_KEY_MGMT_OWE);
09368515
JM
2791
2792 return WLAN_STATUS_SUCCESS;
2793}
91cc34bf 2794
09368515
JM
2795#endif /* CONFIG_OWE */
2796
2797
5586f500 2798static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
b57e086c 2799 const u8 *ies, size_t ies_len, int reassoc)
5586f500
JM
2800{
2801 struct ieee802_11_elems elems;
2802 u16 resp;
ba091c06 2803 const u8 *wpa_ie;
5586f500 2804 size_t wpa_ie_len;
94ddef3e 2805 const u8 *p2p_dev_addr = NULL;
5586f500
JM
2806
2807 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2808 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2809 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
2810 "association request");
2811 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2812 }
2813
2814 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
2815 if (resp != WLAN_STATUS_SUCCESS)
2816 return resp;
2817 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
c551700f
KP
2818 if (resp != WLAN_STATUS_SUCCESS)
2819 return resp;
2820 resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
5586f500
JM
2821 if (resp != WLAN_STATUS_SUCCESS)
2822 return resp;
2823 resp = copy_supp_rates(hapd, sta, &elems);
2824 if (resp != WLAN_STATUS_SUCCESS)
2825 return resp;
9c06f0f6
VN
2826
2827 resp = check_multi_ap(hapd, sta, elems.multi_ap, elems.multi_ap_len);
2828 if (resp != WLAN_STATUS_SUCCESS)
2829 return resp;
2830
d45354be 2831#ifdef CONFIG_IEEE80211N
baae4cb9 2832 resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
5586f500
JM
2833 if (resp != WLAN_STATUS_SUCCESS)
2834 return resp;
29448243
JM
2835 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
2836 !(sta->flags & WLAN_STA_HT)) {
2837 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2838 HOSTAPD_LEVEL_INFO, "Station does not support "
2839 "mandatory HT PHY - reject association");
2840 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
2841 }
d45354be 2842#endif /* CONFIG_IEEE80211N */
5586f500 2843
de3cdf35 2844#ifdef CONFIG_IEEE80211AC
28ffd21c
ARN
2845 if (hapd->iconf->ieee80211ac) {
2846 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
2847 if (resp != WLAN_STATUS_SUCCESS)
2848 return resp;
8a458116 2849
28ffd21c
ARN
2850 resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
2851 if (resp != WLAN_STATUS_SUCCESS)
2852 return resp;
2853 }
8a458116 2854
140e850a
MP
2855 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
2856 !(sta->flags & WLAN_STA_VHT)) {
2857 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2858 HOSTAPD_LEVEL_INFO, "Station does not support "
2859 "mandatory VHT PHY - reject association");
13b24a76 2860 return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
140e850a 2861 }
e7d0e97b
YL
2862
2863 if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
2864 resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
2865 elems.vendor_vht_len);
2866 if (resp != WLAN_STATUS_SUCCESS)
2867 return resp;
2868 }
de3cdf35 2869#endif /* CONFIG_IEEE80211AC */
78d35b16
JC
2870#ifdef CONFIG_IEEE80211AX
2871 if (hapd->iconf->ieee80211ax) {
2872 resp = copy_sta_he_capab(hapd, sta, elems.he_capabilities,
2873 elems.he_capabilities_len);
2874 if (resp != WLAN_STATUS_SUCCESS)
2875 return resp;
2876 }
2877#endif /* CONFIG_IEEE80211AX */
de3cdf35 2878
94ddef3e
JM
2879#ifdef CONFIG_P2P
2880 if (elems.p2p) {
2881 wpabuf_free(sta->p2p_ie);
2882 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2883 P2P_IE_VENDOR_TYPE);
2884 if (sta->p2p_ie)
2885 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
2886 } else {
2887 wpabuf_free(sta->p2p_ie);
2888 sta->p2p_ie = NULL;
2889 }
2890#endif /* CONFIG_P2P */
2891
6fc6879b
JM
2892 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
2893 wpa_ie = elems.rsn_ie;
2894 wpa_ie_len = elems.rsn_ie_len;
2895 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
2896 elems.wpa_ie) {
2897 wpa_ie = elems.wpa_ie;
2898 wpa_ie_len = elems.wpa_ie_len;
2899 } else {
2900 wpa_ie = NULL;
2901 wpa_ie_len = 0;
2902 }
5586f500 2903
ad08c363 2904#ifdef CONFIG_WPS
17f6b900 2905 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
c47cf42e
JM
2906 if (hapd->conf->wps_state && elems.wps_ie) {
2907 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
2908 "Request - assume WPS is used");
2909 sta->flags |= WLAN_STA_WPS;
2910 wpabuf_free(sta->wps_ie);
16e46ec0
JM
2911 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2912 WPS_IE_VENDOR_TYPE);
17f6b900
JM
2913 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
2914 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
2915 sta->flags |= WLAN_STA_WPS2;
2916 }
c47cf42e
JM
2917 wpa_ie = NULL;
2918 wpa_ie_len = 0;
54f489be
JM
2919 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
2920 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
2921 "(Re)Association Request - reject");
2922 return WLAN_STATUS_INVALID_IE;
2923 }
c47cf42e
JM
2924 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
2925 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
2926 "(Re)Association Request - possible WPS use");
2927 sta->flags |= WLAN_STA_MAYBE_WPS;
ad08c363
JM
2928 } else
2929#endif /* CONFIG_WPS */
6fc6879b 2930 if (hapd->conf->wpa && wpa_ie == NULL) {
5586f500
JM
2931 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2932 HOSTAPD_LEVEL_INFO,
2933 "No WPA/RSN IE in association request");
2934 return WLAN_STATUS_INVALID_IE;
6fc6879b
JM
2935 }
2936
2937 if (hapd->conf->wpa && wpa_ie) {
2938 int res;
2939 wpa_ie -= 2;
2940 wpa_ie_len += 2;
2941 if (sta->wpa_sm == NULL)
2942 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e
JM
2943 sta->addr,
2944 p2p_dev_addr);
6fc6879b 2945 if (sta->wpa_sm == NULL) {
5586f500
JM
2946 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
2947 "state machine");
2948 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b 2949 }
36536639 2950 wpa_auth_set_auth_alg(sta->wpa_sm, sta->auth_alg);
6fc6879b 2951 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
2c129a1b 2952 hapd->iface->freq,
6fc6879b 2953 wpa_ie, wpa_ie_len,
09368515
JM
2954 elems.mdie, elems.mdie_len,
2955 elems.owe_dh, elems.owe_dh_len);
ffb62f22 2956 resp = wpa_res_to_status_code(res);
6fc6879b 2957 if (resp != WLAN_STATUS_SUCCESS)
5586f500 2958 return resp;
f3f7540e 2959#ifdef CONFIG_IEEE80211W
edb28006
AK
2960 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
2961 (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
2962 !sta->sa_query_timed_out &&
45c94154
JM
2963 sta->sa_query_count > 0)
2964 ap_check_sa_query_timeout(hapd, sta);
edb28006
AK
2965 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
2966 (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
2967 !sta->sa_query_timed_out &&
8f4617c6 2968 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
5d22a1d5 2969 /*
93b76319
JM
2970 * STA has already been associated with MFP and SA
2971 * Query timeout has not been reached. Reject the
2972 * association attempt temporarily and start SA Query,
2973 * if one is not pending.
5d22a1d5
JM
2974 */
2975
93b76319
JM
2976 if (sta->sa_query_count == 0)
2977 ap_sta_start_sa_query(hapd, sta);
5d22a1d5 2978
5586f500 2979 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
5d22a1d5
JM
2980 }
2981
f3f7540e
JM
2982 if (wpa_auth_uses_mfp(sta->wpa_sm))
2983 sta->flags |= WLAN_STA_MFP;
2984 else
2985 sta->flags &= ~WLAN_STA_MFP;
2986#endif /* CONFIG_IEEE80211W */
6fc6879b 2987
4ec1fd8e 2988#ifdef CONFIG_IEEE80211R_AP
6fc6879b
JM
2989 if (sta->auth_alg == WLAN_AUTH_FT) {
2990 if (!reassoc) {
2991 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
2992 "to use association (not "
2993 "re-association) with FT auth_alg",
2994 MAC2STR(sta->addr));
5586f500 2995 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
2996 }
2997
5586f500
JM
2998 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
2999 ies_len);
6fc6879b 3000 if (resp != WLAN_STATUS_SUCCESS)
5586f500 3001 return resp;
6fc6879b 3002 }
4ec1fd8e 3003#endif /* CONFIG_IEEE80211R_AP */
5586f500 3004
c10347f2 3005#ifdef CONFIG_SAE
9d94e4bb
JM
3006 if (wpa_auth_uses_sae(sta->wpa_sm) && sta->sae &&
3007 sta->sae->state == SAE_ACCEPTED)
3008 wpa_auth_add_sae_pmkid(sta->wpa_sm, sta->sae->pmkid);
3009
c10347f2 3010 if (wpa_auth_uses_sae(sta->wpa_sm) &&
f2991170
JM
3011 sta->auth_alg == WLAN_AUTH_OPEN) {
3012 struct rsn_pmksa_cache_entry *sa;
3013 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
3014 if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
3015 wpa_printf(MSG_DEBUG,
3016 "SAE: No PMKSA cache entry found for "
3017 MACSTR, MAC2STR(sta->addr));
3018 return WLAN_STATUS_INVALID_PMKID;
3019 }
3020 wpa_printf(MSG_DEBUG, "SAE: " MACSTR
3021 " using PMKSA caching", MAC2STR(sta->addr));
3022 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
3023 sta->auth_alg != WLAN_AUTH_SAE &&
3024 !(sta->auth_alg == WLAN_AUTH_FT &&
3025 wpa_auth_uses_ft_sae(sta->wpa_sm))) {
c10347f2
JM
3026 wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
3027 "SAE AKM after non-SAE auth_alg %u",
3028 MAC2STR(sta->addr), sta->auth_alg);
3029 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
3030 }
3031#endif /* CONFIG_SAE */
3032
09368515
JM
3033#ifdef CONFIG_OWE
3034 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3035 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
3036 elems.owe_dh) {
d90f10fa 3037 resp = owe_process_assoc_req(hapd, sta, elems.owe_dh,
09368515
JM
3038 elems.owe_dh_len);
3039 if (resp != WLAN_STATUS_SUCCESS)
3040 return resp;
3041 }
3042#endif /* CONFIG_OWE */
3043
10ec6a5f
JM
3044#ifdef CONFIG_DPP2
3045 dpp_pfs_free(sta->dpp_pfs);
3046 sta->dpp_pfs = NULL;
3047
3048 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
3049 hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
3050 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
3051 elems.owe_dh) {
3052 sta->dpp_pfs = dpp_pfs_init(
3053 wpabuf_head(hapd->conf->dpp_netaccesskey),
3054 wpabuf_len(hapd->conf->dpp_netaccesskey));
3055 if (!sta->dpp_pfs) {
3056 wpa_printf(MSG_DEBUG,
3057 "DPP: Could not initialize PFS");
3058 /* Try to continue without PFS */
3059 goto pfs_fail;
3060 }
3061
3062 if (dpp_pfs_process(sta->dpp_pfs, elems.owe_dh,
3063 elems.owe_dh_len) < 0) {
3064 dpp_pfs_free(sta->dpp_pfs);
3065 sta->dpp_pfs = NULL;
3066 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3067 }
3068 }
3069
3070 wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
3071 sta->dpp_pfs->secret : NULL);
3072 pfs_fail:
3073#endif /* CONFIG_DPP2 */
3074
ff36ff00 3075#ifdef CONFIG_IEEE80211N
f0c7a986 3076 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
ff36ff00 3077 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
5586f500
JM
3078 hostapd_logger(hapd, sta->addr,
3079 HOSTAPD_MODULE_IEEE80211,
3080 HOSTAPD_LEVEL_INFO,
3081 "Station tried to use TKIP with HT "
3082 "association");
3083 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
ff36ff00
JM
3084 }
3085#endif /* CONFIG_IEEE80211N */
a14896e8
JM
3086#ifdef CONFIG_HS20
3087 } else if (hapd->conf->osen) {
3088 if (elems.osen == NULL) {
3089 hostapd_logger(
3090 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3091 HOSTAPD_LEVEL_INFO,
3092 "No HS 2.0 OSEN element in association request");
3093 return WLAN_STATUS_INVALID_IE;
3094 }
3095
3096 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
3097 if (sta->wpa_sm == NULL)
3098 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
3099 sta->addr, NULL);
3100 if (sta->wpa_sm == NULL) {
3101 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
3102 "state machine");
3103 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3104 }
3105 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
3106 elems.osen - 2, elems.osen_len + 2) < 0)
3107 return WLAN_STATUS_INVALID_IE;
3108#endif /* CONFIG_HS20 */
a8d05fca
JM
3109 } else
3110 wpa_auth_sta_no_wpa(sta->wpa_sm);
6fc6879b 3111
b305c684 3112#ifdef CONFIG_P2P
3f4ce13f 3113 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
b305c684
JM
3114#endif /* CONFIG_P2P */
3115
f403dcd6
JM
3116#ifdef CONFIG_HS20
3117 wpabuf_free(sta->hs20_ie);
3118 if (elems.hs20 && elems.hs20_len > 4) {
f3784a6b
JM
3119 int release;
3120
f403dcd6
JM
3121 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
3122 elems.hs20_len - 4);
f3784a6b
JM
3123 release = ((elems.hs20[4] >> 4) & 0x0f) + 1;
3124 if (release >= 2 && !wpa_auth_uses_mfp(sta->wpa_sm)) {
3125 wpa_printf(MSG_DEBUG,
3126 "HS 2.0: PMF not negotiated by release %d station "
3127 MACSTR, release, MAC2STR(sta->addr));
3128 return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
3129 }
3130 } else {
f403dcd6 3131 sta->hs20_ie = NULL;
f3784a6b 3132 }
67cca346
JM
3133
3134 wpabuf_free(sta->roaming_consortium);
3135 if (elems.roaming_cons_sel)
3136 sta->roaming_consortium = wpabuf_alloc_copy(
3137 elems.roaming_cons_sel + 4,
3138 elems.roaming_cons_sel_len - 4);
3139 else
3140 sta->roaming_consortium = NULL;
f403dcd6
JM
3141#endif /* CONFIG_HS20 */
3142
ae667c08
AN
3143#ifdef CONFIG_FST
3144 wpabuf_free(sta->mb_ies);
3145 if (hapd->iface->fst)
3146 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
3147 else
3148 sta->mb_ies = NULL;
3149#endif /* CONFIG_FST */
3150
4c572281 3151#ifdef CONFIG_MBO
6332aaf3
JM
3152 mbo_ap_check_sta_assoc(hapd, sta, &elems);
3153
4c572281
JM
3154 if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
3155 elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
3156 hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3157 wpa_printf(MSG_INFO,
3158 "MBO: Reject WPA2 association without PMF");
3159 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3160 }
3161#endif /* CONFIG_MBO */
3162
99621dc1
MV
3163#if defined(CONFIG_FILS) && defined(CONFIG_OCV)
3164 if (wpa_auth_uses_ocv(sta->wpa_sm) &&
3165 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3166 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3167 sta->auth_alg == WLAN_AUTH_FILS_PK)) {
3168 struct wpa_channel_info ci;
3169 int tx_chanwidth;
3170 int tx_seg1_idx;
3171
3172 if (hostapd_drv_channel_info(hapd, &ci) != 0) {
3173 wpa_printf(MSG_WARNING,
3174 "Failed to get channel info to validate received OCI in FILS (Re)Association Request frame");
3175 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3176 }
3177
3178 if (get_sta_tx_parameters(sta->wpa_sm,
3179 channel_width_to_int(ci.chanwidth),
3180 ci.seg1_idx, &tx_chanwidth,
3181 &tx_seg1_idx) < 0)
3182 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3183
3184 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
3185 tx_chanwidth, tx_seg1_idx) != 0) {
3186 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
3187 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3188 }
3189 }
3190#endif /* CONFIG_FILS && CONFIG_OCV */
3191
adf0478e
JM
3192 ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
3193 elems.supp_op_classes_len);
3194
629e1804
DS
3195 if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
3196 elems.rrm_enabled &&
3197 elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
3198 os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
3199 sizeof(sta->rrm_enabled_capa));
3200
ba72b4b1 3201 if (elems.power_capab) {
3202 sta->min_tx_power = elems.power_capab[0];
3203 sta->max_tx_power = elems.power_capab[1];
3204 sta->power_capab = 1;
3205 } else {
3206 sta->power_capab = 0;
3207 }
3208
5586f500
JM
3209 return WLAN_STATUS_SUCCESS;
3210}
3211
3212
3213static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
3214 u16 reason_code)
3215{
3216 int send_len;
3217 struct ieee80211_mgmt reply;
3218
3219 os_memset(&reply, 0, sizeof(reply));
3220 reply.frame_control =
3221 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
3222 os_memcpy(reply.da, addr, ETH_ALEN);
3223 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
3224 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
3225
3226 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
3227 reply.u.deauth.reason_code = host_to_le16(reason_code);
3228
8cfa3527 3229 if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
5586f500
JM
3230 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
3231 strerror(errno));
3232}
3233
3234
3f81ac07 3235static int add_associated_sta(struct hostapd_data *hapd,
4cf5efec 3236 struct sta_info *sta, int reassoc)
3f81ac07
AO
3237{
3238 struct ieee80211_ht_capabilities ht_cap;
3239 struct ieee80211_vht_capabilities vht_cap;
78d35b16 3240 struct ieee80211_he_capabilities he_cap;
0e3bd7ac 3241 int set = 1;
3f81ac07
AO
3242
3243 /*
3244 * Remove the STA entry to ensure the STA PS state gets cleared and
3245 * configuration gets updated. This is relevant for cases, such as
3246 * FT-over-the-DS, where a station re-associates back to the same AP but
3247 * skips the authentication flow, or if working with a driver that
3248 * does not support full AP client state.
0e3bd7ac
MV
3249 *
3250 * Skip this if the STA has already completed FT reassociation and the
3251 * TK has been configured since the TX/RX PN must not be reset to 0 for
3252 * the same key.
4cf5efec
JM
3253 *
3254 * FT-over-the-DS has a special case where the STA entry (and as such,
3255 * the TK) has not yet been configured to the driver depending on which
3256 * driver interface is used. For that case, allow add-STA operation to
3257 * be used (instead of set-STA). This is needed to allow mac80211-based
3258 * drivers to accept the STA parameter configuration. Since this is
3259 * after a new FT-over-DS exchange, a new TK has been derived, so key
3260 * reinstallation is not a concern for this case.
3f81ac07 3261 */
4cf5efec
JM
3262 wpa_printf(MSG_DEBUG, "Add associated STA " MACSTR
3263 " (added_unassoc=%d auth_alg=%u ft_over_ds=%u reassoc=%d authorized=%d ft_tk=%d fils_tk=%d)",
3264 MAC2STR(sta->addr), sta->added_unassoc, sta->auth_alg,
3265 sta->ft_over_ds, reassoc,
3266 !!(sta->flags & WLAN_STA_AUTHORIZED),
3267 wpa_auth_sta_ft_tk_already_set(sta->wpa_sm),
3268 wpa_auth_sta_fils_tk_already_set(sta->wpa_sm));
3269
0e3bd7ac
MV
3270 if (!sta->added_unassoc &&
3271 (!(sta->flags & WLAN_STA_AUTHORIZED) ||
4cf5efec 3272 (reassoc && sta->ft_over_ds && sta->auth_alg == WLAN_AUTH_FT) ||
2f1357fb
JM
3273 (!wpa_auth_sta_ft_tk_already_set(sta->wpa_sm) &&
3274 !wpa_auth_sta_fils_tk_already_set(sta->wpa_sm)))) {
3f81ac07 3275 hostapd_drv_sta_remove(hapd, sta->addr);
0e3bd7ac
MV
3276 wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
3277 set = 0;
4cf5efec
JM
3278
3279 /* Do not allow the FT-over-DS exception to be used more than
3280 * once per authentication exchange to guarantee a new TK is
3281 * used here */
3282 sta->ft_over_ds = 0;
0e3bd7ac 3283 }
3f81ac07
AO
3284
3285#ifdef CONFIG_IEEE80211N
3286 if (sta->flags & WLAN_STA_HT)
3287 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
3288#endif /* CONFIG_IEEE80211N */
3289#ifdef CONFIG_IEEE80211AC
3290 if (sta->flags & WLAN_STA_VHT)
3291 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
3292#endif /* CONFIG_IEEE80211AC */
78d35b16
JC
3293#ifdef CONFIG_IEEE80211AX
3294 if (sta->flags & WLAN_STA_HE) {
3295 hostapd_get_he_capab(hapd, sta->he_capab, &he_cap,
3296 sta->he_capab_len);
3297 }
3298#endif /* CONFIG_IEEE80211AX */
3f81ac07
AO
3299
3300 /*
3301 * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
3302 * will be set when the ACK frame for the (Re)Association Response frame
3303 * is processed (TX status driver event).
3304 */
3305 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
3306 sta->supported_rates, sta->supported_rates_len,
3307 sta->listen_interval,
3308 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
3309 sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
78d35b16
JC
3310 sta->flags & WLAN_STA_HE ? &he_cap : NULL,
3311 sta->flags & WLAN_STA_HE ? sta->he_capab_len : 0,
3f81ac07 3312 sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
ae33239c 3313 sta->vht_opmode, sta->p2p_ie ? 1 : 0,
0e3bd7ac 3314 set)) {
3f81ac07
AO
3315 hostapd_logger(hapd, sta->addr,
3316 HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
3317 "Could not %s STA to kernel driver",
0e3bd7ac 3318 set ? "set" : "add");
3f81ac07
AO
3319
3320 if (sta->added_unassoc) {
3321 hostapd_drv_sta_remove(hapd, sta->addr);
3322 sta->added_unassoc = 0;
3323 }
3324
3325 return -1;
3326 }
3327
3328 sta->added_unassoc = 0;
3329
3330 return 0;
3331}
3332
3333
bb598c3b 3334static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
05e5e615 3335 const u8 *addr, u16 status_code, int reassoc,
076f1ea1 3336 const u8 *ies, size_t ies_len, int rssi)
5586f500
JM
3337{
3338 int send_len;
91d91abf
JM
3339 u8 *buf;
3340 size_t buflen;
5586f500
JM
3341 struct ieee80211_mgmt *reply;
3342 u8 *p;
91d91abf 3343 u16 res = WLAN_STATUS_SUCCESS;
5586f500 3344
91d91abf
JM
3345 buflen = sizeof(struct ieee80211_mgmt) + 1024;
3346#ifdef CONFIG_FILS
84bb12aa 3347 if (sta && sta->fils_hlp_resp)
91d91abf 3348 buflen += wpabuf_len(sta->fils_hlp_resp);
831d8c9c
JM
3349 if (sta)
3350 buflen += 150;
91d91abf 3351#endif /* CONFIG_FILS */
09368515
JM
3352#ifdef CONFIG_OWE
3353 if (sta && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
d8c8d857 3354 buflen += 150;
09368515 3355#endif /* CONFIG_OWE */
10ec6a5f
JM
3356#ifdef CONFIG_DPP2
3357 if (sta && sta->dpp_pfs)
3358 buflen += 5 + sta->dpp_pfs->curve->prime_len;
3359#endif /* CONFIG_DPP2 */
91d91abf
JM
3360 buf = os_zalloc(buflen);
3361 if (!buf) {
3362 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3363 goto done;
3364 }
5586f500
JM
3365 reply = (struct ieee80211_mgmt *) buf;
3366 reply->frame_control =
3367 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
3368 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
3369 WLAN_FC_STYPE_ASSOC_RESP));
05e5e615 3370 os_memcpy(reply->da, addr, ETH_ALEN);
5586f500
JM
3371 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
3372 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
3373
3374 send_len = IEEE80211_HDRLEN;
3375 send_len += sizeof(reply->u.assoc_resp);
3376 reply->u.assoc_resp.capab_info =
f41ded6f 3377 host_to_le16(hostapd_own_capab_info(hapd));
5586f500 3378 reply->u.assoc_resp.status_code = host_to_le16(status_code);
05e5e615
DL
3379
3380 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0) |
3381 BIT(14) | BIT(15));
5586f500
JM
3382 /* Supported rates */
3383 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
3384 /* Extended supported rates */
3385 p = hostapd_eid_ext_supp_rates(hapd, p);
5586f500 3386
076f1ea1
BL
3387#ifdef CONFIG_MBO
3388 if (status_code == WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
3389 rssi != 0) {
3390 int delta = hapd->iconf->rssi_reject_assoc_rssi - rssi;
3391
3392 p = hostapd_eid_mbo_rssi_assoc_rej(hapd, p, buf + buflen - p,
3393 delta);
3394 }
3395#endif /* CONFIG_MBO */
3396
4ec1fd8e 3397#ifdef CONFIG_IEEE80211R_AP
05e5e615 3398 if (sta && status_code == WLAN_STATUS_SUCCESS) {
5586f500
JM
3399 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
3400 * Transition Information, RSN, [RIC Response] */
3401 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
91d91abf 3402 buf + buflen - p,
5586f500 3403 sta->auth_alg, ies, ies_len);
2cf36d60
JM
3404 if (!p) {
3405 wpa_printf(MSG_DEBUG,
3406 "FT: Failed to write AssocResp IEs");
3407 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3408 goto done;
3409 }
5586f500 3410 }
4ec1fd8e 3411#endif /* CONFIG_IEEE80211R_AP */
831d8c9c
JM
3412#ifdef CONFIG_FILS
3413 if (sta && status_code == WLAN_STATUS_SUCCESS &&
3414 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3415 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3416 sta->auth_alg == WLAN_AUTH_FILS_PK))
3417 p = wpa_auth_write_assoc_resp_fils(sta->wpa_sm, p,
3418 buf + buflen - p,
3419 ies, ies_len);
3420#endif /* CONFIG_FILS */
5586f500 3421
d8c8d857 3422#ifdef CONFIG_OWE
8fc6d884
JM
3423 if (sta && status_code == WLAN_STATUS_SUCCESS &&
3424 (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
d8c8d857
JM
3425 p = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, p,
3426 buf + buflen - p,
3427 ies, ies_len);
3428#endif /* CONFIG_OWE */
3429
5586f500 3430#ifdef CONFIG_IEEE80211W
05e5e615 3431 if (sta && status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
5586f500
JM
3432 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
3433#endif /* CONFIG_IEEE80211W */
3434
1bc774a1
JM
3435#ifdef CONFIG_IEEE80211N
3436 p = hostapd_eid_ht_capabilities(hapd, p);
3437 p = hostapd_eid_ht_operation(hapd, p);
3438#endif /* CONFIG_IEEE80211N */
3439
14708b50 3440#ifdef CONFIG_IEEE80211AC
e7d0e97b 3441 if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
fc72a48a
T
3442 u32 nsts = 0, sta_nsts;
3443
05e5e615 3444 if (sta && hapd->conf->use_sta_nsts && sta->vht_capabilities) {
fc72a48a
T
3445 struct ieee80211_vht_capabilities *capa;
3446
3447 nsts = (hapd->iface->conf->vht_capab >>
3448 VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
3449 capa = sta->vht_capabilities;
3450 sta_nsts = (le_to_host32(capa->vht_capabilities_info) >>
3451 VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
3452
3453 if (nsts < sta_nsts)
3454 nsts = 0;
3455 else
3456 nsts = sta_nsts;
3457 }
3458 p = hostapd_eid_vht_capabilities(hapd, p, nsts);
e7d0e97b
YL
3459 p = hostapd_eid_vht_operation(hapd, p);
3460 }
14708b50
MP
3461#endif /* CONFIG_IEEE80211AC */
3462
78d35b16
JC
3463#ifdef CONFIG_IEEE80211AX
3464 if (hapd->iconf->ieee80211ax) {
3465 p = hostapd_eid_he_capab(hapd, p);
3466 p = hostapd_eid_he_operation(hapd, p);
3467 p = hostapd_eid_spatial_reuse(hapd, p);
3468 p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
3469 }
3470#endif /* CONFIG_IEEE80211AX */
3471
1161ff1e 3472 p = hostapd_eid_ext_capab(hapd, p);
b6668734 3473 p = hostapd_eid_bss_max_idle_period(hapd, p);
05e5e615 3474 if (sta && sta->qos_map_enabled)
c551700f 3475 p = hostapd_eid_qos_map_set(hapd, p);
1161ff1e 3476
347827ff
AN
3477#ifdef CONFIG_FST
3478 if (hapd->iface->fst_ies) {
3479 os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
3480 wpabuf_len(hapd->iface->fst_ies));
3481 p += wpabuf_len(hapd->iface->fst_ies);
3482 }
3483#endif /* CONFIG_FST */
3484
b750dde6
JM
3485#ifdef CONFIG_OWE
3486 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3487 sta && sta->owe_ecdh && status_code == WLAN_STATUS_SUCCESS &&
3488 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE) {
3489 struct wpabuf *pub;
3490
3491 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3492 if (!pub) {
3493 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3494 goto done;
3495 }
3496 /* OWE Diffie-Hellman Parameter element */
3497 *p++ = WLAN_EID_EXTENSION; /* Element ID */
3498 *p++ = 1 + 2 + wpabuf_len(pub); /* Length */
3499 *p++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension */
3500 WPA_PUT_LE16(p, sta->owe_group);
3501 p += 2;
3502 os_memcpy(p, wpabuf_head(pub), wpabuf_len(pub));
3503 p += wpabuf_len(pub);
3504 wpabuf_free(pub);
3505 }
3506#endif /* CONFIG_OWE */
3507
10ec6a5f
JM
3508#ifdef CONFIG_DPP2
3509 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
3510 sta && sta->dpp_pfs && status_code == WLAN_STATUS_SUCCESS &&
3511 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP) {
3512 os_memcpy(p, wpabuf_head(sta->dpp_pfs->ie),
3513 wpabuf_len(sta->dpp_pfs->ie));
3514 p += wpabuf_len(sta->dpp_pfs->ie);
3515 }
3516#endif /* CONFIG_DPP2 */
3517
e7d0e97b 3518#ifdef CONFIG_IEEE80211AC
05e5e615 3519 if (sta && hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
e7d0e97b
YL
3520 p = hostapd_eid_vendor_vht(hapd, p);
3521#endif /* CONFIG_IEEE80211AC */
3522
05e5e615 3523 if (sta && (sta->flags & WLAN_STA_WMM))
1bc774a1
JM
3524 p = hostapd_eid_wmm(hapd, p);
3525
ed7a09f9 3526#ifdef CONFIG_WPS
05e5e615
DL
3527 if (sta &&
3528 ((sta->flags & WLAN_STA_WPS) ||
3529 ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa))) {
ed7a09f9
JM
3530 struct wpabuf *wps = wps_build_assoc_resp_ie();
3531 if (wps) {
3532 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
3533 p += wpabuf_len(wps);
3534 wpabuf_free(wps);
3535 }
3536 }
3537#endif /* CONFIG_WPS */
3538
9c06f0f6
VN
3539 if (sta && (sta->flags & WLAN_STA_MULTI_AP))
3540 p = hostapd_eid_multi_ap(hapd, p);
3541
8ccbe415 3542#ifdef CONFIG_P2P
05e5e615 3543 if (sta && sta->p2p_ie && hapd->p2p_group) {
8ccbe415
JM
3544 struct wpabuf *p2p_resp_ie;
3545 enum p2p_status_code status;
3546 switch (status_code) {
3547 case WLAN_STATUS_SUCCESS:
3548 status = P2P_SC_SUCCESS;
3549 break;
3550 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
3551 status = P2P_SC_FAIL_LIMIT_REACHED;
3552 break;
3553 default:
3554 status = P2P_SC_FAIL_INVALID_PARAMS;
3555 break;
3556 }
3557 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
3558 if (p2p_resp_ie) {
3559 os_memcpy(p, wpabuf_head(p2p_resp_ie),
3560 wpabuf_len(p2p_resp_ie));
3561 p += wpabuf_len(p2p_resp_ie);
3562 wpabuf_free(p2p_resp_ie);
3563 }
3564 }
3565#endif /* CONFIG_P2P */
3566
962473c1
JM
3567#ifdef CONFIG_P2P_MANAGER
3568 if (hapd->conf->p2p & P2P_MANAGE)
3569 p = hostapd_eid_p2p_manage(hapd, p);
3570#endif /* CONFIG_P2P_MANAGER */
3571
91d91abf 3572 p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
fb9a1c3e 3573
a9112270 3574 if (hapd->conf->assocresp_elements &&
91d91abf 3575 (size_t) (buf + buflen - p) >=
a9112270
BKB
3576 wpabuf_len(hapd->conf->assocresp_elements)) {
3577 os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
3578 wpabuf_len(hapd->conf->assocresp_elements));
3579 p += wpabuf_len(hapd->conf->assocresp_elements);
3580 }
3581
5586f500
JM
3582 send_len += p - reply->u.assoc_resp.variable;
3583
e73ffa09 3584#ifdef CONFIG_FILS
05e5e615
DL
3585 if (sta &&
3586 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
e73ffa09
JM
3587 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3588 sta->auth_alg == WLAN_AUTH_FILS_PK) &&
3589 status_code == WLAN_STATUS_SUCCESS) {
3590 struct ieee802_11_elems elems;
3591
3592 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
91d91abf
JM
3593 ParseFailed || !elems.fils_session) {
3594 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3595 goto done;
3596 }
e73ffa09
JM
3597
3598 /* FILS Session */
3599 *p++ = WLAN_EID_EXTENSION; /* Element ID */
3600 *p++ = 1 + FILS_SESSION_LEN; /* Length */
3601 *p++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
3602 os_memcpy(p, elems.fils_session, FILS_SESSION_LEN);
3603 send_len += 2 + 1 + FILS_SESSION_LEN;
3604
3605 send_len = fils_encrypt_assoc(sta->wpa_sm, buf, send_len,
91d91abf
JM
3606 buflen, sta->fils_hlp_resp);
3607 if (send_len < 0) {
3608 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3609 goto done;
3610 }
e73ffa09
JM
3611 }
3612#endif /* CONFIG_FILS */
3613
bb598c3b 3614 if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0) {
5586f500
JM
3615 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
3616 strerror(errno));
91d91abf 3617 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
bb598c3b
AB
3618 }
3619
91d91abf
JM
3620done:
3621 os_free(buf);
3622 return res;
5586f500
JM
3623}
3624
3625
33c8bbd8 3626#ifdef CONFIG_OWE
79ce2d51
AP
3627u8 * owe_assoc_req_process(struct hostapd_data *hapd, struct sta_info *sta,
3628 const u8 *owe_dh, u8 owe_dh_len,
3629 u8 *owe_buf, size_t owe_buf_len, u16 *reason)
33c8bbd8 3630{
a22e235f
AP
3631#ifdef CONFIG_TESTING_OPTIONS
3632 if (hapd->conf->own_ie_override) {
3633 wpa_printf(MSG_DEBUG, "OWE: Using IE override");
3634 *reason = WLAN_STATUS_SUCCESS;
3635 return wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3636 owe_buf_len, NULL, 0);
3637 }
3638#endif /* CONFIG_TESTING_OPTIONS */
3639
cd483be2
AP
3640 if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
3641 wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
3642 owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3643 owe_buf_len, NULL, 0);
3644 *reason = WLAN_STATUS_SUCCESS;
3645 return owe_buf;
3646 }
3647
33c8bbd8
AKP
3648 *reason = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
3649 if (*reason != WLAN_STATUS_SUCCESS)
3650 return NULL;
33c8bbd8 3651
af65ef28
AP
3652 owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3653 owe_buf_len, NULL, 0);
3654
f5701cc6 3655 if (sta->owe_ecdh && owe_buf) {
759da93a
AP
3656 struct wpabuf *pub;
3657
3658 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3659 if (!pub) {
3660 *reason = WLAN_STATUS_UNSPECIFIED_FAILURE;
3661 return owe_buf;
3662 }
3663
3664 /* OWE Diffie-Hellman Parameter element */
3665 *owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
3666 *owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
3667 *owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
3668 */
3669 WPA_PUT_LE16(owe_buf, sta->owe_group);
3670 owe_buf += 2;
3671 os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
3672 owe_buf += wpabuf_len(pub);
3673 wpabuf_free(pub);
3674 }
3675
33c8bbd8
AKP
3676 return owe_buf;
3677}
3678#endif /* CONFIG_OWE */
3679
3680
91d91abf
JM
3681#ifdef CONFIG_FILS
3682
3683void fils_hlp_finish_assoc(struct hostapd_data *hapd, struct sta_info *sta)
3684{
3685 u16 reply_res;
3686
3687 wpa_printf(MSG_DEBUG, "FILS: Finish association with " MACSTR,
3688 MAC2STR(sta->addr));
3689 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
3690 if (!sta->fils_pending_assoc_req)
3691 return;
3692 reply_res = send_assoc_resp(hapd, sta, sta->addr, WLAN_STATUS_SUCCESS,
3693 sta->fils_pending_assoc_is_reassoc,
3694 sta->fils_pending_assoc_req,
076f1ea1 3695 sta->fils_pending_assoc_req_len, 0);
91d91abf
JM
3696 os_free(sta->fils_pending_assoc_req);
3697 sta->fils_pending_assoc_req = NULL;
3698 sta->fils_pending_assoc_req_len = 0;
3699 wpabuf_free(sta->fils_hlp_resp);
3700 sta->fils_hlp_resp = NULL;
3701 wpabuf_free(sta->hlp_dhcp_discover);
3702 sta->hlp_dhcp_discover = NULL;
3703
3704 /*
fd35ed5b
IP
3705 * Remove the station in case transmission of a success response fails.
3706 * At this point the station was already added associated to the driver.
91d91abf 3707 */
fd35ed5b 3708 if (reply_res != WLAN_STATUS_SUCCESS)
91d91abf 3709 hostapd_drv_sta_remove(hapd, sta->addr);
91d91abf
JM
3710}
3711
3712
3713void fils_hlp_timeout(void *eloop_ctx, void *eloop_data)
3714{
3715 struct hostapd_data *hapd = eloop_ctx;
3716 struct sta_info *sta = eloop_data;
3717
3718 wpa_printf(MSG_DEBUG,
3719 "FILS: HLP response timeout - continue with association response for "
3720 MACSTR, MAC2STR(sta->addr));
8b5ddda5
JM
3721 if (sta->fils_drv_assoc_finish)
3722 hostapd_notify_assoc_fils_finish(hapd, sta);
3723 else
3724 fils_hlp_finish_assoc(hapd, sta);
91d91abf
JM
3725}
3726
3727#endif /* CONFIG_FILS */
3728
3729
5586f500 3730static void handle_assoc(struct hostapd_data *hapd,
b57e086c 3731 const struct ieee80211_mgmt *mgmt, size_t len,
076f1ea1 3732 int reassoc, int rssi)
5586f500 3733{
38cb0a2d 3734 u16 capab_info, listen_interval, seq_ctrl, fc;
bb598c3b 3735 u16 resp = WLAN_STATUS_SUCCESS, reply_res;
b57e086c 3736 const u8 *pos;
5586f500
JM
3737 int left, i;
3738 struct sta_info *sta;
78815f3d 3739 u8 *tmp = NULL;
53d17144
DL
3740 struct hostapd_sta_wpa_psk_short *psk = NULL;
3741 char *identity = NULL;
3742 char *radius_cui = NULL;
91d91abf
JM
3743#ifdef CONFIG_FILS
3744 int delay_assoc = 0;
3745#endif /* CONFIG_FILS */
5586f500
JM
3746
3747 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
3748 sizeof(mgmt->u.assoc_req))) {
61323e70
JM
3749 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
3750 reassoc, (unsigned long) len);
5586f500
JM
3751 return;
3752 }
3753
c2aff6b1
JB
3754#ifdef CONFIG_TESTING_OPTIONS
3755 if (reassoc) {
06df2aa6 3756 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
c2aff6b1
JB
3757 drand48() < hapd->iconf->ignore_reassoc_probability) {
3758 wpa_printf(MSG_INFO,
3759 "TESTING: ignoring reassoc request from "
3760 MACSTR, MAC2STR(mgmt->sa));
3761 return;
3762 }
3763 } else {
06df2aa6 3764 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
c2aff6b1
JB
3765 drand48() < hapd->iconf->ignore_assoc_probability) {
3766 wpa_printf(MSG_INFO,
3767 "TESTING: ignoring assoc request from "
3768 MACSTR, MAC2STR(mgmt->sa));
3769 return;
3770 }
3771 }
3772#endif /* CONFIG_TESTING_OPTIONS */
3773
38cb0a2d
IP
3774 fc = le_to_host16(mgmt->frame_control);
3775 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
3776
5586f500
JM
3777 if (reassoc) {
3778 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
3779 listen_interval = le_to_host16(
3780 mgmt->u.reassoc_req.listen_interval);
3781 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
3782 " capab_info=0x%02x listen_interval=%d current_ap="
38cb0a2d 3783 MACSTR " seq_ctrl=0x%x%s",
5586f500 3784 MAC2STR(mgmt->sa), capab_info, listen_interval,
38cb0a2d
IP
3785 MAC2STR(mgmt->u.reassoc_req.current_ap),
3786 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
5586f500
JM
3787 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
3788 pos = mgmt->u.reassoc_req.variable;
3789 } else {
3790 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
3791 listen_interval = le_to_host16(
3792 mgmt->u.assoc_req.listen_interval);
3793 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
38cb0a2d
IP
3794 " capab_info=0x%02x listen_interval=%d "
3795 "seq_ctrl=0x%x%s",
3796 MAC2STR(mgmt->sa), capab_info, listen_interval,
3797 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
5586f500
JM
3798 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
3799 pos = mgmt->u.assoc_req.variable;
3800 }
3801
3802 sta = ap_get_sta(hapd, mgmt->sa);
4ec1fd8e 3803#ifdef CONFIG_IEEE80211R_AP
5586f500
JM
3804 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
3805 (sta->flags & WLAN_STA_AUTH) == 0) {
3806 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
3807 "prior to authentication since it is using "
3808 "over-the-DS FT", MAC2STR(mgmt->sa));
bb598c3b
AB
3809
3810 /*
3811 * Mark station as authenticated, to avoid adding station
3812 * entry in the driver as associated and not authenticated
3813 */
3814 sta->flags |= WLAN_STA_AUTH;
5586f500 3815 } else
4ec1fd8e 3816#endif /* CONFIG_IEEE80211R_AP */
5586f500 3817 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
05e5e615
DL
3818 if (hapd->iface->current_mode &&
3819 hapd->iface->current_mode->mode ==
3820 HOSTAPD_MODE_IEEE80211AD) {
53d17144
DL
3821 int acl_res;
3822 u32 session_timeout, acct_interim_interval;
3823 struct vlan_description vlan_id;
3824
3825 acl_res = ieee802_11_allowed_address(
3826 hapd, mgmt->sa, (const u8 *) mgmt, len,
3827 &session_timeout, &acct_interim_interval,
92eb00ae 3828 &vlan_id, &psk, &identity, &radius_cui, 0);
53d17144 3829 if (acl_res == HOSTAPD_ACL_REJECT) {
92eb00ae
T
3830 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
3831 "Ignore Association Request frame from "
3832 MACSTR " due to ACL reject",
3833 MAC2STR(mgmt->sa));
53d17144
DL
3834 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3835 goto fail;
3836 }
3837 if (acl_res == HOSTAPD_ACL_PENDING)
3838 return;
3839
05e5e615
DL
3840 /* DMG/IEEE 802.11ad does not use authentication.
3841 * Allocate sta entry upon association. */
3842 sta = ap_sta_add(hapd, mgmt->sa);
3843 if (!sta) {
3844 hostapd_logger(hapd, mgmt->sa,
3845 HOSTAPD_MODULE_IEEE80211,
3846 HOSTAPD_LEVEL_INFO,
3847 "Failed to add STA");
3848 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
3849 goto fail;
3850 }
3851
53d17144
DL
3852 acl_res = ieee802_11_set_radius_info(
3853 hapd, sta, acl_res, session_timeout,
3854 acct_interim_interval, &vlan_id, &psk,
3855 &identity, &radius_cui);
3856 if (acl_res) {
3857 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3858 goto fail;
3859 }
3860
05e5e615
DL
3861 hostapd_logger(hapd, sta->addr,
3862 HOSTAPD_MODULE_IEEE80211,
3863 HOSTAPD_LEVEL_DEBUG,
3864 "Skip authentication for DMG/IEEE 802.11ad");
3865 sta->flags |= WLAN_STA_AUTH;
3866 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
3867 sta->auth_alg = WLAN_AUTH_OPEN;
3868 } else {
3869 hostapd_logger(hapd, mgmt->sa,
3870 HOSTAPD_MODULE_IEEE80211,
3871 HOSTAPD_LEVEL_INFO,
3872 "Station tried to associate before authentication (aid=%d flags=0x%x)",
3873 sta ? sta->aid : -1,
3874 sta ? sta->flags : 0);
3875 send_deauth(hapd, mgmt->sa,
3876 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
3877 return;
3878 }
5586f500
JM
3879 }
3880
38cb0a2d
IP
3881 if ((fc & WLAN_FC_RETRY) &&
3882 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
3883 sta->last_seq_ctrl == seq_ctrl &&
fa67debf
JM
3884 sta->last_subtype == (reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
3885 WLAN_FC_STYPE_ASSOC_REQ)) {
38cb0a2d
IP
3886 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3887 HOSTAPD_LEVEL_DEBUG,
3888 "Drop repeated association frame seq_ctrl=0x%x",
3889 seq_ctrl);
3890 return;
3891 }
3892 sta->last_seq_ctrl = seq_ctrl;
3893 sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
3894 WLAN_FC_STYPE_ASSOC_REQ;
3895
5586f500 3896 if (hapd->tkip_countermeasures) {
fe12ae77 3897 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5586f500
JM
3898 goto fail;
3899 }
3900
3901 if (listen_interval > hapd->conf->max_listen_interval) {
3902 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
3903 HOSTAPD_LEVEL_DEBUG,
3904 "Too large Listen Interval (%d)",
3905 listen_interval);
3906 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
3907 goto fail;
3908 }
3909
fb9a1c3e
AS
3910#ifdef CONFIG_MBO
3911 if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
3912 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
3913 goto fail;
3914 }
076f1ea1
BL
3915
3916 if (hapd->iconf->rssi_reject_assoc_rssi && rssi &&
2ea1fce3
JM
3917 rssi < hapd->iconf->rssi_reject_assoc_rssi &&
3918 (sta->auth_rssi == 0 ||
3919 sta->auth_rssi < hapd->iconf->rssi_reject_assoc_rssi)) {
076f1ea1
BL
3920 resp = WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS;
3921 goto fail;
3922 }
fb9a1c3e
AS
3923#endif /* CONFIG_MBO */
3924
629e1804
DS
3925 /*
3926 * sta->capability is used in check_assoc_ies() for RRM enabled
3927 * capability element.
3928 */
3929 sta->capability = capab_info;
3930
78815f3d
JM
3931#ifdef CONFIG_FILS
3932 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3933 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3934 sta->auth_alg == WLAN_AUTH_FILS_PK) {
1489fcf8
JM
3935 int res;
3936
78815f3d
JM
3937 /* The end of the payload is encrypted. Need to decrypt it
3938 * before parsing. */
3939
a1f11e34 3940 tmp = os_memdup(pos, left);
78815f3d
JM
3941 if (!tmp) {
3942 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3943 goto fail;
3944 }
78815f3d 3945
1489fcf8
JM
3946 res = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
3947 len, tmp, left);
3948 if (res < 0) {
78815f3d
JM
3949 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3950 goto fail;
3951 }
3952 pos = tmp;
1489fcf8 3953 left = res;
78815f3d
JM
3954 }
3955#endif /* CONFIG_FILS */
3956
5586f500
JM
3957 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
3958 * is used */
3959 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
3960 if (resp != WLAN_STATUS_SUCCESS)
3961 goto fail;
3962
3963 if (hostapd_get_aid(hapd, sta) < 0) {
3964 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
3965 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
3966 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
3967 goto fail;
3968 }
3969
5586f500
JM
3970 sta->listen_interval = listen_interval;
3971
28d12641
AKP
3972 if (hapd->iface->current_mode &&
3973 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
6fc6879b
JM
3974 sta->flags |= WLAN_STA_NONERP;
3975 for (i = 0; i < sta->supported_rates_len; i++) {
3976 if ((sta->supported_rates[i] & 0x7f) > 22) {
3977 sta->flags &= ~WLAN_STA_NONERP;
3978 break;
3979 }
3980 }
3981 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
3982 sta->nonerp_set = 1;
3983 hapd->iface->num_sta_non_erp++;
3984 if (hapd->iface->num_sta_non_erp == 1)
3985 ieee802_11_set_beacons(hapd->iface);
3986 }
3987
3988 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
3989 !sta->no_short_slot_time_set) {
3990 sta->no_short_slot_time_set = 1;
3991 hapd->iface->num_sta_no_short_slot_time++;
28d12641
AKP
3992 if (hapd->iface->current_mode &&
3993 hapd->iface->current_mode->mode ==
6fc6879b
JM
3994 HOSTAPD_MODE_IEEE80211G &&
3995 hapd->iface->num_sta_no_short_slot_time == 1)
3996 ieee802_11_set_beacons(hapd->iface);
3997 }
3998
3999 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4000 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
4001 else
4002 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
4003
4004 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
4005 !sta->no_short_preamble_set) {
4006 sta->no_short_preamble_set = 1;
4007 hapd->iface->num_sta_no_short_preamble++;
28d12641
AKP
4008 if (hapd->iface->current_mode &&
4009 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
6fc6879b
JM
4010 && hapd->iface->num_sta_no_short_preamble == 1)
4011 ieee802_11_set_beacons(hapd->iface);
4012 }
4013
d45354be 4014#ifdef CONFIG_IEEE80211N
5586f500 4015 update_ht_state(hapd, sta);
d45354be 4016#endif /* CONFIG_IEEE80211N */
de9289c8 4017
6fc6879b
JM
4018 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4019 HOSTAPD_LEVEL_DEBUG,
4020 "association OK (aid %d)", sta->aid);
4021 /* Station will be marked associated, after it acknowledges AssocResp
4022 */
b8281964 4023 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
6fc6879b 4024
6f5c8dbd
JM
4025#ifdef CONFIG_IEEE80211W
4026 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
4027 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
4028 "SA Query procedure", reassoc ? "re" : "");
4029 /* TODO: Send a protected Disassociate frame to the STA using
4030 * the old key and Reason Code "Previous Authentication no
4031 * longer valid". Make sure this is only sent protected since
4032 * unprotected frame would be received by the STA that is now
4033 * trying to associate.
4034 */
4035 }
4036#endif /* CONFIG_IEEE80211W */
4037
6fc6879b
JM
4038 /* Make sure that the previously registered inactivity timer will not
4039 * remove the STA immediately. */
4040 sta->timeout_next = STA_NULLFUNC;
4041
04059ab8
DG
4042#ifdef CONFIG_TAXONOMY
4043 taxonomy_sta_info_assoc_req(hapd, sta, pos, left);
4044#endif /* CONFIG_TAXONOMY */
4045
1f0fdaf0
JM
4046 sta->pending_wds_enable = 0;
4047
5208160b
JM
4048#ifdef CONFIG_FILS
4049 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4050 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
91d91abf
JM
4051 sta->auth_alg == WLAN_AUTH_FILS_PK) {
4052 if (fils_process_hlp(hapd, sta, pos, left) > 0)
4053 delay_assoc = 1;
4054 }
5208160b
JM
4055#endif /* CONFIG_FILS */
4056
6fc6879b 4057 fail:
53d17144
DL
4058 os_free(identity);
4059 os_free(radius_cui);
4060 hostapd_free_psk_list(psk);
4061
3f81ac07
AO
4062 /*
4063 * In case of a successful response, add the station to the driver.
4064 * Otherwise, the kernel may ignore Data frames before we process the
4065 * ACK frame (TX status). In case of a failure, this station will be
4066 * removed.
4067 *
4068 * Note that this is not compliant with the IEEE 802.11 standard that
4069 * states that a non-AP station should transition into the
4070 * authenticated/associated state only after the station acknowledges
4071 * the (Re)Association Response frame. However, still do this as:
4072 *
4073 * 1. In case the station does not acknowledge the (Re)Association
4074 * Response frame, it will be removed.
4075 * 2. Data frames will be dropped in the kernel until the station is
4076 * set into authorized state, and there are no significant known
4077 * issues with processing other non-Data Class 3 frames during this
4078 * window.
4079 */
4cf5efec
JM
4080 if (resp == WLAN_STATUS_SUCCESS && sta &&
4081 add_associated_sta(hapd, sta, reassoc))
3f81ac07
AO
4082 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4083
91d91abf 4084#ifdef CONFIG_FILS
17adac9e
JM
4085 if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS &&
4086 eloop_is_timeout_registered(fils_hlp_timeout, hapd, sta) &&
4087 sta->fils_pending_assoc_req) {
4088 /* Do not reschedule fils_hlp_timeout in case the station
4089 * retransmits (Re)Association Request frame while waiting for
4090 * the previously started FILS HLP wait, so that the timeout can
4091 * be determined from the first pending attempt. */
4092 wpa_printf(MSG_DEBUG,
4093 "FILS: Continue waiting for HLP processing before sending (Re)Association Response frame to "
4094 MACSTR, MAC2STR(sta->addr));
4095 os_free(tmp);
4096 return;
4097 }
91d91abf
JM
4098 if (sta) {
4099 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4100 os_free(sta->fils_pending_assoc_req);
4101 sta->fils_pending_assoc_req = NULL;
4102 sta->fils_pending_assoc_req_len = 0;
4103 wpabuf_free(sta->fils_hlp_resp);
4104 sta->fils_hlp_resp = NULL;
4105 }
4106 if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS) {
4107 sta->fils_pending_assoc_req = tmp;
4108 sta->fils_pending_assoc_req_len = left;
4109 sta->fils_pending_assoc_is_reassoc = reassoc;
8b5ddda5 4110 sta->fils_drv_assoc_finish = 0;
91d91abf
JM
4111 wpa_printf(MSG_DEBUG,
4112 "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
4113 MACSTR, MAC2STR(sta->addr));
4114 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4115 eloop_register_timeout(0, hapd->conf->fils_hlp_wait_time * 1024,
4116 fils_hlp_timeout, hapd, sta);
4117 return;
4118 }
4119#endif /* CONFIG_FILS */
4120
05e5e615 4121 reply_res = send_assoc_resp(hapd, sta, mgmt->sa, resp, reassoc, pos,
076f1ea1 4122 left, rssi);
78815f3d 4123 os_free(tmp);
3f81ac07
AO
4124
4125 /*
4126 * Remove the station in case tranmission of a success response fails
4127 * (the STA was added associated to the driver) or if the station was
4128 * previously added unassociated.
4129 */
05e5e615
DL
4130 if (sta && ((reply_res != WLAN_STATUS_SUCCESS &&
4131 resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc)) {
bb598c3b
AB
4132 hostapd_drv_sta_remove(hapd, sta->addr);
4133 sta->added_unassoc = 0;
4134 }
6fc6879b
JM
4135}
4136
4137
6fc6879b 4138static void handle_disassoc(struct hostapd_data *hapd,
f8b1f695 4139 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
4140{
4141 struct sta_info *sta;
4142
4143 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
61323e70
JM
4144 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
4145 (unsigned long) len);
6fc6879b
JM
4146 return;
4147 }
4148
4149 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
4150 MAC2STR(mgmt->sa),
4151 le_to_host16(mgmt->u.disassoc.reason_code));
4152
6fc6879b
JM
4153 sta = ap_get_sta(hapd, mgmt->sa);
4154 if (sta == NULL) {
61323e70
JM
4155 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
4156 MAC2STR(mgmt->sa));
6fc6879b
JM
4157 return;
4158 }
4159
ae055af4 4160 ap_sta_set_authorized(hapd, sta, 0);
38cb0a2d 4161 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
b8281964 4162 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
6fc6879b
JM
4163 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
4164 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4165 HOSTAPD_LEVEL_INFO, "disassociated");
4166 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
4167 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
4168 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
4169 * authenticated. */
4170 accounting_sta_stop(hapd, sta);
d7c3347f 4171 ieee802_1x_free_station(hapd, sta);
7d597d46 4172 if (sta->ipaddr)
ed4ddb6d 4173 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
bd00c431 4174 ap_sta_ip6addr_del(hapd, sta);
51e2a27a 4175 hostapd_drv_sta_remove(hapd, sta->addr);
bb598c3b 4176 sta->added_unassoc = 0;
6fc6879b
JM
4177
4178 if (sta->timeout_next == STA_NULLFUNC ||
4179 sta->timeout_next == STA_DISASSOC) {
4180 sta->timeout_next = STA_DEAUTH;
4181 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4182 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
4183 hapd, sta);
4184 }
4185
4186 mlme_disassociate_indication(
4187 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
05e5e615
DL
4188
4189 /* DMG/IEEE 802.11ad does not use deauthication. Deallocate sta upon
4190 * disassociation. */
4191 if (hapd->iface->current_mode &&
4192 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
4193 sta->flags &= ~WLAN_STA_AUTH;
4194 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
4195 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4196 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
4197 ap_free_sta(hapd, sta);
4198 }
6fc6879b
JM
4199}
4200
4201
4202static void handle_deauth(struct hostapd_data *hapd,
f8b1f695 4203 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
4204{
4205 struct sta_info *sta;
4206
4207 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
3ec1e902
JM
4208 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
4209 "payload (len=%lu)", (unsigned long) len);
6fc6879b
JM
4210 return;
4211 }
4212
3ec1e902 4213 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
24d75245
BG
4214 " reason_code=%d",
4215 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
6fc6879b 4216
6fc6879b
JM
4217 sta = ap_get_sta(hapd, mgmt->sa);
4218 if (sta == NULL) {
3ec1e902
JM
4219 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
4220 "to deauthenticate, but it is not authenticated",
24d75245 4221 MAC2STR(mgmt->sa));
6fc6879b
JM
4222 return;
4223 }
4224
ae055af4 4225 ap_sta_set_authorized(hapd, sta, 0);
38cb0a2d 4226 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
b8281964
JM
4227 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
4228 WLAN_STA_ASSOC_REQ_OK);
6fc6879b
JM
4229 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
4230 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4231 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
4232 mlme_deauthenticate_indication(
4233 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
4234 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
4235 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
4236 ap_free_sta(hapd, sta);
4237}
4238
4239
4240static void handle_beacon(struct hostapd_data *hapd,
b57e086c 4241 const struct ieee80211_mgmt *mgmt, size_t len,
6fc6879b
JM
4242 struct hostapd_frame_info *fi)
4243{
4244 struct ieee802_11_elems elems;
4245
4246 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
61323e70
JM
4247 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
4248 (unsigned long) len);
6fc6879b
JM
4249 return;
4250 }
4251
3d536eb4 4252 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
6fc6879b
JM
4253 len - (IEEE80211_HDRLEN +
4254 sizeof(mgmt->u.beacon)), &elems,
4255 0);
4256
6fc6879b
JM
4257 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
4258}
4259
4260
5d22a1d5 4261#ifdef CONFIG_IEEE80211W
c4e281fd
JM
4262static int robust_action_frame(u8 category)
4263{
4264 return category != WLAN_ACTION_PUBLIC &&
4265 category != WLAN_ACTION_HT;
4266}
9f64b827 4267#endif /* CONFIG_IEEE80211W */
c4e281fd
JM
4268
4269
912b34f0 4270static int handle_action(struct hostapd_data *hapd,
c5cc7a59
JM
4271 const struct ieee80211_mgmt *mgmt, size_t len,
4272 unsigned int freq)
6fc6879b 4273{
c4e281fd 4274 struct sta_info *sta;
e0785ebb 4275 u8 *action __maybe_unused;
c4e281fd 4276
e0785ebb 4277 if (len < IEEE80211_HDRLEN + 2 + 1) {
6fc6879b
JM
4278 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4279 HOSTAPD_LEVEL_DEBUG,
4280 "handle_action - too short payload (len=%lu)",
4281 (unsigned long) len);
912b34f0 4282 return 0;
6fc6879b
JM
4283 }
4284
e0785ebb
JM
4285 action = (u8 *) &mgmt->u.action.u;
4286 wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
4287 " da " MACSTR " len %d freq %u",
4288 mgmt->u.action.category, *action,
4289 MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) len, freq);
4290
4291 sta = ap_get_sta(hapd, mgmt->sa);
4292
c79938a5
JM
4293 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
4294 (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
4295 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
4296 "frame (category=%u) from unassociated STA " MACSTR,
ae26d302 4297 mgmt->u.action.category, MAC2STR(mgmt->sa));
912b34f0 4298 return 0;
c79938a5
JM
4299 }
4300
c4e281fd
JM
4301#ifdef CONFIG_IEEE80211W
4302 if (sta && (sta->flags & WLAN_STA_MFP) &&
7b2c42f8
JM
4303 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
4304 robust_action_frame(mgmt->u.action.category)) {
c4e281fd
JM
4305 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4306 HOSTAPD_LEVEL_DEBUG,
4307 "Dropped unprotected Robust Action frame from "
4308 "an MFP STA");
912b34f0 4309 return 0;
c4e281fd
JM
4310 }
4311#endif /* CONFIG_IEEE80211W */
4312
38cb0a2d
IP
4313 if (sta) {
4314 u16 fc = le_to_host16(mgmt->frame_control);
4315 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4316
4317 if ((fc & WLAN_FC_RETRY) &&
4318 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4319 sta->last_seq_ctrl == seq_ctrl &&
4320 sta->last_subtype == WLAN_FC_STYPE_ACTION) {
4321 hostapd_logger(hapd, sta->addr,
4322 HOSTAPD_MODULE_IEEE80211,
4323 HOSTAPD_LEVEL_DEBUG,
4324 "Drop repeated action frame seq_ctrl=0x%x",
4325 seq_ctrl);
4326 return 1;
4327 }
4328
4329 sta->last_seq_ctrl = seq_ctrl;
4330 sta->last_subtype = WLAN_FC_STYPE_ACTION;
4331 }
4332
6fc6879b 4333 switch (mgmt->u.action.category) {
4ec1fd8e 4334#ifdef CONFIG_IEEE80211R_AP
6fc6879b 4335 case WLAN_ACTION_FT:
d6c6b1fb
JM
4336 if (!sta ||
4337 wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
6fc6879b
JM
4338 len - IEEE80211_HDRLEN))
4339 break;
912b34f0 4340 return 1;
4ec1fd8e 4341#endif /* CONFIG_IEEE80211R_AP */
c2a71408 4342 case WLAN_ACTION_WMM:
3ae0800c 4343 hostapd_wmm_action(hapd, mgmt, len);
912b34f0 4344 return 1;
5d22a1d5 4345#ifdef CONFIG_IEEE80211W
93b76319 4346 case WLAN_ACTION_SA_QUERY:
700b3f39
JM
4347 ieee802_11_sa_query_action(hapd, mgmt, len);
4348 return 1;
5d22a1d5 4349#endif /* CONFIG_IEEE80211W */
b5bf84ba 4350#ifdef CONFIG_WNM_AP
c79938a5 4351 case WLAN_ACTION_WNM:
dbfb8e82
JM
4352 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
4353 return 1;
b5bf84ba 4354#endif /* CONFIG_WNM_AP */
037378ff
AN
4355#ifdef CONFIG_FST
4356 case WLAN_ACTION_FST:
4357 if (hapd->iface->fst)
4358 fst_rx_action(hapd->iface->fst, mgmt, len);
94edea89
JM
4359 else
4360 wpa_printf(MSG_DEBUG,
4361 "FST: Ignore FST Action frame - no FST attached");
037378ff
AN
4362 return 1;
4363#endif /* CONFIG_FST */
c706d5aa 4364 case WLAN_ACTION_PUBLIC:
5ce00d09 4365 case WLAN_ACTION_PROTECTED_DUAL:
587d60d2 4366#ifdef CONFIG_IEEE80211N
fd66aa63
JM
4367 if (len >= IEEE80211_HDRLEN + 2 &&
4368 mgmt->u.action.u.public_action.action ==
587d60d2 4369 WLAN_PA_20_40_BSS_COEX) {
587d60d2 4370 hostapd_2040_coex_action(hapd, mgmt, len);
115d5e22 4371 return 1;
587d60d2
PX
4372 }
4373#endif /* CONFIG_IEEE80211N */
9c2b8204
JM
4374#ifdef CONFIG_DPP
4375 if (len >= IEEE80211_HDRLEN + 6 &&
4376 mgmt->u.action.u.vs_public_action.action ==
4377 WLAN_PA_VENDOR_SPECIFIC &&
4378 WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
4379 OUI_WFA &&
4380 mgmt->u.action.u.vs_public_action.variable[0] ==
4381 DPP_OUI_TYPE) {
4382 const u8 *pos, *end;
4383
dc4d271c 4384 pos = mgmt->u.action.u.vs_public_action.oui;
9c2b8204
JM
4385 end = ((const u8 *) mgmt) + len;
4386 hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
c5cc7a59 4387 freq);
9c2b8204
JM
4388 return 1;
4389 }
4390 if (len >= IEEE80211_HDRLEN + 2 &&
4391 (mgmt->u.action.u.public_action.action ==
4392 WLAN_PA_GAS_INITIAL_RESP ||
4393 mgmt->u.action.u.public_action.action ==
4394 WLAN_PA_GAS_COMEBACK_RESP)) {
4395 const u8 *pos, *end;
4396
4397 pos = &mgmt->u.action.u.public_action.action;
4398 end = ((const u8 *) mgmt) + len;
4399 gas_query_ap_rx(hapd->gas, mgmt->sa,
4400 mgmt->u.action.category,
4401 pos, end - pos, hapd->iface->freq);
4402 return 1;
4403 }
4404#endif /* CONFIG_DPP */
c706d5aa
JM
4405 if (hapd->public_action_cb) {
4406 hapd->public_action_cb(hapd->public_action_cb_ctx,
4407 (u8 *) mgmt, len,
4408 hapd->iface->freq);
c706d5aa 4409 }
2d9ffe1e 4410 if (hapd->public_action_cb2) {
8dabf4bb 4411 hapd->public_action_cb2(hapd->public_action_cb2_ctx,
2d9ffe1e
JM
4412 (u8 *) mgmt, len,
4413 hapd->iface->freq);
4414 }
4415 if (hapd->public_action_cb || hapd->public_action_cb2)
912b34f0 4416 return 1;
c706d5aa 4417 break;
e44f8bf2
JM
4418 case WLAN_ACTION_VENDOR_SPECIFIC:
4419 if (hapd->vendor_action_cb) {
4420 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
4421 (u8 *) mgmt, len,
4422 hapd->iface->freq) == 0)
912b34f0 4423 return 1;
e44f8bf2
JM
4424 }
4425 break;
2572df34
DS
4426 case WLAN_ACTION_RADIO_MEASUREMENT:
4427 hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
4428 return 1;
6fc6879b
JM
4429 }
4430
4431 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4432 HOSTAPD_LEVEL_DEBUG,
4433 "handle_action - unknown action category %d or invalid "
4434 "frame",
4435 mgmt->u.action.category);
81372e34
JB
4436 if (!is_multicast_ether_addr(mgmt->da) &&
4437 !(mgmt->u.action.category & 0x80) &&
4438 !is_multicast_ether_addr(mgmt->sa)) {
b57e086c
JM
4439 struct ieee80211_mgmt *resp;
4440
6fc6879b
JM
4441 /*
4442 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
4443 * Return the Action frame to the source without change
4444 * except that MSB of the Category set to 1.
4445 */
4446 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
4447 "frame back to sender");
a1f11e34 4448 resp = os_memdup(mgmt, len);
b57e086c 4449 if (resp == NULL)
912b34f0 4450 return 0;
b57e086c
JM
4451 os_memcpy(resp->da, resp->sa, ETH_ALEN);
4452 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
4453 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
4454 resp->u.action.category |= 0x80;
4455
41fe8b42
JM
4456 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
4457 wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
4458 "Action frame");
4459 }
b57e086c 4460 os_free(resp);
6fc6879b 4461 }
912b34f0
JM
4462
4463 return 1;
6fc6879b
JM
4464}
4465
4466
4467/**
4468 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
4469 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
4470 * sent to)
4471 * @buf: management frame data (starting from IEEE 802.11 header)
4472 * @len: length of frame data in octets
a17df5fb 4473 * @fi: meta data about received frame (signal level, etc.)
6fc6879b
JM
4474 *
4475 * Process all incoming IEEE 802.11 management frames. This will be called for
4476 * each frame received from the kernel driver through wlan#ap interface. In
4477 * addition, it can be called to re-inserted pending frames (e.g., when using
4478 * external RADIUS server as an MAC ACL).
4479 */
912b34f0
JM
4480int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
4481 struct hostapd_frame_info *fi)
6fc6879b 4482{
f8b1f695 4483 struct ieee80211_mgmt *mgmt;
f8b1f695 4484 u16 fc, stype;
912b34f0 4485 int ret = 0;
c5cc7a59 4486 unsigned int freq;
659ac96d 4487 int ssi_signal = fi ? fi->ssi_signal : 0;
f8b1f695 4488
cbcf92b4 4489 if (len < 24)
912b34f0 4490 return 0;
cbcf92b4 4491
c5cc7a59
JM
4492 if (fi && fi->freq)
4493 freq = fi->freq;
4494 else
4495 freq = hapd->iface->freq;
4496
f8b1f695
JM
4497 mgmt = (struct ieee80211_mgmt *) buf;
4498 fc = le_to_host16(mgmt->frame_control);
4499 stype = WLAN_FC_GET_STYPE(fc);
6fc6879b
JM
4500
4501 if (stype == WLAN_FC_STYPE_BEACON) {
4502 handle_beacon(hapd, mgmt, len, fi);
912b34f0 4503 return 1;
6fc6879b
JM
4504 }
4505
842c5af5 4506 if (!is_broadcast_ether_addr(mgmt->bssid) &&
3eeee931
AN
4507#ifdef CONFIG_P2P
4508 /* Invitation responses can be sent with the peer MAC as BSSID */
4509 !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
4510 stype == WLAN_FC_STYPE_ACTION) &&
4511#endif /* CONFIG_P2P */
f3e9899e
BC
4512#ifdef CONFIG_MESH
4513 !(hapd->conf->mesh & MESH_ENABLED) &&
4514#endif /* CONFIG_MESH */
fe2c5241 4515 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
61323e70
JM
4516 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
4517 MAC2STR(mgmt->bssid));
912b34f0 4518 return 0;
6fc6879b
JM
4519 }
4520
4521
4522 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
659ac96d 4523 handle_probe_req(hapd, mgmt, len, ssi_signal);
912b34f0 4524 return 1;
6fc6879b
JM
4525 }
4526
efeada91
JM
4527 if ((!is_broadcast_ether_addr(mgmt->da) ||
4528 stype != WLAN_FC_STYPE_ACTION) &&
4529 os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
6fc6879b
JM
4530 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4531 HOSTAPD_LEVEL_DEBUG,
4532 "MGMT: DA=" MACSTR " not our address",
4533 MAC2STR(mgmt->da));
912b34f0 4534 return 0;
6fc6879b
JM
4535 }
4536
b308a304 4537 if (hapd->iconf->track_sta_max_num)
659ac96d 4538 sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
b308a304 4539
6fc6879b
JM
4540 switch (stype) {
4541 case WLAN_FC_STYPE_AUTH:
4542 wpa_printf(MSG_DEBUG, "mgmt::auth");
ff9f40ae 4543 handle_auth(hapd, mgmt, len, ssi_signal, 0);
912b34f0 4544 ret = 1;
6fc6879b
JM
4545 break;
4546 case WLAN_FC_STYPE_ASSOC_REQ:
4547 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
076f1ea1 4548 handle_assoc(hapd, mgmt, len, 0, ssi_signal);
912b34f0 4549 ret = 1;
6fc6879b 4550 break;
6fc6879b
JM
4551 case WLAN_FC_STYPE_REASSOC_REQ:
4552 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
076f1ea1 4553 handle_assoc(hapd, mgmt, len, 1, ssi_signal);
912b34f0 4554 ret = 1;
6fc6879b
JM
4555 break;
4556 case WLAN_FC_STYPE_DISASSOC:
4557 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
4558 handle_disassoc(hapd, mgmt, len);
912b34f0 4559 ret = 1;
6fc6879b
JM
4560 break;
4561 case WLAN_FC_STYPE_DEAUTH:
3ec1e902 4562 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
6fc6879b 4563 handle_deauth(hapd, mgmt, len);
912b34f0 4564 ret = 1;
6fc6879b
JM
4565 break;
4566 case WLAN_FC_STYPE_ACTION:
4567 wpa_printf(MSG_DEBUG, "mgmt::action");
c5cc7a59 4568 ret = handle_action(hapd, mgmt, len, freq);
6fc6879b
JM
4569 break;
4570 default:
4571 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4572 HOSTAPD_LEVEL_DEBUG,
4573 "unknown mgmt frame subtype %d", stype);
4574 break;
4575 }
912b34f0
JM
4576
4577 return ret;
6fc6879b
JM
4578}
4579
4580
4581static void handle_auth_cb(struct hostapd_data *hapd,
f8b1f695 4582 const struct ieee80211_mgmt *mgmt,
6fc6879b
JM
4583 size_t len, int ok)
4584{
4585 u16 auth_alg, auth_transaction, status_code;
4586 struct sta_info *sta;
4587
bb598c3b
AB
4588 sta = ap_get_sta(hapd, mgmt->da);
4589 if (!sta) {
dc1b1c8d
JM
4590 wpa_printf(MSG_DEBUG, "handle_auth_cb: STA " MACSTR
4591 " not found",
bb598c3b
AB
4592 MAC2STR(mgmt->da));
4593 return;
4594 }
4595
4596 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4597 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
4598 status_code = le_to_host16(mgmt->u.auth.status_code);
4599
6fc6879b
JM
4600 if (!ok) {
4601 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
4602 HOSTAPD_LEVEL_NOTICE,
4603 "did not acknowledge authentication response");
bb598c3b 4604 goto fail;
6fc6879b
JM
4605 }
4606
4607 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
61323e70
JM
4608 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
4609 (unsigned long) len);
bb598c3b 4610 goto fail;
6fc6879b
JM
4611 }
4612
4613 if (status_code == WLAN_STATUS_SUCCESS &&
4614 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
4615 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
4616 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4617 HOSTAPD_LEVEL_INFO, "authenticated");
4618 sta->flags |= WLAN_STA_AUTH;
bb598c3b
AB
4619 if (sta->added_unassoc)
4620 hostapd_set_sta_flags(hapd, sta);
4621 return;
4622 }
4623
4624fail:
4625 if (status_code != WLAN_STATUS_SUCCESS && sta->added_unassoc) {
4626 hostapd_drv_sta_remove(hapd, sta->addr);
4627 sta->added_unassoc = 0;
6fc6879b
JM
4628 }
4629}
4630
4631
69dd2967
SM
4632static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
4633 struct sta_info *sta,
4634 char *ifname_wds)
4635{
4636 int i;
f41ded6f 4637 struct hostapd_ssid *ssid = &hapd->conf->ssid;
69dd2967
SM
4638
4639 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
4640 return;
4641
4642 for (i = 0; i < 4; i++) {
4643 if (ssid->wep.key[i] &&
4644 hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
4645 i == ssid->wep.idx, NULL, 0,
4646 ssid->wep.key[i], ssid->wep.len[i])) {
4647 wpa_printf(MSG_WARNING,
4648 "Could not set WEP keys for WDS interface; %s",
4649 ifname_wds);
4650 break;
4651 }
4652 }
4653}
4654
4655
6fc6879b 4656static void handle_assoc_cb(struct hostapd_data *hapd,
f8b1f695 4657 const struct ieee80211_mgmt *mgmt,
6fc6879b
JM
4658 size_t len, int reassoc, int ok)
4659{
4660 u16 status;
4661 struct sta_info *sta;
4662 int new_assoc = 1;
4663
6fc6879b
JM
4664 sta = ap_get_sta(hapd, mgmt->da);
4665 if (!sta) {
61323e70
JM
4666 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
4667 MAC2STR(mgmt->da));
6fc6879b
JM
4668 return;
4669 }
4670
bb598c3b
AB
4671 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
4672 sizeof(mgmt->u.assoc_resp))) {
4673 wpa_printf(MSG_INFO,
4674 "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
4675 reassoc, (unsigned long) len);
3f81ac07
AO
4676 hostapd_drv_sta_remove(hapd, sta->addr);
4677 return;
bb598c3b
AB
4678 }
4679
3f81ac07
AO
4680 if (reassoc)
4681 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
4682 else
4683 status = le_to_host16(mgmt->u.assoc_resp.status_code);
4684
22b42372
FF
4685 if (!ok) {
4686 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
4687 HOSTAPD_LEVEL_DEBUG,
4688 "did not acknowledge association response");
4689 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
3f81ac07
AO
4690 /* The STA is added only in case of SUCCESS */
4691 if (status == WLAN_STATUS_SUCCESS)
4692 hostapd_drv_sta_remove(hapd, sta->addr);
22b42372 4693
3f81ac07
AO
4694 return;
4695 }
22b42372 4696
6fc6879b 4697 if (status != WLAN_STATUS_SUCCESS)
5bdac4ab 4698 return;
6fc6879b
JM
4699
4700 /* Stop previous accounting session, if one is started, and allocate
4701 * new session id for the new session. */
4702 accounting_sta_stop(hapd, sta);
6fc6879b
JM
4703
4704 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4705 HOSTAPD_LEVEL_INFO,
2fc98d02
JM
4706 "associated (aid %d)",
4707 sta->aid);
6fc6879b
JM
4708
4709 if (sta->flags & WLAN_STA_ASSOC)
4710 new_assoc = 0;
4711 sta->flags |= WLAN_STA_ASSOC;
3578e665 4712 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
07e0117d
JM
4713 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
4714 !hapd->conf->osen) ||
4715 sta->auth_alg == WLAN_AUTH_FILS_SK ||
4716 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4717 sta->auth_alg == WLAN_AUTH_FILS_PK ||
ef580012
JM
4718 sta->auth_alg == WLAN_AUTH_FT) {
4719 /*
07e0117d
JM
4720 * Open, static WEP, FT protocol, or FILS; no separate
4721 * authorization step.
ef580012 4722 */
6905dcb1 4723 ap_sta_set_authorized(hapd, sta, 1);
515cf93f 4724 }
6fc6879b
JM
4725
4726 if (reassoc)
4727 mlme_reassociate_indication(hapd, sta);
4728 else
4729 mlme_associate_indication(hapd, sta);
4730
5d22a1d5 4731#ifdef CONFIG_IEEE80211W
93b76319 4732 sta->sa_query_timed_out = 0;
5d22a1d5
JM
4733#endif /* CONFIG_IEEE80211W */
4734
6fc6879b
JM
4735 if (sta->eapol_sm == NULL) {
4736 /*
4737 * This STA does not use RADIUS server for EAP authentication,
4738 * so bind it to the selected VLAN interface now, since the
4739 * interface selection is not going to change anymore.
4740 */
c8e6beab 4741 if (ap_sta_bind_vlan(hapd, sta) < 0)
5bdac4ab 4742 return;
6fc6879b
JM
4743 } else if (sta->vlan_id) {
4744 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
c8e6beab 4745 if (ap_sta_bind_vlan(hapd, sta) < 0)
5bdac4ab 4746 return;
6fc6879b 4747 }
eddd8010 4748
0e8a96a9 4749 hostapd_set_sta_flags(hapd, sta);
6fc6879b 4750
1f0fdaf0
JM
4751 if (!(sta->flags & WLAN_STA_WDS) && sta->pending_wds_enable) {
4752 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for STA "
4753 MACSTR " based on pending request",
4754 MAC2STR(sta->addr));
4755 sta->pending_wds_enable = 0;
4756 sta->flags |= WLAN_STA_WDS;
4757 }
4758
9c06f0f6 4759 if (sta->flags & (WLAN_STA_WDS | WLAN_STA_MULTI_AP)) {
cb2b6666
JM
4760 int ret;
4761 char ifname_wds[IFNAMSIZ + 1];
4762
4763 wpa_printf(MSG_DEBUG, "Reenable 4-address WDS mode for STA "
4764 MACSTR " (aid %u)",
4765 MAC2STR(sta->addr), sta->aid);
4766 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
4767 sta->aid, 1);
4768 if (!ret)
4769 hostapd_set_wds_encryption(hapd, sta, ifname_wds);
4770 }
4771
6fc6879b
JM
4772 if (sta->auth_alg == WLAN_AUTH_FT)
4773 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
4774 else
4775 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
d24df7c3 4776 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
6fc6879b 4777 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
f2accfe7 4778
da24c5aa
JM
4779#ifdef CONFIG_FILS
4780 if ((sta->auth_alg == WLAN_AUTH_FILS_SK ||
4781 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4782 sta->auth_alg == WLAN_AUTH_FILS_PK) &&
4783 fils_set_tk(sta->wpa_sm) < 0) {
4784 wpa_printf(MSG_DEBUG, "FILS: TK configuration failed");
4785 ap_sta_disconnect(hapd, sta, sta->addr,
4786 WLAN_REASON_UNSPECIFIED);
4787 return;
4788 }
4789#endif /* CONFIG_FILS */
4790
f2accfe7
EP
4791 if (sta->pending_eapol_rx) {
4792 struct os_reltime now, age;
4793
4794 os_get_reltime(&now);
4795 os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
4796 if (age.sec == 0 && age.usec < 200000) {
4797 wpa_printf(MSG_DEBUG,
4798 "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
4799 MAC2STR(sta->addr));
4800 ieee802_1x_receive(
4801 hapd, mgmt->da,
4802 wpabuf_head(sta->pending_eapol_rx->buf),
4803 wpabuf_len(sta->pending_eapol_rx->buf));
4804 }
4805 wpabuf_free(sta->pending_eapol_rx->buf);
4806 os_free(sta->pending_eapol_rx);
4807 sta->pending_eapol_rx = NULL;
4808 }
6fc6879b
JM
4809}
4810
4811
4dc03726
JM
4812static void handle_deauth_cb(struct hostapd_data *hapd,
4813 const struct ieee80211_mgmt *mgmt,
4814 size_t len, int ok)
4815{
4816 struct sta_info *sta;
81372e34 4817 if (is_multicast_ether_addr(mgmt->da))
4dc03726
JM
4818 return;
4819 sta = ap_get_sta(hapd, mgmt->da);
4820 if (!sta) {
4821 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
4822 " not found", MAC2STR(mgmt->da));
4823 return;
4824 }
4825 if (ok)
4826 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
4827 MAC2STR(sta->addr));
4828 else
4829 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
4830 "deauth", MAC2STR(sta->addr));
4831
4832 ap_sta_deauth_cb(hapd, sta);
4833}
4834
4835
4836static void handle_disassoc_cb(struct hostapd_data *hapd,
4837 const struct ieee80211_mgmt *mgmt,
4838 size_t len, int ok)
4839{
4840 struct sta_info *sta;
81372e34 4841 if (is_multicast_ether_addr(mgmt->da))
4dc03726
JM
4842 return;
4843 sta = ap_get_sta(hapd, mgmt->da);
4844 if (!sta) {
4845 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
4846 " not found", MAC2STR(mgmt->da));
4847 return;
4848 }
4849 if (ok)
4850 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
4851 MAC2STR(sta->addr));
4852 else
4853 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
4854 "disassoc", MAC2STR(sta->addr));
4855
4856 ap_sta_disassoc_cb(hapd, sta);
4857}
4858
4859
f3383366
JM
4860static void handle_action_cb(struct hostapd_data *hapd,
4861 const struct ieee80211_mgmt *mgmt,
4862 size_t len, int ok)
4863{
4864 struct sta_info *sta;
167f78a5 4865 const struct rrm_measurement_report_element *report;
f3383366
JM
4866
4867 if (is_multicast_ether_addr(mgmt->da))
4868 return;
9c2b8204
JM
4869#ifdef CONFIG_DPP
4870 if (len >= IEEE80211_HDRLEN + 6 &&
4871 mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
4872 mgmt->u.action.u.vs_public_action.action ==
4873 WLAN_PA_VENDOR_SPECIFIC &&
4874 WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
4875 OUI_WFA &&
4876 mgmt->u.action.u.vs_public_action.variable[0] ==
4877 DPP_OUI_TYPE) {
4878 const u8 *pos, *end;
4879
4880 pos = &mgmt->u.action.u.vs_public_action.variable[1];
4881 end = ((const u8 *) mgmt) + len;
4882 hostapd_dpp_tx_status(hapd, mgmt->da, pos, end - pos, ok);
4883 return;
4884 }
4885 if (len >= IEEE80211_HDRLEN + 2 &&
4886 mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
4887 (mgmt->u.action.u.public_action.action ==
4888 WLAN_PA_GAS_INITIAL_REQ ||
4889 mgmt->u.action.u.public_action.action ==
4890 WLAN_PA_GAS_COMEBACK_REQ)) {
4891 const u8 *pos, *end;
4892
4893 pos = mgmt->u.action.u.public_action.variable;
4894 end = ((const u8 *) mgmt) + len;
4895 gas_query_ap_tx_status(hapd->gas, mgmt->da, pos, end - pos, ok);
4896 return;
4897 }
4898#endif /* CONFIG_DPP */
f3383366
JM
4899 sta = ap_get_sta(hapd, mgmt->da);
4900 if (!sta) {
4901 wpa_printf(MSG_DEBUG, "handle_action_cb: STA " MACSTR
4902 " not found", MAC2STR(mgmt->da));
4903 return;
4904 }
4905
167f78a5 4906 if (len < 24 + 5 + sizeof(*report))
f3383366 4907 return;
167f78a5
JM
4908 report = (const struct rrm_measurement_report_element *)
4909 &mgmt->u.action.u.rrm.variable[2];
f3383366 4910 if (mgmt->u.action.category == WLAN_ACTION_RADIO_MEASUREMENT &&
167f78a5
JM
4911 mgmt->u.action.u.rrm.action == WLAN_RRM_RADIO_MEASUREMENT_REQUEST &&
4912 report->eid == WLAN_EID_MEASURE_REQUEST &&
4913 report->len >= 3 &&
4914 report->type == MEASURE_TYPE_BEACON)
f3383366
JM
4915 hostapd_rrm_beacon_req_tx_status(hapd, mgmt, len, ok);
4916}
4917
4918
1c6e69cc
JM
4919/**
4920 * ieee802_11_mgmt_cb - Process management frame TX status callback
4921 * @hapd: hostapd BSS data structure (the BSS from which the management frame
4922 * was sent from)
4923 * @buf: management frame data (starting from IEEE 802.11 header)
4924 * @len: length of frame data in octets
4925 * @stype: management frame subtype from frame control field
4926 * @ok: Whether the frame was ACK'ed
4927 */
f8b1f695 4928void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
6fc6879b
JM
4929 u16 stype, int ok)
4930{
f8b1f695
JM
4931 const struct ieee80211_mgmt *mgmt;
4932 mgmt = (const struct ieee80211_mgmt *) buf;
6fc6879b 4933
93827f45
JM
4934#ifdef CONFIG_TESTING_OPTIONS
4935 if (hapd->ext_mgmt_frame_handling) {
df949062
JM
4936 size_t hex_len = 2 * len + 1;
4937 char *hex = os_malloc(hex_len);
4938
4939 if (hex) {
4940 wpa_snprintf_hex(hex, hex_len, buf, len);
4941 wpa_msg(hapd->msg_ctx, MSG_INFO,
4942 "MGMT-TX-STATUS stype=%u ok=%d buf=%s",
4943 stype, ok, hex);
4944 os_free(hex);
4945 }
93827f45
JM
4946 return;
4947 }
4948#endif /* CONFIG_TESTING_OPTIONS */
4949
6fc6879b
JM
4950 switch (stype) {
4951 case WLAN_FC_STYPE_AUTH:
4952 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
4953 handle_auth_cb(hapd, mgmt, len, ok);
4954 break;
4955 case WLAN_FC_STYPE_ASSOC_RESP:
4956 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
4957 handle_assoc_cb(hapd, mgmt, len, 0, ok);
4958 break;
4959 case WLAN_FC_STYPE_REASSOC_RESP:
4960 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
4961 handle_assoc_cb(hapd, mgmt, len, 1, ok);
4962 break;
4963 case WLAN_FC_STYPE_PROBE_RESP:
077dcfb8 4964 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
6fc6879b
JM
4965 break;
4966 case WLAN_FC_STYPE_DEAUTH:
4dc03726
JM
4967 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
4968 handle_deauth_cb(hapd, mgmt, len, ok);
4969 break;
4970 case WLAN_FC_STYPE_DISASSOC:
4971 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
4972 handle_disassoc_cb(hapd, mgmt, len, ok);
6fc6879b 4973 break;
5d22a1d5 4974 case WLAN_FC_STYPE_ACTION:
077dcfb8 4975 wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
f3383366 4976 handle_action_cb(hapd, mgmt, len, ok);
5d22a1d5 4977 break;
6fc6879b 4978 default:
61323e70 4979 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
6fc6879b
JM
4980 break;
4981 }
4982}
4983
4984
6fc6879b
JM
4985int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
4986{
4987 /* TODO */
4988 return 0;
4989}
4990
4991
4992int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
4993 char *buf, size_t buflen)
4994{
4995 /* TODO */
4996 return 0;
4997}
4998
f8b1f695
JM
4999
5000void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
5001 const u8 *buf, size_t len, int ack)
5002{
5003 struct sta_info *sta;
5004 struct hostapd_iface *iface = hapd->iface;
5005
5006 sta = ap_get_sta(hapd, addr);
5007 if (sta == NULL && iface->num_bss > 1) {
5008 size_t j;
5009 for (j = 0; j < iface->num_bss; j++) {
5010 hapd = iface->bss[j];
5011 sta = ap_get_sta(hapd, addr);
5012 if (sta)
5013 break;
5014 }
5015 }
0c01d65d 5016 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
f8b1f695
JM
5017 return;
5018 if (sta->flags & WLAN_STA_PENDING_POLL) {
5019 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
5020 "activity poll", MAC2STR(sta->addr),
5021 ack ? "ACKed" : "did not ACK");
5022 if (ack)
5023 sta->flags &= ~WLAN_STA_PENDING_POLL;
5024 }
5025
5026 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
5027}
5028
5029
dd840f79
JB
5030void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
5031 const u8 *data, size_t len, int ack)
5032{
5033 struct sta_info *sta;
5034 struct hostapd_iface *iface = hapd->iface;
5035
5036 sta = ap_get_sta(hapd, dst);
5037 if (sta == NULL && iface->num_bss > 1) {
5038 size_t j;
5039 for (j = 0; j < iface->num_bss; j++) {
5040 hapd = iface->bss[j];
5041 sta = ap_get_sta(hapd, dst);
5042 if (sta)
5043 break;
5044 }
5045 }
d9a38716
JM
5046 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
5047 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
5048 MACSTR " that is not currently associated",
5049 MAC2STR(dst));
dd840f79 5050 return;
d9a38716 5051 }
dd840f79
JB
5052
5053 ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
5054}
5055
5056
bcf24348
JB
5057void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
5058{
5059 struct sta_info *sta;
5060 struct hostapd_iface *iface = hapd->iface;
5061
5062 sta = ap_get_sta(hapd, addr);
5063 if (sta == NULL && iface->num_bss > 1) {
5064 size_t j;
5065 for (j = 0; j < iface->num_bss; j++) {
5066 hapd = iface->bss[j];
5067 sta = ap_get_sta(hapd, addr);
5068 if (sta)
5069 break;
5070 }
5071 }
5072 if (sta == NULL)
5073 return;
1854eeca
JM
5074 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
5075 MAC2STR(sta->addr));
bcf24348
JB
5076 if (!(sta->flags & WLAN_STA_PENDING_POLL))
5077 return;
5078
5079 wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
5080 "activity poll", MAC2STR(sta->addr));
5081 sta->flags &= ~WLAN_STA_PENDING_POLL;
5082}
5083
5084
fbbfcbac
FF
5085void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
5086 int wds)
f8b1f695
JM
5087{
5088 struct sta_info *sta;
5089
5090 sta = ap_get_sta(hapd, src);
1f0fdaf0
JM
5091 if (sta &&
5092 ((sta->flags & WLAN_STA_ASSOC) ||
5093 ((sta->flags & WLAN_STA_ASSOC_REQ_OK) && wds))) {
99743811
FF
5094 if (!hapd->conf->wds_sta)
5095 return;
5096
1f0fdaf0
JM
5097 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK)) ==
5098 WLAN_STA_ASSOC_REQ_OK) {
5099 wpa_printf(MSG_DEBUG,
5100 "Postpone 4-address WDS mode enabling for STA "
5101 MACSTR " since TX status for AssocResp is not yet known",
5102 MAC2STR(sta->addr));
5103 sta->pending_wds_enable = 1;
5104 return;
5105 }
5106
fbbfcbac 5107 if (wds && !(sta->flags & WLAN_STA_WDS)) {
69dd2967
SM
5108 int ret;
5109 char ifname_wds[IFNAMSIZ + 1];
5110
fbbfcbac
FF
5111 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
5112 "STA " MACSTR " (aid %u)",
5113 MAC2STR(sta->addr), sta->aid);
5114 sta->flags |= WLAN_STA_WDS;
69dd2967
SM
5115 ret = hostapd_set_wds_sta(hapd, ifname_wds,
5116 sta->addr, sta->aid, 1);
5117 if (!ret)
5118 hostapd_set_wds_encryption(hapd, sta,
5119 ifname_wds);
fbbfcbac 5120 }
f8b1f695 5121 return;
fbbfcbac 5122 }
f8b1f695
JM
5123
5124 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
5125 MACSTR, MAC2STR(src));
81372e34 5126 if (is_multicast_ether_addr(src)) {
1df492df
JM
5127 /* Broadcast bit set in SA?! Ignore the frame silently. */
5128 return;
5129 }
5130
b8281964
JM
5131 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
5132 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
5133 "already been sent, but no TX status yet known - "
5134 "ignore Class 3 frame issue with " MACSTR,
5135 MAC2STR(src));
5136 return;
5137 }
5138
f8b1f695 5139 if (sta && (sta->flags & WLAN_STA_AUTH))
51e2a27a 5140 hostapd_drv_sta_disassoc(
f8b1f695
JM
5141 hapd, src,
5142 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
5143 else
51e2a27a 5144 hostapd_drv_sta_deauth(
f8b1f695
JM
5145 hapd, src,
5146 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
5147}
5148
5149
6fc6879b 5150#endif /* CONFIG_NATIVE_WINDOWS */