blob: 565e9afc19ce569e53e21462039f683126ba8461 [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;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003184 if (is_6ghz_op_class(hapd->iconf->op_class)) {
3185 resp = copy_sta_he_6ghz_capab(hapd, sta,
3186 elems.he_6ghz_band_cap);
3187 if (resp != WLAN_STATUS_SUCCESS)
3188 return resp;
3189 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003190 }
3191#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003192
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003193#ifdef CONFIG_P2P
3194 if (elems.p2p) {
3195 wpabuf_free(sta->p2p_ie);
3196 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
3197 P2P_IE_VENDOR_TYPE);
3198 if (sta->p2p_ie)
3199 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
3200 } else {
3201 wpabuf_free(sta->p2p_ie);
3202 sta->p2p_ie = NULL;
3203 }
3204#endif /* CONFIG_P2P */
3205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
3207 wpa_ie = elems.rsn_ie;
3208 wpa_ie_len = elems.rsn_ie_len;
3209 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
3210 elems.wpa_ie) {
3211 wpa_ie = elems.wpa_ie;
3212 wpa_ie_len = elems.wpa_ie_len;
3213 } else {
3214 wpa_ie = NULL;
3215 wpa_ie_len = 0;
3216 }
3217
3218#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003219 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 if (hapd->conf->wps_state && elems.wps_ie) {
3221 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
3222 "Request - assume WPS is used");
3223 sta->flags |= WLAN_STA_WPS;
3224 wpabuf_free(sta->wps_ie);
3225 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
3226 WPS_IE_VENDOR_TYPE);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003227 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
3228 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
3229 sta->flags |= WLAN_STA_WPS2;
3230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231 wpa_ie = NULL;
3232 wpa_ie_len = 0;
3233 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
3234 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
3235 "(Re)Association Request - reject");
3236 return WLAN_STATUS_INVALID_IE;
3237 }
3238 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
3239 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
3240 "(Re)Association Request - possible WPS use");
3241 sta->flags |= WLAN_STA_MAYBE_WPS;
3242 } else
3243#endif /* CONFIG_WPS */
3244 if (hapd->conf->wpa && wpa_ie == NULL) {
3245 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3246 HOSTAPD_LEVEL_INFO,
3247 "No WPA/RSN IE in association request");
3248 return WLAN_STATUS_INVALID_IE;
3249 }
3250
3251 if (hapd->conf->wpa && wpa_ie) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003252 enum wpa_validate_result res;
3253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254 wpa_ie -= 2;
3255 wpa_ie_len += 2;
3256 if (sta->wpa_sm == NULL)
3257 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003258 sta->addr,
3259 p2p_dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260 if (sta->wpa_sm == NULL) {
3261 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
3262 "state machine");
3263 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3264 }
Hai Shalom021b0b52019-04-10 11:17:58 -07003265 wpa_auth_set_auth_alg(sta->wpa_sm, sta->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
Hai Shalom021b0b52019-04-10 11:17:58 -07003267 hapd->iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 wpa_ie, wpa_ie_len,
Hai Shalomc3565922019-10-28 11:58:20 -07003269 elems.rsnxe ? elems.rsnxe - 2 : NULL,
3270 elems.rsnxe ? elems.rsnxe_len + 2 : 0,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003271 elems.mdie, elems.mdie_len,
3272 elems.owe_dh, elems.owe_dh_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003273 resp = wpa_res_to_status_code(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003274 if (resp != WLAN_STATUS_SUCCESS)
3275 return resp;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003276 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
3277 (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
3278 !sta->sa_query_timed_out &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 sta->sa_query_count > 0)
3280 ap_check_sa_query_timeout(hapd, sta);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003281 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
3282 (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
3283 !sta->sa_query_timed_out &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
3285 /*
3286 * STA has already been associated with MFP and SA
3287 * Query timeout has not been reached. Reject the
3288 * association attempt temporarily and start SA Query,
3289 * if one is not pending.
3290 */
3291
3292 if (sta->sa_query_count == 0)
3293 ap_sta_start_sa_query(hapd, sta);
3294
3295 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
3296 }
3297
3298 if (wpa_auth_uses_mfp(sta->wpa_sm))
3299 sta->flags |= WLAN_STA_MFP;
3300 else
3301 sta->flags &= ~WLAN_STA_MFP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003303#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 if (sta->auth_alg == WLAN_AUTH_FT) {
3305 if (!reassoc) {
3306 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
3307 "to use association (not "
3308 "re-association) with FT auth_alg",
3309 MAC2STR(sta->addr));
3310 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3311 }
3312
3313 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
3314 ies_len);
3315 if (resp != WLAN_STATUS_SUCCESS)
3316 return resp;
3317 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003318#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003320#ifdef CONFIG_SAE
Roshan Pius3a1667e2018-07-03 15:17:14 -07003321 if (wpa_auth_uses_sae(sta->wpa_sm) && sta->sae &&
3322 sta->sae->state == SAE_ACCEPTED)
3323 wpa_auth_add_sae_pmkid(sta->wpa_sm, sta->sae->pmkid);
3324
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003325 if (wpa_auth_uses_sae(sta->wpa_sm) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003326 sta->auth_alg == WLAN_AUTH_OPEN) {
3327 struct rsn_pmksa_cache_entry *sa;
3328 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
3329 if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
3330 wpa_printf(MSG_DEBUG,
3331 "SAE: No PMKSA cache entry found for "
3332 MACSTR, MAC2STR(sta->addr));
3333 return WLAN_STATUS_INVALID_PMKID;
3334 }
3335 wpa_printf(MSG_DEBUG, "SAE: " MACSTR
3336 " using PMKSA caching", MAC2STR(sta->addr));
3337 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
3338 sta->auth_alg != WLAN_AUTH_SAE &&
3339 !(sta->auth_alg == WLAN_AUTH_FT &&
3340 wpa_auth_uses_ft_sae(sta->wpa_sm))) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003341 wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
3342 "SAE AKM after non-SAE auth_alg %u",
3343 MAC2STR(sta->addr), sta->auth_alg);
3344 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
3345 }
Hai Shalomc3565922019-10-28 11:58:20 -07003346
3347 if (hapd->conf->sae_pwe == 2 &&
3348 sta->auth_alg == WLAN_AUTH_SAE &&
3349 sta->sae && sta->sae->tmp && !sta->sae->tmp->h2e &&
3350 elems.rsnxe && elems.rsnxe_len >= 1 &&
3351 (elems.rsnxe[0] & BIT(WLAN_RSNX_CAPAB_SAE_H2E))) {
3352 wpa_printf(MSG_INFO, "SAE: " MACSTR
3353 " indicates support for SAE H2E, but did not use it",
3354 MAC2STR(sta->addr));
3355 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3356 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003357#endif /* CONFIG_SAE */
3358
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003359#ifdef CONFIG_OWE
3360 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3361 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
3362 elems.owe_dh) {
3363 resp = owe_process_assoc_req(hapd, sta, elems.owe_dh,
3364 elems.owe_dh_len);
3365 if (resp != WLAN_STATUS_SUCCESS)
3366 return resp;
3367 }
3368#endif /* CONFIG_OWE */
3369
Hai Shalom021b0b52019-04-10 11:17:58 -07003370#ifdef CONFIG_DPP2
3371 dpp_pfs_free(sta->dpp_pfs);
3372 sta->dpp_pfs = NULL;
3373
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003374 if (DPP_VERSION > 1 &&
3375 (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
Hai Shalom021b0b52019-04-10 11:17:58 -07003376 hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
3377 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
3378 elems.owe_dh) {
3379 sta->dpp_pfs = dpp_pfs_init(
3380 wpabuf_head(hapd->conf->dpp_netaccesskey),
3381 wpabuf_len(hapd->conf->dpp_netaccesskey));
3382 if (!sta->dpp_pfs) {
3383 wpa_printf(MSG_DEBUG,
3384 "DPP: Could not initialize PFS");
3385 /* Try to continue without PFS */
3386 goto pfs_fail;
3387 }
3388
3389 if (dpp_pfs_process(sta->dpp_pfs, elems.owe_dh,
3390 elems.owe_dh_len) < 0) {
3391 dpp_pfs_free(sta->dpp_pfs);
3392 sta->dpp_pfs = NULL;
3393 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3394 }
3395 }
3396
3397 wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
3398 sta->dpp_pfs->secret : NULL);
3399 pfs_fail:
3400#endif /* CONFIG_DPP2 */
3401
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003402 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
3404 hostapd_logger(hapd, sta->addr,
3405 HOSTAPD_MODULE_IEEE80211,
3406 HOSTAPD_LEVEL_INFO,
3407 "Station tried to use TKIP with HT "
3408 "association");
3409 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
3410 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003411#ifdef CONFIG_HS20
3412 } else if (hapd->conf->osen) {
3413 if (elems.osen == NULL) {
3414 hostapd_logger(
3415 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3416 HOSTAPD_LEVEL_INFO,
3417 "No HS 2.0 OSEN element in association request");
3418 return WLAN_STATUS_INVALID_IE;
3419 }
3420
3421 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
3422 if (sta->wpa_sm == NULL)
3423 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
3424 sta->addr, NULL);
3425 if (sta->wpa_sm == NULL) {
3426 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
3427 "state machine");
3428 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3429 }
3430 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
3431 elems.osen - 2, elems.osen_len + 2) < 0)
3432 return WLAN_STATUS_INVALID_IE;
3433#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 } else
3435 wpa_auth_sta_no_wpa(sta->wpa_sm);
3436
3437#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
3439#endif /* CONFIG_P2P */
3440
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003441#ifdef CONFIG_HS20
3442 wpabuf_free(sta->hs20_ie);
3443 if (elems.hs20 && elems.hs20_len > 4) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003444 int release;
3445
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003446 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
3447 elems.hs20_len - 4);
Hai Shalom74f70d42019-02-11 14:42:39 -08003448 release = ((elems.hs20[4] >> 4) & 0x0f) + 1;
Hai Shalomc3565922019-10-28 11:58:20 -07003449 if (release >= 2 && !wpa_auth_uses_mfp(sta->wpa_sm) &&
3450 hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003451 wpa_printf(MSG_DEBUG,
3452 "HS 2.0: PMF not negotiated by release %d station "
3453 MACSTR, release, MAC2STR(sta->addr));
3454 return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
3455 }
3456 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003457 sta->hs20_ie = NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08003458 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003459
3460 wpabuf_free(sta->roaming_consortium);
3461 if (elems.roaming_cons_sel)
3462 sta->roaming_consortium = wpabuf_alloc_copy(
3463 elems.roaming_cons_sel + 4,
3464 elems.roaming_cons_sel_len - 4);
3465 else
3466 sta->roaming_consortium = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003467#endif /* CONFIG_HS20 */
3468
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003469#ifdef CONFIG_FST
3470 wpabuf_free(sta->mb_ies);
3471 if (hapd->iface->fst)
3472 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
3473 else
3474 sta->mb_ies = NULL;
3475#endif /* CONFIG_FST */
3476
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003477#ifdef CONFIG_MBO
3478 mbo_ap_check_sta_assoc(hapd, sta, &elems);
3479
3480 if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
3481 elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
3482 hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3483 wpa_printf(MSG_INFO,
3484 "MBO: Reject WPA2 association without PMF");
3485 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3486 }
3487#endif /* CONFIG_MBO */
3488
Hai Shalom74f70d42019-02-11 14:42:39 -08003489#if defined(CONFIG_FILS) && defined(CONFIG_OCV)
3490 if (wpa_auth_uses_ocv(sta->wpa_sm) &&
3491 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3492 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3493 sta->auth_alg == WLAN_AUTH_FILS_PK)) {
3494 struct wpa_channel_info ci;
3495 int tx_chanwidth;
3496 int tx_seg1_idx;
3497
3498 if (hostapd_drv_channel_info(hapd, &ci) != 0) {
3499 wpa_printf(MSG_WARNING,
3500 "Failed to get channel info to validate received OCI in FILS (Re)Association Request frame");
3501 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3502 }
3503
3504 if (get_sta_tx_parameters(sta->wpa_sm,
3505 channel_width_to_int(ci.chanwidth),
3506 ci.seg1_idx, &tx_chanwidth,
3507 &tx_seg1_idx) < 0)
3508 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3509
3510 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
3511 tx_chanwidth, tx_seg1_idx) != 0) {
3512 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
3513 return WLAN_STATUS_UNSPECIFIED_FAILURE;
3514 }
3515 }
3516#endif /* CONFIG_FILS && CONFIG_OCV */
3517
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003518 ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
3519 elems.supp_op_classes_len);
3520
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003521 if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
3522 elems.rrm_enabled &&
3523 elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
3524 os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
3525 sizeof(sta->rrm_enabled_capa));
3526
Roshan Pius3a1667e2018-07-03 15:17:14 -07003527 if (elems.power_capab) {
3528 sta->min_tx_power = elems.power_capab[0];
3529 sta->max_tx_power = elems.power_capab[1];
3530 sta->power_capab = 1;
3531 } else {
3532 sta->power_capab = 0;
3533 }
3534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535 return WLAN_STATUS_SUCCESS;
3536}
3537
3538
3539static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
3540 u16 reason_code)
3541{
3542 int send_len;
3543 struct ieee80211_mgmt reply;
3544
3545 os_memset(&reply, 0, sizeof(reply));
3546 reply.frame_control =
3547 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
3548 os_memcpy(reply.da, addr, ETH_ALEN);
3549 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
3550 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
3551
3552 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
3553 reply.u.deauth.reason_code = host_to_le16(reason_code);
3554
Hai Shalomfdcde762020-04-02 11:19:20 -07003555 if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0, NULL, 0, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
3557 strerror(errno));
3558}
3559
3560
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003561static int add_associated_sta(struct hostapd_data *hapd,
Hai Shalom74f70d42019-02-11 14:42:39 -08003562 struct sta_info *sta, int reassoc)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003563{
3564 struct ieee80211_ht_capabilities ht_cap;
3565 struct ieee80211_vht_capabilities vht_cap;
Hai Shalom81f62d82019-07-22 12:10:00 -07003566 struct ieee80211_he_capabilities he_cap;
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003567 int set = 1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003568
3569 /*
3570 * Remove the STA entry to ensure the STA PS state gets cleared and
3571 * configuration gets updated. This is relevant for cases, such as
3572 * FT-over-the-DS, where a station re-associates back to the same AP but
3573 * skips the authentication flow, or if working with a driver that
3574 * does not support full AP client state.
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003575 *
3576 * Skip this if the STA has already completed FT reassociation and the
3577 * TK has been configured since the TX/RX PN must not be reset to 0 for
3578 * the same key.
Hai Shalom74f70d42019-02-11 14:42:39 -08003579 *
3580 * FT-over-the-DS has a special case where the STA entry (and as such,
3581 * the TK) has not yet been configured to the driver depending on which
3582 * driver interface is used. For that case, allow add-STA operation to
3583 * be used (instead of set-STA). This is needed to allow mac80211-based
3584 * drivers to accept the STA parameter configuration. Since this is
3585 * after a new FT-over-DS exchange, a new TK has been derived, so key
3586 * reinstallation is not a concern for this case.
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003587 */
Hai Shalom74f70d42019-02-11 14:42:39 -08003588 wpa_printf(MSG_DEBUG, "Add associated STA " MACSTR
3589 " (added_unassoc=%d auth_alg=%u ft_over_ds=%u reassoc=%d authorized=%d ft_tk=%d fils_tk=%d)",
3590 MAC2STR(sta->addr), sta->added_unassoc, sta->auth_alg,
3591 sta->ft_over_ds, reassoc,
3592 !!(sta->flags & WLAN_STA_AUTHORIZED),
3593 wpa_auth_sta_ft_tk_already_set(sta->wpa_sm),
3594 wpa_auth_sta_fils_tk_already_set(sta->wpa_sm));
3595
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003596 if (!sta->added_unassoc &&
3597 (!(sta->flags & WLAN_STA_AUTHORIZED) ||
Hai Shalom74f70d42019-02-11 14:42:39 -08003598 (reassoc && sta->ft_over_ds && sta->auth_alg == WLAN_AUTH_FT) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003599 (!wpa_auth_sta_ft_tk_already_set(sta->wpa_sm) &&
3600 !wpa_auth_sta_fils_tk_already_set(sta->wpa_sm)))) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003601 hostapd_drv_sta_remove(hapd, sta->addr);
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003602 wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
3603 set = 0;
Hai Shalom74f70d42019-02-11 14:42:39 -08003604
3605 /* Do not allow the FT-over-DS exception to be used more than
3606 * once per authentication exchange to guarantee a new TK is
3607 * used here */
3608 sta->ft_over_ds = 0;
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003609 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003610
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003611 if (sta->flags & WLAN_STA_HT)
3612 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003613#ifdef CONFIG_IEEE80211AC
3614 if (sta->flags & WLAN_STA_VHT)
3615 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
3616#endif /* CONFIG_IEEE80211AC */
Hai Shalom81f62d82019-07-22 12:10:00 -07003617#ifdef CONFIG_IEEE80211AX
3618 if (sta->flags & WLAN_STA_HE) {
3619 hostapd_get_he_capab(hapd, sta->he_capab, &he_cap,
3620 sta->he_capab_len);
3621 }
3622#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003623
3624 /*
3625 * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
3626 * will be set when the ACK frame for the (Re)Association Response frame
3627 * is processed (TX status driver event).
3628 */
3629 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
3630 sta->supported_rates, sta->supported_rates_len,
3631 sta->listen_interval,
3632 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
3633 sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
Hai Shalom81f62d82019-07-22 12:10:00 -07003634 sta->flags & WLAN_STA_HE ? &he_cap : NULL,
3635 sta->flags & WLAN_STA_HE ? sta->he_capab_len : 0,
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003636 sta->he_6ghz_capab,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003637 sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003638 sta->vht_opmode, sta->p2p_ie ? 1 : 0,
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003639 set)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003640 hostapd_logger(hapd, sta->addr,
3641 HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
3642 "Could not %s STA to kernel driver",
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02003643 set ? "set" : "add");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003644
3645 if (sta->added_unassoc) {
3646 hostapd_drv_sta_remove(hapd, sta->addr);
3647 sta->added_unassoc = 0;
3648 }
3649
3650 return -1;
3651 }
3652
3653 sta->added_unassoc = 0;
3654
3655 return 0;
3656}
3657
3658
3659static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
Dmitry Shmidt29333592017-01-09 12:27:11 -08003660 const u8 *addr, u16 status_code, int reassoc,
Hai Shalomfdcde762020-04-02 11:19:20 -07003661 const u8 *ies, size_t ies_len, int rssi,
3662 int omit_rsnxe)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663{
3664 int send_len;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003665 u8 *buf;
3666 size_t buflen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667 struct ieee80211_mgmt *reply;
3668 u8 *p;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003669 u16 res = WLAN_STATUS_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003671 buflen = sizeof(struct ieee80211_mgmt) + 1024;
3672#ifdef CONFIG_FILS
3673 if (sta && sta->fils_hlp_resp)
3674 buflen += wpabuf_len(sta->fils_hlp_resp);
Hai Shalom81f62d82019-07-22 12:10:00 -07003675 if (sta)
3676 buflen += 150;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003677#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003678#ifdef CONFIG_OWE
3679 if (sta && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
3680 buflen += 150;
3681#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07003682#ifdef CONFIG_DPP2
3683 if (sta && sta->dpp_pfs)
3684 buflen += 5 + sta->dpp_pfs->curve->prime_len;
3685#endif /* CONFIG_DPP2 */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003686 buf = os_zalloc(buflen);
3687 if (!buf) {
3688 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3689 goto done;
3690 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691 reply = (struct ieee80211_mgmt *) buf;
3692 reply->frame_control =
3693 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
3694 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
3695 WLAN_FC_STYPE_ASSOC_RESP));
Dmitry Shmidt29333592017-01-09 12:27:11 -08003696 os_memcpy(reply->da, addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
3698 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
3699
3700 send_len = IEEE80211_HDRLEN;
3701 send_len += sizeof(reply->u.assoc_resp);
3702 reply->u.assoc_resp.capab_info =
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003703 host_to_le16(hostapd_own_capab_info(hapd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704 reply->u.assoc_resp.status_code = host_to_le16(status_code);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003705
3706 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0) |
3707 BIT(14) | BIT(15));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708 /* Supported rates */
3709 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
3710 /* Extended supported rates */
3711 p = hostapd_eid_ext_supp_rates(hapd, p);
3712
Hai Shalomfdcde762020-04-02 11:19:20 -07003713 /* Radio measurement capabilities */
3714 p = hostapd_eid_rm_enabled_capab(hapd, p, buf + buflen - p);
3715
Hai Shalom74f70d42019-02-11 14:42:39 -08003716#ifdef CONFIG_MBO
3717 if (status_code == WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
3718 rssi != 0) {
3719 int delta = hapd->iconf->rssi_reject_assoc_rssi - rssi;
3720
3721 p = hostapd_eid_mbo_rssi_assoc_rej(hapd, p, buf + buflen - p,
3722 delta);
3723 }
3724#endif /* CONFIG_MBO */
3725
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003726#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt29333592017-01-09 12:27:11 -08003727 if (sta && status_code == WLAN_STATUS_SUCCESS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003728 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
3729 * Transition Information, RSN, [RIC Response] */
3730 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003731 buf + buflen - p,
Hai Shalomfdcde762020-04-02 11:19:20 -07003732 sta->auth_alg, ies, ies_len,
3733 omit_rsnxe);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003734 if (!p) {
3735 wpa_printf(MSG_DEBUG,
3736 "FT: Failed to write AssocResp IEs");
3737 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3738 goto done;
3739 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003741#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom81f62d82019-07-22 12:10:00 -07003742#ifdef CONFIG_FILS
3743 if (sta && status_code == WLAN_STATUS_SUCCESS &&
3744 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
3745 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3746 sta->auth_alg == WLAN_AUTH_FILS_PK))
3747 p = wpa_auth_write_assoc_resp_fils(sta->wpa_sm, p,
3748 buf + buflen - p,
3749 ies, ies_len);
3750#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003752#ifdef CONFIG_OWE
Hai Shalom74f70d42019-02-11 14:42:39 -08003753 if (sta && status_code == WLAN_STATUS_SUCCESS &&
3754 (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003755 p = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, p,
3756 buf + buflen - p,
3757 ies, ies_len);
3758#endif /* CONFIG_OWE */
3759
Dmitry Shmidt29333592017-01-09 12:27:11 -08003760 if (sta && status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763 p = hostapd_eid_ht_capabilities(hapd, p);
3764 p = hostapd_eid_ht_operation(hapd, p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003765
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003766#ifdef CONFIG_IEEE80211AC
Hai Shalomc3565922019-10-28 11:58:20 -07003767 if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac &&
3768 !is_6ghz_op_class(hapd->iconf->op_class)) {
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003769 u32 nsts = 0, sta_nsts;
3770
Dmitry Shmidt29333592017-01-09 12:27:11 -08003771 if (sta && hapd->conf->use_sta_nsts && sta->vht_capabilities) {
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003772 struct ieee80211_vht_capabilities *capa;
3773
3774 nsts = (hapd->iface->conf->vht_capab >>
3775 VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
3776 capa = sta->vht_capabilities;
3777 sta_nsts = (le_to_host32(capa->vht_capabilities_info) >>
3778 VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
3779
3780 if (nsts < sta_nsts)
3781 nsts = 0;
3782 else
3783 nsts = sta_nsts;
3784 }
3785 p = hostapd_eid_vht_capabilities(hapd, p, nsts);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003786 p = hostapd_eid_vht_operation(hapd, p);
3787 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003788#endif /* CONFIG_IEEE80211AC */
3789
Hai Shalom81f62d82019-07-22 12:10:00 -07003790#ifdef CONFIG_IEEE80211AX
3791 if (hapd->iconf->ieee80211ax) {
3792 p = hostapd_eid_he_capab(hapd, p, IEEE80211_MODE_AP);
3793 p = hostapd_eid_he_operation(hapd, p);
3794 p = hostapd_eid_spatial_reuse(hapd, p);
3795 p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003796 p = hostapd_eid_he_6ghz_band_cap(hapd, p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003797 }
3798#endif /* CONFIG_IEEE80211AX */
3799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800 p = hostapd_eid_ext_capab(hapd, p);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003801 p = hostapd_eid_bss_max_idle_period(hapd, p);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003802 if (sta && sta->qos_map_enabled)
Dmitry Shmidt051af732013-10-22 13:52:46 -07003803 p = hostapd_eid_qos_map_set(hapd, p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003804
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003805#ifdef CONFIG_FST
3806 if (hapd->iface->fst_ies) {
3807 os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
3808 wpabuf_len(hapd->iface->fst_ies));
3809 p += wpabuf_len(hapd->iface->fst_ies);
3810 }
3811#endif /* CONFIG_FST */
3812
Hai Shalomfdcde762020-04-02 11:19:20 -07003813#ifdef CONFIG_TESTING_OPTIONS
3814 if (hapd->conf->rsnxe_override_ft &&
3815 buf + buflen - p >=
3816 (long int) wpabuf_len(hapd->conf->rsnxe_override_ft) &&
3817 sta && sta->auth_alg == WLAN_AUTH_FT) {
3818 wpa_printf(MSG_DEBUG, "TESTING: RSNXE FT override");
3819 os_memcpy(p, wpabuf_head(hapd->conf->rsnxe_override_ft),
3820 wpabuf_len(hapd->conf->rsnxe_override_ft));
3821 p += wpabuf_len(hapd->conf->rsnxe_override_ft);
3822 goto rsnxe_done;
3823 }
3824#endif /* CONFIG_TESTING_OPTIONS */
3825 if (!omit_rsnxe)
3826 p = hostapd_eid_rsnxe(hapd, p, buf + buflen - p);
3827#ifdef CONFIG_TESTING_OPTIONS
3828rsnxe_done:
3829#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalomc3565922019-10-28 11:58:20 -07003830
Hai Shalom021b0b52019-04-10 11:17:58 -07003831#ifdef CONFIG_OWE
3832 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
3833 sta && sta->owe_ecdh && status_code == WLAN_STATUS_SUCCESS &&
3834 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE) {
3835 struct wpabuf *pub;
3836
3837 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
3838 if (!pub) {
3839 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3840 goto done;
3841 }
3842 /* OWE Diffie-Hellman Parameter element */
3843 *p++ = WLAN_EID_EXTENSION; /* Element ID */
3844 *p++ = 1 + 2 + wpabuf_len(pub); /* Length */
3845 *p++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension */
3846 WPA_PUT_LE16(p, sta->owe_group);
3847 p += 2;
3848 os_memcpy(p, wpabuf_head(pub), wpabuf_len(pub));
3849 p += wpabuf_len(pub);
3850 wpabuf_free(pub);
3851 }
3852#endif /* CONFIG_OWE */
3853
3854#ifdef CONFIG_DPP2
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003855 if (DPP_VERSION > 1 && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
Hai Shalom021b0b52019-04-10 11:17:58 -07003856 sta && sta->dpp_pfs && status_code == WLAN_STATUS_SUCCESS &&
3857 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP) {
3858 os_memcpy(p, wpabuf_head(sta->dpp_pfs->ie),
3859 wpabuf_len(sta->dpp_pfs->ie));
3860 p += wpabuf_len(sta->dpp_pfs->ie);
3861 }
3862#endif /* CONFIG_DPP2 */
3863
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003864#ifdef CONFIG_IEEE80211AC
Dmitry Shmidt29333592017-01-09 12:27:11 -08003865 if (sta && hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003866 p = hostapd_eid_vendor_vht(hapd, p);
3867#endif /* CONFIG_IEEE80211AC */
3868
Dmitry Shmidt29333592017-01-09 12:27:11 -08003869 if (sta && (sta->flags & WLAN_STA_WMM))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870 p = hostapd_eid_wmm(hapd, p);
3871
3872#ifdef CONFIG_WPS
Dmitry Shmidt29333592017-01-09 12:27:11 -08003873 if (sta &&
3874 ((sta->flags & WLAN_STA_WPS) ||
3875 ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 struct wpabuf *wps = wps_build_assoc_resp_ie();
3877 if (wps) {
3878 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
3879 p += wpabuf_len(wps);
3880 wpabuf_free(wps);
3881 }
3882 }
3883#endif /* CONFIG_WPS */
3884
Hai Shalom74f70d42019-02-11 14:42:39 -08003885 if (sta && (sta->flags & WLAN_STA_MULTI_AP))
3886 p = hostapd_eid_multi_ap(hapd, p);
3887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888#ifdef CONFIG_P2P
Dmitry Shmidt29333592017-01-09 12:27:11 -08003889 if (sta && sta->p2p_ie && hapd->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003890 struct wpabuf *p2p_resp_ie;
3891 enum p2p_status_code status;
3892 switch (status_code) {
3893 case WLAN_STATUS_SUCCESS:
3894 status = P2P_SC_SUCCESS;
3895 break;
3896 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
3897 status = P2P_SC_FAIL_LIMIT_REACHED;
3898 break;
3899 default:
3900 status = P2P_SC_FAIL_INVALID_PARAMS;
3901 break;
3902 }
3903 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
3904 if (p2p_resp_ie) {
3905 os_memcpy(p, wpabuf_head(p2p_resp_ie),
3906 wpabuf_len(p2p_resp_ie));
3907 p += wpabuf_len(p2p_resp_ie);
3908 wpabuf_free(p2p_resp_ie);
3909 }
3910 }
3911#endif /* CONFIG_P2P */
3912
3913#ifdef CONFIG_P2P_MANAGER
3914 if (hapd->conf->p2p & P2P_MANAGE)
3915 p = hostapd_eid_p2p_manage(hapd, p);
3916#endif /* CONFIG_P2P_MANAGER */
3917
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003918 p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003919
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003920 if (hapd->conf->assocresp_elements &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003921 (size_t) (buf + buflen - p) >=
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003922 wpabuf_len(hapd->conf->assocresp_elements)) {
3923 os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
3924 wpabuf_len(hapd->conf->assocresp_elements));
3925 p += wpabuf_len(hapd->conf->assocresp_elements);
3926 }
3927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 send_len += p - reply->u.assoc_resp.variable;
3929
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003930#ifdef CONFIG_FILS
Dmitry Shmidt29333592017-01-09 12:27:11 -08003931 if (sta &&
3932 (sta->auth_alg == WLAN_AUTH_FILS_SK ||
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003933 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
3934 sta->auth_alg == WLAN_AUTH_FILS_PK) &&
3935 status_code == WLAN_STATUS_SUCCESS) {
3936 struct ieee802_11_elems elems;
3937
3938 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003939 ParseFailed || !elems.fils_session) {
3940 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3941 goto done;
3942 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003943
3944 /* FILS Session */
3945 *p++ = WLAN_EID_EXTENSION; /* Element ID */
3946 *p++ = 1 + FILS_SESSION_LEN; /* Length */
3947 *p++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
3948 os_memcpy(p, elems.fils_session, FILS_SESSION_LEN);
3949 send_len += 2 + 1 + FILS_SESSION_LEN;
3950
3951 send_len = fils_encrypt_assoc(sta->wpa_sm, buf, send_len,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003952 buflen, sta->fils_hlp_resp);
3953 if (send_len < 0) {
3954 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
3955 goto done;
3956 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003957 }
3958#endif /* CONFIG_FILS */
3959
Hai Shalomfdcde762020-04-02 11:19:20 -07003960 if (hostapd_drv_send_mlme(hapd, reply, send_len, 0, NULL, 0, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
3962 strerror(errno));
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003963 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003964 }
3965
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003966done:
3967 os_free(buf);
3968 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969}
3970
3971
Roshan Pius3a1667e2018-07-03 15:17:14 -07003972#ifdef CONFIG_OWE
3973u8 * owe_assoc_req_process(struct hostapd_data *hapd, struct sta_info *sta,
3974 const u8 *owe_dh, u8 owe_dh_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07003975 u8 *owe_buf, size_t owe_buf_len, u16 *status)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003976{
3977#ifdef CONFIG_TESTING_OPTIONS
3978 if (hapd->conf->own_ie_override) {
3979 wpa_printf(MSG_DEBUG, "OWE: Using IE override");
Hai Shalomfdcde762020-04-02 11:19:20 -07003980 *status = WLAN_STATUS_SUCCESS;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003981 return wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3982 owe_buf_len, NULL, 0);
3983 }
3984#endif /* CONFIG_TESTING_OPTIONS */
3985
3986 if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
3987 wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
3988 owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
3989 owe_buf_len, NULL, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -07003990 *status = WLAN_STATUS_SUCCESS;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003991 return owe_buf;
3992 }
3993
Hai Shalom81f62d82019-07-22 12:10:00 -07003994 if (sta->owe_pmk && sta->external_dh_updated) {
3995 wpa_printf(MSG_DEBUG, "OWE: Using previously derived PMK");
Hai Shalomfdcde762020-04-02 11:19:20 -07003996 *status = WLAN_STATUS_SUCCESS;
Hai Shalom81f62d82019-07-22 12:10:00 -07003997 return owe_buf;
3998 }
3999
Hai Shalomfdcde762020-04-02 11:19:20 -07004000 *status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
4001 if (*status != WLAN_STATUS_SUCCESS)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004002 return NULL;
4003
4004 owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
4005 owe_buf_len, NULL, 0);
4006
4007 if (sta->owe_ecdh && owe_buf) {
4008 struct wpabuf *pub;
4009
4010 pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
4011 if (!pub) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004012 *status = WLAN_STATUS_UNSPECIFIED_FAILURE;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004013 return owe_buf;
4014 }
4015
4016 /* OWE Diffie-Hellman Parameter element */
4017 *owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
4018 *owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
4019 *owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
4020 */
4021 WPA_PUT_LE16(owe_buf, sta->owe_group);
4022 owe_buf += 2;
4023 os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
4024 owe_buf += wpabuf_len(pub);
4025 wpabuf_free(pub);
4026 }
4027
4028 return owe_buf;
4029}
4030#endif /* CONFIG_OWE */
4031
4032
Paul Stewart092955c2017-02-06 09:13:09 -08004033#ifdef CONFIG_FILS
4034
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004035void fils_hlp_finish_assoc(struct hostapd_data *hapd, struct sta_info *sta)
Paul Stewart092955c2017-02-06 09:13:09 -08004036{
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004037 u16 reply_res;
Paul Stewart092955c2017-02-06 09:13:09 -08004038
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004039 wpa_printf(MSG_DEBUG, "FILS: Finish association with " MACSTR,
4040 MAC2STR(sta->addr));
4041 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4042 if (!sta->fils_pending_assoc_req)
Paul Stewart092955c2017-02-06 09:13:09 -08004043 return;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004044 reply_res = send_assoc_resp(hapd, sta, sta->addr, WLAN_STATUS_SUCCESS,
4045 sta->fils_pending_assoc_is_reassoc,
4046 sta->fils_pending_assoc_req,
Hai Shalomfdcde762020-04-02 11:19:20 -07004047 sta->fils_pending_assoc_req_len, 0, 0);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004048 os_free(sta->fils_pending_assoc_req);
4049 sta->fils_pending_assoc_req = NULL;
4050 sta->fils_pending_assoc_req_len = 0;
4051 wpabuf_free(sta->fils_hlp_resp);
4052 sta->fils_hlp_resp = NULL;
4053 wpabuf_free(sta->hlp_dhcp_discover);
4054 sta->hlp_dhcp_discover = NULL;
Paul Stewart092955c2017-02-06 09:13:09 -08004055
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004056 /*
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004057 * Remove the station in case transmission of a success response fails.
4058 * At this point the station was already added associated to the driver.
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004059 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004060 if (reply_res != WLAN_STATUS_SUCCESS)
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004061 hostapd_drv_sta_remove(hapd, sta->addr);
Paul Stewart092955c2017-02-06 09:13:09 -08004062}
4063
4064
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004065void fils_hlp_timeout(void *eloop_ctx, void *eloop_data)
Paul Stewart092955c2017-02-06 09:13:09 -08004066{
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004067 struct hostapd_data *hapd = eloop_ctx;
4068 struct sta_info *sta = eloop_data;
Paul Stewart092955c2017-02-06 09:13:09 -08004069
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004070 wpa_printf(MSG_DEBUG,
4071 "FILS: HLP response timeout - continue with association response for "
4072 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004073 if (sta->fils_drv_assoc_finish)
4074 hostapd_notify_assoc_fils_finish(hapd, sta);
4075 else
4076 fils_hlp_finish_assoc(hapd, sta);
Paul Stewart092955c2017-02-06 09:13:09 -08004077}
4078
4079#endif /* CONFIG_FILS */
4080
4081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082static void handle_assoc(struct hostapd_data *hapd,
4083 const struct ieee80211_mgmt *mgmt, size_t len,
Hai Shalom74f70d42019-02-11 14:42:39 -08004084 int reassoc, int rssi)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004086 u16 capab_info, listen_interval, seq_ctrl, fc;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004087 int resp = WLAN_STATUS_SUCCESS;
4088 u16 reply_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089 const u8 *pos;
4090 int left, i;
4091 struct sta_info *sta;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004092 u8 *tmp = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004093#ifdef CONFIG_FILS
4094 int delay_assoc = 0;
4095#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07004096 int omit_rsnxe = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097
4098 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
4099 sizeof(mgmt->u.assoc_req))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004100 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
4101 reassoc, (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 return;
4103 }
4104
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004105#ifdef CONFIG_TESTING_OPTIONS
4106 if (reassoc) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004107 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004108 drand48() < hapd->iconf->ignore_reassoc_probability) {
4109 wpa_printf(MSG_INFO,
4110 "TESTING: ignoring reassoc request from "
4111 MACSTR, MAC2STR(mgmt->sa));
4112 return;
4113 }
4114 } else {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004115 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004116 drand48() < hapd->iconf->ignore_assoc_probability) {
4117 wpa_printf(MSG_INFO,
4118 "TESTING: ignoring assoc request from "
4119 MACSTR, MAC2STR(mgmt->sa));
4120 return;
4121 }
4122 }
4123#endif /* CONFIG_TESTING_OPTIONS */
4124
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004125 fc = le_to_host16(mgmt->frame_control);
4126 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 if (reassoc) {
4129 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
4130 listen_interval = le_to_host16(
4131 mgmt->u.reassoc_req.listen_interval);
4132 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
4133 " capab_info=0x%02x listen_interval=%d current_ap="
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004134 MACSTR " seq_ctrl=0x%x%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135 MAC2STR(mgmt->sa), capab_info, listen_interval,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004136 MAC2STR(mgmt->u.reassoc_req.current_ap),
4137 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
4139 pos = mgmt->u.reassoc_req.variable;
4140 } else {
4141 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
4142 listen_interval = le_to_host16(
4143 mgmt->u.assoc_req.listen_interval);
4144 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004145 " capab_info=0x%02x listen_interval=%d "
4146 "seq_ctrl=0x%x%s",
4147 MAC2STR(mgmt->sa), capab_info, listen_interval,
4148 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
4150 pos = mgmt->u.assoc_req.variable;
4151 }
4152
4153 sta = ap_get_sta(hapd, mgmt->sa);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004154#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
4156 (sta->flags & WLAN_STA_AUTH) == 0) {
4157 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
4158 "prior to authentication since it is using "
4159 "over-the-DS FT", MAC2STR(mgmt->sa));
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004160
4161 /*
4162 * Mark station as authenticated, to avoid adding station
4163 * entry in the driver as associated and not authenticated
4164 */
4165 sta->flags |= WLAN_STA_AUTH;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166 } else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004167#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
Dmitry Shmidt29333592017-01-09 12:27:11 -08004169 if (hapd->iface->current_mode &&
4170 hapd->iface->current_mode->mode ==
4171 HOSTAPD_MODE_IEEE80211AD) {
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004172 int acl_res;
Hai Shalomfdcde762020-04-02 11:19:20 -07004173 struct radius_sta info;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004174
Hai Shalomfdcde762020-04-02 11:19:20 -07004175 acl_res = ieee802_11_allowed_address(hapd, mgmt->sa,
4176 (const u8 *) mgmt,
4177 len, &info);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004178 if (acl_res == HOSTAPD_ACL_REJECT) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004179 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
4180 "Ignore Association Request frame from "
4181 MACSTR " due to ACL reject",
4182 MAC2STR(mgmt->sa));
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004183 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4184 goto fail;
4185 }
4186 if (acl_res == HOSTAPD_ACL_PENDING)
4187 return;
4188
Dmitry Shmidt29333592017-01-09 12:27:11 -08004189 /* DMG/IEEE 802.11ad does not use authentication.
4190 * Allocate sta entry upon association. */
4191 sta = ap_sta_add(hapd, mgmt->sa);
4192 if (!sta) {
4193 hostapd_logger(hapd, mgmt->sa,
4194 HOSTAPD_MODULE_IEEE80211,
4195 HOSTAPD_LEVEL_INFO,
4196 "Failed to add STA");
4197 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4198 goto fail;
4199 }
4200
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004201 acl_res = ieee802_11_set_radius_info(
Hai Shalomfdcde762020-04-02 11:19:20 -07004202 hapd, sta, acl_res, &info);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004203 if (acl_res) {
4204 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4205 goto fail;
4206 }
4207
Dmitry Shmidt29333592017-01-09 12:27:11 -08004208 hostapd_logger(hapd, sta->addr,
4209 HOSTAPD_MODULE_IEEE80211,
4210 HOSTAPD_LEVEL_DEBUG,
4211 "Skip authentication for DMG/IEEE 802.11ad");
4212 sta->flags |= WLAN_STA_AUTH;
4213 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
4214 sta->auth_alg = WLAN_AUTH_OPEN;
4215 } else {
4216 hostapd_logger(hapd, mgmt->sa,
4217 HOSTAPD_MODULE_IEEE80211,
4218 HOSTAPD_LEVEL_INFO,
4219 "Station tried to associate before authentication (aid=%d flags=0x%x)",
4220 sta ? sta->aid : -1,
4221 sta ? sta->flags : 0);
4222 send_deauth(hapd, mgmt->sa,
4223 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
4224 return;
4225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004226 }
4227
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004228 if ((fc & WLAN_FC_RETRY) &&
4229 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4230 sta->last_seq_ctrl == seq_ctrl &&
Paul Stewart092955c2017-02-06 09:13:09 -08004231 sta->last_subtype == (reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
4232 WLAN_FC_STYPE_ASSOC_REQ)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004233 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4234 HOSTAPD_LEVEL_DEBUG,
4235 "Drop repeated association frame seq_ctrl=0x%x",
4236 seq_ctrl);
4237 return;
4238 }
4239 sta->last_seq_ctrl = seq_ctrl;
4240 sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
4241 WLAN_FC_STYPE_ASSOC_REQ;
4242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 if (hapd->tkip_countermeasures) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004244 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004245 goto fail;
4246 }
4247
4248 if (listen_interval > hapd->conf->max_listen_interval) {
4249 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4250 HOSTAPD_LEVEL_DEBUG,
4251 "Too large Listen Interval (%d)",
4252 listen_interval);
4253 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
4254 goto fail;
4255 }
4256
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004257#ifdef CONFIG_MBO
4258 if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
4259 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4260 goto fail;
4261 }
Hai Shalom74f70d42019-02-11 14:42:39 -08004262
4263 if (hapd->iconf->rssi_reject_assoc_rssi && rssi &&
4264 rssi < hapd->iconf->rssi_reject_assoc_rssi &&
4265 (sta->auth_rssi == 0 ||
4266 sta->auth_rssi < hapd->iconf->rssi_reject_assoc_rssi)) {
4267 resp = WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS;
4268 goto fail;
4269 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004270#endif /* CONFIG_MBO */
4271
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004272 /*
4273 * sta->capability is used in check_assoc_ies() for RRM enabled
4274 * capability element.
4275 */
4276 sta->capability = capab_info;
4277
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004278#ifdef CONFIG_FILS
4279 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4280 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
4281 sta->auth_alg == WLAN_AUTH_FILS_PK) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004282 int res;
4283
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004284 /* The end of the payload is encrypted. Need to decrypt it
4285 * before parsing. */
4286
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004287 tmp = os_memdup(pos, left);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004288 if (!tmp) {
4289 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4290 goto fail;
4291 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004292
Roshan Pius3a1667e2018-07-03 15:17:14 -07004293 res = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
4294 len, tmp, left);
4295 if (res < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004296 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4297 goto fail;
4298 }
4299 pos = tmp;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004300 left = res;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004301 }
4302#endif /* CONFIG_FILS */
4303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004304 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
4305 * is used */
4306 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
4307 if (resp != WLAN_STATUS_SUCCESS)
4308 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07004309 omit_rsnxe = !get_ie(pos, left, WLAN_EID_RSNX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310
4311 if (hostapd_get_aid(hapd, sta) < 0) {
4312 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4313 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
4314 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4315 goto fail;
4316 }
4317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 sta->listen_interval = listen_interval;
4319
Roshan Pius3a1667e2018-07-03 15:17:14 -07004320 if (hapd->iface->current_mode &&
4321 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 sta->flags |= WLAN_STA_NONERP;
4323 for (i = 0; i < sta->supported_rates_len; i++) {
4324 if ((sta->supported_rates[i] & 0x7f) > 22) {
4325 sta->flags &= ~WLAN_STA_NONERP;
4326 break;
4327 }
4328 }
4329 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
4330 sta->nonerp_set = 1;
4331 hapd->iface->num_sta_non_erp++;
4332 if (hapd->iface->num_sta_non_erp == 1)
4333 ieee802_11_set_beacons(hapd->iface);
4334 }
4335
4336 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
4337 !sta->no_short_slot_time_set) {
4338 sta->no_short_slot_time_set = 1;
4339 hapd->iface->num_sta_no_short_slot_time++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004340 if (hapd->iface->current_mode &&
4341 hapd->iface->current_mode->mode ==
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004342 HOSTAPD_MODE_IEEE80211G &&
4343 hapd->iface->num_sta_no_short_slot_time == 1)
4344 ieee802_11_set_beacons(hapd->iface);
4345 }
4346
4347 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4348 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
4349 else
4350 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
4351
4352 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
4353 !sta->no_short_preamble_set) {
4354 sta->no_short_preamble_set = 1;
4355 hapd->iface->num_sta_no_short_preamble++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004356 if (hapd->iface->current_mode &&
4357 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358 && hapd->iface->num_sta_no_short_preamble == 1)
4359 ieee802_11_set_beacons(hapd->iface);
4360 }
4361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 update_ht_state(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004363
4364 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4365 HOSTAPD_LEVEL_DEBUG,
4366 "association OK (aid %d)", sta->aid);
4367 /* Station will be marked associated, after it acknowledges AssocResp
4368 */
4369 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
4370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
4372 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
4373 "SA Query procedure", reassoc ? "re" : "");
4374 /* TODO: Send a protected Disassociate frame to the STA using
4375 * the old key and Reason Code "Previous Authentication no
4376 * longer valid". Make sure this is only sent protected since
4377 * unprotected frame would be received by the STA that is now
4378 * trying to associate.
4379 */
4380 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382 /* Make sure that the previously registered inactivity timer will not
4383 * remove the STA immediately. */
4384 sta->timeout_next = STA_NULLFUNC;
4385
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004386#ifdef CONFIG_TAXONOMY
4387 taxonomy_sta_info_assoc_req(hapd, sta, pos, left);
4388#endif /* CONFIG_TAXONOMY */
4389
Dmitry Shmidt29333592017-01-09 12:27:11 -08004390 sta->pending_wds_enable = 0;
4391
Paul Stewart092955c2017-02-06 09:13:09 -08004392#ifdef CONFIG_FILS
4393 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
4394 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004395 sta->auth_alg == WLAN_AUTH_FILS_PK) {
4396 if (fils_process_hlp(hapd, sta, pos, left) > 0)
4397 delay_assoc = 1;
4398 }
Paul Stewart092955c2017-02-06 09:13:09 -08004399#endif /* CONFIG_FILS */
4400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401 fail:
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004402
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004403 /*
4404 * In case of a successful response, add the station to the driver.
4405 * Otherwise, the kernel may ignore Data frames before we process the
4406 * ACK frame (TX status). In case of a failure, this station will be
4407 * removed.
4408 *
4409 * Note that this is not compliant with the IEEE 802.11 standard that
4410 * states that a non-AP station should transition into the
4411 * authenticated/associated state only after the station acknowledges
4412 * the (Re)Association Response frame. However, still do this as:
4413 *
4414 * 1. In case the station does not acknowledge the (Re)Association
4415 * Response frame, it will be removed.
4416 * 2. Data frames will be dropped in the kernel until the station is
4417 * set into authorized state, and there are no significant known
4418 * issues with processing other non-Data Class 3 frames during this
4419 * window.
4420 */
Hai Shalom74f70d42019-02-11 14:42:39 -08004421 if (resp == WLAN_STATUS_SUCCESS && sta &&
4422 add_associated_sta(hapd, sta, reassoc))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004423 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4424
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004425#ifdef CONFIG_FILS
Hai Shalom74f70d42019-02-11 14:42:39 -08004426 if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS &&
4427 eloop_is_timeout_registered(fils_hlp_timeout, hapd, sta) &&
4428 sta->fils_pending_assoc_req) {
4429 /* Do not reschedule fils_hlp_timeout in case the station
4430 * retransmits (Re)Association Request frame while waiting for
4431 * the previously started FILS HLP wait, so that the timeout can
4432 * be determined from the first pending attempt. */
4433 wpa_printf(MSG_DEBUG,
4434 "FILS: Continue waiting for HLP processing before sending (Re)Association Response frame to "
4435 MACSTR, MAC2STR(sta->addr));
4436 os_free(tmp);
4437 return;
4438 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004439 if (sta) {
4440 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4441 os_free(sta->fils_pending_assoc_req);
4442 sta->fils_pending_assoc_req = NULL;
4443 sta->fils_pending_assoc_req_len = 0;
4444 wpabuf_free(sta->fils_hlp_resp);
4445 sta->fils_hlp_resp = NULL;
4446 }
4447 if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS) {
4448 sta->fils_pending_assoc_req = tmp;
4449 sta->fils_pending_assoc_req_len = left;
4450 sta->fils_pending_assoc_is_reassoc = reassoc;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004451 sta->fils_drv_assoc_finish = 0;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004452 wpa_printf(MSG_DEBUG,
4453 "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
4454 MACSTR, MAC2STR(sta->addr));
4455 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
4456 eloop_register_timeout(0, hapd->conf->fils_hlp_wait_time * 1024,
4457 fils_hlp_timeout, hapd, sta);
4458 return;
4459 }
4460#endif /* CONFIG_FILS */
4461
Hai Shalomb755a2a2020-04-23 21:49:02 -07004462 if (resp >= 0)
4463 reply_res = send_assoc_resp(hapd, sta, mgmt->sa, resp, reassoc,
4464 pos, left, rssi, omit_rsnxe);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004465 os_free(tmp);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004466
4467 /*
4468 * Remove the station in case tranmission of a success response fails
4469 * (the STA was added associated to the driver) or if the station was
4470 * previously added unassociated.
4471 */
Dmitry Shmidt29333592017-01-09 12:27:11 -08004472 if (sta && ((reply_res != WLAN_STATUS_SUCCESS &&
4473 resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004474 hostapd_drv_sta_remove(hapd, sta->addr);
4475 sta->added_unassoc = 0;
4476 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004477}
4478
4479
4480static void handle_disassoc(struct hostapd_data *hapd,
4481 const struct ieee80211_mgmt *mgmt, size_t len)
4482{
4483 struct sta_info *sta;
4484
4485 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004486 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
4487 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488 return;
4489 }
4490
4491 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
4492 MAC2STR(mgmt->sa),
4493 le_to_host16(mgmt->u.disassoc.reason_code));
4494
4495 sta = ap_get_sta(hapd, mgmt->sa);
4496 if (sta == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004497 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
4498 MAC2STR(mgmt->sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 return;
4500 }
4501
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004502 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004503 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
Hai Shalomfdcde762020-04-02 11:19:20 -07004505 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
4507 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4508 HOSTAPD_LEVEL_INFO, "disassociated");
4509 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
4510 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
4511 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
4512 * authenticated. */
4513 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004514 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004515 if (sta->ipaddr)
4516 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
4517 ap_sta_ip6addr_del(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004519 sta->added_unassoc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004520
4521 if (sta->timeout_next == STA_NULLFUNC ||
4522 sta->timeout_next == STA_DISASSOC) {
4523 sta->timeout_next = STA_DEAUTH;
4524 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4525 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
4526 hapd, sta);
4527 }
4528
4529 mlme_disassociate_indication(
4530 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
Dmitry Shmidt29333592017-01-09 12:27:11 -08004531
4532 /* DMG/IEEE 802.11ad does not use deauthication. Deallocate sta upon
4533 * disassociation. */
4534 if (hapd->iface->current_mode &&
4535 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
4536 sta->flags &= ~WLAN_STA_AUTH;
4537 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
4538 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4539 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
4540 ap_free_sta(hapd, sta);
4541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542}
4543
4544
4545static void handle_deauth(struct hostapd_data *hapd,
4546 const struct ieee80211_mgmt *mgmt, size_t len)
4547{
4548 struct sta_info *sta;
4549
4550 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004551 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
4552 "payload (len=%lu)", (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553 return;
4554 }
4555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004556 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004557 " reason_code=%d",
4558 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
4559
4560 sta = ap_get_sta(hapd, mgmt->sa);
4561 if (sta == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004562 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
4563 "to deauthenticate, but it is not authenticated",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 MAC2STR(mgmt->sa));
4565 return;
4566 }
4567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004568 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004569 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
4571 WLAN_STA_ASSOC_REQ_OK);
Hai Shalomfdcde762020-04-02 11:19:20 -07004572 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
4574 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4575 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
4576 mlme_deauthenticate_indication(
4577 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
4578 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
4579 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
4580 ap_free_sta(hapd, sta);
4581}
4582
4583
4584static void handle_beacon(struct hostapd_data *hapd,
4585 const struct ieee80211_mgmt *mgmt, size_t len,
4586 struct hostapd_frame_info *fi)
4587{
4588 struct ieee802_11_elems elems;
4589
4590 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004591 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
4592 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 return;
4594 }
4595
4596 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
4597 len - (IEEE80211_HDRLEN +
4598 sizeof(mgmt->u.beacon)), &elems,
4599 0);
4600
4601 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
4602}
4603
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605static int robust_action_frame(u8 category)
4606{
4607 return category != WLAN_ACTION_PUBLIC &&
4608 category != WLAN_ACTION_HT;
4609}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610
4611
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004612static int handle_action(struct hostapd_data *hapd,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004613 const struct ieee80211_mgmt *mgmt, size_t len,
4614 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615{
4616 struct sta_info *sta;
Hai Shalom74f70d42019-02-11 14:42:39 -08004617 u8 *action __maybe_unused;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004618
Hai Shalom74f70d42019-02-11 14:42:39 -08004619 if (len < IEEE80211_HDRLEN + 2 + 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4621 HOSTAPD_LEVEL_DEBUG,
4622 "handle_action - too short payload (len=%lu)",
4623 (unsigned long) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004624 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 }
4626
Hai Shalom74f70d42019-02-11 14:42:39 -08004627 action = (u8 *) &mgmt->u.action.u;
4628 wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
4629 " da " MACSTR " len %d freq %u",
4630 mgmt->u.action.category, *action,
4631 MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) len, freq);
4632
4633 sta = ap_get_sta(hapd, mgmt->sa);
4634
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004635 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
4636 (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
4637 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
4638 "frame (category=%u) from unassociated STA " MACSTR,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004639 mgmt->u.action.category, MAC2STR(mgmt->sa));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004640 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004641 }
4642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 if (sta && (sta->flags & WLAN_STA_MFP) &&
Dmitry Shmidt18463232014-01-24 12:29:41 -08004644 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
4645 robust_action_frame(mgmt->u.action.category)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4647 HOSTAPD_LEVEL_DEBUG,
4648 "Dropped unprotected Robust Action frame from "
4649 "an MFP STA");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004650 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004653 if (sta) {
4654 u16 fc = le_to_host16(mgmt->frame_control);
4655 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4656
4657 if ((fc & WLAN_FC_RETRY) &&
4658 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4659 sta->last_seq_ctrl == seq_ctrl &&
4660 sta->last_subtype == WLAN_FC_STYPE_ACTION) {
4661 hostapd_logger(hapd, sta->addr,
4662 HOSTAPD_MODULE_IEEE80211,
4663 HOSTAPD_LEVEL_DEBUG,
4664 "Drop repeated action frame seq_ctrl=0x%x",
4665 seq_ctrl);
4666 return 1;
4667 }
4668
4669 sta->last_seq_ctrl = seq_ctrl;
4670 sta->last_subtype = WLAN_FC_STYPE_ACTION;
4671 }
4672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 switch (mgmt->u.action.category) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004674#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675 case WLAN_ACTION_FT:
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004676 if (!sta ||
4677 wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678 len - IEEE80211_HDRLEN))
4679 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004680 return 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004681#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 case WLAN_ACTION_WMM:
4683 hostapd_wmm_action(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004684 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685 case WLAN_ACTION_SA_QUERY:
Hai Shalom021b0b52019-04-10 11:17:58 -07004686 ieee802_11_sa_query_action(hapd, mgmt, len);
4687 return 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004688#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004689 case WLAN_ACTION_WNM:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004690 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
4691 return 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004692#endif /* CONFIG_WNM_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004693#ifdef CONFIG_FST
4694 case WLAN_ACTION_FST:
4695 if (hapd->iface->fst)
4696 fst_rx_action(hapd->iface->fst, mgmt, len);
4697 else
4698 wpa_printf(MSG_DEBUG,
4699 "FST: Ignore FST Action frame - no FST attached");
4700 return 1;
4701#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 case WLAN_ACTION_PUBLIC:
Dmitry Shmidt18463232014-01-24 12:29:41 -08004703 case WLAN_ACTION_PROTECTED_DUAL:
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07004704 if (len >= IEEE80211_HDRLEN + 2 &&
4705 mgmt->u.action.u.public_action.action ==
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004706 WLAN_PA_20_40_BSS_COEX) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004707 hostapd_2040_coex_action(hapd, mgmt, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004708 return 1;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004709 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004710#ifdef CONFIG_DPP
4711 if (len >= IEEE80211_HDRLEN + 6 &&
4712 mgmt->u.action.u.vs_public_action.action ==
4713 WLAN_PA_VENDOR_SPECIFIC &&
4714 WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
4715 OUI_WFA &&
4716 mgmt->u.action.u.vs_public_action.variable[0] ==
4717 DPP_OUI_TYPE) {
4718 const u8 *pos, *end;
4719
4720 pos = mgmt->u.action.u.vs_public_action.oui;
4721 end = ((const u8 *) mgmt) + len;
4722 hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004723 freq);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004724 return 1;
4725 }
4726 if (len >= IEEE80211_HDRLEN + 2 &&
4727 (mgmt->u.action.u.public_action.action ==
4728 WLAN_PA_GAS_INITIAL_RESP ||
4729 mgmt->u.action.u.public_action.action ==
4730 WLAN_PA_GAS_COMEBACK_RESP)) {
4731 const u8 *pos, *end;
4732
4733 pos = &mgmt->u.action.u.public_action.action;
4734 end = ((const u8 *) mgmt) + len;
4735 gas_query_ap_rx(hapd->gas, mgmt->sa,
4736 mgmt->u.action.category,
4737 pos, end - pos, hapd->iface->freq);
4738 return 1;
4739 }
4740#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741 if (hapd->public_action_cb) {
4742 hapd->public_action_cb(hapd->public_action_cb_ctx,
4743 (u8 *) mgmt, len,
4744 hapd->iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004746 if (hapd->public_action_cb2) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004747 hapd->public_action_cb2(hapd->public_action_cb2_ctx,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004748 (u8 *) mgmt, len,
4749 hapd->iface->freq);
4750 }
4751 if (hapd->public_action_cb || hapd->public_action_cb2)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004752 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 break;
4754 case WLAN_ACTION_VENDOR_SPECIFIC:
4755 if (hapd->vendor_action_cb) {
4756 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
4757 (u8 *) mgmt, len,
4758 hapd->iface->freq) == 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004759 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760 }
4761 break;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004762 case WLAN_ACTION_RADIO_MEASUREMENT:
4763 hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
4764 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004765 }
4766
4767 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4768 HOSTAPD_LEVEL_DEBUG,
4769 "handle_action - unknown action category %d or invalid "
4770 "frame",
4771 mgmt->u.action.category);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004772 if (!is_multicast_ether_addr(mgmt->da) &&
4773 !(mgmt->u.action.category & 0x80) &&
4774 !is_multicast_ether_addr(mgmt->sa)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775 struct ieee80211_mgmt *resp;
4776
4777 /*
4778 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
4779 * Return the Action frame to the source without change
4780 * except that MSB of the Category set to 1.
4781 */
4782 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
4783 "frame back to sender");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004784 resp = os_memdup(mgmt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785 if (resp == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004786 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787 os_memcpy(resp->da, resp->sa, ETH_ALEN);
4788 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
4789 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
4790 resp->u.action.category |= 0x80;
4791
Hai Shalomfdcde762020-04-02 11:19:20 -07004792 if (hostapd_drv_send_mlme(hapd, resp, len, 0, NULL, 0, 0) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004793 wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
4794 "Action frame");
4795 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796 os_free(resp);
4797 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004798
4799 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800}
4801
4802
4803/**
4804 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
4805 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
4806 * sent to)
4807 * @buf: management frame data (starting from IEEE 802.11 header)
4808 * @len: length of frame data in octets
4809 * @fi: meta data about received frame (signal level, etc.)
4810 *
4811 * Process all incoming IEEE 802.11 management frames. This will be called for
4812 * each frame received from the kernel driver through wlan#ap interface. In
4813 * addition, it can be called to re-inserted pending frames (e.g., when using
4814 * external RADIUS server as an MAC ACL).
4815 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004816int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
4817 struct hostapd_frame_info *fi)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818{
4819 struct ieee80211_mgmt *mgmt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 u16 fc, stype;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004821 int ret = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004822 unsigned int freq;
4823 int ssi_signal = fi ? fi->ssi_signal : 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825 if (len < 24)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004826 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827
Roshan Pius3a1667e2018-07-03 15:17:14 -07004828 if (fi && fi->freq)
4829 freq = fi->freq;
4830 else
4831 freq = hapd->iface->freq;
4832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833 mgmt = (struct ieee80211_mgmt *) buf;
4834 fc = le_to_host16(mgmt->frame_control);
4835 stype = WLAN_FC_GET_STYPE(fc);
4836
Hai Shalomc3565922019-10-28 11:58:20 -07004837 if (is_multicast_ether_addr(mgmt->sa) ||
4838 is_zero_ether_addr(mgmt->sa) ||
4839 os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
4840 /* Do not process any frames with unexpected/invalid SA so that
4841 * we do not add any state for unexpected STA addresses or end
4842 * up sending out frames to unexpected destination. */
4843 wpa_printf(MSG_DEBUG, "MGMT: Invalid SA=" MACSTR
4844 " in received frame - ignore this frame silently",
4845 MAC2STR(mgmt->sa));
4846 return 0;
4847 }
4848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849 if (stype == WLAN_FC_STYPE_BEACON) {
4850 handle_beacon(hapd, mgmt, len, fi);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004851 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004852 }
4853
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07004854 if (!is_broadcast_ether_addr(mgmt->bssid) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004855#ifdef CONFIG_P2P
4856 /* Invitation responses can be sent with the peer MAC as BSSID */
4857 !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
4858 stype == WLAN_FC_STYPE_ACTION) &&
4859#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004860#ifdef CONFIG_MESH
4861 !(hapd->conf->mesh & MESH_ENABLED) &&
4862#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004863 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004864 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
4865 MAC2STR(mgmt->bssid));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004866 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 }
4868
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004869 if (hapd->iface->state != HAPD_IFACE_ENABLED) {
4870 wpa_printf(MSG_DEBUG, "MGMT: Ignore management frame while interface is not enabled (SA=" MACSTR " DA=" MACSTR " subtype=%u)",
4871 MAC2STR(mgmt->sa), MAC2STR(mgmt->da), stype);
4872 return 1;
4873 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874
4875 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004876 handle_probe_req(hapd, mgmt, len, ssi_signal);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004877 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 }
4879
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004880 if ((!is_broadcast_ether_addr(mgmt->da) ||
4881 stype != WLAN_FC_STYPE_ACTION) &&
4882 os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4884 HOSTAPD_LEVEL_DEBUG,
4885 "MGMT: DA=" MACSTR " not our address",
4886 MAC2STR(mgmt->da));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004887 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 }
4889
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004890 if (hapd->iconf->track_sta_max_num)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004891 sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 switch (stype) {
4894 case WLAN_FC_STYPE_AUTH:
4895 wpa_printf(MSG_DEBUG, "mgmt::auth");
Hai Shalom021b0b52019-04-10 11:17:58 -07004896 handle_auth(hapd, mgmt, len, ssi_signal, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004897 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004898 break;
4899 case WLAN_FC_STYPE_ASSOC_REQ:
4900 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
Hai Shalom74f70d42019-02-11 14:42:39 -08004901 handle_assoc(hapd, mgmt, len, 0, ssi_signal);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004902 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004903 break;
4904 case WLAN_FC_STYPE_REASSOC_REQ:
4905 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
Hai Shalom74f70d42019-02-11 14:42:39 -08004906 handle_assoc(hapd, mgmt, len, 1, ssi_signal);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004907 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 break;
4909 case WLAN_FC_STYPE_DISASSOC:
4910 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
4911 handle_disassoc(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004912 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913 break;
4914 case WLAN_FC_STYPE_DEAUTH:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004915 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 handle_deauth(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004917 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918 break;
4919 case WLAN_FC_STYPE_ACTION:
4920 wpa_printf(MSG_DEBUG, "mgmt::action");
Roshan Pius3a1667e2018-07-03 15:17:14 -07004921 ret = handle_action(hapd, mgmt, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922 break;
4923 default:
4924 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
4925 HOSTAPD_LEVEL_DEBUG,
4926 "unknown mgmt frame subtype %d", stype);
4927 break;
4928 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004929
4930 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931}
4932
4933
4934static void handle_auth_cb(struct hostapd_data *hapd,
4935 const struct ieee80211_mgmt *mgmt,
4936 size_t len, int ok)
4937{
4938 u16 auth_alg, auth_transaction, status_code;
4939 struct sta_info *sta;
4940
Hai Shalome5e28bb2019-01-28 14:51:04 -08004941 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
4942 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
4943 (unsigned long) len);
4944
4945 /*
4946 * Initialize status_code here because we are not able to read
4947 * it from the short payload.
4948 */
4949 status_code = WLAN_STATUS_UNSPECIFIED_FAILURE;
4950 goto fail;
4951 }
4952
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004953 sta = ap_get_sta(hapd, mgmt->da);
4954 if (!sta) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004955 wpa_printf(MSG_DEBUG, "handle_auth_cb: STA " MACSTR
4956 " not found",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004957 MAC2STR(mgmt->da));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 return;
4959 }
4960
4961 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4962 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
4963 status_code = le_to_host16(mgmt->u.auth.status_code);
4964
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004965 if (!ok) {
4966 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
4967 HOSTAPD_LEVEL_NOTICE,
4968 "did not acknowledge authentication response");
4969 goto fail;
4970 }
4971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 if (status_code == WLAN_STATUS_SUCCESS &&
4973 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
4974 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
4975 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
4976 HOSTAPD_LEVEL_INFO, "authenticated");
4977 sta->flags |= WLAN_STA_AUTH;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004978 if (sta->added_unassoc)
4979 hostapd_set_sta_flags(hapd, sta);
4980 return;
4981 }
4982
4983fail:
4984 if (status_code != WLAN_STATUS_SUCCESS && sta->added_unassoc) {
4985 hostapd_drv_sta_remove(hapd, sta->addr);
4986 sta->added_unassoc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987 }
4988}
4989
4990
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004991static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
4992 struct sta_info *sta,
4993 char *ifname_wds)
4994{
Hai Shalomfdcde762020-04-02 11:19:20 -07004995#ifdef CONFIG_WEP
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004996 int i;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004997 struct hostapd_ssid *ssid = &hapd->conf->ssid;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004998
4999 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
5000 return;
5001
5002 for (i = 0; i < 4; i++) {
5003 if (ssid->wep.key[i] &&
5004 hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
Hai Shalomfdcde762020-04-02 11:19:20 -07005005 0, i == ssid->wep.idx, NULL, 0,
5006 ssid->wep.key[i], ssid->wep.len[i],
5007 i == ssid->wep.idx ?
5008 KEY_FLAG_GROUP_RX_TX_DEFAULT :
5009 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005010 wpa_printf(MSG_WARNING,
5011 "Could not set WEP keys for WDS interface; %s",
5012 ifname_wds);
5013 break;
5014 }
5015 }
Hai Shalomfdcde762020-04-02 11:19:20 -07005016#endif /* CONFIG_WEP */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005017}
5018
5019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020static void handle_assoc_cb(struct hostapd_data *hapd,
5021 const struct ieee80211_mgmt *mgmt,
5022 size_t len, int reassoc, int ok)
5023{
5024 u16 status;
5025 struct sta_info *sta;
5026 int new_assoc = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028 sta = ap_get_sta(hapd, mgmt->da);
5029 if (!sta) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005030 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
5031 MAC2STR(mgmt->da));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 return;
5033 }
5034
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005035 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
5036 sizeof(mgmt->u.assoc_resp))) {
5037 wpa_printf(MSG_INFO,
5038 "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
5039 reassoc, (unsigned long) len);
5040 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005041 return;
5042 }
5043
5044 if (reassoc)
5045 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
5046 else
5047 status = le_to_host16(mgmt->u.assoc_resp.status_code);
5048
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005049 if (!ok) {
5050 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
5051 HOSTAPD_LEVEL_DEBUG,
5052 "did not acknowledge association response");
5053 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
5054 /* The STA is added only in case of SUCCESS */
5055 if (status == WLAN_STATUS_SUCCESS)
5056 hostapd_drv_sta_remove(hapd, sta->addr);
5057
5058 return;
5059 }
5060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 if (status != WLAN_STATUS_SUCCESS)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07005062 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063
5064 /* Stop previous accounting session, if one is started, and allocate
5065 * new session id for the new session. */
5066 accounting_sta_stop(hapd, sta);
5067
5068 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5069 HOSTAPD_LEVEL_INFO,
5070 "associated (aid %d)",
5071 sta->aid);
5072
5073 if (sta->flags & WLAN_STA_ASSOC)
5074 new_assoc = 0;
5075 sta->flags |= WLAN_STA_ASSOC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005076 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005077 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
5078 !hapd->conf->osen) ||
5079 sta->auth_alg == WLAN_AUTH_FILS_SK ||
5080 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
5081 sta->auth_alg == WLAN_AUTH_FILS_PK ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082 sta->auth_alg == WLAN_AUTH_FT) {
5083 /*
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005084 * Open, static WEP, FT protocol, or FILS; no separate
5085 * authorization step.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086 */
5087 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 }
5089
5090 if (reassoc)
5091 mlme_reassociate_indication(hapd, sta);
5092 else
5093 mlme_associate_indication(hapd, sta);
5094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095 sta->sa_query_timed_out = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005097 if (sta->eapol_sm == NULL) {
5098 /*
5099 * This STA does not use RADIUS server for EAP authentication,
5100 * so bind it to the selected VLAN interface now, since the
5101 * interface selection is not going to change anymore.
5102 */
Dmitry Shmidt83474442015-04-15 13:47:09 -07005103 if (ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07005104 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105 } else if (sta->vlan_id) {
5106 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
Dmitry Shmidt83474442015-04-15 13:47:09 -07005107 if (ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07005108 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005109 }
5110
5111 hostapd_set_sta_flags(hapd, sta);
5112
Dmitry Shmidt29333592017-01-09 12:27:11 -08005113 if (!(sta->flags & WLAN_STA_WDS) && sta->pending_wds_enable) {
5114 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for STA "
5115 MACSTR " based on pending request",
5116 MAC2STR(sta->addr));
5117 sta->pending_wds_enable = 0;
5118 sta->flags |= WLAN_STA_WDS;
5119 }
5120
Hai Shalom74f70d42019-02-11 14:42:39 -08005121 if (sta->flags & (WLAN_STA_WDS | WLAN_STA_MULTI_AP)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005122 int ret;
5123 char ifname_wds[IFNAMSIZ + 1];
5124
5125 wpa_printf(MSG_DEBUG, "Reenable 4-address WDS mode for STA "
5126 MACSTR " (aid %u)",
5127 MAC2STR(sta->addr), sta->aid);
5128 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
5129 sta->aid, 1);
5130 if (!ret)
5131 hostapd_set_wds_encryption(hapd, sta, ifname_wds);
5132 }
5133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134 if (sta->auth_alg == WLAN_AUTH_FT)
5135 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
5136 else
5137 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
5138 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005140
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005141#ifdef CONFIG_FILS
5142 if ((sta->auth_alg == WLAN_AUTH_FILS_SK ||
5143 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
5144 sta->auth_alg == WLAN_AUTH_FILS_PK) &&
5145 fils_set_tk(sta->wpa_sm) < 0) {
5146 wpa_printf(MSG_DEBUG, "FILS: TK configuration failed");
5147 ap_sta_disconnect(hapd, sta, sta->addr,
5148 WLAN_REASON_UNSPECIFIED);
5149 return;
5150 }
5151#endif /* CONFIG_FILS */
5152
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005153 if (sta->pending_eapol_rx) {
5154 struct os_reltime now, age;
5155
5156 os_get_reltime(&now);
5157 os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
5158 if (age.sec == 0 && age.usec < 200000) {
5159 wpa_printf(MSG_DEBUG,
5160 "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
5161 MAC2STR(sta->addr));
5162 ieee802_1x_receive(
5163 hapd, mgmt->da,
5164 wpabuf_head(sta->pending_eapol_rx->buf),
5165 wpabuf_len(sta->pending_eapol_rx->buf));
5166 }
5167 wpabuf_free(sta->pending_eapol_rx->buf);
5168 os_free(sta->pending_eapol_rx);
5169 sta->pending_eapol_rx = NULL;
5170 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171}
5172
5173
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005174static void handle_deauth_cb(struct hostapd_data *hapd,
5175 const struct ieee80211_mgmt *mgmt,
5176 size_t len, int ok)
5177{
5178 struct sta_info *sta;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005179 if (is_multicast_ether_addr(mgmt->da))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005180 return;
5181 sta = ap_get_sta(hapd, mgmt->da);
5182 if (!sta) {
5183 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
5184 " not found", MAC2STR(mgmt->da));
5185 return;
5186 }
5187 if (ok)
5188 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
5189 MAC2STR(sta->addr));
5190 else
5191 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
5192 "deauth", MAC2STR(sta->addr));
5193
5194 ap_sta_deauth_cb(hapd, sta);
5195}
5196
5197
5198static void handle_disassoc_cb(struct hostapd_data *hapd,
5199 const struct ieee80211_mgmt *mgmt,
5200 size_t len, int ok)
5201{
5202 struct sta_info *sta;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005203 if (is_multicast_ether_addr(mgmt->da))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005204 return;
5205 sta = ap_get_sta(hapd, mgmt->da);
5206 if (!sta) {
5207 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
5208 " not found", MAC2STR(mgmt->da));
5209 return;
5210 }
5211 if (ok)
5212 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
5213 MAC2STR(sta->addr));
5214 else
5215 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
5216 "disassoc", MAC2STR(sta->addr));
5217
5218 ap_sta_disassoc_cb(hapd, sta);
5219}
5220
5221
Dmitry Shmidt29333592017-01-09 12:27:11 -08005222static void handle_action_cb(struct hostapd_data *hapd,
5223 const struct ieee80211_mgmt *mgmt,
5224 size_t len, int ok)
5225{
5226 struct sta_info *sta;
Paul Stewart092955c2017-02-06 09:13:09 -08005227 const struct rrm_measurement_report_element *report;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005228
5229 if (is_multicast_ether_addr(mgmt->da))
5230 return;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005231#ifdef CONFIG_DPP
5232 if (len >= IEEE80211_HDRLEN + 6 &&
5233 mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
5234 mgmt->u.action.u.vs_public_action.action ==
5235 WLAN_PA_VENDOR_SPECIFIC &&
5236 WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
5237 OUI_WFA &&
5238 mgmt->u.action.u.vs_public_action.variable[0] ==
5239 DPP_OUI_TYPE) {
5240 const u8 *pos, *end;
5241
5242 pos = &mgmt->u.action.u.vs_public_action.variable[1];
5243 end = ((const u8 *) mgmt) + len;
5244 hostapd_dpp_tx_status(hapd, mgmt->da, pos, end - pos, ok);
5245 return;
5246 }
5247 if (len >= IEEE80211_HDRLEN + 2 &&
5248 mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
5249 (mgmt->u.action.u.public_action.action ==
5250 WLAN_PA_GAS_INITIAL_REQ ||
5251 mgmt->u.action.u.public_action.action ==
5252 WLAN_PA_GAS_COMEBACK_REQ)) {
5253 const u8 *pos, *end;
5254
5255 pos = mgmt->u.action.u.public_action.variable;
5256 end = ((const u8 *) mgmt) + len;
5257 gas_query_ap_tx_status(hapd->gas, mgmt->da, pos, end - pos, ok);
5258 return;
5259 }
5260#endif /* CONFIG_DPP */
Dmitry Shmidt29333592017-01-09 12:27:11 -08005261 sta = ap_get_sta(hapd, mgmt->da);
5262 if (!sta) {
5263 wpa_printf(MSG_DEBUG, "handle_action_cb: STA " MACSTR
5264 " not found", MAC2STR(mgmt->da));
5265 return;
5266 }
5267
Paul Stewart092955c2017-02-06 09:13:09 -08005268 if (len < 24 + 5 + sizeof(*report))
Dmitry Shmidt29333592017-01-09 12:27:11 -08005269 return;
Paul Stewart092955c2017-02-06 09:13:09 -08005270 report = (const struct rrm_measurement_report_element *)
5271 &mgmt->u.action.u.rrm.variable[2];
Dmitry Shmidt29333592017-01-09 12:27:11 -08005272 if (mgmt->u.action.category == WLAN_ACTION_RADIO_MEASUREMENT &&
Paul Stewart092955c2017-02-06 09:13:09 -08005273 mgmt->u.action.u.rrm.action == WLAN_RRM_RADIO_MEASUREMENT_REQUEST &&
5274 report->eid == WLAN_EID_MEASURE_REQUEST &&
5275 report->len >= 3 &&
5276 report->type == MEASURE_TYPE_BEACON)
Dmitry Shmidt29333592017-01-09 12:27:11 -08005277 hostapd_rrm_beacon_req_tx_status(hapd, mgmt, len, ok);
5278}
5279
5280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005281/**
5282 * ieee802_11_mgmt_cb - Process management frame TX status callback
5283 * @hapd: hostapd BSS data structure (the BSS from which the management frame
5284 * was sent from)
5285 * @buf: management frame data (starting from IEEE 802.11 header)
5286 * @len: length of frame data in octets
5287 * @stype: management frame subtype from frame control field
5288 * @ok: Whether the frame was ACK'ed
5289 */
5290void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
5291 u16 stype, int ok)
5292{
5293 const struct ieee80211_mgmt *mgmt;
5294 mgmt = (const struct ieee80211_mgmt *) buf;
5295
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005296#ifdef CONFIG_TESTING_OPTIONS
5297 if (hapd->ext_mgmt_frame_handling) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005298 size_t hex_len = 2 * len + 1;
5299 char *hex = os_malloc(hex_len);
5300
5301 if (hex) {
5302 wpa_snprintf_hex(hex, hex_len, buf, len);
5303 wpa_msg(hapd->msg_ctx, MSG_INFO,
5304 "MGMT-TX-STATUS stype=%u ok=%d buf=%s",
5305 stype, ok, hex);
5306 os_free(hex);
5307 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005308 return;
5309 }
5310#endif /* CONFIG_TESTING_OPTIONS */
5311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312 switch (stype) {
5313 case WLAN_FC_STYPE_AUTH:
5314 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
5315 handle_auth_cb(hapd, mgmt, len, ok);
5316 break;
5317 case WLAN_FC_STYPE_ASSOC_RESP:
5318 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
5319 handle_assoc_cb(hapd, mgmt, len, 0, ok);
5320 break;
5321 case WLAN_FC_STYPE_REASSOC_RESP:
5322 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
5323 handle_assoc_cb(hapd, mgmt, len, 1, ok);
5324 break;
5325 case WLAN_FC_STYPE_PROBE_RESP:
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005326 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005327 break;
5328 case WLAN_FC_STYPE_DEAUTH:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005329 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
5330 handle_deauth_cb(hapd, mgmt, len, ok);
5331 break;
5332 case WLAN_FC_STYPE_DISASSOC:
5333 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
5334 handle_disassoc_cb(hapd, mgmt, len, ok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005335 break;
5336 case WLAN_FC_STYPE_ACTION:
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005337 wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
Dmitry Shmidt29333592017-01-09 12:27:11 -08005338 handle_action_cb(hapd, mgmt, len, ok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339 break;
5340 default:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005341 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342 break;
5343 }
5344}
5345
5346
5347int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
5348{
5349 /* TODO */
5350 return 0;
5351}
5352
5353
5354int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
5355 char *buf, size_t buflen)
5356{
5357 /* TODO */
5358 return 0;
5359}
5360
5361
5362void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
5363 const u8 *buf, size_t len, int ack)
5364{
5365 struct sta_info *sta;
5366 struct hostapd_iface *iface = hapd->iface;
5367
5368 sta = ap_get_sta(hapd, addr);
5369 if (sta == NULL && iface->num_bss > 1) {
5370 size_t j;
5371 for (j = 0; j < iface->num_bss; j++) {
5372 hapd = iface->bss[j];
5373 sta = ap_get_sta(hapd, addr);
5374 if (sta)
5375 break;
5376 }
5377 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005378 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379 return;
5380 if (sta->flags & WLAN_STA_PENDING_POLL) {
5381 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
5382 "activity poll", MAC2STR(sta->addr),
5383 ack ? "ACKed" : "did not ACK");
5384 if (ack)
5385 sta->flags &= ~WLAN_STA_PENDING_POLL;
5386 }
5387
5388 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
5389}
5390
5391
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005392void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
5393 const u8 *data, size_t len, int ack)
5394{
5395 struct sta_info *sta;
5396 struct hostapd_iface *iface = hapd->iface;
5397
5398 sta = ap_get_sta(hapd, dst);
5399 if (sta == NULL && iface->num_bss > 1) {
5400 size_t j;
5401 for (j = 0; j < iface->num_bss; j++) {
5402 hapd = iface->bss[j];
5403 sta = ap_get_sta(hapd, dst);
5404 if (sta)
5405 break;
5406 }
5407 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005408 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
5409 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
5410 MACSTR " that is not currently associated",
5411 MAC2STR(dst));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005412 return;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005413 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005414
5415 ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
5416}
5417
5418
5419void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
5420{
5421 struct sta_info *sta;
5422 struct hostapd_iface *iface = hapd->iface;
5423
5424 sta = ap_get_sta(hapd, addr);
5425 if (sta == NULL && iface->num_bss > 1) {
5426 size_t j;
5427 for (j = 0; j < iface->num_bss; j++) {
5428 hapd = iface->bss[j];
5429 sta = ap_get_sta(hapd, addr);
5430 if (sta)
5431 break;
5432 }
5433 }
5434 if (sta == NULL)
5435 return;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005436 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
5437 MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005438 if (!(sta->flags & WLAN_STA_PENDING_POLL))
5439 return;
5440
5441 wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
5442 "activity poll", MAC2STR(sta->addr));
5443 sta->flags &= ~WLAN_STA_PENDING_POLL;
5444}
5445
5446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
5448 int wds)
5449{
5450 struct sta_info *sta;
5451
5452 sta = ap_get_sta(hapd, src);
Dmitry Shmidt29333592017-01-09 12:27:11 -08005453 if (sta &&
5454 ((sta->flags & WLAN_STA_ASSOC) ||
5455 ((sta->flags & WLAN_STA_ASSOC_REQ_OK) && wds))) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005456 if (!hapd->conf->wds_sta)
5457 return;
5458
Dmitry Shmidt29333592017-01-09 12:27:11 -08005459 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK)) ==
5460 WLAN_STA_ASSOC_REQ_OK) {
5461 wpa_printf(MSG_DEBUG,
5462 "Postpone 4-address WDS mode enabling for STA "
5463 MACSTR " since TX status for AssocResp is not yet known",
5464 MAC2STR(sta->addr));
5465 sta->pending_wds_enable = 1;
5466 return;
5467 }
5468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469 if (wds && !(sta->flags & WLAN_STA_WDS)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005470 int ret;
5471 char ifname_wds[IFNAMSIZ + 1];
5472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
5474 "STA " MACSTR " (aid %u)",
5475 MAC2STR(sta->addr), sta->aid);
5476 sta->flags |= WLAN_STA_WDS;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005477 ret = hostapd_set_wds_sta(hapd, ifname_wds,
5478 sta->addr, sta->aid, 1);
5479 if (!ret)
5480 hostapd_set_wds_encryption(hapd, sta,
5481 ifname_wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482 }
5483 return;
5484 }
5485
5486 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
5487 MACSTR, MAC2STR(src));
Hai Shalomc3565922019-10-28 11:58:20 -07005488 if (is_multicast_ether_addr(src) || is_zero_ether_addr(src) ||
5489 os_memcmp(src, hapd->own_addr, ETH_ALEN) == 0) {
5490 /* Broadcast bit set in SA or unexpected SA?! Ignore the frame
5491 * silently. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005492 return;
5493 }
5494
5495 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
5496 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
5497 "already been sent, but no TX status yet known - "
5498 "ignore Class 3 frame issue with " MACSTR,
5499 MAC2STR(src));
5500 return;
5501 }
5502
5503 if (sta && (sta->flags & WLAN_STA_AUTH))
5504 hostapd_drv_sta_disassoc(
5505 hapd, src,
5506 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
5507 else
5508 hostapd_drv_sta_deauth(
5509 hapd, src,
5510 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
5511}
5512
5513
5514#endif /* CONFIG_NATIVE_WINDOWS */