blob: e6aa83dd3358640d1289882d43b07a21d36ed5c5 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / IEEE 802.11 Management
Dmitry Shmidt29333592017-01-09 12:27:11 -08003 * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080016#include "crypto/sha256.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070017#include "crypto/sha384.h"
18#include "crypto/sha512.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080019#include "crypto/random.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "common/ieee802_11_defs.h"
21#include "common/ieee802_11_common.h"
22#include "common/wpa_ctrl.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080023#include "common/sae.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070024#include "common/dpp.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080025#include "common/ocv.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070026#include "common/wpa_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "radius/radius.h"
28#include "radius/radius_client.h"
29#include "p2p/p2p.h"
30#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080031#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#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"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080038#include "pmksa_cache_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#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"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080046#include "wnm_ap.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080047#include "hw_features.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048#include "ieee802_11.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080049#include "dfs.h"
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080050#include "mbo_ap.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070051#include "rrm.h"
Dmitry Shmidtaca489e2016-09-28 15:44:14 -070052#include "taxonomy.h"
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080053#include "fils_hlp.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070054#include "dpp_hostapd.h"
55#include "gas_query_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056
57
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070058#ifdef CONFIG_FILS
59static struct wpabuf *
60prepare_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 */
Hai Shalom021b0b52019-04-10 11:17:58 -070067static void handle_auth(struct hostapd_data *hapd,
68 const struct ieee80211_mgmt *mgmt, size_t len,
69 int rssi, int from_queue);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070070
Hai Shalom74f70d42019-02-11 14:42:39 -080071
72u8 * 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
88{
89 u8 *pos = eid;
90 int i, num, count;
Hai Shalomfdcde762020-04-02 11:19:20 -070091 int h2e_required;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092
93 if (hapd->iface->current_rates == NULL)
94 return eid;
95
96 *pos++ = WLAN_EID_SUPP_RATES;
97 num = hapd->iface->num_rates;
98 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
99 num++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800100 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
101 num++;
Hai Shalomfdcde762020-04-02 11:19:20 -0700102 h2e_required = (hapd->conf->sae_pwe == 1 ||
103 hostapd_sae_pw_id_in_use(hapd->conf) == 2) &&
104 hapd->conf->sae_pwe != 3 &&
105 wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt);
106 if (h2e_required)
Hai Shalomc3565922019-10-28 11:58:20 -0700107 num++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108 if (num > 8) {
109 /* rest of the rates are encoded in Extended supported
110 * rates element */
111 num = 8;
112 }
113
114 *pos++ = num;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
116 i++) {
117 count++;
118 *pos = hapd->iface->current_rates[i].rate / 5;
119 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
120 *pos |= 0x80;
121 pos++;
122 }
123
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800124 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
125 count++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800127 }
128
129 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
130 count++;
131 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
132 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133
Hai Shalomfdcde762020-04-02 11:19:20 -0700134 if (h2e_required && count < 8) {
Hai Shalomc3565922019-10-28 11:58:20 -0700135 count++;
136 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY;
137 }
138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139 return pos;
140}
141
142
143u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
144{
145 u8 *pos = eid;
146 int i, num, count;
Hai Shalomfdcde762020-04-02 11:19:20 -0700147 int h2e_required;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700148
149 if (hapd->iface->current_rates == NULL)
150 return eid;
151
152 num = hapd->iface->num_rates;
153 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
154 num++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800155 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
156 num++;
Hai Shalomfdcde762020-04-02 11:19:20 -0700157 h2e_required = (hapd->conf->sae_pwe == 1 ||
158 hostapd_sae_pw_id_in_use(hapd->conf) == 2) &&
159 hapd->conf->sae_pwe != 3 &&
160 wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt);
161 if (h2e_required)
Hai Shalomc3565922019-10-28 11:58:20 -0700162 num++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 if (num <= 8)
164 return eid;
165 num -= 8;
166
167 *pos++ = WLAN_EID_EXT_SUPP_RATES;
168 *pos++ = num;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
170 i++) {
171 count++;
172 if (count <= 8)
173 continue; /* already in SuppRates IE */
174 *pos = hapd->iface->current_rates[i].rate / 5;
175 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
176 *pos |= 0x80;
177 pos++;
178 }
179
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800180 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
181 count++;
182 if (count > 8)
183 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
184 }
185
186 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
187 count++;
188 if (count > 8)
189 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191
Hai Shalomfdcde762020-04-02 11:19:20 -0700192 if (h2e_required) {
Hai Shalomc3565922019-10-28 11:58:20 -0700193 count++;
194 if (count > 8)
195 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY;
196 }
197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 return pos;
199}
200
201
Hai Shalomfdcde762020-04-02 11:19:20 -0700202u8 * hostapd_eid_rm_enabled_capab(struct hostapd_data *hapd, u8 *eid,
203 size_t len)
204{
205 size_t i;
206
207 for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
208 if (hapd->conf->radio_measurements[i])
209 break;
210 }
211
212 if (i == RRM_CAPABILITIES_IE_LEN || len < 2 + RRM_CAPABILITIES_IE_LEN)
213 return eid;
214
215 *eid++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
216 *eid++ = RRM_CAPABILITIES_IE_LEN;
217 os_memcpy(eid, hapd->conf->radio_measurements, RRM_CAPABILITIES_IE_LEN);
218
219 return eid + RRM_CAPABILITIES_IE_LEN;
220}
221
222
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700223u16 hostapd_own_capab_info(struct hostapd_data *hapd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224{
225 int capab = WLAN_CAPABILITY_ESS;
Hai Shalomfdcde762020-04-02 11:19:20 -0700226 int privacy = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800227 int dfs;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700228 int i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800229
230 /* Check if any of configured channels require DFS */
231 dfs = hostapd_is_dfs_required(hapd->iface);
232 if (dfs < 0) {
233 wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
234 dfs);
235 dfs = 0;
236 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237
238 if (hapd->iface->num_sta_no_short_preamble == 0 &&
239 hapd->iconf->preamble == SHORT_PREAMBLE)
240 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
241
Hai Shalomfdcde762020-04-02 11:19:20 -0700242#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 privacy = hapd->conf->ssid.wep.keys_set;
244
245 if (hapd->conf->ieee802_1x &&
246 (hapd->conf->default_wep_key_len ||
247 hapd->conf->individual_wep_key_len))
248 privacy = 1;
Hai Shalomfdcde762020-04-02 11:19:20 -0700249#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250
251 if (hapd->conf->wpa)
252 privacy = 1;
253
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800254#ifdef CONFIG_HS20
255 if (hapd->conf->osen)
256 privacy = 1;
257#endif /* CONFIG_HS20 */
258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259 if (privacy)
260 capab |= WLAN_CAPABILITY_PRIVACY;
261
262 if (hapd->iface->current_mode &&
263 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
264 hapd->iface->num_sta_no_short_slot_time == 0)
265 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
266
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800267 /*
268 * Currently, Spectrum Management capability bit is set when directly
269 * requested in configuration by spectrum_mgmt_required or when AP is
270 * running on DFS channel.
271 * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
272 */
273 if (hapd->iface->current_mode &&
274 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
275 (hapd->iconf->spectrum_mgmt_required || dfs))
276 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
277
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700278 for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
279 if (hapd->conf->radio_measurements[i]) {
280 capab |= IEEE80211_CAP_RRM;
281 break;
282 }
283 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 return capab;
286}
287
288
Hai Shalomfdcde762020-04-02 11:19:20 -0700289#ifdef CONFIG_WEP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800290#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
292 u16 auth_transaction, const u8 *challenge,
293 int iswep)
294{
295 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
296 HOSTAPD_LEVEL_DEBUG,
297 "authentication (shared key, transaction %d)",
298 auth_transaction);
299
300 if (auth_transaction == 1) {
301 if (!sta->challenge) {
302 /* Generate a pseudo-random challenge */
303 u8 key[8];
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
306 if (sta->challenge == NULL)
307 return WLAN_STATUS_UNSPECIFIED_FAILURE;
308
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800309 if (os_get_random(key, sizeof(key)) < 0) {
310 os_free(sta->challenge);
311 sta->challenge = NULL;
312 return WLAN_STATUS_UNSPECIFIED_FAILURE;
313 }
314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 rc4_skip(key, sizeof(key), 0,
316 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
317 }
318 return 0;
319 }
320
321 if (auth_transaction != 3)
322 return WLAN_STATUS_UNSPECIFIED_FAILURE;
323
324 /* Transaction 3 */
325 if (!iswep || !sta->challenge || !challenge ||
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700326 os_memcmp_const(sta->challenge, challenge,
327 WLAN_AUTH_CHALLENGE_LEN)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
329 HOSTAPD_LEVEL_INFO,
330 "shared key authentication - invalid "
331 "challenge-response");
332 return WLAN_STATUS_CHALLENGE_FAIL;
333 }
334
335 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
336 HOSTAPD_LEVEL_DEBUG,
337 "authentication OK (shared key)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 sta->flags |= WLAN_STA_AUTH;
339 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 os_free(sta->challenge);
341 sta->challenge = NULL;
342
343 return 0;
344}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800345#endif /* CONFIG_NO_RC4 */
Hai Shalomfdcde762020-04-02 11:19:20 -0700346#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347
348
Hai Shalomfdcde762020-04-02 11:19:20 -0700349static int send_auth_reply(struct hostapd_data *hapd, struct sta_info *sta,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800350 const u8 *dst, const u8 *bssid,
351 u16 auth_alg, u16 auth_transaction, u16 resp,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700352 const u8 *ies, size_t ies_len, const char *dbg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353{
354 struct ieee80211_mgmt *reply;
355 u8 *buf;
356 size_t rlen;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800357 int reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358
359 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
360 buf = os_zalloc(rlen);
361 if (buf == NULL)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800362 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363
364 reply = (struct ieee80211_mgmt *) buf;
365 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
366 WLAN_FC_STYPE_AUTH);
367 os_memcpy(reply->da, dst, ETH_ALEN);
368 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
369 os_memcpy(reply->bssid, bssid, ETH_ALEN);
370
371 reply->u.auth.auth_alg = host_to_le16(auth_alg);
372 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
373 reply->u.auth.status_code = host_to_le16(resp);
374
375 if (ies && ies_len)
376 os_memcpy(reply->u.auth.variable, ies, ies_len);
377
378 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
Roshan Pius3a1667e2018-07-03 15:17:14 -0700379 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu) (dbg=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 MAC2STR(dst), auth_alg, auth_transaction,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700381 resp, (unsigned long) ies_len, dbg);
Hai Shalomfdcde762020-04-02 11:19:20 -0700382#ifdef CONFIG_TESTING_OPTIONS
383#ifdef CONFIG_SAE
384 if (hapd->conf->sae_confirm_immediate == 2 &&
385 auth_alg == WLAN_AUTH_SAE) {
386 if (auth_transaction == 1 && sta &&
387 (resp == WLAN_STATUS_SUCCESS ||
388 resp == WLAN_STATUS_SAE_HASH_TO_ELEMENT)) {
389 wpa_printf(MSG_DEBUG,
390 "TESTING: Postpone SAE Commit transmission until Confirm is ready");
391 os_free(sta->sae_postponed_commit);
392 sta->sae_postponed_commit = buf;
393 sta->sae_postponed_commit_len = rlen;
394 return WLAN_STATUS_SUCCESS;
395 }
396
397 if (auth_transaction == 2 && sta && sta->sae_postponed_commit) {
398 wpa_printf(MSG_DEBUG,
399 "TESTING: Send postponed SAE Commit first, immediately followed by SAE Confirm");
400 if (hostapd_drv_send_mlme(hapd,
401 sta->sae_postponed_commit,
402 sta->sae_postponed_commit_len,
403 0, NULL, 0, 0) < 0)
404 wpa_printf(MSG_INFO, "send_auth_reply: send failed");
405 os_free(sta->sae_postponed_commit);
406 sta->sae_postponed_commit = NULL;
407 sta->sae_postponed_commit_len = 0;
408 }
409 }
410#endif /* CONFIG_SAE */
411#endif /* CONFIG_TESTING_OPTIONS */
412 if (hostapd_drv_send_mlme(hapd, reply, rlen, 0, NULL, 0, 0) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800413 wpa_printf(MSG_INFO, "send_auth_reply: send failed");
414 else
415 reply_res = WLAN_STATUS_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416
417 os_free(buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800418
419 return reply_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420}
421
422
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800423#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
425 u16 auth_transaction, u16 status,
426 const u8 *ies, size_t ies_len)
427{
428 struct hostapd_data *hapd = ctx;
429 struct sta_info *sta;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800430 int reply_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
Hai Shalomfdcde762020-04-02 11:19:20 -0700432 reply_res = send_auth_reply(hapd, NULL, dst, bssid, WLAN_AUTH_FT,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700433 auth_transaction, status, ies, ies_len,
434 "auth-ft-finish");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435
436 sta = ap_get_sta(hapd, dst);
437 if (sta == NULL)
438 return;
439
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800440 if (sta->added_unassoc && (reply_res != WLAN_STATUS_SUCCESS ||
441 status != WLAN_STATUS_SUCCESS)) {
442 hostapd_drv_sta_remove(hapd, sta->addr);
443 sta->added_unassoc = 0;
444 return;
445 }
446
447 if (status != WLAN_STATUS_SUCCESS)
448 return;
449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
451 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
452 sta->flags |= WLAN_STA_AUTH;
453 mlme_authenticate_indication(hapd, sta);
454}
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800455#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456
457
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800458#ifdef CONFIG_SAE
459
Roshan Pius3a1667e2018-07-03 15:17:14 -0700460static void sae_set_state(struct sta_info *sta, enum sae_state state,
461 const char *reason)
462{
463 wpa_printf(MSG_DEBUG, "SAE: State %s -> %s for peer " MACSTR " (%s)",
464 sae_state_txt(sta->sae->state), sae_state_txt(state),
465 MAC2STR(sta->addr), reason);
466 sta->sae->state = state;
467}
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800468
469
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800470static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
Hai Shalomc3565922019-10-28 11:58:20 -0700471 struct sta_info *sta, int update,
472 int status_code)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800473{
474 struct wpabuf *buf;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700475 const char *password = NULL;
476 struct sae_password_entry *pw;
477 const char *rx_id = NULL;
Hai Shalomc3565922019-10-28 11:58:20 -0700478 int use_pt = 0;
479 struct sae_pt *pt = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800480
Hai Shalomc3565922019-10-28 11:58:20 -0700481 if (sta->sae->tmp) {
Roshan Pius3a1667e2018-07-03 15:17:14 -0700482 rx_id = sta->sae->tmp->pw_id;
Hai Shalomc3565922019-10-28 11:58:20 -0700483 use_pt = sta->sae->tmp->h2e;
484 }
485
Hai Shalomfdcde762020-04-02 11:19:20 -0700486 if (rx_id && hapd->conf->sae_pwe != 3)
487 use_pt = 1;
488 else if (status_code == WLAN_STATUS_SUCCESS)
Hai Shalomc3565922019-10-28 11:58:20 -0700489 use_pt = 0;
490 else if (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT)
491 use_pt = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700492
493 for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
494 if (!is_broadcast_ether_addr(pw->peer_addr) &&
495 os_memcmp(pw->peer_addr, sta->addr, ETH_ALEN) != 0)
496 continue;
497 if ((rx_id && !pw->identifier) || (!rx_id && pw->identifier))
498 continue;
499 if (rx_id && pw->identifier &&
500 os_strcmp(rx_id, pw->identifier) != 0)
501 continue;
502 password = pw->password;
Hai Shalomc3565922019-10-28 11:58:20 -0700503 pt = pw->pt;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700504 break;
505 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700506 if (!password) {
Hai Shalomc3565922019-10-28 11:58:20 -0700507 password = hapd->conf->ssid.wpa_passphrase;
508 pt = hapd->conf->ssid.pt;
509 }
510 if (!password || (use_pt && !pt)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800511 wpa_printf(MSG_DEBUG, "SAE: No password available");
512 return NULL;
513 }
514
Hai Shalomc3565922019-10-28 11:58:20 -0700515 if (update && use_pt &&
516 sae_prepare_commit_pt(sta->sae, pt, hapd->own_addr, sta->addr,
517 NULL) < 0)
518 return NULL;
519
520 if (update && !use_pt &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800521 sae_prepare_commit(hapd->own_addr, sta->addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700522 (u8 *) password, os_strlen(password), rx_id,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800523 sta->sae) < 0) {
524 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
525 return NULL;
526 }
527
Hai Shalom021b0b52019-04-10 11:17:58 -0700528 if (pw && pw->vlan_id) {
529 if (!sta->sae->tmp) {
530 wpa_printf(MSG_INFO,
531 "SAE: No temporary data allocated - cannot store VLAN ID");
532 return NULL;
533 }
534 sta->sae->tmp->vlan_id = pw->vlan_id;
535 }
536
Roshan Pius3a1667e2018-07-03 15:17:14 -0700537 buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN +
538 (rx_id ? 3 + os_strlen(rx_id) : 0));
Hai Shalomfdcde762020-04-02 11:19:20 -0700539 if (buf &&
540 sae_write_commit(sta->sae, buf, sta->sae->tmp ?
541 sta->sae->tmp->anti_clogging_token : NULL,
542 rx_id) < 0) {
543 wpabuf_free(buf);
544 buf = NULL;
545 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800546
547 return buf;
548}
549
550
551static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
552 struct sta_info *sta)
553{
554 struct wpabuf *buf;
555
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800556 buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800557 if (buf == NULL)
558 return NULL;
559
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800560 sae_write_confirm(sta->sae, buf);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800561
562 return buf;
563}
564
565
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800566static int auth_sae_send_commit(struct hostapd_data *hapd,
567 struct sta_info *sta,
Hai Shalomc3565922019-10-28 11:58:20 -0700568 const u8 *bssid, int update, int status_code)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800569{
570 struct wpabuf *data;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800571 int reply_res;
Hai Shalomc3565922019-10-28 11:58:20 -0700572 u16 status;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800573
Hai Shalomc3565922019-10-28 11:58:20 -0700574 data = auth_build_sae_commit(hapd, sta, update, status_code);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700575 if (!data && sta->sae->tmp && sta->sae->tmp->pw_id)
576 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800577 if (data == NULL)
578 return WLAN_STATUS_UNSPECIFIED_FAILURE;
579
Hai Shalomc3565922019-10-28 11:58:20 -0700580 status = (sta->sae->tmp && sta->sae->tmp->h2e) ?
581 WLAN_STATUS_SAE_HASH_TO_ELEMENT : WLAN_STATUS_SUCCESS;
Hai Shalomfdcde762020-04-02 11:19:20 -0700582 reply_res = send_auth_reply(hapd, sta, sta->addr, bssid,
583 WLAN_AUTH_SAE, 1,
Hai Shalomc3565922019-10-28 11:58:20 -0700584 status, wpabuf_head(data),
Roshan Pius3a1667e2018-07-03 15:17:14 -0700585 wpabuf_len(data), "sae-send-commit");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800586
587 wpabuf_free(data);
588
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800589 return reply_res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800590}
591
592
593static int auth_sae_send_confirm(struct hostapd_data *hapd,
594 struct sta_info *sta,
595 const u8 *bssid)
596{
597 struct wpabuf *data;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800598 int reply_res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800599
600 data = auth_build_sae_confirm(hapd, sta);
601 if (data == NULL)
602 return WLAN_STATUS_UNSPECIFIED_FAILURE;
603
Hai Shalomfdcde762020-04-02 11:19:20 -0700604 reply_res = send_auth_reply(hapd, sta, sta->addr, bssid,
605 WLAN_AUTH_SAE, 2,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800606 WLAN_STATUS_SUCCESS, wpabuf_head(data),
Roshan Pius3a1667e2018-07-03 15:17:14 -0700607 wpabuf_len(data), "sae-send-confirm");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800608
609 wpabuf_free(data);
610
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800611 return reply_res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800612}
613
614
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800615static int use_sae_anti_clogging(struct hostapd_data *hapd)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800616{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800617 struct sta_info *sta;
618 unsigned int open = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800619
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800620 if (hapd->conf->sae_anti_clogging_threshold == 0)
621 return 1;
622
623 for (sta = hapd->sta_list; sta; sta = sta->next) {
624 if (!sta->sae)
625 continue;
626 if (sta->sae->state != SAE_COMMITTED &&
627 sta->sae->state != SAE_CONFIRMED)
628 continue;
629 open++;
630 if (open >= hapd->conf->sae_anti_clogging_threshold)
631 return 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800632 }
633
Hai Shalom021b0b52019-04-10 11:17:58 -0700634 /* In addition to already existing open SAE sessions, check whether
635 * there are enough pending commit messages in the processing queue to
636 * potentially result in too many open sessions. */
637 if (open + dl_list_len(&hapd->sae_commit_queue) >=
638 hapd->conf->sae_anti_clogging_threshold)
639 return 1;
640
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800641 return 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800642}
643
644
Hai Shalomfdcde762020-04-02 11:19:20 -0700645static int sae_token_hash(struct hostapd_data *hapd, const u8 *addr, u8 *idx)
Hai Shalom021b0b52019-04-10 11:17:58 -0700646{
647 u8 hash[SHA256_MAC_LEN];
648
Hai Shalomfdcde762020-04-02 11:19:20 -0700649 if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
650 addr, ETH_ALEN, hash) < 0)
651 return -1;
652 *idx = hash[0];
653 return 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700654}
655
656
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800657static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
658 const u8 *token, size_t token_len)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800659{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800660 u8 mac[SHA256_MAC_LEN];
Hai Shalom021b0b52019-04-10 11:17:58 -0700661 const u8 *addrs[2];
662 size_t len[2];
663 u16 token_idx;
664 u8 idx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800665
Hai Shalomfdcde762020-04-02 11:19:20 -0700666 if (token_len != SHA256_MAC_LEN || sae_token_hash(hapd, addr, &idx) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800667 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700668 token_idx = hapd->sae_pending_token_idx[idx];
669 if (token_idx == 0 || token_idx != WPA_GET_BE16(token)) {
670 wpa_printf(MSG_DEBUG, "SAE: Invalid anti-clogging token from "
671 MACSTR " - token_idx 0x%04x, expected 0x%04x",
672 MAC2STR(addr), WPA_GET_BE16(token), token_idx);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800673 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700674 }
675
676 addrs[0] = addr;
677 len[0] = ETH_ALEN;
678 addrs[1] = token;
679 len[1] = 2;
680 if (hmac_sha256_vector(hapd->sae_token_key, sizeof(hapd->sae_token_key),
681 2, addrs, len, mac) < 0 ||
682 os_memcmp_const(token + 2, &mac[2], SHA256_MAC_LEN - 2) != 0)
683 return -1;
684
685 hapd->sae_pending_token_idx[idx] = 0; /* invalidate used token */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800686
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800687 return 0;
688}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800689
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800690
691static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
Hai Shalomfdcde762020-04-02 11:19:20 -0700692 int group, const u8 *addr, int h2e)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800693{
694 struct wpabuf *buf;
695 u8 *token;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800696 struct os_reltime now;
Hai Shalom021b0b52019-04-10 11:17:58 -0700697 u8 idx[2];
698 const u8 *addrs[2];
699 size_t len[2];
700 u8 p_idx;
701 u16 token_idx;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800702
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800703 os_get_reltime(&now);
704 if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
Hai Shalom021b0b52019-04-10 11:17:58 -0700705 os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60) ||
706 hapd->sae_token_idx == 0xffff) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800707 if (random_get_bytes(hapd->sae_token_key,
708 sizeof(hapd->sae_token_key)) < 0)
709 return NULL;
710 wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
711 hapd->sae_token_key, sizeof(hapd->sae_token_key));
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800712 hapd->last_sae_token_key_update = now;
Hai Shalom021b0b52019-04-10 11:17:58 -0700713 hapd->sae_token_idx = 0;
714 os_memset(hapd->sae_pending_token_idx, 0,
715 sizeof(hapd->sae_pending_token_idx));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800716 }
717
Hai Shalomfdcde762020-04-02 11:19:20 -0700718 buf = wpabuf_alloc(sizeof(le16) + 3 + SHA256_MAC_LEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800719 if (buf == NULL)
720 return NULL;
721
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800722 wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
723
Hai Shalomfdcde762020-04-02 11:19:20 -0700724 if (h2e) {
725 /* Encapsulate Anti-clogging Token field in a container IE */
726 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
727 wpabuf_put_u8(buf, 1 + SHA256_MAC_LEN);
728 wpabuf_put_u8(buf, WLAN_EID_EXT_ANTI_CLOGGING_TOKEN);
729 }
730
731 if (sae_token_hash(hapd, addr, &p_idx) < 0) {
732 wpabuf_free(buf);
733 return NULL;
734 }
Hai Shalom021b0b52019-04-10 11:17:58 -0700735 token_idx = hapd->sae_pending_token_idx[p_idx];
736 if (!token_idx) {
737 hapd->sae_token_idx++;
738 token_idx = hapd->sae_token_idx;
739 hapd->sae_pending_token_idx[p_idx] = token_idx;
740 }
741 WPA_PUT_BE16(idx, token_idx);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800742 token = wpabuf_put(buf, SHA256_MAC_LEN);
Hai Shalom021b0b52019-04-10 11:17:58 -0700743 addrs[0] = addr;
744 len[0] = ETH_ALEN;
745 addrs[1] = idx;
746 len[1] = sizeof(idx);
747 if (hmac_sha256_vector(hapd->sae_token_key, sizeof(hapd->sae_token_key),
748 2, addrs, len, token) < 0) {
749 wpabuf_free(buf);
750 return NULL;
751 }
752 WPA_PUT_BE16(token, token_idx);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800753
754 return buf;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800755}
756
757
Roshan Pius3a1667e2018-07-03 15:17:14 -0700758static int sae_check_big_sync(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800759{
Roshan Pius3a1667e2018-07-03 15:17:14 -0700760 if (sta->sae->sync > hapd->conf->sae_sync) {
761 sae_set_state(sta, SAE_NOTHING, "Sync > dot11RSNASAESync");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800762 sta->sae->sync = 0;
763 return -1;
764 }
765 return 0;
766}
767
768
769static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
770{
771 struct hostapd_data *hapd = eloop_ctx;
772 struct sta_info *sta = eloop_data;
773 int ret;
774
Roshan Pius3a1667e2018-07-03 15:17:14 -0700775 if (sae_check_big_sync(hapd, sta))
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800776 return;
777 sta->sae->sync++;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700778 wpa_printf(MSG_DEBUG, "SAE: Auth SAE retransmit timer for " MACSTR
Roshan Pius3a1667e2018-07-03 15:17:14 -0700779 " (sync=%d state=%s)",
780 MAC2STR(sta->addr), sta->sae->sync,
781 sae_state_txt(sta->sae->state));
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800782
783 switch (sta->sae->state) {
784 case SAE_COMMITTED:
Hai Shalomc3565922019-10-28 11:58:20 -0700785 ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0, -1);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800786 eloop_register_timeout(0,
787 hapd->dot11RSNASAERetransPeriod * 1000,
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800788 auth_sae_retransmit_timer, hapd, sta);
789 break;
790 case SAE_CONFIRMED:
791 ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800792 eloop_register_timeout(0,
793 hapd->dot11RSNASAERetransPeriod * 1000,
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800794 auth_sae_retransmit_timer, hapd, sta);
795 break;
796 default:
797 ret = -1;
798 break;
799 }
800
801 if (ret != WLAN_STATUS_SUCCESS)
802 wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
803}
804
805
806void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
807{
808 eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
809}
810
811
812static void sae_set_retransmit_timer(struct hostapd_data *hapd,
813 struct sta_info *sta)
814{
815 if (!(hapd->conf->mesh & MESH_ENABLED))
816 return;
817
818 eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800819 eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800820 auth_sae_retransmit_timer, hapd, sta);
821}
822
823
Hai Shalom5f92bc92019-04-18 11:54:11 -0700824static void sae_sme_send_external_auth_status(struct hostapd_data *hapd,
825 struct sta_info *sta, u16 status)
826{
827 struct external_auth params;
828
829 os_memset(&params, 0, sizeof(params));
830 params.status = status;
831 params.bssid = sta->addr;
Hai Shalom81f62d82019-07-22 12:10:00 -0700832 if (status == WLAN_STATUS_SUCCESS && sta->sae &&
833 !hapd->conf->disable_pmksa_caching)
Hai Shalom5f92bc92019-04-18 11:54:11 -0700834 params.pmkid = sta->sae->pmkid;
835
836 hostapd_drv_send_external_auth_status(hapd, &params);
837}
838
839
Dmitry Shmidte4663042016-04-04 10:07:49 -0700840void sae_accept_sta(struct hostapd_data *hapd, struct sta_info *sta)
841{
Hai Shalom021b0b52019-04-10 11:17:58 -0700842#ifndef CONFIG_NO_VLAN
843 struct vlan_description vlan_desc;
844
845 if (sta->sae->tmp && sta->sae->tmp->vlan_id > 0) {
846 wpa_printf(MSG_DEBUG, "SAE: Assign STA " MACSTR
847 " to VLAN ID %d",
848 MAC2STR(sta->addr), sta->sae->tmp->vlan_id);
849
850 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
851 vlan_desc.notempty = 1;
852 vlan_desc.untagged = sta->sae->tmp->vlan_id;
853 if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
854 wpa_printf(MSG_INFO,
855 "Invalid VLAN ID %d in sae_password",
856 sta->sae->tmp->vlan_id);
857 return;
858 }
859
860 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0 ||
861 ap_sta_bind_vlan(hapd, sta) < 0) {
862 wpa_printf(MSG_INFO,
863 "Failed to assign VLAN ID %d from sae_password to "
864 MACSTR, sta->sae->tmp->vlan_id,
865 MAC2STR(sta->addr));
866 return;
867 }
868 }
869#endif /* CONFIG_NO_VLAN */
870
Dmitry Shmidte4663042016-04-04 10:07:49 -0700871 sta->flags |= WLAN_STA_AUTH;
872 sta->auth_alg = WLAN_AUTH_SAE;
873 mlme_authenticate_indication(hapd, sta);
874 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700875 sae_set_state(sta, SAE_ACCEPTED, "Accept Confirm");
Hai Shalomfdcde762020-04-02 11:19:20 -0700876 crypto_bignum_deinit(sta->sae->peer_commit_scalar_accepted, 0);
877 sta->sae->peer_commit_scalar_accepted = sta->sae->peer_commit_scalar;
878 sta->sae->peer_commit_scalar = NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700879 wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
880 sta->sae->pmk, sta->sae->pmkid);
Hai Shalom5f92bc92019-04-18 11:54:11 -0700881 sae_sme_send_external_auth_status(hapd, sta, WLAN_STATUS_SUCCESS);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700882}
883
884
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800885static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
Hai Shalomc3565922019-10-28 11:58:20 -0700886 const u8 *bssid, u16 auth_transaction, u16 status_code,
887 int allow_reuse, int *sta_removed)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800888{
889 int ret;
890
Hai Shalom5f92bc92019-04-18 11:54:11 -0700891 *sta_removed = 0;
892
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800893 if (auth_transaction != 1 && auth_transaction != 2)
894 return WLAN_STATUS_UNSPECIFIED_FAILURE;
895
Roshan Pius3a1667e2018-07-03 15:17:14 -0700896 wpa_printf(MSG_DEBUG, "SAE: Peer " MACSTR " state=%s auth_trans=%u",
897 MAC2STR(sta->addr), sae_state_txt(sta->sae->state),
898 auth_transaction);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800899 switch (sta->sae->state) {
900 case SAE_NOTHING:
901 if (auth_transaction == 1) {
Hai Shalomc3565922019-10-28 11:58:20 -0700902 if (sta->sae->tmp)
903 sta->sae->tmp->h2e = status_code ==
904 WLAN_STATUS_SAE_HASH_TO_ELEMENT;
Hai Shalom021b0b52019-04-10 11:17:58 -0700905 ret = auth_sae_send_commit(hapd, sta, bssid,
Hai Shalomc3565922019-10-28 11:58:20 -0700906 !allow_reuse, status_code);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800907 if (ret)
908 return ret;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700909 sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800910
911 if (sae_process_commit(sta->sae) < 0)
912 return WLAN_STATUS_UNSPECIFIED_FAILURE;
913
914 /*
Hai Shalomc3565922019-10-28 11:58:20 -0700915 * In mesh case, both Commit and Confirm are sent
916 * immediately. In infrastructure BSS, by default, only
917 * a single Authentication frame (Commit) is expected
918 * from the AP here and the second one (Confirm) will
919 * be sent once the STA has sent its second
920 * Authentication frame (Confirm). This behavior can be
921 * overridden with explicit configuration so that the
922 * infrastructure BSS case sends both frames together.
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800923 */
Hai Shalomc3565922019-10-28 11:58:20 -0700924 if ((hapd->conf->mesh & MESH_ENABLED) ||
925 hapd->conf->sae_confirm_immediate) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800926 /*
927 * Send both Commit and Confirm immediately
928 * based on SAE finite state machine
929 * Nothing -> Confirm transition.
930 */
931 ret = auth_sae_send_confirm(hapd, sta, bssid);
932 if (ret)
933 return ret;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700934 sae_set_state(sta, SAE_CONFIRMED,
935 "Sent Confirm (mesh)");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800936 } else {
937 /*
938 * For infrastructure BSS, send only the Commit
939 * message now to get alternating sequence of
940 * Authentication frames between the AP and STA.
941 * Confirm will be sent in
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800942 * Committed -> Confirmed/Accepted transition
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800943 * when receiving Confirm from STA.
944 */
945 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800946 sta->sae->sync = 0;
947 sae_set_retransmit_timer(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800948 } else {
949 hostapd_logger(hapd, sta->addr,
950 HOSTAPD_MODULE_IEEE80211,
951 HOSTAPD_LEVEL_DEBUG,
952 "SAE confirm before commit");
953 }
954 break;
955 case SAE_COMMITTED:
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800956 sae_clear_retransmit_timer(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800957 if (auth_transaction == 1) {
958 if (sae_process_commit(sta->sae) < 0)
959 return WLAN_STATUS_UNSPECIFIED_FAILURE;
960
961 ret = auth_sae_send_confirm(hapd, sta, bssid);
962 if (ret)
963 return ret;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700964 sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800965 sta->sae->sync = 0;
966 sae_set_retransmit_timer(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800967 } else if (hapd->conf->mesh & MESH_ENABLED) {
968 /*
969 * In mesh case, follow SAE finite state machine and
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800970 * send Commit now, if sync count allows.
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800971 */
Roshan Pius3a1667e2018-07-03 15:17:14 -0700972 if (sae_check_big_sync(hapd, sta))
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800973 return WLAN_STATUS_SUCCESS;
974 sta->sae->sync++;
975
Hai Shalomc3565922019-10-28 11:58:20 -0700976 ret = auth_sae_send_commit(hapd, sta, bssid, 0,
977 status_code);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800978 if (ret)
979 return ret;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800980
981 sae_set_retransmit_timer(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800982 } else {
983 /*
984 * For instructure BSS, send the postponed Confirm from
985 * Nothing -> Confirmed transition that was reduced to
986 * Nothing -> Committed above.
987 */
988 ret = auth_sae_send_confirm(hapd, sta, bssid);
989 if (ret)
990 return ret;
991
Roshan Pius3a1667e2018-07-03 15:17:14 -0700992 sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800993
994 /*
995 * Since this was triggered on Confirm RX, run another
996 * step to get to Accepted without waiting for
997 * additional events.
998 */
Hai Shalom021b0b52019-04-10 11:17:58 -0700999 return sae_sm_step(hapd, sta, bssid, auth_transaction,
Hai Shalomc3565922019-10-28 11:58:20 -07001000 WLAN_STATUS_SUCCESS, 0, sta_removed);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001001 }
1002 break;
1003 case SAE_CONFIRMED:
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001004 sae_clear_retransmit_timer(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001005 if (auth_transaction == 1) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001006 if (sae_check_big_sync(hapd, sta))
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001007 return WLAN_STATUS_SUCCESS;
1008 sta->sae->sync++;
1009
Hai Shalomc3565922019-10-28 11:58:20 -07001010 ret = auth_sae_send_commit(hapd, sta, bssid, 1,
1011 status_code);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001012 if (ret)
1013 return ret;
1014
1015 if (sae_process_commit(sta->sae) < 0)
1016 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1017
1018 ret = auth_sae_send_confirm(hapd, sta, bssid);
1019 if (ret)
1020 return ret;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001021
1022 sae_set_retransmit_timer(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001023 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001024 sta->sae->send_confirm = 0xffff;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001025 sae_accept_sta(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001026 }
1027 break;
1028 case SAE_ACCEPTED:
Roshan Pius3a1667e2018-07-03 15:17:14 -07001029 if (auth_transaction == 1 &&
1030 (hapd->conf->mesh & MESH_ENABLED)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001031 wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
1032 ") doing reauthentication",
1033 MAC2STR(sta->addr));
Dmitry Shmidte4663042016-04-04 10:07:49 -07001034 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
Hai Shalom5f92bc92019-04-18 11:54:11 -07001035 ap_free_sta(hapd, sta);
1036 *sta_removed = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001037 } else if (auth_transaction == 1) {
1038 wpa_printf(MSG_DEBUG, "SAE: Start reauthentication");
Hai Shalomc3565922019-10-28 11:58:20 -07001039 ret = auth_sae_send_commit(hapd, sta, bssid, 1,
1040 status_code);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001041 if (ret)
1042 return ret;
1043 sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
1044
1045 if (sae_process_commit(sta->sae) < 0)
1046 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1047 sta->sae->sync = 0;
1048 sae_set_retransmit_timer(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001049 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001050 if (sae_check_big_sync(hapd, sta))
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001051 return WLAN_STATUS_SUCCESS;
1052 sta->sae->sync++;
1053
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001054 ret = auth_sae_send_confirm(hapd, sta, bssid);
1055 sae_clear_temp_data(sta->sae);
1056 if (ret)
1057 return ret;
1058 }
1059 break;
1060 default:
1061 wpa_printf(MSG_ERROR, "SAE: invalid state %d",
1062 sta->sae->state);
1063 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1064 }
1065 return WLAN_STATUS_SUCCESS;
1066}
1067
1068
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001069static void sae_pick_next_group(struct hostapd_data *hapd, struct sta_info *sta)
1070{
1071 struct sae_data *sae = sta->sae;
1072 int i, *groups = hapd->conf->sae_groups;
Hai Shalom021b0b52019-04-10 11:17:58 -07001073 int default_groups[] = { 19, 0 };
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001074
1075 if (sae->state != SAE_COMMITTED)
1076 return;
1077
1078 wpa_printf(MSG_DEBUG, "SAE: Previously selected group: %d", sae->group);
1079
Hai Shalom021b0b52019-04-10 11:17:58 -07001080 if (!groups)
1081 groups = default_groups;
1082 for (i = 0; groups[i] > 0; i++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001083 if (sae->group == groups[i])
1084 break;
1085 }
1086
Hai Shalom021b0b52019-04-10 11:17:58 -07001087 if (groups[i] <= 0) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001088 wpa_printf(MSG_DEBUG,
1089 "SAE: Previously selected group not found from the current configuration");
1090 return;
1091 }
1092
1093 for (;;) {
1094 i++;
1095 if (groups[i] <= 0) {
1096 wpa_printf(MSG_DEBUG,
1097 "SAE: No alternative group enabled");
1098 return;
1099 }
1100
1101 if (sae_set_group(sae, groups[i]) < 0)
1102 continue;
1103
1104 break;
1105 }
1106 wpa_printf(MSG_DEBUG, "SAE: Selected new group: %d", groups[i]);
1107}
1108
1109
Hai Shalomc3565922019-10-28 11:58:20 -07001110static int sae_status_success(struct hostapd_data *hapd, u16 status_code)
1111{
Hai Shalomfdcde762020-04-02 11:19:20 -07001112 int sae_pwe = hapd->conf->sae_pwe;
1113 int id_in_use;
1114
1115 id_in_use = hostapd_sae_pw_id_in_use(hapd->conf);
1116 if (id_in_use == 2 && sae_pwe != 3)
1117 sae_pwe = 1;
1118 else if (id_in_use == 1 && sae_pwe == 0)
1119 sae_pwe = 2;
1120
1121 return ((sae_pwe == 0 || sae_pwe == 3) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001122 status_code == WLAN_STATUS_SUCCESS) ||
Hai Shalomfdcde762020-04-02 11:19:20 -07001123 (sae_pwe == 1 &&
Hai Shalomc3565922019-10-28 11:58:20 -07001124 status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT) ||
Hai Shalomfdcde762020-04-02 11:19:20 -07001125 (sae_pwe == 2 &&
Hai Shalomc3565922019-10-28 11:58:20 -07001126 (status_code == WLAN_STATUS_SUCCESS ||
1127 status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT));
1128}
1129
1130
1131static int sae_is_group_enabled(struct hostapd_data *hapd, int group)
1132{
1133 int *groups = hapd->conf->sae_groups;
1134 int default_groups[] = { 19, 0 };
1135 int i;
1136
1137 if (!groups)
1138 groups = default_groups;
1139
1140 for (i = 0; groups[i] > 0; i++) {
1141 if (groups[i] == group)
1142 return 1;
1143 }
1144
1145 return 0;
1146}
1147
1148
1149static int check_sae_rejected_groups(struct hostapd_data *hapd,
1150 const struct wpabuf *groups)
1151{
1152 size_t i, count;
1153 const u8 *pos;
1154
1155 if (!groups)
1156 return 0;
1157
1158 pos = wpabuf_head(groups);
1159 count = wpabuf_len(groups) / 2;
1160 for (i = 0; i < count; i++) {
1161 int enabled;
1162 u16 group;
1163
1164 group = WPA_GET_LE16(pos);
1165 pos += 2;
1166 enabled = sae_is_group_enabled(hapd, group);
1167 wpa_printf(MSG_DEBUG, "SAE: Rejected group %u is %s",
1168 group, enabled ? "enabled" : "disabled");
1169 if (enabled)
1170 return 1;
1171 }
1172
1173 return 0;
1174}
1175
1176
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001177static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
1178 const struct ieee80211_mgmt *mgmt, size_t len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001179 u16 auth_transaction, u16 status_code)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001180{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001181 int resp = WLAN_STATUS_SUCCESS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001182 struct wpabuf *data = NULL;
Hai Shalom021b0b52019-04-10 11:17:58 -07001183 int *groups = hapd->conf->sae_groups;
1184 int default_groups[] = { 19, 0 };
1185 const u8 *pos, *end;
Hai Shalom5f92bc92019-04-18 11:54:11 -07001186 int sta_removed = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001187
1188 if (!groups)
1189 groups = default_groups;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001190
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001191#ifdef CONFIG_TESTING_OPTIONS
1192 if (hapd->conf->sae_reflection_attack && auth_transaction == 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001193 wpa_printf(MSG_DEBUG, "SAE: TESTING - reflection attack");
1194 pos = mgmt->u.auth.variable;
1195 end = ((const u8 *) mgmt) + len;
Hai Shalomfdcde762020-04-02 11:19:20 -07001196 send_auth_reply(hapd, sta, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001197 auth_transaction, resp, pos, end - pos,
1198 "auth-sae-reflection-attack");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001199 goto remove_sta;
1200 }
1201
1202 if (hapd->conf->sae_commit_override && auth_transaction == 1) {
1203 wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
Hai Shalomfdcde762020-04-02 11:19:20 -07001204 send_auth_reply(hapd, sta, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001205 auth_transaction, resp,
1206 wpabuf_head(hapd->conf->sae_commit_override),
Roshan Pius3a1667e2018-07-03 15:17:14 -07001207 wpabuf_len(hapd->conf->sae_commit_override),
1208 "sae-commit-override");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001209 goto remove_sta;
1210 }
1211#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001212 if (!sta->sae) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001213 if (auth_transaction != 1 ||
Hai Shalomc3565922019-10-28 11:58:20 -07001214 !sae_status_success(hapd, status_code)) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001215 wpa_printf(MSG_DEBUG, "SAE: Unexpected Status Code %u",
1216 status_code);
1217 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1218 goto reply;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001219 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001220 sta->sae = os_zalloc(sizeof(*sta->sae));
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001221 if (!sta->sae) {
1222 resp = -1;
1223 goto remove_sta;
1224 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001225 sae_set_state(sta, SAE_NOTHING, "Init");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001226 sta->sae->sync = 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001227 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001228
Dmitry Shmidte4663042016-04-04 10:07:49 -07001229 if (sta->mesh_sae_pmksa_caching) {
1230 wpa_printf(MSG_DEBUG,
1231 "SAE: Cancel use of mesh PMKSA caching because peer starts SAE authentication");
1232 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1233 sta->mesh_sae_pmksa_caching = 0;
1234 }
1235
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001236 if (auth_transaction == 1) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001237 const u8 *token = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001238 size_t token_len = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001239 int allow_reuse = 0;
1240
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001241 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1242 HOSTAPD_LEVEL_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07001243 "start SAE authentication (RX commit, status=%u (%s))",
1244 status_code, status2str(status_code));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001245
1246 if ((hapd->conf->mesh & MESH_ENABLED) &&
1247 status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
1248 sta->sae->tmp) {
1249 pos = mgmt->u.auth.variable;
1250 end = ((const u8 *) mgmt) + len;
1251 if (pos + sizeof(le16) > end) {
1252 wpa_printf(MSG_ERROR,
1253 "SAE: Too short anti-clogging token request");
1254 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1255 goto reply;
1256 }
Hai Shalom021b0b52019-04-10 11:17:58 -07001257 resp = sae_group_allowed(sta->sae, groups,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001258 WPA_GET_LE16(pos));
1259 if (resp != WLAN_STATUS_SUCCESS) {
1260 wpa_printf(MSG_ERROR,
1261 "SAE: Invalid group in anti-clogging token request");
1262 goto reply;
1263 }
1264 pos += sizeof(le16);
1265
1266 wpabuf_free(sta->sae->tmp->anti_clogging_token);
1267 sta->sae->tmp->anti_clogging_token =
1268 wpabuf_alloc_copy(pos, end - pos);
1269 if (sta->sae->tmp->anti_clogging_token == NULL) {
1270 wpa_printf(MSG_ERROR,
1271 "SAE: Failed to alloc for anti-clogging token");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001272 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1273 goto remove_sta;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001274 }
1275
1276 /*
1277 * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
1278 * is 76, a new Commit Message shall be constructed
1279 * with the Anti-Clogging Token from the received
1280 * Authentication frame, and the commit-scalar and
1281 * COMMIT-ELEMENT previously sent.
1282 */
Hai Shalomc3565922019-10-28 11:58:20 -07001283 resp = auth_sae_send_commit(hapd, sta, mgmt->bssid, 0,
1284 status_code);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001285 if (resp != WLAN_STATUS_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001286 wpa_printf(MSG_ERROR,
1287 "SAE: Failed to send commit message");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001288 goto remove_sta;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001289 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001290 sae_set_state(sta, SAE_COMMITTED,
1291 "Sent Commit (anti-clogging token case in mesh)");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001292 sta->sae->sync = 0;
1293 sae_set_retransmit_timer(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001294 return;
1295 }
1296
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001297 if ((hapd->conf->mesh & MESH_ENABLED) &&
1298 status_code ==
1299 WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1300 sta->sae->tmp) {
1301 wpa_printf(MSG_DEBUG,
1302 "SAE: Peer did not accept our SAE group");
1303 sae_pick_next_group(hapd, sta);
1304 goto remove_sta;
1305 }
1306
Hai Shalomc3565922019-10-28 11:58:20 -07001307 if (!sae_status_success(hapd, status_code))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001308 goto remove_sta;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001309
Roshan Pius3a1667e2018-07-03 15:17:14 -07001310 if (!(hapd->conf->mesh & MESH_ENABLED) &&
1311 sta->sae->state == SAE_COMMITTED) {
1312 /* This is needed in the infrastructure BSS case to
1313 * address a sequence where a STA entry may remain in
1314 * hostapd across two attempts to do SAE authentication
1315 * by the same STA. The second attempt may end up trying
1316 * to use a different group and that would not be
1317 * allowed if we remain in Committed state with the
1318 * previously set parameters. */
Hai Shalom021b0b52019-04-10 11:17:58 -07001319 pos = mgmt->u.auth.variable;
1320 end = ((const u8 *) mgmt) + len;
1321 if (end - pos >= (int) sizeof(le16) &&
1322 sae_group_allowed(sta->sae, groups,
1323 WPA_GET_LE16(pos)) ==
1324 WLAN_STATUS_SUCCESS) {
1325 /* Do not waste resources deriving the same PWE
1326 * again since the same group is reused. */
1327 sae_set_state(sta, SAE_NOTHING,
1328 "Allow previous PWE to be reused");
1329 allow_reuse = 1;
1330 } else {
1331 sae_set_state(sta, SAE_NOTHING,
1332 "Clear existing state to allow restart");
1333 sae_clear_data(sta->sae);
1334 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001335 }
1336
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001337 resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
1338 ((const u8 *) mgmt) + len -
1339 mgmt->u.auth.variable, &token,
Hai Shalomc3565922019-10-28 11:58:20 -07001340 &token_len, groups, status_code ==
1341 WLAN_STATUS_SAE_HASH_TO_ELEMENT);
Dmitry Shmidt41712582015-06-29 11:02:15 -07001342 if (resp == SAE_SILENTLY_DISCARD) {
1343 wpa_printf(MSG_DEBUG,
1344 "SAE: Drop commit message from " MACSTR " due to reflection attack",
1345 MAC2STR(sta->addr));
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001346 goto remove_sta;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001347 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001348
1349 if (resp == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER) {
1350 wpa_msg(hapd->msg_ctx, MSG_INFO,
1351 WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER
1352 MACSTR, MAC2STR(sta->addr));
1353 sae_clear_retransmit_timer(hapd, sta);
1354 sae_set_state(sta, SAE_NOTHING,
1355 "Unknown Password Identifier");
1356 goto remove_sta;
1357 }
1358
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001359 if (token && check_sae_token(hapd, sta->addr, token, token_len)
1360 < 0) {
1361 wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
1362 "incorrect token from " MACSTR,
1363 MAC2STR(sta->addr));
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001364 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1365 goto remove_sta;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001366 }
1367
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001368 if (resp != WLAN_STATUS_SUCCESS)
1369 goto reply;
1370
Hai Shalomc3565922019-10-28 11:58:20 -07001371 if (sta->sae->tmp &&
1372 check_sae_rejected_groups(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001373 hapd, sta->sae->tmp->peer_rejected_groups)) {
Hai Shalomc3565922019-10-28 11:58:20 -07001374 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001375 goto reply;
Hai Shalomc3565922019-10-28 11:58:20 -07001376 }
1377
Hai Shalom021b0b52019-04-10 11:17:58 -07001378 if (!token && use_sae_anti_clogging(hapd) && !allow_reuse) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001379 int h2e = 0;
1380
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001381 wpa_printf(MSG_DEBUG,
1382 "SAE: Request anti-clogging token from "
1383 MACSTR, MAC2STR(sta->addr));
Hai Shalomfdcde762020-04-02 11:19:20 -07001384 if (sta->sae->tmp)
1385 h2e = sta->sae->tmp->h2e;
1386 if (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT)
1387 h2e = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001388 data = auth_build_token_req(hapd, sta->sae->group,
Hai Shalomfdcde762020-04-02 11:19:20 -07001389 sta->addr, h2e);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001390 resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
1391 if (hapd->conf->mesh & MESH_ENABLED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07001392 sae_set_state(sta, SAE_NOTHING,
1393 "Request anti-clogging token case in mesh");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001394 goto reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001395 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001396
Hai Shalom021b0b52019-04-10 11:17:58 -07001397 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction,
Hai Shalomc3565922019-10-28 11:58:20 -07001398 status_code, allow_reuse, &sta_removed);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001399 } else if (auth_transaction == 2) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001400 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1401 HOSTAPD_LEVEL_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07001402 "SAE authentication (RX confirm, status=%u (%s))",
1403 status_code, status2str(status_code));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001404 if (status_code != WLAN_STATUS_SUCCESS)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001405 goto remove_sta;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001406 if (sta->sae->state >= SAE_CONFIRMED ||
1407 !(hapd->conf->mesh & MESH_ENABLED)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001408 const u8 *var;
1409 size_t var_len;
1410 u16 peer_send_confirm;
1411
1412 var = mgmt->u.auth.variable;
1413 var_len = ((u8 *) mgmt) + len - mgmt->u.auth.variable;
1414 if (var_len < 2) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001415 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001416 goto reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001417 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001418
1419 peer_send_confirm = WPA_GET_LE16(var);
1420
1421 if (sta->sae->state == SAE_ACCEPTED &&
1422 (peer_send_confirm <= sta->sae->rc ||
1423 peer_send_confirm == 0xffff)) {
1424 wpa_printf(MSG_DEBUG,
1425 "SAE: Silently ignore unexpected Confirm from peer "
1426 MACSTR
1427 " (peer-send-confirm=%u Rc=%u)",
1428 MAC2STR(sta->addr),
1429 peer_send_confirm, sta->sae->rc);
1430 return;
1431 }
1432
1433 if (sae_check_confirm(sta->sae, var, var_len) < 0) {
1434 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1435 goto reply;
1436 }
1437 sta->sae->rc = peer_send_confirm;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001438 }
Hai Shalomc3565922019-10-28 11:58:20 -07001439 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction,
1440 status_code, 0, &sta_removed);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001441 } else {
1442 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1443 HOSTAPD_LEVEL_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07001444 "unexpected SAE authentication transaction %u (status=%u (%s))",
1445 auth_transaction, status_code,
1446 status2str(status_code));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001447 if (status_code != WLAN_STATUS_SUCCESS)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001448 goto remove_sta;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001449 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1450 }
1451
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001452reply:
Hai Shalom5f92bc92019-04-18 11:54:11 -07001453 if (!sta_removed && resp != WLAN_STATUS_SUCCESS) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001454 pos = mgmt->u.auth.variable;
1455 end = ((const u8 *) mgmt) + len;
1456
1457 /* Copy the Finite Cyclic Group field from the request if we
1458 * rejected it as unsupported group. */
1459 if (resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
1460 !data && end - pos >= 2)
1461 data = wpabuf_alloc_copy(pos, 2);
1462
Hai Shalom5f92bc92019-04-18 11:54:11 -07001463 sae_sme_send_external_auth_status(hapd, sta, resp);
Hai Shalomfdcde762020-04-02 11:19:20 -07001464 send_auth_reply(hapd, sta, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001465 auth_transaction, resp,
1466 data ? wpabuf_head(data) : (u8 *) "",
Roshan Pius3a1667e2018-07-03 15:17:14 -07001467 data ? wpabuf_len(data) : 0, "auth-sae");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001468 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001469
1470remove_sta:
Hai Shalom5f92bc92019-04-18 11:54:11 -07001471 if (!sta_removed && sta->added_unassoc &&
1472 (resp != WLAN_STATUS_SUCCESS ||
1473 status_code != WLAN_STATUS_SUCCESS)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001474 hostapd_drv_sta_remove(hapd, sta->addr);
1475 sta->added_unassoc = 0;
1476 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001477 wpabuf_free(data);
1478}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001479
1480
1481/**
1482 * auth_sae_init_committed - Send COMMIT and start SAE in committed state
1483 * @hapd: BSS data for the device initiating the authentication
1484 * @sta: the peer to which commit authentication frame is sent
1485 *
1486 * This function implements Init event handling (IEEE Std 802.11-2012,
1487 * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
1488 * sta->sae structure should be initialized appropriately via a call to
1489 * sae_prepare_commit().
1490 */
1491int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
1492{
1493 int ret;
1494
1495 if (!sta->sae || !sta->sae->tmp)
1496 return -1;
1497
1498 if (sta->sae->state != SAE_NOTHING)
1499 return -1;
1500
Hai Shalomc3565922019-10-28 11:58:20 -07001501 ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0, -1);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001502 if (ret)
1503 return -1;
1504
Roshan Pius3a1667e2018-07-03 15:17:14 -07001505 sae_set_state(sta, SAE_COMMITTED, "Init and sent commit");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001506 sta->sae->sync = 0;
1507 sae_set_retransmit_timer(hapd, sta);
1508
1509 return 0;
1510}
1511
Hai Shalom021b0b52019-04-10 11:17:58 -07001512
1513void auth_sae_process_commit(void *eloop_ctx, void *user_ctx)
1514{
1515 struct hostapd_data *hapd = eloop_ctx;
1516 struct hostapd_sae_commit_queue *q;
1517 unsigned int queue_len;
1518
1519 q = dl_list_first(&hapd->sae_commit_queue,
1520 struct hostapd_sae_commit_queue, list);
1521 if (!q)
1522 return;
1523 wpa_printf(MSG_DEBUG,
1524 "SAE: Process next available message from queue");
1525 dl_list_del(&q->list);
1526 handle_auth(hapd, (const struct ieee80211_mgmt *) q->msg, q->len,
1527 q->rssi, 1);
1528 os_free(q);
1529
1530 if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
1531 return;
1532 queue_len = dl_list_len(&hapd->sae_commit_queue);
1533 eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
1534 hapd, NULL);
1535}
1536
1537
1538static void auth_sae_queue(struct hostapd_data *hapd,
1539 const struct ieee80211_mgmt *mgmt, size_t len,
1540 int rssi)
1541{
1542 struct hostapd_sae_commit_queue *q, *q2;
1543 unsigned int queue_len;
1544 const struct ieee80211_mgmt *mgmt2;
1545
1546 queue_len = dl_list_len(&hapd->sae_commit_queue);
1547 if (queue_len >= 15) {
1548 wpa_printf(MSG_DEBUG,
1549 "SAE: No more room in message queue - drop the new frame from "
1550 MACSTR, MAC2STR(mgmt->sa));
1551 return;
1552 }
1553
1554 wpa_printf(MSG_DEBUG, "SAE: Queue Authentication message from "
1555 MACSTR " for processing (queue_len %u)", MAC2STR(mgmt->sa),
1556 queue_len);
1557 q = os_zalloc(sizeof(*q) + len);
1558 if (!q)
1559 return;
1560 q->rssi = rssi;
1561 q->len = len;
1562 os_memcpy(q->msg, mgmt, len);
1563
1564 /* Check whether there is already a queued Authentication frame from the
1565 * same station with the same transaction number and if so, replace that
1566 * queue entry with the new one. This avoids issues with a peer that
1567 * sends multiple times (e.g., due to frequent SAE retries). There is no
1568 * point in us trying to process the old attempts after a new one has
1569 * obsoleted them. */
1570 dl_list_for_each(q2, &hapd->sae_commit_queue,
1571 struct hostapd_sae_commit_queue, list) {
1572 mgmt2 = (const struct ieee80211_mgmt *) q2->msg;
1573 if (os_memcmp(mgmt->sa, mgmt2->sa, ETH_ALEN) == 0 &&
1574 mgmt->u.auth.auth_transaction ==
1575 mgmt2->u.auth.auth_transaction) {
1576 wpa_printf(MSG_DEBUG,
1577 "SAE: Replace queued message from same STA with same transaction number");
1578 dl_list_add(&q2->list, &q->list);
1579 dl_list_del(&q2->list);
1580 os_free(q2);
1581 goto queued;
1582 }
1583 }
1584
1585 /* No pending identical entry, so add to the end of the queue */
1586 dl_list_add_tail(&hapd->sae_commit_queue, &q->list);
1587
1588queued:
1589 if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
1590 return;
1591 eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
1592 hapd, NULL);
1593}
1594
1595
1596static int auth_sae_queued_addr(struct hostapd_data *hapd, const u8 *addr)
1597{
1598 struct hostapd_sae_commit_queue *q;
1599 const struct ieee80211_mgmt *mgmt;
1600
1601 dl_list_for_each(q, &hapd->sae_commit_queue,
1602 struct hostapd_sae_commit_queue, list) {
1603 mgmt = (const struct ieee80211_mgmt *) q->msg;
1604 if (os_memcmp(addr, mgmt->sa, ETH_ALEN) == 0)
1605 return 1;
1606 }
1607
1608 return 0;
1609}
1610
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001611#endif /* CONFIG_SAE */
1612
1613
Hai Shalomfdcde762020-04-02 11:19:20 -07001614static u16 wpa_res_to_status_code(enum wpa_validate_result res)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001615{
Hai Shalomfdcde762020-04-02 11:19:20 -07001616 switch (res) {
1617 case WPA_IE_OK:
1618 return WLAN_STATUS_SUCCESS;
1619 case WPA_INVALID_IE:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001620 return WLAN_STATUS_INVALID_IE;
Hai Shalomfdcde762020-04-02 11:19:20 -07001621 case WPA_INVALID_GROUP:
1622 return WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1623 case WPA_INVALID_PAIRWISE:
1624 return WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1625 case WPA_INVALID_AKMP:
1626 return WLAN_STATUS_AKMP_NOT_VALID;
1627 case WPA_NOT_ENABLED:
1628 return WLAN_STATUS_INVALID_IE;
1629 case WPA_ALLOC_FAIL:
1630 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1631 case WPA_MGMT_FRAME_PROTECTION_VIOLATION:
1632 return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1633 case WPA_INVALID_MGMT_GROUP_CIPHER:
1634 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1635 case WPA_INVALID_MDIE:
1636 return WLAN_STATUS_INVALID_MDIE;
1637 case WPA_INVALID_PROTO:
1638 return WLAN_STATUS_INVALID_IE;
1639 case WPA_INVALID_PMKID:
1640 return WLAN_STATUS_INVALID_PMKID;
1641 case WPA_DENIED_OTHER_REASON:
1642 return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1643 }
1644 return WLAN_STATUS_INVALID_IE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001645}
1646
1647
1648#ifdef CONFIG_FILS
1649
1650static void handle_auth_fils_finish(struct hostapd_data *hapd,
1651 struct sta_info *sta, u16 resp,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001652 struct wpabuf *data, int pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001653
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001654void handle_auth_fils(struct hostapd_data *hapd, struct sta_info *sta,
1655 const u8 *pos, size_t len, u16 auth_alg,
1656 u16 auth_transaction, u16 status_code,
1657 void (*cb)(struct hostapd_data *hapd,
1658 struct sta_info *sta, u16 resp,
1659 struct wpabuf *data, int pub))
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001660{
1661 u16 resp = WLAN_STATUS_SUCCESS;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001662 const u8 *end;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001663 struct ieee802_11_elems elems;
Hai Shalomfdcde762020-04-02 11:19:20 -07001664 enum wpa_validate_result res;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001665 struct wpa_ie_data rsn;
1666 struct rsn_pmksa_cache_entry *pmksa = NULL;
1667
1668 if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
1669 return;
1670
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001671 end = pos + len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001672
1673 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
1674 pos, end - pos);
1675
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001676 /* TODO: FILS PK */
1677#ifdef CONFIG_FILS_SK_PFS
1678 if (auth_alg == WLAN_AUTH_FILS_SK_PFS) {
1679 u16 group;
1680 struct wpabuf *pub;
1681 size_t elem_len;
1682
1683 /* Using FILS PFS */
1684
1685 /* Finite Cyclic Group */
1686 if (end - pos < 2) {
1687 wpa_printf(MSG_DEBUG,
1688 "FILS: No room for Finite Cyclic Group");
1689 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1690 goto fail;
1691 }
1692 group = WPA_GET_LE16(pos);
1693 pos += 2;
1694 if (group != hapd->conf->fils_dh_group) {
1695 wpa_printf(MSG_DEBUG,
1696 "FILS: Unsupported Finite Cyclic Group: %u (expected %u)",
1697 group, hapd->conf->fils_dh_group);
1698 resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1699 goto fail;
1700 }
1701
1702 crypto_ecdh_deinit(sta->fils_ecdh);
1703 sta->fils_ecdh = crypto_ecdh_init(group);
1704 if (!sta->fils_ecdh) {
1705 wpa_printf(MSG_INFO,
1706 "FILS: Could not initialize ECDH with group %d",
1707 group);
1708 resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1709 goto fail;
1710 }
1711
1712 pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
1713 if (!pub) {
1714 wpa_printf(MSG_DEBUG,
1715 "FILS: Failed to derive ECDH public key");
1716 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1717 goto fail;
1718 }
1719 elem_len = wpabuf_len(pub);
1720 wpabuf_free(pub);
1721
1722 /* Element */
1723 if ((size_t) (end - pos) < elem_len) {
1724 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
1725 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1726 goto fail;
1727 }
1728
1729 wpabuf_free(sta->fils_g_sta);
1730 sta->fils_g_sta = wpabuf_alloc_copy(pos, elem_len);
1731 wpabuf_clear_free(sta->fils_dh_ss);
1732 sta->fils_dh_ss = crypto_ecdh_set_peerkey(sta->fils_ecdh, 1,
1733 pos, elem_len);
1734 if (!sta->fils_dh_ss) {
1735 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
1736 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1737 goto fail;
1738 }
1739 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", sta->fils_dh_ss);
1740 pos += elem_len;
1741 } else {
1742 crypto_ecdh_deinit(sta->fils_ecdh);
1743 sta->fils_ecdh = NULL;
1744 wpabuf_clear_free(sta->fils_dh_ss);
1745 sta->fils_dh_ss = NULL;
1746 }
1747#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001748
1749 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
1750 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
1751 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
1752 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1753 goto fail;
1754 }
1755
1756 /* RSNE */
1757 wpa_hexdump(MSG_DEBUG, "FILS: RSN element",
1758 elems.rsn_ie, elems.rsn_ie_len);
1759 if (!elems.rsn_ie ||
1760 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
1761 &rsn) < 0) {
1762 wpa_printf(MSG_DEBUG, "FILS: No valid RSN element");
1763 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1764 goto fail;
1765 }
1766
1767 if (!sta->wpa_sm)
1768 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
1769 NULL);
1770 if (!sta->wpa_sm) {
1771 wpa_printf(MSG_DEBUG,
1772 "FILS: Failed to initialize RSN state machine");
1773 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1774 goto fail;
1775 }
1776
1777 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
Hai Shalom021b0b52019-04-10 11:17:58 -07001778 hapd->iface->freq,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001779 elems.rsn_ie - 2, elems.rsn_ie_len + 2,
Hai Shalomc3565922019-10-28 11:58:20 -07001780 elems.rsnxe ? elems.rsnxe - 2 : NULL,
1781 elems.rsnxe ? elems.rsnxe_len + 2 : 0,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001782 elems.mdie, elems.mdie_len, NULL, 0);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001783 resp = wpa_res_to_status_code(res);
1784 if (resp != WLAN_STATUS_SUCCESS)
1785 goto fail;
1786
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001787 if (!elems.fils_nonce) {
1788 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
1789 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1790 goto fail;
1791 }
1792 wpa_hexdump(MSG_DEBUG, "FILS: SNonce", elems.fils_nonce,
1793 FILS_NONCE_LEN);
1794 os_memcpy(sta->fils_snonce, elems.fils_nonce, FILS_NONCE_LEN);
1795
1796 /* PMKID List */
1797 if (rsn.pmkid && rsn.num_pmkid > 0) {
1798 u8 num;
1799 const u8 *pmkid;
1800
1801 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
1802 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
1803
1804 pmkid = rsn.pmkid;
1805 num = rsn.num_pmkid;
1806 while (num) {
1807 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
1808 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr,
1809 pmkid);
1810 if (pmksa)
1811 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001812 pmksa = wpa_auth_pmksa_get_fils_cache_id(hapd->wpa_auth,
1813 sta->addr,
1814 pmkid);
1815 if (pmksa)
1816 break;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001817 pmkid += PMKID_LEN;
1818 num--;
1819 }
1820 }
1821 if (pmksa && wpa_auth_sta_key_mgmt(sta->wpa_sm) != pmksa->akmp) {
1822 wpa_printf(MSG_DEBUG,
1823 "FILS: Matching PMKSA cache entry has different AKMP (0x%x != 0x%x) - ignore",
1824 wpa_auth_sta_key_mgmt(sta->wpa_sm), pmksa->akmp);
1825 pmksa = NULL;
1826 }
1827 if (pmksa)
1828 wpa_printf(MSG_DEBUG, "FILS: Found matching PMKSA cache entry");
1829
1830 /* FILS Session */
1831 if (!elems.fils_session) {
1832 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
1833 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1834 goto fail;
1835 }
1836 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
1837 FILS_SESSION_LEN);
1838 os_memcpy(sta->fils_session, elems.fils_session, FILS_SESSION_LEN);
1839
Hai Shalomfdcde762020-04-02 11:19:20 -07001840 /* Wrapped Data */
1841 if (elems.wrapped_data) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001842 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07001843 elems.wrapped_data,
1844 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001845 if (!pmksa) {
1846#ifndef CONFIG_NO_RADIUS
1847 if (!sta->eapol_sm) {
1848 sta->eapol_sm =
1849 ieee802_1x_alloc_eapol_sm(hapd, sta);
1850 }
1851 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001852 "FILS: Forward EAP-Initiate/Re-auth to authentication server");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001853 ieee802_1x_encapsulate_radius(
Hai Shalomfdcde762020-04-02 11:19:20 -07001854 hapd, sta, elems.wrapped_data,
1855 elems.wrapped_data_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001856 sta->fils_pending_cb = cb;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001857 wpa_printf(MSG_DEBUG,
1858 "FILS: Will send Authentication frame once the response from authentication server is available");
1859 sta->flags |= WLAN_STA_PENDING_FILS_ERP;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001860 /* Calculate pending PMKID here so that we do not need
1861 * to maintain a copy of the EAP-Initiate/Reauth
1862 * message. */
1863 if (fils_pmkid_erp(wpa_auth_sta_key_mgmt(sta->wpa_sm),
Hai Shalomfdcde762020-04-02 11:19:20 -07001864 elems.wrapped_data,
1865 elems.wrapped_data_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001866 sta->fils_erp_pmkid) == 0)
1867 sta->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001868 return;
1869#else /* CONFIG_NO_RADIUS */
1870 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1871 goto fail;
1872#endif /* CONFIG_NO_RADIUS */
1873 }
1874 }
1875
1876fail:
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001877 if (cb) {
1878 struct wpabuf *data;
1879 int pub = 0;
1880
1881 data = prepare_auth_resp_fils(hapd, sta, &resp, pmksa, NULL,
1882 NULL, 0, &pub);
1883 if (!data) {
1884 wpa_printf(MSG_DEBUG,
1885 "%s: prepare_auth_resp_fils() returned failure",
1886 __func__);
1887 }
1888
1889 cb(hapd, sta, resp, data, pub);
1890 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001891}
1892
1893
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001894static struct wpabuf *
1895prepare_auth_resp_fils(struct hostapd_data *hapd,
1896 struct sta_info *sta, u16 *resp,
1897 struct rsn_pmksa_cache_entry *pmksa,
1898 struct wpabuf *erp_resp,
1899 const u8 *msk, size_t msk_len,
1900 int *is_pub)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001901{
1902 u8 fils_nonce[FILS_NONCE_LEN];
1903 size_t ielen;
1904 struct wpabuf *data = NULL;
1905 const u8 *ie;
1906 u8 *ie_buf = NULL;
1907 const u8 *pmk = NULL;
1908 size_t pmk_len = 0;
Paul Stewart092955c2017-02-06 09:13:09 -08001909 u8 pmk_buf[PMK_LEN_MAX];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001910 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001911
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001912 if (*resp != WLAN_STATUS_SUCCESS)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001913 goto fail;
1914
1915 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
1916 if (!ie) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001917 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001918 goto fail;
1919 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001920
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001921 if (pmksa) {
1922 /* Add PMKID of the selected PMKSA into RSNE */
1923 ie_buf = os_malloc(ielen + 2 + 2 + PMKID_LEN);
1924 if (!ie_buf) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001925 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001926 goto fail;
1927 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001928
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001929 os_memcpy(ie_buf, ie, ielen);
1930 if (wpa_insert_pmkid(ie_buf, &ielen, pmksa->pmkid) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001931 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001932 goto fail;
1933 }
1934 ie = ie_buf;
1935 }
1936
1937 if (random_get_bytes(fils_nonce, FILS_NONCE_LEN) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001938 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001939 goto fail;
1940 }
1941 wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS Nonce",
1942 fils_nonce, FILS_NONCE_LEN);
1943
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001944#ifdef CONFIG_FILS_SK_PFS
1945 if (sta->fils_dh_ss && sta->fils_ecdh) {
1946 pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
1947 if (!pub) {
1948 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1949 goto fail;
1950 }
1951 }
1952#endif /* CONFIG_FILS_SK_PFS */
1953
1954 data = wpabuf_alloc(1000 + ielen + (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001955 if (!data) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001956 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001957 goto fail;
1958 }
1959
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001960 /* TODO: FILS PK */
1961#ifdef CONFIG_FILS_SK_PFS
1962 if (pub) {
1963 /* Finite Cyclic Group */
1964 wpabuf_put_le16(data, hapd->conf->fils_dh_group);
1965
1966 /* Element */
1967 wpabuf_put_buf(data, pub);
1968 }
1969#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001970
1971 /* RSNE */
1972 wpabuf_put_data(data, ie, ielen);
1973
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001974 /* MDE when using FILS+FT (already included in ie,ielen with RSNE) */
1975
1976#ifdef CONFIG_IEEE80211R_AP
1977 if (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm))) {
1978 /* FTE[R1KH-ID,R0KH-ID] when using FILS+FT */
1979 int res;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001980 int use_sha384 = wpa_key_mgmt_sha384(
1981 wpa_auth_sta_key_mgmt(sta->wpa_sm));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001982
Roshan Pius3a1667e2018-07-03 15:17:14 -07001983 res = wpa_auth_write_fte(hapd->wpa_auth, use_sha384,
1984 wpabuf_put(data, 0),
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001985 wpabuf_tailroom(data));
1986 if (res < 0) {
1987 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1988 goto fail;
1989 }
1990 wpabuf_put(data, res);
1991 }
1992#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001993
1994 /* FILS Nonce */
1995 wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
1996 wpabuf_put_u8(data, 1 + FILS_NONCE_LEN); /* Length */
1997 /* Element ID Extension */
1998 wpabuf_put_u8(data, WLAN_EID_EXT_FILS_NONCE);
1999 wpabuf_put_data(data, fils_nonce, FILS_NONCE_LEN);
2000
2001 /* FILS Session */
2002 wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
2003 wpabuf_put_u8(data, 1 + FILS_SESSION_LEN); /* Length */
2004 /* Element ID Extension */
2005 wpabuf_put_u8(data, WLAN_EID_EXT_FILS_SESSION);
2006 wpabuf_put_data(data, sta->fils_session, FILS_SESSION_LEN);
2007
Hai Shalomfdcde762020-04-02 11:19:20 -07002008 /* Wrapped Data */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002009 if (!pmksa && erp_resp) {
2010 wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
2011 wpabuf_put_u8(data, 1 + wpabuf_len(erp_resp)); /* Length */
2012 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07002013 wpabuf_put_u8(data, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002014 wpabuf_put_buf(data, erp_resp);
2015
Paul Stewart092955c2017-02-06 09:13:09 -08002016 if (fils_rmsk_to_pmk(wpa_auth_sta_key_mgmt(sta->wpa_sm),
2017 msk, msk_len, sta->fils_snonce, fils_nonce,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002018 sta->fils_dh_ss ?
2019 wpabuf_head(sta->fils_dh_ss) : NULL,
2020 sta->fils_dh_ss ?
2021 wpabuf_len(sta->fils_dh_ss) : 0,
2022 pmk_buf, &pmk_len)) {
Paul Stewart092955c2017-02-06 09:13:09 -08002023 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002024 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Paul Stewart092955c2017-02-06 09:13:09 -08002025 wpabuf_free(data);
2026 data = NULL;
2027 goto fail;
2028 }
2029 pmk = pmk_buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002030
2031 /* Don't use DHss in PTK derivation if PMKSA caching is not
2032 * used. */
2033 wpabuf_clear_free(sta->fils_dh_ss);
2034 sta->fils_dh_ss = NULL;
2035
2036 if (sta->fils_erp_pmkid_set) {
2037 /* TODO: get PMKLifetime from WPA parameters */
2038 unsigned int dot11RSNAConfigPMKLifetime = 43200;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002039 int session_timeout;
2040
2041 session_timeout = dot11RSNAConfigPMKLifetime;
2042 if (sta->session_timeout_set) {
2043 struct os_reltime now, diff;
2044
2045 os_get_reltime(&now);
2046 os_reltime_sub(&sta->session_timeout, &now,
2047 &diff);
2048 session_timeout = diff.sec;
2049 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002050
2051 sta->fils_erp_pmkid_set = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07002052 wpa_auth_add_fils_pmk_pmkid(sta->wpa_sm, pmk, pmk_len,
2053 sta->fils_erp_pmkid);
Hai Shalom021b0b52019-04-10 11:17:58 -07002054 if (!hapd->conf->disable_pmksa_caching &&
2055 wpa_auth_pmksa_add2(
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002056 hapd->wpa_auth, sta->addr,
2057 pmk, pmk_len,
2058 sta->fils_erp_pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002059 session_timeout,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002060 wpa_auth_sta_key_mgmt(sta->wpa_sm)) < 0) {
2061 wpa_printf(MSG_ERROR,
2062 "FILS: Failed to add PMKSA cache entry based on ERP");
2063 }
2064 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002065 } else if (pmksa) {
2066 pmk = pmksa->pmk;
2067 pmk_len = pmksa->pmk_len;
2068 }
2069
2070 if (!pmk) {
2071 wpa_printf(MSG_DEBUG, "FILS: No PMK available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002072 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002073 wpabuf_free(data);
2074 data = NULL;
2075 goto fail;
2076 }
2077
2078 if (fils_auth_pmk_to_ptk(sta->wpa_sm, pmk, pmk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002079 sta->fils_snonce, fils_nonce,
2080 sta->fils_dh_ss ?
2081 wpabuf_head(sta->fils_dh_ss) : NULL,
2082 sta->fils_dh_ss ?
2083 wpabuf_len(sta->fils_dh_ss) : 0,
2084 sta->fils_g_sta, pub) < 0) {
2085 *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002086 wpabuf_free(data);
2087 data = NULL;
2088 goto fail;
2089 }
2090
2091fail:
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002092 if (is_pub)
2093 *is_pub = pub != NULL;
2094 os_free(ie_buf);
2095 wpabuf_free(pub);
2096 wpabuf_clear_free(sta->fils_dh_ss);
2097 sta->fils_dh_ss = NULL;
2098#ifdef CONFIG_FILS_SK_PFS
2099 crypto_ecdh_deinit(sta->fils_ecdh);
2100 sta->fils_ecdh = NULL;
2101#endif /* CONFIG_FILS_SK_PFS */
2102 return data;
2103}
2104
2105
2106static void handle_auth_fils_finish(struct hostapd_data *hapd,
2107 struct sta_info *sta, u16 resp,
2108 struct wpabuf *data, int pub)
2109{
2110 u16 auth_alg;
2111
2112 auth_alg = (pub ||
2113 resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) ?
2114 WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
Hai Shalomfdcde762020-04-02 11:19:20 -07002115 send_auth_reply(hapd, sta, sta->addr, hapd->own_addr, auth_alg, 2, resp,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002116 data ? wpabuf_head(data) : (u8 *) "",
Roshan Pius3a1667e2018-07-03 15:17:14 -07002117 data ? wpabuf_len(data) : 0, "auth-fils-finish");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002118 wpabuf_free(data);
2119
2120 if (resp == WLAN_STATUS_SUCCESS) {
2121 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2122 HOSTAPD_LEVEL_DEBUG,
2123 "authentication OK (FILS)");
2124 sta->flags |= WLAN_STA_AUTH;
2125 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002126 sta->auth_alg = pub ? WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002127 mlme_authenticate_indication(hapd, sta);
2128 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002129}
2130
2131
2132void ieee802_11_finish_fils_auth(struct hostapd_data *hapd,
2133 struct sta_info *sta, int success,
2134 struct wpabuf *erp_resp,
2135 const u8 *msk, size_t msk_len)
2136{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002137 struct wpabuf *data;
2138 int pub = 0;
2139 u16 resp;
2140
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002141 sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002142
2143 if (!sta->fils_pending_cb)
2144 return;
2145 resp = success ? WLAN_STATUS_SUCCESS : WLAN_STATUS_UNSPECIFIED_FAILURE;
2146 data = prepare_auth_resp_fils(hapd, sta, &resp, NULL, erp_resp,
2147 msk, msk_len, &pub);
2148 if (!data) {
2149 wpa_printf(MSG_DEBUG,
2150 "%s: prepare_auth_resp_fils() returned failure",
2151 __func__);
2152 }
2153 sta->fils_pending_cb(hapd, sta, resp, data, pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002154}
2155
2156#endif /* CONFIG_FILS */
2157
2158
Hai Shalomfdcde762020-04-02 11:19:20 -07002159static int ieee802_11_allowed_address(struct hostapd_data *hapd, const u8 *addr,
2160 const u8 *msg, size_t len,
2161 struct radius_sta *info)
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002162{
2163 int res;
2164
Hai Shalomfdcde762020-04-02 11:19:20 -07002165 res = hostapd_allowed_address(hapd, addr, msg, len, info, 0);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002166
2167 if (res == HOSTAPD_ACL_REJECT) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002168 wpa_printf(MSG_DEBUG, "Station " MACSTR
2169 " not allowed to authenticate",
2170 MAC2STR(addr));
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002171 return HOSTAPD_ACL_REJECT;
2172 }
2173
2174 if (res == HOSTAPD_ACL_PENDING) {
2175 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
2176 " waiting for an external authentication",
2177 MAC2STR(addr));
2178 /* Authentication code will re-send the authentication frame
2179 * after it has received (and cached) information from the
2180 * external source. */
2181 return HOSTAPD_ACL_PENDING;
2182 }
2183
2184 return res;
2185}
2186
2187
2188static int
2189ieee802_11_set_radius_info(struct hostapd_data *hapd, struct sta_info *sta,
Hai Shalomfdcde762020-04-02 11:19:20 -07002190 int res, struct radius_sta *info)
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002191{
Hai Shalomfdcde762020-04-02 11:19:20 -07002192 u32 session_timeout = info->session_timeout;
2193 u32 acct_interim_interval = info->acct_interim_interval;
2194 struct vlan_description *vlan_id = &info->vlan_id;
2195 struct hostapd_sta_wpa_psk_short *psk = info->psk;
2196 char *identity = info->identity;
2197 char *radius_cui = info->radius_cui;
2198
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002199 if (vlan_id->notempty &&
2200 !hostapd_vlan_valid(hapd->conf->vlan, vlan_id)) {
2201 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
2202 HOSTAPD_LEVEL_INFO,
2203 "Invalid VLAN %d%s received from RADIUS server",
2204 vlan_id->untagged,
2205 vlan_id->tagged[0] ? "+" : "");
2206 return -1;
2207 }
2208 if (ap_sta_set_vlan(hapd, sta, vlan_id) < 0)
2209 return -1;
2210 if (sta->vlan_id)
2211 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
2212 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
2213
2214 hostapd_free_psk_list(sta->psk);
Hai Shalomfdcde762020-04-02 11:19:20 -07002215 if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED)
2216 hostapd_copy_psk_list(&sta->psk, psk);
2217 else
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002218 sta->psk = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002219
Roshan Pius3a1667e2018-07-03 15:17:14 -07002220 os_free(sta->identity);
Hai Shalomfdcde762020-04-02 11:19:20 -07002221 if (identity)
2222 sta->identity = os_strdup(identity);
2223 else
2224 sta->identity = NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002225
2226 os_free(sta->radius_cui);
Hai Shalomfdcde762020-04-02 11:19:20 -07002227 if (radius_cui)
2228 sta->radius_cui = os_strdup(radius_cui);
2229 else
2230 sta->radius_cui = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002231
2232 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
2233 sta->acct_interim_interval = acct_interim_interval;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002234 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT) {
2235 sta->session_timeout_set = 1;
2236 os_get_reltime(&sta->session_timeout);
2237 sta->session_timeout.sec += session_timeout;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002238 ap_sta_session_timeout(hapd, sta, session_timeout);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002239 } else {
2240 sta->session_timeout_set = 0;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002241 ap_sta_no_session_timeout(hapd, sta);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002242 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002243
2244 return 0;
2245}
2246
2247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248static void handle_auth(struct hostapd_data *hapd,
Hai Shalom74f70d42019-02-11 14:42:39 -08002249 const struct ieee80211_mgmt *mgmt, size_t len,
Hai Shalom021b0b52019-04-10 11:17:58 -07002250 int rssi, int from_queue)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251{
2252 u16 auth_alg, auth_transaction, status_code;
2253 u16 resp = WLAN_STATUS_SUCCESS;
2254 struct sta_info *sta = NULL;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002255 int res, reply_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 u16 fc;
2257 const u8 *challenge = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
2259 size_t resp_ies_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002260 u16 seq_ctrl;
Hai Shalomfdcde762020-04-02 11:19:20 -07002261 struct radius_sta rad_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262
2263 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002264 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
2265 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 return;
2267 }
2268
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002269#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002270 if (hapd->iconf->ignore_auth_probability > 0.0 &&
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002271 drand48() < hapd->iconf->ignore_auth_probability) {
2272 wpa_printf(MSG_INFO,
2273 "TESTING: ignoring auth frame from " MACSTR,
2274 MAC2STR(mgmt->sa));
2275 return;
2276 }
2277#endif /* CONFIG_TESTING_OPTIONS */
2278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2280 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2281 status_code = le_to_host16(mgmt->u.auth.status_code);
2282 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002283 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284
2285 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
2286 2 + WLAN_AUTH_CHALLENGE_LEN &&
2287 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
2288 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
2289 challenge = &mgmt->u.auth.variable[2];
2290
2291 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002292 "auth_transaction=%d status_code=%d wep=%d%s "
Hai Shalom021b0b52019-04-10 11:17:58 -07002293 "seq_ctrl=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
2295 status_code, !!(fc & WLAN_FC_ISWEP),
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002296 challenge ? " challenge" : "",
Hai Shalom021b0b52019-04-10 11:17:58 -07002297 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "",
2298 from_queue ? " (from queue)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002300#ifdef CONFIG_NO_RC4
2301 if (auth_alg == WLAN_AUTH_SHARED_KEY) {
2302 wpa_printf(MSG_INFO,
2303 "Unsupported authentication algorithm (%d)",
2304 auth_alg);
2305 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
2306 goto fail;
2307 }
2308#endif /* CONFIG_NO_RC4 */
2309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310 if (hapd->tkip_countermeasures) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002311 wpa_printf(MSG_DEBUG,
2312 "Ongoing TKIP countermeasures (Michael MIC failure) - reject authentication");
2313 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 goto fail;
2315 }
2316
2317 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
2318 auth_alg == WLAN_AUTH_OPEN) ||
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002319#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002320 (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 auth_alg == WLAN_AUTH_FT) ||
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002322#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002323#ifdef CONFIG_SAE
2324 (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
2325 auth_alg == WLAN_AUTH_SAE) ||
2326#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002327#ifdef CONFIG_FILS
2328 (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
2329 auth_alg == WLAN_AUTH_FILS_SK) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002330 (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
2331 hapd->conf->fils_dh_group &&
2332 auth_alg == WLAN_AUTH_FILS_SK_PFS) ||
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002333#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
2335 auth_alg == WLAN_AUTH_SHARED_KEY))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002336 wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
2337 auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
2339 goto fail;
2340 }
2341
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002342 if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002344 wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
2345 auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
2347 goto fail;
2348 }
2349
2350 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002351 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
2352 MAC2STR(mgmt->sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2354 goto fail;
2355 }
2356
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002357 if (hapd->conf->no_auth_if_seen_on) {
2358 struct hostapd_data *other;
2359
2360 other = sta_track_seen_on(hapd->iface, mgmt->sa,
2361 hapd->conf->no_auth_if_seen_on);
2362 if (other) {
2363 u8 *pos;
2364 u32 info;
2365 u8 op_class, channel, phytype;
2366
2367 wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
2368 MACSTR " since STA has been seen on %s",
2369 hapd->conf->iface, MAC2STR(mgmt->sa),
2370 hapd->conf->no_auth_if_seen_on);
2371
2372 resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
2373 pos = &resp_ies[0];
2374 *pos++ = WLAN_EID_NEIGHBOR_REPORT;
2375 *pos++ = 13;
2376 os_memcpy(pos, other->own_addr, ETH_ALEN);
2377 pos += ETH_ALEN;
2378 info = 0; /* TODO: BSSID Information */
2379 WPA_PUT_LE32(pos, info);
2380 pos += 4;
2381 if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
2382 phytype = 8; /* dmg */
2383 else if (other->iconf->ieee80211ac)
2384 phytype = 9; /* vht */
2385 else if (other->iconf->ieee80211n)
2386 phytype = 7; /* ht */
2387 else if (other->iconf->hw_mode ==
2388 HOSTAPD_MODE_IEEE80211A)
2389 phytype = 4; /* ofdm */
2390 else if (other->iconf->hw_mode ==
2391 HOSTAPD_MODE_IEEE80211G)
2392 phytype = 6; /* erp */
2393 else
2394 phytype = 5; /* hrdsss */
2395 if (ieee80211_freq_to_channel_ext(
2396 hostapd_hw_get_freq(other,
2397 other->iconf->channel),
2398 other->iconf->secondary_channel,
2399 other->iconf->ieee80211ac,
2400 &op_class, &channel) == NUM_HOSTAPD_MODES) {
2401 op_class = 0;
2402 channel = other->iconf->channel;
2403 }
2404 *pos++ = op_class;
2405 *pos++ = channel;
2406 *pos++ = phytype;
2407 resp_ies_len = pos - &resp_ies[0];
2408 goto fail;
2409 }
2410 }
2411
Hai Shalomfdcde762020-04-02 11:19:20 -07002412 res = ieee802_11_allowed_address(hapd, mgmt->sa, (const u8 *) mgmt, len,
2413 &rad_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414 if (res == HOSTAPD_ACL_REJECT) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002415 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
2416 "Ignore Authentication frame from " MACSTR
2417 " due to ACL reject", MAC2STR(mgmt->sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2419 goto fail;
2420 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002421 if (res == HOSTAPD_ACL_PENDING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423
Hai Shalom021b0b52019-04-10 11:17:58 -07002424#ifdef CONFIG_SAE
2425 if (auth_alg == WLAN_AUTH_SAE && !from_queue &&
2426 (auth_transaction == 1 ||
2427 (auth_transaction == 2 && auth_sae_queued_addr(hapd, mgmt->sa)))) {
2428 /* Handle SAE Authentication commit message through a queue to
2429 * provide more control for postponing the needed heavy
2430 * processing under a possible DoS attack scenario. In addition,
2431 * queue SAE Authentication confirm message if there happens to
2432 * be a queued commit message from the same peer. This is needed
2433 * to avoid reordering Authentication frames within the same
2434 * SAE exchange. */
2435 auth_sae_queue(hapd, mgmt, len, rssi);
2436 return;
2437 }
2438#endif /* CONFIG_SAE */
2439
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002440 sta = ap_get_sta(hapd, mgmt->sa);
2441 if (sta) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002442 sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
Hai Shalom74f70d42019-02-11 14:42:39 -08002443 sta->ft_over_ds = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002444 if ((fc & WLAN_FC_RETRY) &&
2445 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2446 sta->last_seq_ctrl == seq_ctrl &&
2447 sta->last_subtype == WLAN_FC_STYPE_AUTH) {
2448 hostapd_logger(hapd, sta->addr,
2449 HOSTAPD_MODULE_IEEE80211,
2450 HOSTAPD_LEVEL_DEBUG,
2451 "Drop repeated authentication frame seq_ctrl=0x%x",
2452 seq_ctrl);
2453 return;
2454 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002455#ifdef CONFIG_MESH
2456 if ((hapd->conf->mesh & MESH_ENABLED) &&
2457 sta->plink_state == PLINK_BLOCKED) {
2458 wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
2459 " is blocked - drop Authentication frame",
2460 MAC2STR(mgmt->sa));
2461 return;
2462 }
2463#endif /* CONFIG_MESH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002464 } else {
2465#ifdef CONFIG_MESH
2466 if (hapd->conf->mesh & MESH_ENABLED) {
2467 /* if the mesh peer is not available, we don't do auth.
2468 */
2469 wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002470 " not yet known - drop Authentication frame",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002471 MAC2STR(mgmt->sa));
2472 /*
2473 * Save a copy of the frame so that it can be processed
2474 * if a new peer entry is added shortly after this.
2475 */
2476 wpabuf_free(hapd->mesh_pending_auth);
2477 hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
2478 os_get_reltime(&hapd->mesh_pending_auth_time);
2479 return;
2480 }
2481#endif /* CONFIG_MESH */
2482
2483 sta = ap_sta_add(hapd, mgmt->sa);
2484 if (!sta) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002485 wpa_printf(MSG_DEBUG, "ap_sta_add() failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002486 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2487 goto fail;
2488 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002490 sta->last_seq_ctrl = seq_ctrl;
2491 sta->last_subtype = WLAN_FC_STYPE_AUTH;
Hai Shalom74f70d42019-02-11 14:42:39 -08002492#ifdef CONFIG_MBO
2493 sta->auth_rssi = rssi;
2494#endif /* CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495
Hai Shalomfdcde762020-04-02 11:19:20 -07002496 res = ieee802_11_set_radius_info(hapd, sta, res, &rad_info);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002497 if (res) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002498 wpa_printf(MSG_DEBUG, "ieee802_11_set_radius_info() failed");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002499 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2500 goto fail;
2501 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 sta->flags &= ~WLAN_STA_PREAUTH;
2504 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
2505
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002506 /*
2507 * If the driver supports full AP client state, add a station to the
2508 * driver before sending authentication reply to make sure the driver
2509 * has resources, and not to go through the entire authentication and
2510 * association handshake, and fail it at the end.
2511 *
2512 * If this is not the first transaction, in a multi-step authentication
2513 * algorithm, the station already exists in the driver
2514 * (sta->added_unassoc = 1) so skip it.
2515 *
2516 * In mesh mode, the station was already added to the driver when the
2517 * NEW_PEER_CANDIDATE event is received.
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002518 *
2519 * If PMF was negotiated for the existing association, skip this to
2520 * avoid dropping the STA entry and the associated keys. This is needed
2521 * to allow the original connection work until the attempt can complete
2522 * (re)association, so that unprotected Authentication frame cannot be
2523 * used to bypass PMF protection.
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002524 */
2525 if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002526 (!(sta->flags & WLAN_STA_MFP) || !ap_sta_is_authorized(sta)) &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002527 !(hapd->conf->mesh & MESH_ENABLED) &&
2528 !(sta->added_unassoc)) {
Hai Shalomb755a2a2020-04-23 21:49:02 -07002529 if (ap_sta_re_add(hapd, sta) < 0) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002530 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2531 goto fail;
2532 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002533 }
2534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 switch (auth_alg) {
2536 case WLAN_AUTH_OPEN:
2537 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2538 HOSTAPD_LEVEL_DEBUG,
2539 "authentication OK (open system)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 sta->flags |= WLAN_STA_AUTH;
2541 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
2542 sta->auth_alg = WLAN_AUTH_OPEN;
2543 mlme_authenticate_indication(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002545#ifdef CONFIG_WEP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002546#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547 case WLAN_AUTH_SHARED_KEY:
2548 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
2549 fc & WLAN_FC_ISWEP);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002550 if (resp != 0)
2551 wpa_printf(MSG_DEBUG,
2552 "auth_shared_key() failed: status=%d", resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
2554 mlme_authenticate_indication(hapd, sta);
2555 if (sta->challenge && auth_transaction == 1) {
2556 resp_ies[0] = WLAN_EID_CHALLENGE;
2557 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
2558 os_memcpy(resp_ies + 2, sta->challenge,
2559 WLAN_AUTH_CHALLENGE_LEN);
2560 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
2561 }
2562 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002563#endif /* CONFIG_NO_RC4 */
Hai Shalomfdcde762020-04-02 11:19:20 -07002564#endif /* CONFIG_WEP */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002565#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566 case WLAN_AUTH_FT:
2567 sta->auth_alg = WLAN_AUTH_FT;
2568 if (sta->wpa_sm == NULL)
2569 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002570 sta->addr, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 if (sta->wpa_sm == NULL) {
2572 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
2573 "state machine");
2574 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2575 goto fail;
2576 }
2577 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
2578 auth_transaction, mgmt->u.auth.variable,
2579 len - IEEE80211_HDRLEN -
2580 sizeof(mgmt->u.auth),
2581 handle_auth_ft_finish, hapd);
2582 /* handle_auth_ft_finish() callback will complete auth. */
2583 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002584#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002585#ifdef CONFIG_SAE
2586 case WLAN_AUTH_SAE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002587#ifdef CONFIG_MESH
2588 if (status_code == WLAN_STATUS_SUCCESS &&
2589 hapd->conf->mesh & MESH_ENABLED) {
2590 if (sta->wpa_sm == NULL)
2591 sta->wpa_sm =
2592 wpa_auth_sta_init(hapd->wpa_auth,
2593 sta->addr, NULL);
2594 if (sta->wpa_sm == NULL) {
2595 wpa_printf(MSG_DEBUG,
2596 "SAE: Failed to initialize WPA state machine");
2597 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2598 goto fail;
2599 }
2600 }
2601#endif /* CONFIG_MESH */
2602 handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
2603 status_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002604 return;
2605#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002606#ifdef CONFIG_FILS
2607 case WLAN_AUTH_FILS_SK:
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002608 case WLAN_AUTH_FILS_SK_PFS:
2609 handle_auth_fils(hapd, sta, mgmt->u.auth.variable,
2610 len - IEEE80211_HDRLEN - sizeof(mgmt->u.auth),
2611 auth_alg, auth_transaction, status_code,
2612 handle_auth_fils_finish);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002613 return;
2614#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615 }
2616
2617 fail:
Hai Shalomfdcde762020-04-02 11:19:20 -07002618 reply_res = send_auth_reply(hapd, sta, mgmt->sa, mgmt->bssid, auth_alg,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002619 auth_transaction + 1, resp, resp_ies,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002620 resp_ies_len, "handle-auth");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002621
2622 if (sta && sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
2623 reply_res != WLAN_STATUS_SUCCESS)) {
2624 hostapd_drv_sta_remove(hapd, sta->addr);
2625 sta->added_unassoc = 0;
2626 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627}
2628
2629
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002630int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631{
2632 int i, j = 32, aid;
2633
2634 /* get a unique AID */
2635 if (sta->aid > 0) {
2636 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
2637 return 0;
2638 }
2639
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002640 if (TEST_FAIL())
2641 return -1;
2642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 for (i = 0; i < AID_WORDS; i++) {
2644 if (hapd->sta_aid[i] == (u32) -1)
2645 continue;
2646 for (j = 0; j < 32; j++) {
2647 if (!(hapd->sta_aid[i] & BIT(j)))
2648 break;
2649 }
2650 if (j < 32)
2651 break;
2652 }
2653 if (j == 32)
2654 return -1;
2655 aid = i * 32 + j + 1;
2656 if (aid > 2007)
2657 return -1;
2658
2659 sta->aid = aid;
2660 hapd->sta_aid[i] |= BIT(j);
2661 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
2662 return 0;
2663}
2664
2665
2666static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
2667 const u8 *ssid_ie, size_t ssid_ie_len)
2668{
2669 if (ssid_ie == NULL)
2670 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2671
2672 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
2673 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2675 HOSTAPD_LEVEL_INFO,
2676 "Station tried to associate with unknown SSID "
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002677 "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2679 }
2680
2681 return WLAN_STATUS_SUCCESS;
2682}
2683
2684
2685static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
2686 const u8 *wmm_ie, size_t wmm_ie_len)
2687{
2688 sta->flags &= ~WLAN_STA_WMM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002689 sta->qosinfo = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690 if (wmm_ie && hapd->conf->wmm_enabled) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002691 struct wmm_information_element *wmm;
2692
2693 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694 hostapd_logger(hapd, sta->addr,
2695 HOSTAPD_MODULE_WPA,
2696 HOSTAPD_LEVEL_DEBUG,
2697 "invalid WMM element in association "
2698 "request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002699 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2700 }
2701
2702 sta->flags |= WLAN_STA_WMM;
2703 wmm = (struct wmm_information_element *) wmm_ie;
2704 sta->qosinfo = wmm->qos_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 }
2706 return WLAN_STATUS_SUCCESS;
2707}
2708
Hai Shalom74f70d42019-02-11 14:42:39 -08002709static u16 check_multi_ap(struct hostapd_data *hapd, struct sta_info *sta,
2710 const u8 *multi_ap_ie, size_t multi_ap_len)
2711{
2712 u8 multi_ap_value = 0;
2713
2714 sta->flags &= ~WLAN_STA_MULTI_AP;
2715
2716 if (!hapd->conf->multi_ap)
2717 return WLAN_STATUS_SUCCESS;
2718
2719 if (multi_ap_ie) {
2720 const u8 *multi_ap_subelem;
2721
2722 multi_ap_subelem = get_ie(multi_ap_ie + 4,
2723 multi_ap_len - 4,
2724 MULTI_AP_SUB_ELEM_TYPE);
2725 if (multi_ap_subelem && multi_ap_subelem[1] == 1) {
2726 multi_ap_value = multi_ap_subelem[2];
2727 } else {
2728 hostapd_logger(hapd, sta->addr,
2729 HOSTAPD_MODULE_IEEE80211,
2730 HOSTAPD_LEVEL_INFO,
2731 "Multi-AP IE has missing or invalid Multi-AP subelement");
2732 return WLAN_STATUS_INVALID_IE;
2733 }
2734 }
2735
Hai Shalom021b0b52019-04-10 11:17:58 -07002736 if (multi_ap_value && multi_ap_value != MULTI_AP_BACKHAUL_STA)
2737 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2738 HOSTAPD_LEVEL_INFO,
2739 "Multi-AP IE with unexpected value 0x%02x",
2740 multi_ap_value);
Hai Shalom74f70d42019-02-11 14:42:39 -08002741
Hai Shalom021b0b52019-04-10 11:17:58 -07002742 if (!(multi_ap_value & MULTI_AP_BACKHAUL_STA)) {
2743 if (hapd->conf->multi_ap & FRONTHAUL_BSS)
2744 return WLAN_STATUS_SUCCESS;
Hai Shalom74f70d42019-02-11 14:42:39 -08002745
Hai Shalom021b0b52019-04-10 11:17:58 -07002746 hostapd_logger(hapd, sta->addr,
2747 HOSTAPD_MODULE_IEEE80211,
2748 HOSTAPD_LEVEL_INFO,
2749 "Non-Multi-AP STA tries to associate with backhaul-only BSS");
2750 return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
Hai Shalom74f70d42019-02-11 14:42:39 -08002751 }
2752
Hai Shalom021b0b52019-04-10 11:17:58 -07002753 if (!(hapd->conf->multi_ap & BACKHAUL_BSS))
2754 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2755 HOSTAPD_LEVEL_DEBUG,
2756 "Backhaul STA tries to associate with fronthaul-only BSS");
2757
2758 sta->flags |= WLAN_STA_MULTI_AP;
2759 return WLAN_STATUS_SUCCESS;
Hai Shalom74f70d42019-02-11 14:42:39 -08002760}
2761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762
2763static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
2764 struct ieee802_11_elems *elems)
2765{
Dmitry Shmidt29333592017-01-09 12:27:11 -08002766 /* Supported rates not used in IEEE 802.11ad/DMG */
2767 if (hapd->iface->current_mode &&
2768 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD)
2769 return WLAN_STATUS_SUCCESS;
2770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 if (!elems->supp_rates) {
2772 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2773 HOSTAPD_LEVEL_DEBUG,
2774 "No supported rates element in AssocReq");
2775 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2776 }
2777
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002778 if (elems->supp_rates_len + elems->ext_supp_rates_len >
2779 sizeof(sta->supported_rates)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2781 HOSTAPD_LEVEL_DEBUG,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002782 "Invalid supported rates element length %d+%d",
2783 elems->supp_rates_len,
2784 elems->ext_supp_rates_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2786 }
2787
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002788 sta->supported_rates_len = merge_byte_arrays(
2789 sta->supported_rates, sizeof(sta->supported_rates),
2790 elems->supp_rates, elems->supp_rates_len,
2791 elems->ext_supp_rates, elems->ext_supp_rates_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792
2793 return WLAN_STATUS_SUCCESS;
2794}
2795
2796
Dmitry Shmidt051af732013-10-22 13:52:46 -07002797static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
2798 const u8 *ext_capab_ie, size_t ext_capab_ie_len)
2799{
2800#ifdef CONFIG_INTERWORKING
2801 /* check for QoS Map support */
2802 if (ext_capab_ie_len >= 5) {
2803 if (ext_capab_ie[4] & 0x01)
2804 sta->qos_map_enabled = 1;
2805 }
2806#endif /* CONFIG_INTERWORKING */
2807
Roshan Pius3a1667e2018-07-03 15:17:14 -07002808 if (ext_capab_ie_len > 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002809 sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
Roshan Pius3a1667e2018-07-03 15:17:14 -07002810 os_free(sta->ext_capability);
2811 sta->ext_capability = os_malloc(1 + ext_capab_ie_len);
2812 if (sta->ext_capability) {
2813 sta->ext_capability[0] = ext_capab_ie_len;
2814 os_memcpy(sta->ext_capability + 1, ext_capab_ie,
2815 ext_capab_ie_len);
2816 }
2817 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002818
Dmitry Shmidt051af732013-10-22 13:52:46 -07002819 return WLAN_STATUS_SUCCESS;
2820}
2821
2822
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002823#ifdef CONFIG_OWE
2824
2825static int owe_group_supported(struct hostapd_data *hapd, u16 group)
2826{
2827 int i;
2828 int *groups = hapd->conf->owe_groups;
2829
2830 if (group != 19 && group != 20 && group != 21)
2831 return 0;
2832
2833 if (!groups)
2834 return 1;
2835
2836 for (i = 0; groups[i] > 0; i++) {
2837 if (groups[i] == group)
2838 return 1;
2839 }
2840
2841 return 0;
2842}
2843
2844
2845static u16 owe_process_assoc_req(struct hostapd_data *hapd,
2846 struct sta_info *sta, const u8 *owe_dh,
2847 u8 owe_dh_len)
2848{
2849 struct wpabuf *secret, *pub, *hkey;
2850 int res;
2851 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
2852 const char *info = "OWE Key Generation";
2853 const u8 *addr[2];
2854 size_t len[2];
2855 u16 group;
2856 size_t hash_len, prime_len;
2857
2858 if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
2859 wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
2860 return WLAN_STATUS_SUCCESS;
2861 }
2862
2863 group = WPA_GET_LE16(owe_dh);
2864 if (!owe_group_supported(hapd, group)) {
2865 wpa_printf(MSG_DEBUG, "OWE: Unsupported DH group %u", group);
2866 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2867 }
2868 if (group == 19)
2869 prime_len = 32;
2870 else if (group == 20)
2871 prime_len = 48;
2872 else if (group == 21)
2873 prime_len = 66;
2874 else
2875 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2876
2877 crypto_ecdh_deinit(sta->owe_ecdh);
2878 sta->owe_ecdh = crypto_ecdh_init(group);
2879 if (!sta->owe_ecdh)
2880 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2881 sta->owe_group = group;
2882
2883 secret = crypto_ecdh_set_peerkey(sta->owe_ecdh, 0, owe_dh + 2,
2884 owe_dh_len - 2);
2885 secret = wpabuf_zeropad(secret, prime_len);
2886 if (!secret) {
2887 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
2888 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2889 }
2890 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
2891
2892 /* prk = HKDF-extract(C | A | group, z) */
2893
2894 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
2895 if (!pub) {
2896 wpabuf_clear_free(secret);
2897 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2898 }
2899
2900 /* PMKID = Truncate-128(Hash(C | A)) */
2901 addr[0] = owe_dh + 2;
2902 len[0] = owe_dh_len - 2;
2903 addr[1] = wpabuf_head(pub);
2904 len[1] = wpabuf_len(pub);
2905 if (group == 19) {
2906 res = sha256_vector(2, addr, len, pmkid);
2907 hash_len = SHA256_MAC_LEN;
2908 } else if (group == 20) {
2909 res = sha384_vector(2, addr, len, pmkid);
2910 hash_len = SHA384_MAC_LEN;
2911 } else if (group == 21) {
2912 res = sha512_vector(2, addr, len, pmkid);
2913 hash_len = SHA512_MAC_LEN;
2914 } else {
2915 wpabuf_free(pub);
2916 wpabuf_clear_free(secret);
2917 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2918 }
2919 pub = wpabuf_zeropad(pub, prime_len);
2920 if (res < 0 || !pub) {
2921 wpabuf_free(pub);
2922 wpabuf_clear_free(secret);
2923 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2924 }
2925
2926 hkey = wpabuf_alloc(owe_dh_len - 2 + wpabuf_len(pub) + 2);
2927 if (!hkey) {
2928 wpabuf_free(pub);
2929 wpabuf_clear_free(secret);
2930 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2931 }
2932
2933 wpabuf_put_data(hkey, owe_dh + 2, owe_dh_len - 2); /* C */
2934 wpabuf_put_buf(hkey, pub); /* A */
2935 wpabuf_free(pub);
2936 wpabuf_put_le16(hkey, group); /* group */
2937 if (group == 19)
2938 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
2939 wpabuf_head(secret), wpabuf_len(secret), prk);
2940 else if (group == 20)
2941 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
2942 wpabuf_head(secret), wpabuf_len(secret), prk);
2943 else if (group == 21)
2944 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
2945 wpabuf_head(secret), wpabuf_len(secret), prk);
2946 wpabuf_clear_free(hkey);
2947 wpabuf_clear_free(secret);
2948 if (res < 0)
2949 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2950
2951 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
2952
2953 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
2954
2955 os_free(sta->owe_pmk);
2956 sta->owe_pmk = os_malloc(hash_len);
2957 if (!sta->owe_pmk) {
2958 os_memset(prk, 0, SHA512_MAC_LEN);
2959 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2960 }
2961
2962 if (group == 19)
2963 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
2964 os_strlen(info), sta->owe_pmk, hash_len);
2965 else if (group == 20)
2966 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
2967 os_strlen(info), sta->owe_pmk, hash_len);
2968 else if (group == 21)
2969 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
2970 os_strlen(info), sta->owe_pmk, hash_len);
2971 os_memset(prk, 0, SHA512_MAC_LEN);
2972 if (res < 0) {
2973 os_free(sta->owe_pmk);
2974 sta->owe_pmk = NULL;
2975 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2976 }
2977 sta->owe_pmk_len = hash_len;
2978
2979 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sta->owe_pmk, sta->owe_pmk_len);
2980 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
2981 wpa_auth_pmksa_add2(hapd->wpa_auth, sta->addr, sta->owe_pmk,
2982 sta->owe_pmk_len, pmkid, 0, WPA_KEY_MGMT_OWE);
2983
2984 return WLAN_STATUS_SUCCESS;
2985}
2986
Hai Shalom81f62d82019-07-22 12:10:00 -07002987
2988u16 owe_validate_request(struct hostapd_data *hapd, const u8 *peer,
2989 const u8 *rsn_ie, size_t rsn_ie_len,
2990 const u8 *owe_dh, size_t owe_dh_len)
2991{
2992 struct wpa_ie_data data;
2993 int res;
2994
2995 if (!rsn_ie || rsn_ie_len < 2) {
2996 wpa_printf(MSG_DEBUG, "OWE: Invalid RSNE from " MACSTR,
2997 MAC2STR(peer));
2998 return WLAN_STATUS_INVALID_IE;
2999 }
3000 rsn_ie -= 2;
3001 rsn_ie_len += 2;
3002
3003 res = wpa_parse_wpa_ie_rsn(rsn_ie, rsn_ie_len, &data);
3004 if (res) {
3005 wpa_printf(MSG_DEBUG, "Failed to parse RSNE from " MACSTR
3006 " (res=%d)", MAC2STR(peer), res);
3007 wpa_hexdump(MSG_DEBUG, "RSNE", rsn_ie, rsn_ie_len);
3008 return wpa_res_to_status_code(res);
3009 }
3010 if (!(data.key_mgmt & WPA_KEY_MGMT_OWE)) {
3011 wpa_printf(MSG_DEBUG,
3012 "OWE: Unexpected key mgmt 0x%x from " MACSTR,
3013 (unsigned int) data.key_mgmt, MAC2STR(peer));
3014 return WLAN_STATUS_AKMP_NOT_VALID;
3015 }
3016 if (!owe_dh) {
3017 wpa_printf(MSG_DEBUG,
3018 "OWE: No Diffie-Hellman Parameter element from "
3019 MACSTR, MAC2STR(peer));
3020 return WLAN_STATUS_AKMP_NOT_VALID;
3021 }
3022
3023 return WLAN_STATUS_SUCCESS;
3024}
3025
3026
3027u16 owe_process_rsn_ie(struct hostapd_data *hapd,
3028 struct sta_info *sta,
3029 const u8 *rsn_ie, size_t rsn_ie_len,
3030 const u8 *owe_dh, size_t owe_dh_len)
3031{
3032 u16 status;
3033 u8 *owe_buf, ie[256 * 2];
3034 size_t ie_len = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07003035 enum wpa_validate_result res;
Hai Shalom81f62d82019-07-22 12:10:00 -07003036
3037 if (!rsn_ie || rsn_ie_len < 2) {
3038 wpa_printf(MSG_DEBUG, "OWE: No RSNE in (Re)AssocReq");
3039 status = WLAN_STATUS_INVALID_IE;
3040 goto end;
3041 }
3042
3043 if (!sta->wpa_sm)
3044 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
3045 NULL);
3046 if (!sta->wpa_sm) {
3047 wpa_printf(MSG_WARNING,
3048 "OWE: Failed to initialize WPA state machine");
3049 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
3050 goto end;
3051 }
3052 rsn_ie -= 2;
3053 rsn_ie_len += 2;
3054 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
3055 hapd->iface->freq, rsn_ie, rsn_ie_len,
Hai Shalomc3565922019-10-28 11:58:20 -07003056 NULL, 0, NULL, 0, owe_dh, owe_dh_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07003057 status = wpa_res_to_status_code(res);
3058 if (status != WLAN_STATUS_SUCCESS)
3059 goto end;
3060 status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
3061 if (status != WLAN_STATUS_SUCCESS)
3062 goto end;
3063 owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, ie, sizeof(ie),
3064 NULL, 0);
3065 if (!owe_buf) {
3066 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
3067 goto end;
3068 }
3069
3070 if (sta->owe_ecdh) {
3071 struct wpabuf *pub;
3072
3073 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3074 if (!pub) {
3075 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
3076 goto end;
3077 }
3078
3079 /* OWE Diffie-Hellman Parameter element */
3080 *owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
3081 *owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
3082 *owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
3083 */
3084 WPA_PUT_LE16(owe_buf, sta->owe_group);
3085 owe_buf += 2;
3086 os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
3087 owe_buf += wpabuf_len(pub);
3088 wpabuf_free(pub);
3089 sta->external_dh_updated = 1;
3090 }
3091 ie_len = owe_buf - ie;
3092
3093end:
3094 wpa_printf(MSG_DEBUG, "OWE: Update status %d, ie len %d for peer "
3095 MACSTR, status, (unsigned int) ie_len,
3096 MAC2STR(sta->addr));
3097 hostapd_drv_update_dh_ie(hapd, sta->addr, status,
3098 status == WLAN_STATUS_SUCCESS ? ie : NULL,
3099 ie_len);
3100
3101 return status;
3102}
3103
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003104#endif /* CONFIG_OWE */
3105
3106
Hai Shalomb755a2a2020-04-23 21:49:02 -07003107static int check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108 const u8 *ies, size_t ies_len, int reassoc)
3109{
3110 struct ieee802_11_elems elems;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003111 int resp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 const u8 *wpa_ie;
3113 size_t wpa_ie_len;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003114 const u8 *p2p_dev_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115
3116 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
3117 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3118 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
3119 "association request");
3120 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3121 }
3122
3123 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
3124 if (resp != WLAN_STATUS_SUCCESS)
3125 return resp;
3126 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
3127 if (resp != WLAN_STATUS_SUCCESS)
3128 return resp;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003129 resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
3130 if (resp != WLAN_STATUS_SUCCESS)
3131 return resp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 resp = copy_supp_rates(hapd, sta, &elems);
3133 if (resp != WLAN_STATUS_SUCCESS)
3134 return resp;
Hai Shalom74f70d42019-02-11 14:42:39 -08003135
3136 resp = check_multi_ap(hapd, sta, elems.multi_ap, elems.multi_ap_len);
3137 if (resp != WLAN_STATUS_SUCCESS)
3138 return resp;
3139
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003140 resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 if (resp != WLAN_STATUS_SUCCESS)
3142 return resp;
3143 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
3144 !(sta->flags & WLAN_STA_HT)) {
3145 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3146 HOSTAPD_LEVEL_INFO, "Station does not support "
3147 "mandatory HT PHY - reject association");
3148 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
3149 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003151#ifdef CONFIG_IEEE80211AC
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003152 if (hapd->iconf->ieee80211ac) {
3153 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
3154 if (resp != WLAN_STATUS_SUCCESS)
3155 return resp;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003156
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003157 resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
3158 if (resp != WLAN_STATUS_SUCCESS)
3159 return resp;
3160 }
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003161
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003162 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
3163 !(sta->flags & WLAN_STA_VHT)) {
3164 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3165 HOSTAPD_LEVEL_INFO, "Station does not support "
3166 "mandatory VHT PHY - reject association");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003167 return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003168 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003169
3170 if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
3171 resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
3172 elems.vendor_vht_len);
3173 if (resp != WLAN_STATUS_SUCCESS)
3174 return resp;
3175 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003176#endif /* CONFIG_IEEE80211AC */
Hai Shalom81f62d82019-07-22 12:10:00 -07003177#ifdef CONFIG_IEEE80211AX
3178 if (hapd->iconf->ieee80211ax) {
3179 resp = copy_sta_he_capab(hapd, sta, IEEE80211_MODE_AP,
3180 elems.he_capabilities,
3181 elems.he_capabilities_len);
3182 if (resp != WLAN_STATUS_SUCCESS)
3183 return resp;
3184 }
3185#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003186
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003187#ifdef CONFIG_P2P
3188 if (elems.p2p) {
3189 wpabuf_free(sta->p2p_ie);
3190 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
3191 P2P_IE_VENDOR_TYPE);
3192 if (sta->p2p_ie)
3193 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
3194 } else {
3195 wpabuf_free(sta->p2p_ie);
3196 sta->p2p_ie = NULL;
3197 }
3198#endif /* CONFIG_P2P */
3199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
3201 wpa_ie = elems.rsn_ie;
3202 wpa_ie_len = elems.rsn_ie_len;
3203 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
3204 elems.wpa_ie) {
3205 wpa_ie = elems.wpa_ie;
3206 wpa_ie_len = elems.wpa_ie_len;
3207 } else {
3208 wpa_ie = NULL;
3209 wpa_ie_len = 0;
3210 }
3211
3212#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003213 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 if (hapd->conf->wps_state && elems.wps_ie) {
3215 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
3216 "Request - assume WPS is used");
3217 sta->flags |= WLAN_STA_WPS;
3218 wpabuf_free(sta->wps_ie);
3219 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
3220 WPS_IE_VENDOR_TYPE);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003221 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
3222 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
3223 sta->flags |= WLAN_STA_WPS2;
3224 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 wpa_ie = NULL;
3226 wpa_ie_len = 0;
3227 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
3228 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
3229 "(Re)Association Request - reject");
3230 return WLAN_STATUS_INVALID_IE;
3231 }
3232 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
3233 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
3234 "(Re)Association Request - possible WPS use");
3235 sta->flags |= WLAN_STA_MAYBE_WPS;
3236 } else
3237#endif /* CONFIG_WPS */
3238 if (hapd->conf->wpa && wpa_ie == NULL) {
3239 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3240 HOSTAPD_LEVEL_INFO,
3241 "No WPA/RSN IE in association request");
3242 return WLAN_STATUS_INVALID_IE;
3243 }
3244
3245 if (hapd->conf->wpa && wpa_ie) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003246 enum wpa_validate_result res;
3247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 wpa_ie -= 2;
3249 wpa_ie_len += 2;
3250 if (sta->wpa_sm == NULL)
3251 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003252 sta->addr,
3253 p2p_dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254 if (sta->wpa_sm == NULL) {
3255 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
3256 "state machine");
3257 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3258 }
Hai Shalom021b0b52019-04-10 11:17:58 -07003259 wpa_auth_set_auth_alg(sta->wpa_sm, sta->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
Hai Shalom021b0b52019-04-10 11:17:58 -07003261 hapd->iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 wpa_ie, wpa_ie_len,
Hai Shalomc3565922019-10-28 11:58:20 -07003263 elems.rsnxe ? elems.rsnxe - 2 : NULL,
3264 elems.rsnxe ? elems.rsnxe_len + 2 : 0,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003265 elems.mdie, elems.mdie_len,
3266 elems.owe_dh, elems.owe_dh_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003267 resp = wpa_res_to_status_code(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 if (resp != WLAN_STATUS_SUCCESS)
3269 return resp;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003270 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
3271 (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
3272 !sta->sa_query_timed_out &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003273 sta->sa_query_count > 0)
3274 ap_check_sa_query_timeout(hapd, sta);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003275 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
3276 (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
3277 !sta->sa_query_timed_out &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
3279 /*
3280 * STA has already been associated with MFP and SA
3281 * Query timeout has not been reached. Reject the
3282 * association attempt temporarily and start SA Query,
3283 * if one is not pending.
3284 */
3285
3286 if (sta->sa_query_count == 0)
3287 ap_sta_start_sa_query(hapd, sta);
3288
3289 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
3290 }
3291
3292 if (wpa_auth_uses_mfp(sta->wpa_sm))
3293 sta->flags |= WLAN_STA_MFP;
3294 else
3295 sta->flags &= ~WLAN_STA_MFP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003297#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 if (sta->auth_alg == WLAN_AUTH_FT) {
3299 if (!reassoc) {
3300 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
3301 "to use association (not "
3302 "re-association) with FT auth_alg",
3303 MAC2STR(sta->addr));
3304 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3305 }
3306
3307 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
3308 ies_len);
3309 if (resp != WLAN_STATUS_SUCCESS)
3310 return resp;
3311 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003312#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003314#ifdef CONFIG_SAE
Roshan Pius3a1667e2018-07-03 15:17:14 -07003315 if (wpa_auth_uses_sae(sta->wpa_sm) && sta->sae &&
3316 sta->sae->state == SAE_ACCEPTED)
3317 wpa_auth_add_sae_pmkid(sta->wpa_sm, sta->sae->pmkid);
3318
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003319 if (wpa_auth_uses_sae(sta->wpa_sm) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003320 sta->auth_alg == WLAN_AUTH_OPEN) {
3321 struct rsn_pmksa_cache_entry *sa;
3322 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
3323 if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
3324 wpa_printf(MSG_DEBUG,
3325 "SAE: No PMKSA cache entry found for "
3326 MACSTR, MAC2STR(sta->addr));
3327 return WLAN_STATUS_INVALID_PMKID;
3328 }
3329 wpa_printf(MSG_DEBUG, "SAE: " MACSTR
3330 " using PMKSA caching", MAC2STR(sta->addr));
3331 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
3332 sta->auth_alg != WLAN_AUTH_SAE &&
3333 !(sta->auth_alg == WLAN_AUTH_FT &&
3334 wpa_auth_uses_ft_sae(sta->wpa_sm))) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003335 wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
3336 "SAE AKM after non-SAE auth_alg %u",
3337 MAC2STR(sta->addr), sta->auth_alg);
3338 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
3339 }
Hai Shalomc3565922019-10-28 11:58:20 -07003340
3341 if (hapd->conf->sae_pwe == 2 &&
3342 sta->auth_alg == WLAN_AUTH_SAE &&
3343 sta->sae && sta->sae->tmp && !sta->sae->tmp->h2e &&
3344 elems.rsnxe && elems.rsnxe_len >= 1 &&
3345 (elems.rsnxe[0] & BIT(WLAN_RSNX_CAPAB_SAE_H2E))) {
3346 wpa_printf(MSG_INFO, "SAE: " MACSTR
3347 " indicates support for SAE H2E, but did not use it",
3348 MAC2STR(sta->addr));
3349 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3350 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003351#endif /* CONFIG_SAE */
3352
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003353#ifdef CONFIG_OWE
3354 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3355 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
3356 elems.owe_dh) {
3357 resp = owe_process_assoc_req(hapd, sta, elems.owe_dh,
3358 elems.owe_dh_len);
3359 if (resp != WLAN_STATUS_SUCCESS)
3360 return resp;
3361 }
3362#endif /* CONFIG_OWE */
3363
Hai Shalom021b0b52019-04-10 11:17:58 -07003364#ifdef CONFIG_DPP2
3365 dpp_pfs_free(sta->dpp_pfs);
3366 sta->dpp_pfs = NULL;
3367
3368 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
3369 hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
3370 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
3371 elems.owe_dh) {
3372 sta->dpp_pfs = dpp_pfs_init(
3373 wpabuf_head(hapd->conf->dpp_netaccesskey),
3374 wpabuf_len(hapd->conf->dpp_netaccesskey));
3375 if (!sta->dpp_pfs) {
3376 wpa_printf(MSG_DEBUG,
3377 "DPP: Could not initialize PFS");
3378 /* Try to continue without PFS */
3379 goto pfs_fail;
3380 }
3381
3382 if (dpp_pfs_process(sta->dpp_pfs, elems.owe_dh,
3383 elems.owe_dh_len) < 0) {
3384 dpp_pfs_free(sta->dpp_pfs);
3385 sta->dpp_pfs = NULL;
3386 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3387 }
3388 }
3389
3390 wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
3391 sta->dpp_pfs->secret : NULL);
3392 pfs_fail:
3393#endif /* CONFIG_DPP2 */
3394
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003395 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
3397 hostapd_logger(hapd, sta->addr,
3398 HOSTAPD_MODULE_IEEE80211,
3399 HOSTAPD_LEVEL_INFO,
3400 "Station tried to use TKIP with HT "
3401 "association");
3402 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
3403 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003404#ifdef CONFIG_HS20
3405 } else if (hapd->conf->osen) {
3406 if (elems.osen == NULL) {
3407 hostapd_logger(
3408 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3409 HOSTAPD_LEVEL_INFO,
3410 "No HS 2.0 OSEN element in association request");
3411 return WLAN_STATUS_INVALID_IE;
3412 }
3413
3414 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
3415 if (sta->wpa_sm == NULL)
3416 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
3417 sta->addr, NULL);
3418 if (sta->wpa_sm == NULL) {
3419 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
3420 "state machine");
3421 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3422 }
3423 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
3424 elems.osen - 2, elems.osen_len + 2) < 0)
3425 return WLAN_STATUS_INVALID_IE;
3426#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427 } else
3428 wpa_auth_sta_no_wpa(sta->wpa_sm);
3429
3430#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
3432#endif /* CONFIG_P2P */
3433
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003434#ifdef CONFIG_HS20
3435 wpabuf_free(sta->hs20_ie);
3436 if (elems.hs20 && elems.hs20_len > 4) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003437 int release;
3438
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003439 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
3440 elems.hs20_len - 4);
Hai Shalom74f70d42019-02-11 14:42:39 -08003441 release = ((elems.hs20[4] >> 4) & 0x0f) + 1;
Hai Shalomc3565922019-10-28 11:58:20 -07003442 if (release >= 2 && !wpa_auth_uses_mfp(sta->wpa_sm) &&
3443 hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003444 wpa_printf(MSG_DEBUG,
3445 "HS 2.0: PMF not negotiated by release %d station "
3446 MACSTR, release, MAC2STR(sta->addr));
3447 return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
3448 }
3449 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003450 sta->hs20_ie = NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08003451 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003452
3453 wpabuf_free(sta->roaming_consortium);
3454 if (elems.roaming_cons_sel)
3455 sta->roaming_consortium = wpabuf_alloc_copy(
3456 elems.roaming_cons_sel + 4,
3457 elems.roaming_cons_sel_len - 4);
3458 else
3459 sta->roaming_consortium = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003460#endif /* CONFIG_HS20 */
3461
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003462#ifdef CONFIG_FST
3463 wpabuf_free(sta->mb_ies);
3464 if (hapd->iface->fst)
3465 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
3466 else
3467 sta->mb_ies = NULL;
3468#endif /* CONFIG_FST */
3469
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003470#ifdef CONFIG_MBO
3471 mbo_ap_check_sta_assoc(hapd, sta, &elems);
3472
3473 if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
3474 elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
3475 hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3476 wpa_printf(MSG_INFO,
3477 "MBO: Reject WPA2 association without PMF");
3478 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3479 }
3480#endif /* CONFIG_MBO */
3481
Hai Shalom74f70d42019-02-11 14:42:39 -08003482#if defined(CONFIG_FILS) && defined(CONFIG_OCV)
3483 if (wpa_auth_uses_ocv(sta->wpa_sm) &&
3484 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3485 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3486 sta->auth_alg == WLAN_AUTH_FILS_PK)) {
3487 struct wpa_channel_info ci;
3488 int tx_chanwidth;
3489 int tx_seg1_idx;
3490
3491 if (hostapd_drv_channel_info(hapd, &ci) != 0) {
3492 wpa_printf(MSG_WARNING,
3493 "Failed to get channel info to validate received OCI in FILS (Re)Association Request frame");
3494 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3495 }
3496
3497 if (get_sta_tx_parameters(sta->wpa_sm,
3498 channel_width_to_int(ci.chanwidth),
3499 ci.seg1_idx, &tx_chanwidth,
3500 &tx_seg1_idx) < 0)
3501 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3502
3503 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
3504 tx_chanwidth, tx_seg1_idx) != 0) {
3505 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
3506 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3507 }
3508 }
3509#endif /* CONFIG_FILS && CONFIG_OCV */
3510
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003511 ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
3512 elems.supp_op_classes_len);
3513
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003514 if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
3515 elems.rrm_enabled &&
3516 elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
3517 os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
3518 sizeof(sta->rrm_enabled_capa));
3519
Roshan Pius3a1667e2018-07-03 15:17:14 -07003520 if (elems.power_capab) {
3521 sta->min_tx_power = elems.power_capab[0];
3522 sta->max_tx_power = elems.power_capab[1];
3523 sta->power_capab = 1;
3524 } else {
3525 sta->power_capab = 0;
3526 }
3527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528 return WLAN_STATUS_SUCCESS;
3529}
3530
3531
3532static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
3533 u16 reason_code)
3534{
3535 int send_len;
3536 struct ieee80211_mgmt reply;
3537
3538 os_memset(&reply, 0, sizeof(reply));
3539 reply.frame_control =
3540 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
3541 os_memcpy(reply.da, addr, ETH_ALEN);
3542 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
3543 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
3544
3545 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
3546 reply.u.deauth.reason_code = host_to_le16(reason_code);
3547
Hai Shalomfdcde762020-04-02 11:19:20 -07003548 if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0, NULL, 0, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003549 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
3550 strerror(errno));
3551}
3552
3553
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003554static int add_associated_sta(struct hostapd_data *hapd,
Hai Shalom74f70d42019-02-11 14:42:39 -08003555 struct sta_info *sta, int reassoc)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003556{
3557 struct ieee80211_ht_capabilities ht_cap;
3558 struct ieee80211_vht_capabilities vht_cap;
Hai Shalom81f62d82019-07-22 12:10:00 -07003559 struct ieee80211_he_capabilities he_cap;
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003560 int set = 1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003561
3562 /*
3563 * Remove the STA entry to ensure the STA PS state gets cleared and
3564 * configuration gets updated. This is relevant for cases, such as
3565 * FT-over-the-DS, where a station re-associates back to the same AP but
3566 * skips the authentication flow, or if working with a driver that
3567 * does not support full AP client state.
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003568 *
3569 * Skip this if the STA has already completed FT reassociation and the
3570 * TK has been configured since the TX/RX PN must not be reset to 0 for
3571 * the same key.
Hai Shalom74f70d42019-02-11 14:42:39 -08003572 *
3573 * FT-over-the-DS has a special case where the STA entry (and as such,
3574 * the TK) has not yet been configured to the driver depending on which
3575 * driver interface is used. For that case, allow add-STA operation to
3576 * be used (instead of set-STA). This is needed to allow mac80211-based
3577 * drivers to accept the STA parameter configuration. Since this is
3578 * after a new FT-over-DS exchange, a new TK has been derived, so key
3579 * reinstallation is not a concern for this case.
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003580 */
Hai Shalom74f70d42019-02-11 14:42:39 -08003581 wpa_printf(MSG_DEBUG, "Add associated STA " MACSTR
3582 " (added_unassoc=%d auth_alg=%u ft_over_ds=%u reassoc=%d authorized=%d ft_tk=%d fils_tk=%d)",
3583 MAC2STR(sta->addr), sta->added_unassoc, sta->auth_alg,
3584 sta->ft_over_ds, reassoc,
3585 !!(sta->flags & WLAN_STA_AUTHORIZED),
3586 wpa_auth_sta_ft_tk_already_set(sta->wpa_sm),
3587 wpa_auth_sta_fils_tk_already_set(sta->wpa_sm));
3588
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003589 if (!sta->added_unassoc &&
3590 (!(sta->flags & WLAN_STA_AUTHORIZED) ||
Hai Shalom74f70d42019-02-11 14:42:39 -08003591 (reassoc && sta->ft_over_ds && sta->auth_alg == WLAN_AUTH_FT) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003592 (!wpa_auth_sta_ft_tk_already_set(sta->wpa_sm) &&
3593 !wpa_auth_sta_fils_tk_already_set(sta->wpa_sm)))) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003594 hostapd_drv_sta_remove(hapd, sta->addr);
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003595 wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
3596 set = 0;
Hai Shalom74f70d42019-02-11 14:42:39 -08003597
3598 /* Do not allow the FT-over-DS exception to be used more than
3599 * once per authentication exchange to guarantee a new TK is
3600 * used here */
3601 sta->ft_over_ds = 0;
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003602 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003603
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003604 if (sta->flags & WLAN_STA_HT)
3605 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003606#ifdef CONFIG_IEEE80211AC
3607 if (sta->flags & WLAN_STA_VHT)
3608 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
3609#endif /* CONFIG_IEEE80211AC */
Hai Shalom81f62d82019-07-22 12:10:00 -07003610#ifdef CONFIG_IEEE80211AX
3611 if (sta->flags & WLAN_STA_HE) {
3612 hostapd_get_he_capab(hapd, sta->he_capab, &he_cap,
3613 sta->he_capab_len);
3614 }
3615#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003616
3617 /*
3618 * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
3619 * will be set when the ACK frame for the (Re)Association Response frame
3620 * is processed (TX status driver event).
3621 */
3622 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
3623 sta->supported_rates, sta->supported_rates_len,
3624 sta->listen_interval,
3625 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
3626 sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
Hai Shalom81f62d82019-07-22 12:10:00 -07003627 sta->flags & WLAN_STA_HE ? &he_cap : NULL,
3628 sta->flags & WLAN_STA_HE ? sta->he_capab_len : 0,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003629 sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003630 sta->vht_opmode, sta->p2p_ie ? 1 : 0,
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003631 set)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003632 hostapd_logger(hapd, sta->addr,
3633 HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
3634 "Could not %s STA to kernel driver",
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003635 set ? "set" : "add");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003636
3637 if (sta->added_unassoc) {
3638 hostapd_drv_sta_remove(hapd, sta->addr);
3639 sta->added_unassoc = 0;
3640 }
3641
3642 return -1;
3643 }
3644
3645 sta->added_unassoc = 0;
3646
3647 return 0;
3648}
3649
3650
3651static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
Dmitry Shmidt29333592017-01-09 12:27:11 -08003652 const u8 *addr, u16 status_code, int reassoc,
Hai Shalomfdcde762020-04-02 11:19:20 -07003653 const u8 *ies, size_t ies_len, int rssi,
3654 int omit_rsnxe)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655{
3656 int send_len;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003657 u8 *buf;
3658 size_t buflen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 struct ieee80211_mgmt *reply;
3660 u8 *p;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003661 u16 res = WLAN_STATUS_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003663 buflen = sizeof(struct ieee80211_mgmt) + 1024;
3664#ifdef CONFIG_FILS
3665 if (sta && sta->fils_hlp_resp)
3666 buflen += wpabuf_len(sta->fils_hlp_resp);
Hai Shalom81f62d82019-07-22 12:10:00 -07003667 if (sta)
3668 buflen += 150;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003669#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003670#ifdef CONFIG_OWE
3671 if (sta && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
3672 buflen += 150;
3673#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07003674#ifdef CONFIG_DPP2
3675 if (sta && sta->dpp_pfs)
3676 buflen += 5 + sta->dpp_pfs->curve->prime_len;
3677#endif /* CONFIG_DPP2 */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003678 buf = os_zalloc(buflen);
3679 if (!buf) {
3680 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3681 goto done;
3682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683 reply = (struct ieee80211_mgmt *) buf;
3684 reply->frame_control =
3685 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
3686 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
3687 WLAN_FC_STYPE_ASSOC_RESP));
Dmitry Shmidt29333592017-01-09 12:27:11 -08003688 os_memcpy(reply->da, addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
3690 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
3691
3692 send_len = IEEE80211_HDRLEN;
3693 send_len += sizeof(reply->u.assoc_resp);
3694 reply->u.assoc_resp.capab_info =
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003695 host_to_le16(hostapd_own_capab_info(hapd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 reply->u.assoc_resp.status_code = host_to_le16(status_code);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003697
3698 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0) |
3699 BIT(14) | BIT(15));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 /* Supported rates */
3701 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
3702 /* Extended supported rates */
3703 p = hostapd_eid_ext_supp_rates(hapd, p);
3704
Hai Shalomfdcde762020-04-02 11:19:20 -07003705 /* Radio measurement capabilities */
3706 p = hostapd_eid_rm_enabled_capab(hapd, p, buf + buflen - p);
3707
Hai Shalom74f70d42019-02-11 14:42:39 -08003708#ifdef CONFIG_MBO
3709 if (status_code == WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
3710 rssi != 0) {
3711 int delta = hapd->iconf->rssi_reject_assoc_rssi - rssi;
3712
3713 p = hostapd_eid_mbo_rssi_assoc_rej(hapd, p, buf + buflen - p,
3714 delta);
3715 }
3716#endif /* CONFIG_MBO */
3717
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003718#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt29333592017-01-09 12:27:11 -08003719 if (sta && status_code == WLAN_STATUS_SUCCESS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
3721 * Transition Information, RSN, [RIC Response] */
3722 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003723 buf + buflen - p,
Hai Shalomfdcde762020-04-02 11:19:20 -07003724 sta->auth_alg, ies, ies_len,
3725 omit_rsnxe);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003726 if (!p) {
3727 wpa_printf(MSG_DEBUG,
3728 "FT: Failed to write AssocResp IEs");
3729 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3730 goto done;
3731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003733#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom81f62d82019-07-22 12:10:00 -07003734#ifdef CONFIG_FILS
3735 if (sta && status_code == WLAN_STATUS_SUCCESS &&
3736 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3737 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3738 sta->auth_alg == WLAN_AUTH_FILS_PK))
3739 p = wpa_auth_write_assoc_resp_fils(sta->wpa_sm, p,
3740 buf + buflen - p,
3741 ies, ies_len);
3742#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003744#ifdef CONFIG_OWE
Hai Shalom74f70d42019-02-11 14:42:39 -08003745 if (sta && status_code == WLAN_STATUS_SUCCESS &&
3746 (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003747 p = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, p,
3748 buf + buflen - p,
3749 ies, ies_len);
3750#endif /* CONFIG_OWE */
3751
Dmitry Shmidt29333592017-01-09 12:27:11 -08003752 if (sta && status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 p = hostapd_eid_ht_capabilities(hapd, p);
3756 p = hostapd_eid_ht_operation(hapd, p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003758#ifdef CONFIG_IEEE80211AC
Hai Shalomc3565922019-10-28 11:58:20 -07003759 if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac &&
3760 !is_6ghz_op_class(hapd->iconf->op_class)) {
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003761 u32 nsts = 0, sta_nsts;
3762
Dmitry Shmidt29333592017-01-09 12:27:11 -08003763 if (sta && hapd->conf->use_sta_nsts && sta->vht_capabilities) {
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003764 struct ieee80211_vht_capabilities *capa;
3765
3766 nsts = (hapd->iface->conf->vht_capab >>
3767 VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
3768 capa = sta->vht_capabilities;
3769 sta_nsts = (le_to_host32(capa->vht_capabilities_info) >>
3770 VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
3771
3772 if (nsts < sta_nsts)
3773 nsts = 0;
3774 else
3775 nsts = sta_nsts;
3776 }
3777 p = hostapd_eid_vht_capabilities(hapd, p, nsts);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003778 p = hostapd_eid_vht_operation(hapd, p);
3779 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003780#endif /* CONFIG_IEEE80211AC */
3781
Hai Shalom81f62d82019-07-22 12:10:00 -07003782#ifdef CONFIG_IEEE80211AX
3783 if (hapd->iconf->ieee80211ax) {
3784 p = hostapd_eid_he_capab(hapd, p, IEEE80211_MODE_AP);
3785 p = hostapd_eid_he_operation(hapd, p);
3786 p = hostapd_eid_spatial_reuse(hapd, p);
3787 p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
3788 }
3789#endif /* CONFIG_IEEE80211AX */
3790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 p = hostapd_eid_ext_capab(hapd, p);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003792 p = hostapd_eid_bss_max_idle_period(hapd, p);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003793 if (sta && sta->qos_map_enabled)
Dmitry Shmidt051af732013-10-22 13:52:46 -07003794 p = hostapd_eid_qos_map_set(hapd, p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003796#ifdef CONFIG_FST
3797 if (hapd->iface->fst_ies) {
3798 os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
3799 wpabuf_len(hapd->iface->fst_ies));
3800 p += wpabuf_len(hapd->iface->fst_ies);
3801 }
3802#endif /* CONFIG_FST */
3803
Hai Shalomfdcde762020-04-02 11:19:20 -07003804#ifdef CONFIG_TESTING_OPTIONS
3805 if (hapd->conf->rsnxe_override_ft &&
3806 buf + buflen - p >=
3807 (long int) wpabuf_len(hapd->conf->rsnxe_override_ft) &&
3808 sta && sta->auth_alg == WLAN_AUTH_FT) {
3809 wpa_printf(MSG_DEBUG, "TESTING: RSNXE FT override");
3810 os_memcpy(p, wpabuf_head(hapd->conf->rsnxe_override_ft),
3811 wpabuf_len(hapd->conf->rsnxe_override_ft));
3812 p += wpabuf_len(hapd->conf->rsnxe_override_ft);
3813 goto rsnxe_done;
3814 }
3815#endif /* CONFIG_TESTING_OPTIONS */
3816 if (!omit_rsnxe)
3817 p = hostapd_eid_rsnxe(hapd, p, buf + buflen - p);
3818#ifdef CONFIG_TESTING_OPTIONS
3819rsnxe_done:
3820#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalomc3565922019-10-28 11:58:20 -07003821
Hai Shalom021b0b52019-04-10 11:17:58 -07003822#ifdef CONFIG_OWE
3823 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3824 sta && sta->owe_ecdh && status_code == WLAN_STATUS_SUCCESS &&
3825 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE) {
3826 struct wpabuf *pub;
3827
3828 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3829 if (!pub) {
3830 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3831 goto done;
3832 }
3833 /* OWE Diffie-Hellman Parameter element */
3834 *p++ = WLAN_EID_EXTENSION; /* Element ID */
3835 *p++ = 1 + 2 + wpabuf_len(pub); /* Length */
3836 *p++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension */
3837 WPA_PUT_LE16(p, sta->owe_group);
3838 p += 2;
3839 os_memcpy(p, wpabuf_head(pub), wpabuf_len(pub));
3840 p += wpabuf_len(pub);
3841 wpabuf_free(pub);
3842 }
3843#endif /* CONFIG_OWE */
3844
3845#ifdef CONFIG_DPP2
3846 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
3847 sta && sta->dpp_pfs && status_code == WLAN_STATUS_SUCCESS &&
3848 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP) {
3849 os_memcpy(p, wpabuf_head(sta->dpp_pfs->ie),
3850 wpabuf_len(sta->dpp_pfs->ie));
3851 p += wpabuf_len(sta->dpp_pfs->ie);
3852 }
3853#endif /* CONFIG_DPP2 */
3854
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003855#ifdef CONFIG_IEEE80211AC
Dmitry Shmidt29333592017-01-09 12:27:11 -08003856 if (sta && hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003857 p = hostapd_eid_vendor_vht(hapd, p);
3858#endif /* CONFIG_IEEE80211AC */
3859
Dmitry Shmidt29333592017-01-09 12:27:11 -08003860 if (sta && (sta->flags & WLAN_STA_WMM))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 p = hostapd_eid_wmm(hapd, p);
3862
3863#ifdef CONFIG_WPS
Dmitry Shmidt29333592017-01-09 12:27:11 -08003864 if (sta &&
3865 ((sta->flags & WLAN_STA_WPS) ||
3866 ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 struct wpabuf *wps = wps_build_assoc_resp_ie();
3868 if (wps) {
3869 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
3870 p += wpabuf_len(wps);
3871 wpabuf_free(wps);
3872 }
3873 }
3874#endif /* CONFIG_WPS */
3875
Hai Shalom74f70d42019-02-11 14:42:39 -08003876 if (sta && (sta->flags & WLAN_STA_MULTI_AP))
3877 p = hostapd_eid_multi_ap(hapd, p);
3878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879#ifdef CONFIG_P2P
Dmitry Shmidt29333592017-01-09 12:27:11 -08003880 if (sta && sta->p2p_ie && hapd->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003881 struct wpabuf *p2p_resp_ie;
3882 enum p2p_status_code status;
3883 switch (status_code) {
3884 case WLAN_STATUS_SUCCESS:
3885 status = P2P_SC_SUCCESS;
3886 break;
3887 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
3888 status = P2P_SC_FAIL_LIMIT_REACHED;
3889 break;
3890 default:
3891 status = P2P_SC_FAIL_INVALID_PARAMS;
3892 break;
3893 }
3894 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
3895 if (p2p_resp_ie) {
3896 os_memcpy(p, wpabuf_head(p2p_resp_ie),
3897 wpabuf_len(p2p_resp_ie));
3898 p += wpabuf_len(p2p_resp_ie);
3899 wpabuf_free(p2p_resp_ie);
3900 }
3901 }
3902#endif /* CONFIG_P2P */
3903
3904#ifdef CONFIG_P2P_MANAGER
3905 if (hapd->conf->p2p & P2P_MANAGE)
3906 p = hostapd_eid_p2p_manage(hapd, p);
3907#endif /* CONFIG_P2P_MANAGER */
3908
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003909 p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003910
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003911 if (hapd->conf->assocresp_elements &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003912 (size_t) (buf + buflen - p) >=
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003913 wpabuf_len(hapd->conf->assocresp_elements)) {
3914 os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
3915 wpabuf_len(hapd->conf->assocresp_elements));
3916 p += wpabuf_len(hapd->conf->assocresp_elements);
3917 }
3918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003919 send_len += p - reply->u.assoc_resp.variable;
3920
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003921#ifdef CONFIG_FILS
Dmitry Shmidt29333592017-01-09 12:27:11 -08003922 if (sta &&
3923 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003924 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3925 sta->auth_alg == WLAN_AUTH_FILS_PK) &&
3926 status_code == WLAN_STATUS_SUCCESS) {
3927 struct ieee802_11_elems elems;
3928
3929 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003930 ParseFailed || !elems.fils_session) {
3931 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3932 goto done;
3933 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003934
3935 /* FILS Session */
3936 *p++ = WLAN_EID_EXTENSION; /* Element ID */
3937 *p++ = 1 + FILS_SESSION_LEN; /* Length */
3938 *p++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
3939 os_memcpy(p, elems.fils_session, FILS_SESSION_LEN);
3940 send_len += 2 + 1 + FILS_SESSION_LEN;
3941
3942 send_len = fils_encrypt_assoc(sta->wpa_sm, buf, send_len,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003943 buflen, sta->fils_hlp_resp);
3944 if (send_len < 0) {
3945 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3946 goto done;
3947 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003948 }
3949#endif /* CONFIG_FILS */
3950
Hai Shalomfdcde762020-04-02 11:19:20 -07003951 if (hostapd_drv_send_mlme(hapd, reply, send_len, 0, NULL, 0, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
3953 strerror(errno));
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003954 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003955 }
3956
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003957done:
3958 os_free(buf);
3959 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960}
3961
3962
Roshan Pius3a1667e2018-07-03 15:17:14 -07003963#ifdef CONFIG_OWE
3964u8 * owe_assoc_req_process(struct hostapd_data *hapd, struct sta_info *sta,
3965 const u8 *owe_dh, u8 owe_dh_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07003966 u8 *owe_buf, size_t owe_buf_len, u16 *status)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003967{
3968#ifdef CONFIG_TESTING_OPTIONS
3969 if (hapd->conf->own_ie_override) {
3970 wpa_printf(MSG_DEBUG, "OWE: Using IE override");
Hai Shalomfdcde762020-04-02 11:19:20 -07003971 *status = WLAN_STATUS_SUCCESS;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003972 return wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3973 owe_buf_len, NULL, 0);
3974 }
3975#endif /* CONFIG_TESTING_OPTIONS */
3976
3977 if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
3978 wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
3979 owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3980 owe_buf_len, NULL, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -07003981 *status = WLAN_STATUS_SUCCESS;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003982 return owe_buf;
3983 }
3984
Hai Shalom81f62d82019-07-22 12:10:00 -07003985 if (sta->owe_pmk && sta->external_dh_updated) {
3986 wpa_printf(MSG_DEBUG, "OWE: Using previously derived PMK");
Hai Shalomfdcde762020-04-02 11:19:20 -07003987 *status = WLAN_STATUS_SUCCESS;
Hai Shalom81f62d82019-07-22 12:10:00 -07003988 return owe_buf;
3989 }
3990
Hai Shalomfdcde762020-04-02 11:19:20 -07003991 *status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
3992 if (*status != WLAN_STATUS_SUCCESS)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003993 return NULL;
3994
3995 owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3996 owe_buf_len, NULL, 0);
3997
3998 if (sta->owe_ecdh && owe_buf) {
3999 struct wpabuf *pub;
4000
4001 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
4002 if (!pub) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004003 *status = WLAN_STATUS_UNSPECIFIED_FAILURE;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004004 return owe_buf;
4005 }
4006
4007 /* OWE Diffie-Hellman Parameter element */
4008 *owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
4009 *owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
4010 *owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
4011 */
4012 WPA_PUT_LE16(owe_buf, sta->owe_group);
4013 owe_buf += 2;
4014 os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
4015 owe_buf += wpabuf_len(pub);
4016 wpabuf_free(pub);
4017 }
4018
4019 return owe_buf;
4020}
4021#endif /* CONFIG_OWE */
4022
4023
Paul Stewart092955c2017-02-06 09:13:09 -08004024#ifdef CONFIG_FILS
4025
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004026void fils_hlp_finish_assoc(struct hostapd_data *hapd, struct sta_info *sta)
Paul Stewart092955c2017-02-06 09:13:09 -08004027{
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004028 u16 reply_res;
Paul Stewart092955c2017-02-06 09:13:09 -08004029
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004030 wpa_printf(MSG_DEBUG, "FILS: Finish association with " MACSTR,
4031 MAC2STR(sta->addr));
4032 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4033 if (!sta->fils_pending_assoc_req)
Paul Stewart092955c2017-02-06 09:13:09 -08004034 return;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004035 reply_res = send_assoc_resp(hapd, sta, sta->addr, WLAN_STATUS_SUCCESS,
4036 sta->fils_pending_assoc_is_reassoc,
4037 sta->fils_pending_assoc_req,
Hai Shalomfdcde762020-04-02 11:19:20 -07004038 sta->fils_pending_assoc_req_len, 0, 0);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004039 os_free(sta->fils_pending_assoc_req);
4040 sta->fils_pending_assoc_req = NULL;
4041 sta->fils_pending_assoc_req_len = 0;
4042 wpabuf_free(sta->fils_hlp_resp);
4043 sta->fils_hlp_resp = NULL;
4044 wpabuf_free(sta->hlp_dhcp_discover);
4045 sta->hlp_dhcp_discover = NULL;
Paul Stewart092955c2017-02-06 09:13:09 -08004046
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004047 /*
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004048 * Remove the station in case transmission of a success response fails.
4049 * At this point the station was already added associated to the driver.
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004050 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004051 if (reply_res != WLAN_STATUS_SUCCESS)
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004052 hostapd_drv_sta_remove(hapd, sta->addr);
Paul Stewart092955c2017-02-06 09:13:09 -08004053}
4054
4055
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004056void fils_hlp_timeout(void *eloop_ctx, void *eloop_data)
Paul Stewart092955c2017-02-06 09:13:09 -08004057{
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004058 struct hostapd_data *hapd = eloop_ctx;
4059 struct sta_info *sta = eloop_data;
Paul Stewart092955c2017-02-06 09:13:09 -08004060
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004061 wpa_printf(MSG_DEBUG,
4062 "FILS: HLP response timeout - continue with association response for "
4063 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004064 if (sta->fils_drv_assoc_finish)
4065 hostapd_notify_assoc_fils_finish(hapd, sta);
4066 else
4067 fils_hlp_finish_assoc(hapd, sta);
Paul Stewart092955c2017-02-06 09:13:09 -08004068}
4069
4070#endif /* CONFIG_FILS */
4071
4072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073static void handle_assoc(struct hostapd_data *hapd,
4074 const struct ieee80211_mgmt *mgmt, size_t len,
Hai Shalom74f70d42019-02-11 14:42:39 -08004075 int reassoc, int rssi)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004077 u16 capab_info, listen_interval, seq_ctrl, fc;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004078 int resp = WLAN_STATUS_SUCCESS;
4079 u16 reply_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080 const u8 *pos;
4081 int left, i;
4082 struct sta_info *sta;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004083 u8 *tmp = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004084#ifdef CONFIG_FILS
4085 int delay_assoc = 0;
4086#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07004087 int omit_rsnxe = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088
4089 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
4090 sizeof(mgmt->u.assoc_req))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004091 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
4092 reassoc, (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 return;
4094 }
4095
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004096#ifdef CONFIG_TESTING_OPTIONS
4097 if (reassoc) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004098 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004099 drand48() < hapd->iconf->ignore_reassoc_probability) {
4100 wpa_printf(MSG_INFO,
4101 "TESTING: ignoring reassoc request from "
4102 MACSTR, MAC2STR(mgmt->sa));
4103 return;
4104 }
4105 } else {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004106 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004107 drand48() < hapd->iconf->ignore_assoc_probability) {
4108 wpa_printf(MSG_INFO,
4109 "TESTING: ignoring assoc request from "
4110 MACSTR, MAC2STR(mgmt->sa));
4111 return;
4112 }
4113 }
4114#endif /* CONFIG_TESTING_OPTIONS */
4115
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004116 fc = le_to_host16(mgmt->frame_control);
4117 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 if (reassoc) {
4120 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
4121 listen_interval = le_to_host16(
4122 mgmt->u.reassoc_req.listen_interval);
4123 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
4124 " capab_info=0x%02x listen_interval=%d current_ap="
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004125 MACSTR " seq_ctrl=0x%x%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 MAC2STR(mgmt->sa), capab_info, listen_interval,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004127 MAC2STR(mgmt->u.reassoc_req.current_ap),
4128 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
4130 pos = mgmt->u.reassoc_req.variable;
4131 } else {
4132 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
4133 listen_interval = le_to_host16(
4134 mgmt->u.assoc_req.listen_interval);
4135 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004136 " capab_info=0x%02x listen_interval=%d "
4137 "seq_ctrl=0x%x%s",
4138 MAC2STR(mgmt->sa), capab_info, listen_interval,
4139 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004140 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
4141 pos = mgmt->u.assoc_req.variable;
4142 }
4143
4144 sta = ap_get_sta(hapd, mgmt->sa);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004145#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
4147 (sta->flags & WLAN_STA_AUTH) == 0) {
4148 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
4149 "prior to authentication since it is using "
4150 "over-the-DS FT", MAC2STR(mgmt->sa));
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004151
4152 /*
4153 * Mark station as authenticated, to avoid adding station
4154 * entry in the driver as associated and not authenticated
4155 */
4156 sta->flags |= WLAN_STA_AUTH;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 } else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004158#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
Dmitry Shmidt29333592017-01-09 12:27:11 -08004160 if (hapd->iface->current_mode &&
4161 hapd->iface->current_mode->mode ==
4162 HOSTAPD_MODE_IEEE80211AD) {
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004163 int acl_res;
Hai Shalomfdcde762020-04-02 11:19:20 -07004164 struct radius_sta info;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004165
Hai Shalomfdcde762020-04-02 11:19:20 -07004166 acl_res = ieee802_11_allowed_address(hapd, mgmt->sa,
4167 (const u8 *) mgmt,
4168 len, &info);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004169 if (acl_res == HOSTAPD_ACL_REJECT) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004170 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
4171 "Ignore Association Request frame from "
4172 MACSTR " due to ACL reject",
4173 MAC2STR(mgmt->sa));
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004174 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4175 goto fail;
4176 }
4177 if (acl_res == HOSTAPD_ACL_PENDING)
4178 return;
4179
Dmitry Shmidt29333592017-01-09 12:27:11 -08004180 /* DMG/IEEE 802.11ad does not use authentication.
4181 * Allocate sta entry upon association. */
4182 sta = ap_sta_add(hapd, mgmt->sa);
4183 if (!sta) {
4184 hostapd_logger(hapd, mgmt->sa,
4185 HOSTAPD_MODULE_IEEE80211,
4186 HOSTAPD_LEVEL_INFO,
4187 "Failed to add STA");
4188 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4189 goto fail;
4190 }
4191
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004192 acl_res = ieee802_11_set_radius_info(
Hai Shalomfdcde762020-04-02 11:19:20 -07004193 hapd, sta, acl_res, &info);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004194 if (acl_res) {
4195 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4196 goto fail;
4197 }
4198
Dmitry Shmidt29333592017-01-09 12:27:11 -08004199 hostapd_logger(hapd, sta->addr,
4200 HOSTAPD_MODULE_IEEE80211,
4201 HOSTAPD_LEVEL_DEBUG,
4202 "Skip authentication for DMG/IEEE 802.11ad");
4203 sta->flags |= WLAN_STA_AUTH;
4204 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
4205 sta->auth_alg = WLAN_AUTH_OPEN;
4206 } else {
4207 hostapd_logger(hapd, mgmt->sa,
4208 HOSTAPD_MODULE_IEEE80211,
4209 HOSTAPD_LEVEL_INFO,
4210 "Station tried to associate before authentication (aid=%d flags=0x%x)",
4211 sta ? sta->aid : -1,
4212 sta ? sta->flags : 0);
4213 send_deauth(hapd, mgmt->sa,
4214 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
4215 return;
4216 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004217 }
4218
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004219 if ((fc & WLAN_FC_RETRY) &&
4220 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4221 sta->last_seq_ctrl == seq_ctrl &&
Paul Stewart092955c2017-02-06 09:13:09 -08004222 sta->last_subtype == (reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
4223 WLAN_FC_STYPE_ASSOC_REQ)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004224 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4225 HOSTAPD_LEVEL_DEBUG,
4226 "Drop repeated association frame seq_ctrl=0x%x",
4227 seq_ctrl);
4228 return;
4229 }
4230 sta->last_seq_ctrl = seq_ctrl;
4231 sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
4232 WLAN_FC_STYPE_ASSOC_REQ;
4233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 if (hapd->tkip_countermeasures) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004235 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 goto fail;
4237 }
4238
4239 if (listen_interval > hapd->conf->max_listen_interval) {
4240 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4241 HOSTAPD_LEVEL_DEBUG,
4242 "Too large Listen Interval (%d)",
4243 listen_interval);
4244 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
4245 goto fail;
4246 }
4247
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004248#ifdef CONFIG_MBO
4249 if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
4250 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4251 goto fail;
4252 }
Hai Shalom74f70d42019-02-11 14:42:39 -08004253
4254 if (hapd->iconf->rssi_reject_assoc_rssi && rssi &&
4255 rssi < hapd->iconf->rssi_reject_assoc_rssi &&
4256 (sta->auth_rssi == 0 ||
4257 sta->auth_rssi < hapd->iconf->rssi_reject_assoc_rssi)) {
4258 resp = WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS;
4259 goto fail;
4260 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004261#endif /* CONFIG_MBO */
4262
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004263 /*
4264 * sta->capability is used in check_assoc_ies() for RRM enabled
4265 * capability element.
4266 */
4267 sta->capability = capab_info;
4268
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004269#ifdef CONFIG_FILS
4270 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4271 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4272 sta->auth_alg == WLAN_AUTH_FILS_PK) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004273 int res;
4274
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004275 /* The end of the payload is encrypted. Need to decrypt it
4276 * before parsing. */
4277
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004278 tmp = os_memdup(pos, left);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004279 if (!tmp) {
4280 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4281 goto fail;
4282 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004283
Roshan Pius3a1667e2018-07-03 15:17:14 -07004284 res = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
4285 len, tmp, left);
4286 if (res < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004287 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4288 goto fail;
4289 }
4290 pos = tmp;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004291 left = res;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004292 }
4293#endif /* CONFIG_FILS */
4294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
4296 * is used */
4297 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
4298 if (resp != WLAN_STATUS_SUCCESS)
4299 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07004300 omit_rsnxe = !get_ie(pos, left, WLAN_EID_RSNX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004301
4302 if (hostapd_get_aid(hapd, sta) < 0) {
4303 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4304 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
4305 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4306 goto fail;
4307 }
4308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 sta->listen_interval = listen_interval;
4310
Roshan Pius3a1667e2018-07-03 15:17:14 -07004311 if (hapd->iface->current_mode &&
4312 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313 sta->flags |= WLAN_STA_NONERP;
4314 for (i = 0; i < sta->supported_rates_len; i++) {
4315 if ((sta->supported_rates[i] & 0x7f) > 22) {
4316 sta->flags &= ~WLAN_STA_NONERP;
4317 break;
4318 }
4319 }
4320 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
4321 sta->nonerp_set = 1;
4322 hapd->iface->num_sta_non_erp++;
4323 if (hapd->iface->num_sta_non_erp == 1)
4324 ieee802_11_set_beacons(hapd->iface);
4325 }
4326
4327 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
4328 !sta->no_short_slot_time_set) {
4329 sta->no_short_slot_time_set = 1;
4330 hapd->iface->num_sta_no_short_slot_time++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004331 if (hapd->iface->current_mode &&
4332 hapd->iface->current_mode->mode ==
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333 HOSTAPD_MODE_IEEE80211G &&
4334 hapd->iface->num_sta_no_short_slot_time == 1)
4335 ieee802_11_set_beacons(hapd->iface);
4336 }
4337
4338 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4339 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
4340 else
4341 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
4342
4343 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
4344 !sta->no_short_preamble_set) {
4345 sta->no_short_preamble_set = 1;
4346 hapd->iface->num_sta_no_short_preamble++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004347 if (hapd->iface->current_mode &&
4348 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349 && hapd->iface->num_sta_no_short_preamble == 1)
4350 ieee802_11_set_beacons(hapd->iface);
4351 }
4352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353 update_ht_state(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004354
4355 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4356 HOSTAPD_LEVEL_DEBUG,
4357 "association OK (aid %d)", sta->aid);
4358 /* Station will be marked associated, after it acknowledges AssocResp
4359 */
4360 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
4361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
4363 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
4364 "SA Query procedure", reassoc ? "re" : "");
4365 /* TODO: Send a protected Disassociate frame to the STA using
4366 * the old key and Reason Code "Previous Authentication no
4367 * longer valid". Make sure this is only sent protected since
4368 * unprotected frame would be received by the STA that is now
4369 * trying to associate.
4370 */
4371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373 /* Make sure that the previously registered inactivity timer will not
4374 * remove the STA immediately. */
4375 sta->timeout_next = STA_NULLFUNC;
4376
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004377#ifdef CONFIG_TAXONOMY
4378 taxonomy_sta_info_assoc_req(hapd, sta, pos, left);
4379#endif /* CONFIG_TAXONOMY */
4380
Dmitry Shmidt29333592017-01-09 12:27:11 -08004381 sta->pending_wds_enable = 0;
4382
Paul Stewart092955c2017-02-06 09:13:09 -08004383#ifdef CONFIG_FILS
4384 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4385 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004386 sta->auth_alg == WLAN_AUTH_FILS_PK) {
4387 if (fils_process_hlp(hapd, sta, pos, left) > 0)
4388 delay_assoc = 1;
4389 }
Paul Stewart092955c2017-02-06 09:13:09 -08004390#endif /* CONFIG_FILS */
4391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392 fail:
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004393
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004394 /*
4395 * In case of a successful response, add the station to the driver.
4396 * Otherwise, the kernel may ignore Data frames before we process the
4397 * ACK frame (TX status). In case of a failure, this station will be
4398 * removed.
4399 *
4400 * Note that this is not compliant with the IEEE 802.11 standard that
4401 * states that a non-AP station should transition into the
4402 * authenticated/associated state only after the station acknowledges
4403 * the (Re)Association Response frame. However, still do this as:
4404 *
4405 * 1. In case the station does not acknowledge the (Re)Association
4406 * Response frame, it will be removed.
4407 * 2. Data frames will be dropped in the kernel until the station is
4408 * set into authorized state, and there are no significant known
4409 * issues with processing other non-Data Class 3 frames during this
4410 * window.
4411 */
Hai Shalom74f70d42019-02-11 14:42:39 -08004412 if (resp == WLAN_STATUS_SUCCESS && sta &&
4413 add_associated_sta(hapd, sta, reassoc))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004414 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4415
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004416#ifdef CONFIG_FILS
Hai Shalom74f70d42019-02-11 14:42:39 -08004417 if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS &&
4418 eloop_is_timeout_registered(fils_hlp_timeout, hapd, sta) &&
4419 sta->fils_pending_assoc_req) {
4420 /* Do not reschedule fils_hlp_timeout in case the station
4421 * retransmits (Re)Association Request frame while waiting for
4422 * the previously started FILS HLP wait, so that the timeout can
4423 * be determined from the first pending attempt. */
4424 wpa_printf(MSG_DEBUG,
4425 "FILS: Continue waiting for HLP processing before sending (Re)Association Response frame to "
4426 MACSTR, MAC2STR(sta->addr));
4427 os_free(tmp);
4428 return;
4429 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004430 if (sta) {
4431 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4432 os_free(sta->fils_pending_assoc_req);
4433 sta->fils_pending_assoc_req = NULL;
4434 sta->fils_pending_assoc_req_len = 0;
4435 wpabuf_free(sta->fils_hlp_resp);
4436 sta->fils_hlp_resp = NULL;
4437 }
4438 if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS) {
4439 sta->fils_pending_assoc_req = tmp;
4440 sta->fils_pending_assoc_req_len = left;
4441 sta->fils_pending_assoc_is_reassoc = reassoc;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004442 sta->fils_drv_assoc_finish = 0;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004443 wpa_printf(MSG_DEBUG,
4444 "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
4445 MACSTR, MAC2STR(sta->addr));
4446 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4447 eloop_register_timeout(0, hapd->conf->fils_hlp_wait_time * 1024,
4448 fils_hlp_timeout, hapd, sta);
4449 return;
4450 }
4451#endif /* CONFIG_FILS */
4452
Hai Shalomb755a2a2020-04-23 21:49:02 -07004453 if (resp >= 0)
4454 reply_res = send_assoc_resp(hapd, sta, mgmt->sa, resp, reassoc,
4455 pos, left, rssi, omit_rsnxe);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004456 os_free(tmp);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004457
4458 /*
4459 * Remove the station in case tranmission of a success response fails
4460 * (the STA was added associated to the driver) or if the station was
4461 * previously added unassociated.
4462 */
Dmitry Shmidt29333592017-01-09 12:27:11 -08004463 if (sta && ((reply_res != WLAN_STATUS_SUCCESS &&
4464 resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004465 hostapd_drv_sta_remove(hapd, sta->addr);
4466 sta->added_unassoc = 0;
4467 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468}
4469
4470
4471static void handle_disassoc(struct hostapd_data *hapd,
4472 const struct ieee80211_mgmt *mgmt, size_t len)
4473{
4474 struct sta_info *sta;
4475
4476 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004477 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
4478 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479 return;
4480 }
4481
4482 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
4483 MAC2STR(mgmt->sa),
4484 le_to_host16(mgmt->u.disassoc.reason_code));
4485
4486 sta = ap_get_sta(hapd, mgmt->sa);
4487 if (sta == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004488 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
4489 MAC2STR(mgmt->sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490 return;
4491 }
4492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004493 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004494 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
Hai Shalomfdcde762020-04-02 11:19:20 -07004496 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
4498 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4499 HOSTAPD_LEVEL_INFO, "disassociated");
4500 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
4501 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
4502 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
4503 * authenticated. */
4504 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004505 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004506 if (sta->ipaddr)
4507 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
4508 ap_sta_ip6addr_del(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004509 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004510 sta->added_unassoc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004511
4512 if (sta->timeout_next == STA_NULLFUNC ||
4513 sta->timeout_next == STA_DISASSOC) {
4514 sta->timeout_next = STA_DEAUTH;
4515 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4516 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
4517 hapd, sta);
4518 }
4519
4520 mlme_disassociate_indication(
4521 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
Dmitry Shmidt29333592017-01-09 12:27:11 -08004522
4523 /* DMG/IEEE 802.11ad does not use deauthication. Deallocate sta upon
4524 * disassociation. */
4525 if (hapd->iface->current_mode &&
4526 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
4527 sta->flags &= ~WLAN_STA_AUTH;
4528 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
4529 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4530 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
4531 ap_free_sta(hapd, sta);
4532 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533}
4534
4535
4536static void handle_deauth(struct hostapd_data *hapd,
4537 const struct ieee80211_mgmt *mgmt, size_t len)
4538{
4539 struct sta_info *sta;
4540
4541 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004542 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
4543 "payload (len=%lu)", (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 return;
4545 }
4546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004547 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 " reason_code=%d",
4549 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
4550
4551 sta = ap_get_sta(hapd, mgmt->sa);
4552 if (sta == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004553 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
4554 "to deauthenticate, but it is not authenticated",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555 MAC2STR(mgmt->sa));
4556 return;
4557 }
4558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004559 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004560 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004561 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
4562 WLAN_STA_ASSOC_REQ_OK);
Hai Shalomfdcde762020-04-02 11:19:20 -07004563 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
4565 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4566 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
4567 mlme_deauthenticate_indication(
4568 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
4569 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
4570 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
4571 ap_free_sta(hapd, sta);
4572}
4573
4574
4575static void handle_beacon(struct hostapd_data *hapd,
4576 const struct ieee80211_mgmt *mgmt, size_t len,
4577 struct hostapd_frame_info *fi)
4578{
4579 struct ieee802_11_elems elems;
4580
4581 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004582 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
4583 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584 return;
4585 }
4586
4587 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
4588 len - (IEEE80211_HDRLEN +
4589 sizeof(mgmt->u.beacon)), &elems,
4590 0);
4591
4592 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
4593}
4594
4595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596static int robust_action_frame(u8 category)
4597{
4598 return category != WLAN_ACTION_PUBLIC &&
4599 category != WLAN_ACTION_HT;
4600}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601
4602
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004603static int handle_action(struct hostapd_data *hapd,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004604 const struct ieee80211_mgmt *mgmt, size_t len,
4605 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606{
4607 struct sta_info *sta;
Hai Shalom74f70d42019-02-11 14:42:39 -08004608 u8 *action __maybe_unused;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609
Hai Shalom74f70d42019-02-11 14:42:39 -08004610 if (len < IEEE80211_HDRLEN + 2 + 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4612 HOSTAPD_LEVEL_DEBUG,
4613 "handle_action - too short payload (len=%lu)",
4614 (unsigned long) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004615 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616 }
4617
Hai Shalom74f70d42019-02-11 14:42:39 -08004618 action = (u8 *) &mgmt->u.action.u;
4619 wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
4620 " da " MACSTR " len %d freq %u",
4621 mgmt->u.action.category, *action,
4622 MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) len, freq);
4623
4624 sta = ap_get_sta(hapd, mgmt->sa);
4625
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004626 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
4627 (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
4628 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
4629 "frame (category=%u) from unassociated STA " MACSTR,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004630 mgmt->u.action.category, MAC2STR(mgmt->sa));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004631 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004632 }
4633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634 if (sta && (sta->flags & WLAN_STA_MFP) &&
Dmitry Shmidt18463232014-01-24 12:29:41 -08004635 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
4636 robust_action_frame(mgmt->u.action.category)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4638 HOSTAPD_LEVEL_DEBUG,
4639 "Dropped unprotected Robust Action frame from "
4640 "an MFP STA");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004641 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004644 if (sta) {
4645 u16 fc = le_to_host16(mgmt->frame_control);
4646 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4647
4648 if ((fc & WLAN_FC_RETRY) &&
4649 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4650 sta->last_seq_ctrl == seq_ctrl &&
4651 sta->last_subtype == WLAN_FC_STYPE_ACTION) {
4652 hostapd_logger(hapd, sta->addr,
4653 HOSTAPD_MODULE_IEEE80211,
4654 HOSTAPD_LEVEL_DEBUG,
4655 "Drop repeated action frame seq_ctrl=0x%x",
4656 seq_ctrl);
4657 return 1;
4658 }
4659
4660 sta->last_seq_ctrl = seq_ctrl;
4661 sta->last_subtype = WLAN_FC_STYPE_ACTION;
4662 }
4663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004664 switch (mgmt->u.action.category) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004665#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 case WLAN_ACTION_FT:
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004667 if (!sta ||
4668 wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669 len - IEEE80211_HDRLEN))
4670 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004671 return 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004672#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 case WLAN_ACTION_WMM:
4674 hostapd_wmm_action(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004675 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676 case WLAN_ACTION_SA_QUERY:
Hai Shalom021b0b52019-04-10 11:17:58 -07004677 ieee802_11_sa_query_action(hapd, mgmt, len);
4678 return 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004679#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004680 case WLAN_ACTION_WNM:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004681 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
4682 return 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004683#endif /* CONFIG_WNM_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004684#ifdef CONFIG_FST
4685 case WLAN_ACTION_FST:
4686 if (hapd->iface->fst)
4687 fst_rx_action(hapd->iface->fst, mgmt, len);
4688 else
4689 wpa_printf(MSG_DEBUG,
4690 "FST: Ignore FST Action frame - no FST attached");
4691 return 1;
4692#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004693 case WLAN_ACTION_PUBLIC:
Dmitry Shmidt18463232014-01-24 12:29:41 -08004694 case WLAN_ACTION_PROTECTED_DUAL:
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07004695 if (len >= IEEE80211_HDRLEN + 2 &&
4696 mgmt->u.action.u.public_action.action ==
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004697 WLAN_PA_20_40_BSS_COEX) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004698 hostapd_2040_coex_action(hapd, mgmt, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004699 return 1;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004700 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004701#ifdef CONFIG_DPP
4702 if (len >= IEEE80211_HDRLEN + 6 &&
4703 mgmt->u.action.u.vs_public_action.action ==
4704 WLAN_PA_VENDOR_SPECIFIC &&
4705 WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
4706 OUI_WFA &&
4707 mgmt->u.action.u.vs_public_action.variable[0] ==
4708 DPP_OUI_TYPE) {
4709 const u8 *pos, *end;
4710
4711 pos = mgmt->u.action.u.vs_public_action.oui;
4712 end = ((const u8 *) mgmt) + len;
4713 hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004714 freq);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004715 return 1;
4716 }
4717 if (len >= IEEE80211_HDRLEN + 2 &&
4718 (mgmt->u.action.u.public_action.action ==
4719 WLAN_PA_GAS_INITIAL_RESP ||
4720 mgmt->u.action.u.public_action.action ==
4721 WLAN_PA_GAS_COMEBACK_RESP)) {
4722 const u8 *pos, *end;
4723
4724 pos = &mgmt->u.action.u.public_action.action;
4725 end = ((const u8 *) mgmt) + len;
4726 gas_query_ap_rx(hapd->gas, mgmt->sa,
4727 mgmt->u.action.category,
4728 pos, end - pos, hapd->iface->freq);
4729 return 1;
4730 }
4731#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732 if (hapd->public_action_cb) {
4733 hapd->public_action_cb(hapd->public_action_cb_ctx,
4734 (u8 *) mgmt, len,
4735 hapd->iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004736 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004737 if (hapd->public_action_cb2) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004738 hapd->public_action_cb2(hapd->public_action_cb2_ctx,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004739 (u8 *) mgmt, len,
4740 hapd->iface->freq);
4741 }
4742 if (hapd->public_action_cb || hapd->public_action_cb2)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004743 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744 break;
4745 case WLAN_ACTION_VENDOR_SPECIFIC:
4746 if (hapd->vendor_action_cb) {
4747 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
4748 (u8 *) mgmt, len,
4749 hapd->iface->freq) == 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004750 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751 }
4752 break;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004753 case WLAN_ACTION_RADIO_MEASUREMENT:
4754 hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
4755 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 }
4757
4758 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4759 HOSTAPD_LEVEL_DEBUG,
4760 "handle_action - unknown action category %d or invalid "
4761 "frame",
4762 mgmt->u.action.category);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004763 if (!is_multicast_ether_addr(mgmt->da) &&
4764 !(mgmt->u.action.category & 0x80) &&
4765 !is_multicast_ether_addr(mgmt->sa)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766 struct ieee80211_mgmt *resp;
4767
4768 /*
4769 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
4770 * Return the Action frame to the source without change
4771 * except that MSB of the Category set to 1.
4772 */
4773 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
4774 "frame back to sender");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004775 resp = os_memdup(mgmt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004776 if (resp == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004777 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 os_memcpy(resp->da, resp->sa, ETH_ALEN);
4779 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
4780 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
4781 resp->u.action.category |= 0x80;
4782
Hai Shalomfdcde762020-04-02 11:19:20 -07004783 if (hostapd_drv_send_mlme(hapd, resp, len, 0, NULL, 0, 0) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004784 wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
4785 "Action frame");
4786 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787 os_free(resp);
4788 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004789
4790 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791}
4792
4793
4794/**
4795 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
4796 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
4797 * sent to)
4798 * @buf: management frame data (starting from IEEE 802.11 header)
4799 * @len: length of frame data in octets
4800 * @fi: meta data about received frame (signal level, etc.)
4801 *
4802 * Process all incoming IEEE 802.11 management frames. This will be called for
4803 * each frame received from the kernel driver through wlan#ap interface. In
4804 * addition, it can be called to re-inserted pending frames (e.g., when using
4805 * external RADIUS server as an MAC ACL).
4806 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004807int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
4808 struct hostapd_frame_info *fi)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809{
4810 struct ieee80211_mgmt *mgmt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811 u16 fc, stype;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004812 int ret = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004813 unsigned int freq;
4814 int ssi_signal = fi ? fi->ssi_signal : 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816 if (len < 24)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004817 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818
Roshan Pius3a1667e2018-07-03 15:17:14 -07004819 if (fi && fi->freq)
4820 freq = fi->freq;
4821 else
4822 freq = hapd->iface->freq;
4823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 mgmt = (struct ieee80211_mgmt *) buf;
4825 fc = le_to_host16(mgmt->frame_control);
4826 stype = WLAN_FC_GET_STYPE(fc);
4827
Hai Shalomc3565922019-10-28 11:58:20 -07004828 if (is_multicast_ether_addr(mgmt->sa) ||
4829 is_zero_ether_addr(mgmt->sa) ||
4830 os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
4831 /* Do not process any frames with unexpected/invalid SA so that
4832 * we do not add any state for unexpected STA addresses or end
4833 * up sending out frames to unexpected destination. */
4834 wpa_printf(MSG_DEBUG, "MGMT: Invalid SA=" MACSTR
4835 " in received frame - ignore this frame silently",
4836 MAC2STR(mgmt->sa));
4837 return 0;
4838 }
4839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004840 if (stype == WLAN_FC_STYPE_BEACON) {
4841 handle_beacon(hapd, mgmt, len, fi);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004842 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 }
4844
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07004845 if (!is_broadcast_ether_addr(mgmt->bssid) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004846#ifdef CONFIG_P2P
4847 /* Invitation responses can be sent with the peer MAC as BSSID */
4848 !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
4849 stype == WLAN_FC_STYPE_ACTION) &&
4850#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004851#ifdef CONFIG_MESH
4852 !(hapd->conf->mesh & MESH_ENABLED) &&
4853#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004855 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
4856 MAC2STR(mgmt->bssid));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004857 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858 }
4859
4860
4861 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004862 handle_probe_req(hapd, mgmt, len, ssi_signal);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004863 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 }
4865
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004866 if ((!is_broadcast_ether_addr(mgmt->da) ||
4867 stype != WLAN_FC_STYPE_ACTION) &&
4868 os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4870 HOSTAPD_LEVEL_DEBUG,
4871 "MGMT: DA=" MACSTR " not our address",
4872 MAC2STR(mgmt->da));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004873 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874 }
4875
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004876 if (hapd->iconf->track_sta_max_num)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004877 sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004879 switch (stype) {
4880 case WLAN_FC_STYPE_AUTH:
4881 wpa_printf(MSG_DEBUG, "mgmt::auth");
Hai Shalom021b0b52019-04-10 11:17:58 -07004882 handle_auth(hapd, mgmt, len, ssi_signal, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004883 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 break;
4885 case WLAN_FC_STYPE_ASSOC_REQ:
4886 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
Hai Shalom74f70d42019-02-11 14:42:39 -08004887 handle_assoc(hapd, mgmt, len, 0, ssi_signal);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004888 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 break;
4890 case WLAN_FC_STYPE_REASSOC_REQ:
4891 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
Hai Shalom74f70d42019-02-11 14:42:39 -08004892 handle_assoc(hapd, mgmt, len, 1, ssi_signal);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004893 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004894 break;
4895 case WLAN_FC_STYPE_DISASSOC:
4896 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
4897 handle_disassoc(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004898 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899 break;
4900 case WLAN_FC_STYPE_DEAUTH:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004901 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 handle_deauth(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004903 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 break;
4905 case WLAN_FC_STYPE_ACTION:
4906 wpa_printf(MSG_DEBUG, "mgmt::action");
Roshan Pius3a1667e2018-07-03 15:17:14 -07004907 ret = handle_action(hapd, mgmt, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 break;
4909 default:
4910 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4911 HOSTAPD_LEVEL_DEBUG,
4912 "unknown mgmt frame subtype %d", stype);
4913 break;
4914 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004915
4916 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917}
4918
4919
4920static void handle_auth_cb(struct hostapd_data *hapd,
4921 const struct ieee80211_mgmt *mgmt,
4922 size_t len, int ok)
4923{
4924 u16 auth_alg, auth_transaction, status_code;
4925 struct sta_info *sta;
4926
Hai Shalome5e28bb2019-01-28 14:51:04 -08004927 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
4928 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
4929 (unsigned long) len);
4930
4931 /*
4932 * Initialize status_code here because we are not able to read
4933 * it from the short payload.
4934 */
4935 status_code = WLAN_STATUS_UNSPECIFIED_FAILURE;
4936 goto fail;
4937 }
4938
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004939 sta = ap_get_sta(hapd, mgmt->da);
4940 if (!sta) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004941 wpa_printf(MSG_DEBUG, "handle_auth_cb: STA " MACSTR
4942 " not found",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004943 MAC2STR(mgmt->da));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 return;
4945 }
4946
4947 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4948 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
4949 status_code = le_to_host16(mgmt->u.auth.status_code);
4950
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004951 if (!ok) {
4952 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
4953 HOSTAPD_LEVEL_NOTICE,
4954 "did not acknowledge authentication response");
4955 goto fail;
4956 }
4957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 if (status_code == WLAN_STATUS_SUCCESS &&
4959 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
4960 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
4961 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4962 HOSTAPD_LEVEL_INFO, "authenticated");
4963 sta->flags |= WLAN_STA_AUTH;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004964 if (sta->added_unassoc)
4965 hostapd_set_sta_flags(hapd, sta);
4966 return;
4967 }
4968
4969fail:
4970 if (status_code != WLAN_STATUS_SUCCESS && sta->added_unassoc) {
4971 hostapd_drv_sta_remove(hapd, sta->addr);
4972 sta->added_unassoc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 }
4974}
4975
4976
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004977static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
4978 struct sta_info *sta,
4979 char *ifname_wds)
4980{
Hai Shalomfdcde762020-04-02 11:19:20 -07004981#ifdef CONFIG_WEP
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004982 int i;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004983 struct hostapd_ssid *ssid = &hapd->conf->ssid;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004984
4985 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
4986 return;
4987
4988 for (i = 0; i < 4; i++) {
4989 if (ssid->wep.key[i] &&
4990 hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
Hai Shalomfdcde762020-04-02 11:19:20 -07004991 0, i == ssid->wep.idx, NULL, 0,
4992 ssid->wep.key[i], ssid->wep.len[i],
4993 i == ssid->wep.idx ?
4994 KEY_FLAG_GROUP_RX_TX_DEFAULT :
4995 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004996 wpa_printf(MSG_WARNING,
4997 "Could not set WEP keys for WDS interface; %s",
4998 ifname_wds);
4999 break;
5000 }
5001 }
Hai Shalomfdcde762020-04-02 11:19:20 -07005002#endif /* CONFIG_WEP */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005003}
5004
5005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006static void handle_assoc_cb(struct hostapd_data *hapd,
5007 const struct ieee80211_mgmt *mgmt,
5008 size_t len, int reassoc, int ok)
5009{
5010 u16 status;
5011 struct sta_info *sta;
5012 int new_assoc = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 sta = ap_get_sta(hapd, mgmt->da);
5015 if (!sta) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005016 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
5017 MAC2STR(mgmt->da));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 return;
5019 }
5020
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005021 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
5022 sizeof(mgmt->u.assoc_resp))) {
5023 wpa_printf(MSG_INFO,
5024 "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
5025 reassoc, (unsigned long) len);
5026 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005027 return;
5028 }
5029
5030 if (reassoc)
5031 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
5032 else
5033 status = le_to_host16(mgmt->u.assoc_resp.status_code);
5034
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005035 if (!ok) {
5036 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
5037 HOSTAPD_LEVEL_DEBUG,
5038 "did not acknowledge association response");
5039 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
5040 /* The STA is added only in case of SUCCESS */
5041 if (status == WLAN_STATUS_SUCCESS)
5042 hostapd_drv_sta_remove(hapd, sta->addr);
5043
5044 return;
5045 }
5046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005047 if (status != WLAN_STATUS_SUCCESS)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07005048 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049
5050 /* Stop previous accounting session, if one is started, and allocate
5051 * new session id for the new session. */
5052 accounting_sta_stop(hapd, sta);
5053
5054 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5055 HOSTAPD_LEVEL_INFO,
5056 "associated (aid %d)",
5057 sta->aid);
5058
5059 if (sta->flags & WLAN_STA_ASSOC)
5060 new_assoc = 0;
5061 sta->flags |= WLAN_STA_ASSOC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005062 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005063 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
5064 !hapd->conf->osen) ||
5065 sta->auth_alg == WLAN_AUTH_FILS_SK ||
5066 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
5067 sta->auth_alg == WLAN_AUTH_FILS_PK ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068 sta->auth_alg == WLAN_AUTH_FT) {
5069 /*
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005070 * Open, static WEP, FT protocol, or FILS; no separate
5071 * authorization step.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072 */
5073 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 }
5075
5076 if (reassoc)
5077 mlme_reassociate_indication(hapd, sta);
5078 else
5079 mlme_associate_indication(hapd, sta);
5080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081 sta->sa_query_timed_out = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083 if (sta->eapol_sm == NULL) {
5084 /*
5085 * This STA does not use RADIUS server for EAP authentication,
5086 * so bind it to the selected VLAN interface now, since the
5087 * interface selection is not going to change anymore.
5088 */
Dmitry Shmidt83474442015-04-15 13:47:09 -07005089 if (ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07005090 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091 } else if (sta->vlan_id) {
5092 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
Dmitry Shmidt83474442015-04-15 13:47:09 -07005093 if (ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07005094 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095 }
5096
5097 hostapd_set_sta_flags(hapd, sta);
5098
Dmitry Shmidt29333592017-01-09 12:27:11 -08005099 if (!(sta->flags & WLAN_STA_WDS) && sta->pending_wds_enable) {
5100 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for STA "
5101 MACSTR " based on pending request",
5102 MAC2STR(sta->addr));
5103 sta->pending_wds_enable = 0;
5104 sta->flags |= WLAN_STA_WDS;
5105 }
5106
Hai Shalom74f70d42019-02-11 14:42:39 -08005107 if (sta->flags & (WLAN_STA_WDS | WLAN_STA_MULTI_AP)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005108 int ret;
5109 char ifname_wds[IFNAMSIZ + 1];
5110
5111 wpa_printf(MSG_DEBUG, "Reenable 4-address WDS mode for STA "
5112 MACSTR " (aid %u)",
5113 MAC2STR(sta->addr), sta->aid);
5114 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
5115 sta->aid, 1);
5116 if (!ret)
5117 hostapd_set_wds_encryption(hapd, sta, ifname_wds);
5118 }
5119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120 if (sta->auth_alg == WLAN_AUTH_FT)
5121 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
5122 else
5123 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
5124 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005126
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005127#ifdef CONFIG_FILS
5128 if ((sta->auth_alg == WLAN_AUTH_FILS_SK ||
5129 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
5130 sta->auth_alg == WLAN_AUTH_FILS_PK) &&
5131 fils_set_tk(sta->wpa_sm) < 0) {
5132 wpa_printf(MSG_DEBUG, "FILS: TK configuration failed");
5133 ap_sta_disconnect(hapd, sta, sta->addr,
5134 WLAN_REASON_UNSPECIFIED);
5135 return;
5136 }
5137#endif /* CONFIG_FILS */
5138
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005139 if (sta->pending_eapol_rx) {
5140 struct os_reltime now, age;
5141
5142 os_get_reltime(&now);
5143 os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
5144 if (age.sec == 0 && age.usec < 200000) {
5145 wpa_printf(MSG_DEBUG,
5146 "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
5147 MAC2STR(sta->addr));
5148 ieee802_1x_receive(
5149 hapd, mgmt->da,
5150 wpabuf_head(sta->pending_eapol_rx->buf),
5151 wpabuf_len(sta->pending_eapol_rx->buf));
5152 }
5153 wpabuf_free(sta->pending_eapol_rx->buf);
5154 os_free(sta->pending_eapol_rx);
5155 sta->pending_eapol_rx = NULL;
5156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157}
5158
5159
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005160static void handle_deauth_cb(struct hostapd_data *hapd,
5161 const struct ieee80211_mgmt *mgmt,
5162 size_t len, int ok)
5163{
5164 struct sta_info *sta;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005165 if (is_multicast_ether_addr(mgmt->da))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005166 return;
5167 sta = ap_get_sta(hapd, mgmt->da);
5168 if (!sta) {
5169 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
5170 " not found", MAC2STR(mgmt->da));
5171 return;
5172 }
5173 if (ok)
5174 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
5175 MAC2STR(sta->addr));
5176 else
5177 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
5178 "deauth", MAC2STR(sta->addr));
5179
5180 ap_sta_deauth_cb(hapd, sta);
5181}
5182
5183
5184static void handle_disassoc_cb(struct hostapd_data *hapd,
5185 const struct ieee80211_mgmt *mgmt,
5186 size_t len, int ok)
5187{
5188 struct sta_info *sta;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005189 if (is_multicast_ether_addr(mgmt->da))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005190 return;
5191 sta = ap_get_sta(hapd, mgmt->da);
5192 if (!sta) {
5193 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
5194 " not found", MAC2STR(mgmt->da));
5195 return;
5196 }
5197 if (ok)
5198 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
5199 MAC2STR(sta->addr));
5200 else
5201 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
5202 "disassoc", MAC2STR(sta->addr));
5203
5204 ap_sta_disassoc_cb(hapd, sta);
5205}
5206
5207
Dmitry Shmidt29333592017-01-09 12:27:11 -08005208static void handle_action_cb(struct hostapd_data *hapd,
5209 const struct ieee80211_mgmt *mgmt,
5210 size_t len, int ok)
5211{
5212 struct sta_info *sta;
Paul Stewart092955c2017-02-06 09:13:09 -08005213 const struct rrm_measurement_report_element *report;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005214
5215 if (is_multicast_ether_addr(mgmt->da))
5216 return;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005217#ifdef CONFIG_DPP
5218 if (len >= IEEE80211_HDRLEN + 6 &&
5219 mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
5220 mgmt->u.action.u.vs_public_action.action ==
5221 WLAN_PA_VENDOR_SPECIFIC &&
5222 WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
5223 OUI_WFA &&
5224 mgmt->u.action.u.vs_public_action.variable[0] ==
5225 DPP_OUI_TYPE) {
5226 const u8 *pos, *end;
5227
5228 pos = &mgmt->u.action.u.vs_public_action.variable[1];
5229 end = ((const u8 *) mgmt) + len;
5230 hostapd_dpp_tx_status(hapd, mgmt->da, pos, end - pos, ok);
5231 return;
5232 }
5233 if (len >= IEEE80211_HDRLEN + 2 &&
5234 mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
5235 (mgmt->u.action.u.public_action.action ==
5236 WLAN_PA_GAS_INITIAL_REQ ||
5237 mgmt->u.action.u.public_action.action ==
5238 WLAN_PA_GAS_COMEBACK_REQ)) {
5239 const u8 *pos, *end;
5240
5241 pos = mgmt->u.action.u.public_action.variable;
5242 end = ((const u8 *) mgmt) + len;
5243 gas_query_ap_tx_status(hapd->gas, mgmt->da, pos, end - pos, ok);
5244 return;
5245 }
5246#endif /* CONFIG_DPP */
Dmitry Shmidt29333592017-01-09 12:27:11 -08005247 sta = ap_get_sta(hapd, mgmt->da);
5248 if (!sta) {
5249 wpa_printf(MSG_DEBUG, "handle_action_cb: STA " MACSTR
5250 " not found", MAC2STR(mgmt->da));
5251 return;
5252 }
5253
Paul Stewart092955c2017-02-06 09:13:09 -08005254 if (len < 24 + 5 + sizeof(*report))
Dmitry Shmidt29333592017-01-09 12:27:11 -08005255 return;
Paul Stewart092955c2017-02-06 09:13:09 -08005256 report = (const struct rrm_measurement_report_element *)
5257 &mgmt->u.action.u.rrm.variable[2];
Dmitry Shmidt29333592017-01-09 12:27:11 -08005258 if (mgmt->u.action.category == WLAN_ACTION_RADIO_MEASUREMENT &&
Paul Stewart092955c2017-02-06 09:13:09 -08005259 mgmt->u.action.u.rrm.action == WLAN_RRM_RADIO_MEASUREMENT_REQUEST &&
5260 report->eid == WLAN_EID_MEASURE_REQUEST &&
5261 report->len >= 3 &&
5262 report->type == MEASURE_TYPE_BEACON)
Dmitry Shmidt29333592017-01-09 12:27:11 -08005263 hostapd_rrm_beacon_req_tx_status(hapd, mgmt, len, ok);
5264}
5265
5266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267/**
5268 * ieee802_11_mgmt_cb - Process management frame TX status callback
5269 * @hapd: hostapd BSS data structure (the BSS from which the management frame
5270 * was sent from)
5271 * @buf: management frame data (starting from IEEE 802.11 header)
5272 * @len: length of frame data in octets
5273 * @stype: management frame subtype from frame control field
5274 * @ok: Whether the frame was ACK'ed
5275 */
5276void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
5277 u16 stype, int ok)
5278{
5279 const struct ieee80211_mgmt *mgmt;
5280 mgmt = (const struct ieee80211_mgmt *) buf;
5281
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005282#ifdef CONFIG_TESTING_OPTIONS
5283 if (hapd->ext_mgmt_frame_handling) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005284 size_t hex_len = 2 * len + 1;
5285 char *hex = os_malloc(hex_len);
5286
5287 if (hex) {
5288 wpa_snprintf_hex(hex, hex_len, buf, len);
5289 wpa_msg(hapd->msg_ctx, MSG_INFO,
5290 "MGMT-TX-STATUS stype=%u ok=%d buf=%s",
5291 stype, ok, hex);
5292 os_free(hex);
5293 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005294 return;
5295 }
5296#endif /* CONFIG_TESTING_OPTIONS */
5297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298 switch (stype) {
5299 case WLAN_FC_STYPE_AUTH:
5300 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
5301 handle_auth_cb(hapd, mgmt, len, ok);
5302 break;
5303 case WLAN_FC_STYPE_ASSOC_RESP:
5304 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
5305 handle_assoc_cb(hapd, mgmt, len, 0, ok);
5306 break;
5307 case WLAN_FC_STYPE_REASSOC_RESP:
5308 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
5309 handle_assoc_cb(hapd, mgmt, len, 1, ok);
5310 break;
5311 case WLAN_FC_STYPE_PROBE_RESP:
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005312 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005313 break;
5314 case WLAN_FC_STYPE_DEAUTH:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005315 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
5316 handle_deauth_cb(hapd, mgmt, len, ok);
5317 break;
5318 case WLAN_FC_STYPE_DISASSOC:
5319 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
5320 handle_disassoc_cb(hapd, mgmt, len, ok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005321 break;
5322 case WLAN_FC_STYPE_ACTION:
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005323 wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
Dmitry Shmidt29333592017-01-09 12:27:11 -08005324 handle_action_cb(hapd, mgmt, len, ok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325 break;
5326 default:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005327 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328 break;
5329 }
5330}
5331
5332
5333int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
5334{
5335 /* TODO */
5336 return 0;
5337}
5338
5339
5340int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
5341 char *buf, size_t buflen)
5342{
5343 /* TODO */
5344 return 0;
5345}
5346
5347
5348void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
5349 const u8 *buf, size_t len, int ack)
5350{
5351 struct sta_info *sta;
5352 struct hostapd_iface *iface = hapd->iface;
5353
5354 sta = ap_get_sta(hapd, addr);
5355 if (sta == NULL && iface->num_bss > 1) {
5356 size_t j;
5357 for (j = 0; j < iface->num_bss; j++) {
5358 hapd = iface->bss[j];
5359 sta = ap_get_sta(hapd, addr);
5360 if (sta)
5361 break;
5362 }
5363 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005364 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365 return;
5366 if (sta->flags & WLAN_STA_PENDING_POLL) {
5367 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
5368 "activity poll", MAC2STR(sta->addr),
5369 ack ? "ACKed" : "did not ACK");
5370 if (ack)
5371 sta->flags &= ~WLAN_STA_PENDING_POLL;
5372 }
5373
5374 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
5375}
5376
5377
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005378void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
5379 const u8 *data, size_t len, int ack)
5380{
5381 struct sta_info *sta;
5382 struct hostapd_iface *iface = hapd->iface;
5383
5384 sta = ap_get_sta(hapd, dst);
5385 if (sta == NULL && iface->num_bss > 1) {
5386 size_t j;
5387 for (j = 0; j < iface->num_bss; j++) {
5388 hapd = iface->bss[j];
5389 sta = ap_get_sta(hapd, dst);
5390 if (sta)
5391 break;
5392 }
5393 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005394 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
5395 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
5396 MACSTR " that is not currently associated",
5397 MAC2STR(dst));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005398 return;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005399 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005400
5401 ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
5402}
5403
5404
5405void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
5406{
5407 struct sta_info *sta;
5408 struct hostapd_iface *iface = hapd->iface;
5409
5410 sta = ap_get_sta(hapd, addr);
5411 if (sta == NULL && iface->num_bss > 1) {
5412 size_t j;
5413 for (j = 0; j < iface->num_bss; j++) {
5414 hapd = iface->bss[j];
5415 sta = ap_get_sta(hapd, addr);
5416 if (sta)
5417 break;
5418 }
5419 }
5420 if (sta == NULL)
5421 return;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005422 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
5423 MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005424 if (!(sta->flags & WLAN_STA_PENDING_POLL))
5425 return;
5426
5427 wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
5428 "activity poll", MAC2STR(sta->addr));
5429 sta->flags &= ~WLAN_STA_PENDING_POLL;
5430}
5431
5432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005433void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
5434 int wds)
5435{
5436 struct sta_info *sta;
5437
5438 sta = ap_get_sta(hapd, src);
Dmitry Shmidt29333592017-01-09 12:27:11 -08005439 if (sta &&
5440 ((sta->flags & WLAN_STA_ASSOC) ||
5441 ((sta->flags & WLAN_STA_ASSOC_REQ_OK) && wds))) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005442 if (!hapd->conf->wds_sta)
5443 return;
5444
Dmitry Shmidt29333592017-01-09 12:27:11 -08005445 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK)) ==
5446 WLAN_STA_ASSOC_REQ_OK) {
5447 wpa_printf(MSG_DEBUG,
5448 "Postpone 4-address WDS mode enabling for STA "
5449 MACSTR " since TX status for AssocResp is not yet known",
5450 MAC2STR(sta->addr));
5451 sta->pending_wds_enable = 1;
5452 return;
5453 }
5454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 if (wds && !(sta->flags & WLAN_STA_WDS)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005456 int ret;
5457 char ifname_wds[IFNAMSIZ + 1];
5458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
5460 "STA " MACSTR " (aid %u)",
5461 MAC2STR(sta->addr), sta->aid);
5462 sta->flags |= WLAN_STA_WDS;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005463 ret = hostapd_set_wds_sta(hapd, ifname_wds,
5464 sta->addr, sta->aid, 1);
5465 if (!ret)
5466 hostapd_set_wds_encryption(hapd, sta,
5467 ifname_wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 }
5469 return;
5470 }
5471
5472 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
5473 MACSTR, MAC2STR(src));
Hai Shalomc3565922019-10-28 11:58:20 -07005474 if (is_multicast_ether_addr(src) || is_zero_ether_addr(src) ||
5475 os_memcmp(src, hapd->own_addr, ETH_ALEN) == 0) {
5476 /* Broadcast bit set in SA or unexpected SA?! Ignore the frame
5477 * silently. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005478 return;
5479 }
5480
5481 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
5482 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
5483 "already been sent, but no TX status yet known - "
5484 "ignore Class 3 frame issue with " MACSTR,
5485 MAC2STR(src));
5486 return;
5487 }
5488
5489 if (sta && (sta->flags & WLAN_STA_AUTH))
5490 hostapd_drv_sta_disassoc(
5491 hapd, src,
5492 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
5493 else
5494 hostapd_drv_sta_deauth(
5495 hapd, src,
5496 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
5497}
5498
5499
5500#endif /* CONFIG_NATIVE_WINDOWS */