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