]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/ieee802_11.c
QCA vendor subcommand for LL_STATS extension
[thirdparty/hostap.git] / src / ap / ieee802_11.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / IEEE 802.11 Management
f2991170 3 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
1057d78e 9#include "utils/includes.h"
6fc6879b
JM
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
1057d78e
JM
13#include "utils/common.h"
14#include "utils/eloop.h"
03da66bd 15#include "crypto/crypto.h"
d136c376
JM
16#include "crypto/sha256.h"
17#include "crypto/random.h"
81f4f619
JM
18#include "common/ieee802_11_defs.h"
19#include "common/ieee802_11_common.h"
03da66bd 20#include "common/wpa_ctrl.h"
98efcc41 21#include "common/sae.h"
03da66bd
JM
22#include "radius/radius.h"
23#include "radius/radius_client.h"
e44f8bf2 24#include "p2p/p2p.h"
ed7a09f9 25#include "wps/wps.h"
037378ff 26#include "fst/fst.h"
6fc6879b 27#include "hostapd.h"
6fc6879b 28#include "beacon.h"
6fc6879b
JM
29#include "ieee802_11_auth.h"
30#include "sta_info.h"
6fc6879b 31#include "ieee802_1x.h"
6226e38d 32#include "wpa_auth.h"
f2991170 33#include "pmksa_cache_auth.h"
1057d78e 34#include "wmm.h"
6fc6879b
JM
35#include "ap_list.h"
36#include "accounting.h"
6226e38d
JM
37#include "ap_config.h"
38#include "ap_mlme.h"
dce044cc 39#include "p2p_hostapd.h"
cee7d66b 40#include "ap_drv_ops.h"
c79938a5 41#include "wnm_ap.h"
0e2412d0 42#include "hw_features.h"
6226e38d 43#include "ieee802_11.h"
3d7ad2f6 44#include "dfs.h"
6332aaf3 45#include "mbo_ap.h"
2572df34 46#include "rrm.h"
6fc6879b
JM
47
48
49u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
50{
51 u8 *pos = eid;
52 int i, num, count;
53
54 if (hapd->iface->current_rates == NULL)
55 return eid;
56
57 *pos++ = WLAN_EID_SUPP_RATES;
58 num = hapd->iface->num_rates;
29448243
JM
59 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
60 num++;
202d97d4
JB
61 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
62 num++;
6fc6879b
JM
63 if (num > 8) {
64 /* rest of the rates are encoded in Extended supported
65 * rates element */
66 num = 8;
67 }
68
69 *pos++ = num;
6fc6879b
JM
70 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
71 i++) {
72 count++;
73 *pos = hapd->iface->current_rates[i].rate / 5;
74 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
75 *pos |= 0x80;
76 pos++;
77 }
78
202d97d4
JB
79 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
80 count++;
29448243 81 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
202d97d4
JB
82 }
83
84 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
85 count++;
86 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
87 }
29448243 88
6fc6879b
JM
89 return pos;
90}
91
92
93u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
94{
95 u8 *pos = eid;
96 int i, num, count;
97
98 if (hapd->iface->current_rates == NULL)
99 return eid;
100
101 num = hapd->iface->num_rates;
29448243
JM
102 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
103 num++;
202d97d4
JB
104 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
105 num++;
6fc6879b
JM
106 if (num <= 8)
107 return eid;
108 num -= 8;
109
110 *pos++ = WLAN_EID_EXT_SUPP_RATES;
111 *pos++ = num;
6fc6879b
JM
112 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
113 i++) {
114 count++;
115 if (count <= 8)
116 continue; /* already in SuppRates IE */
117 *pos = hapd->iface->current_rates[i].rate / 5;
118 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
119 *pos |= 0x80;
120 pos++;
121 }
122
202d97d4
JB
123 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
124 count++;
125 if (count > 8)
126 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
127 }
128
129 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
130 count++;
131 if (count > 8)
132 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
133 }
29448243 134
6fc6879b
JM
135 return pos;
136}
137
138
f41ded6f 139u16 hostapd_own_capab_info(struct hostapd_data *hapd)
6fc6879b
JM
140{
141 int capab = WLAN_CAPABILITY_ESS;
142 int privacy;
3d7ad2f6 143 int dfs;
01018212 144 int i;
3d7ad2f6
C
145
146 /* Check if any of configured channels require DFS */
147 dfs = hostapd_is_dfs_required(hapd->iface);
148 if (dfs < 0) {
149 wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
150 dfs);
151 dfs = 0;
152 }
6fc6879b
JM
153
154 if (hapd->iface->num_sta_no_short_preamble == 0 &&
155 hapd->iconf->preamble == SHORT_PREAMBLE)
156 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
157
158 privacy = hapd->conf->ssid.wep.keys_set;
159
160 if (hapd->conf->ieee802_1x &&
161 (hapd->conf->default_wep_key_len ||
162 hapd->conf->individual_wep_key_len))
163 privacy = 1;
164
165 if (hapd->conf->wpa)
166 privacy = 1;
167
a14896e8
JM
168#ifdef CONFIG_HS20
169 if (hapd->conf->osen)
170 privacy = 1;
171#endif /* CONFIG_HS20 */
172
6fc6879b
JM
173 if (privacy)
174 capab |= WLAN_CAPABILITY_PRIVACY;
175
176 if (hapd->iface->current_mode &&
177 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
178 hapd->iface->num_sta_no_short_slot_time == 0)
179 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
180
3d7ad2f6
C
181 /*
182 * Currently, Spectrum Management capability bit is set when directly
183 * requested in configuration by spectrum_mgmt_required or when AP is
184 * running on DFS channel.
185 * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
186 */
187 if (hapd->iface->current_mode &&
188 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
189 (hapd->iconf->spectrum_mgmt_required || dfs))
190 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
191
01018212
DS
192 for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
193 if (hapd->conf->radio_measurements[i]) {
194 capab |= IEEE80211_CAP_RRM;
195 break;
196 }
197 }
0629eeb4 198
6fc6879b
JM
199 return capab;
200}
201
202
7cb53ded 203#ifndef CONFIG_NO_RC4
6fc6879b 204static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
b57e086c
JM
205 u16 auth_transaction, const u8 *challenge,
206 int iswep)
6fc6879b
JM
207{
208 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
209 HOSTAPD_LEVEL_DEBUG,
210 "authentication (shared key, transaction %d)",
211 auth_transaction);
212
213 if (auth_transaction == 1) {
214 if (!sta->challenge) {
215 /* Generate a pseudo-random challenge */
216 u8 key[8];
f441e5af 217
6fc6879b
JM
218 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
219 if (sta->challenge == NULL)
220 return WLAN_STATUS_UNSPECIFIED_FAILURE;
221
f441e5af
NL
222 if (os_get_random(key, sizeof(key)) < 0) {
223 os_free(sta->challenge);
224 sta->challenge = NULL;
225 return WLAN_STATUS_UNSPECIFIED_FAILURE;
226 }
227
8ef16831
JM
228 rc4_skip(key, sizeof(key), 0,
229 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
6fc6879b
JM
230 }
231 return 0;
232 }
233
234 if (auth_transaction != 3)
235 return WLAN_STATUS_UNSPECIFIED_FAILURE;
236
237 /* Transaction 3 */
238 if (!iswep || !sta->challenge || !challenge ||
34ef46ce
JM
239 os_memcmp_const(sta->challenge, challenge,
240 WLAN_AUTH_CHALLENGE_LEN)) {
6fc6879b
JM
241 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
242 HOSTAPD_LEVEL_INFO,
243 "shared key authentication - invalid "
244 "challenge-response");
245 return WLAN_STATUS_CHALLENGE_FAIL;
246 }
247
248 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
249 HOSTAPD_LEVEL_DEBUG,
250 "authentication OK (shared key)");
6fc6879b
JM
251 sta->flags |= WLAN_STA_AUTH;
252 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
6fc6879b
JM
253 os_free(sta->challenge);
254 sta->challenge = NULL;
255
256 return 0;
257}
7cb53ded 258#endif /* CONFIG_NO_RC4 */
6fc6879b
JM
259
260
bb598c3b
AB
261static int send_auth_reply(struct hostapd_data *hapd,
262 const u8 *dst, const u8 *bssid,
263 u16 auth_alg, u16 auth_transaction, u16 resp,
264 const u8 *ies, size_t ies_len)
6fc6879b
JM
265{
266 struct ieee80211_mgmt *reply;
267 u8 *buf;
268 size_t rlen;
bb598c3b 269 int reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
270
271 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
272 buf = os_zalloc(rlen);
273 if (buf == NULL)
bb598c3b 274 return -1;
6fc6879b
JM
275
276 reply = (struct ieee80211_mgmt *) buf;
277 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
278 WLAN_FC_STYPE_AUTH);
6fc6879b
JM
279 os_memcpy(reply->da, dst, ETH_ALEN);
280 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
281 os_memcpy(reply->bssid, bssid, ETH_ALEN);
282
283 reply->u.auth.auth_alg = host_to_le16(auth_alg);
284 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
285 reply->u.auth.status_code = host_to_le16(resp);
286
287 if (ies && ies_len)
288 os_memcpy(reply->u.auth.variable, ies, ies_len);
289
290 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
291 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
292 MAC2STR(dst), auth_alg, auth_transaction,
293 resp, (unsigned long) ies_len);
8cfa3527 294 if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
bb598c3b
AB
295 wpa_printf(MSG_INFO, "send_auth_reply: send failed");
296 else
297 reply_res = WLAN_STATUS_SUCCESS;
6fc6879b
JM
298
299 os_free(buf);
bb598c3b
AB
300
301 return reply_res;
6fc6879b
JM
302}
303
304
305#ifdef CONFIG_IEEE80211R
306static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
307 u16 auth_transaction, u16 status,
308 const u8 *ies, size_t ies_len)
309{
310 struct hostapd_data *hapd = ctx;
311 struct sta_info *sta;
bb598c3b 312 int reply_res;
6fc6879b 313
bb598c3b
AB
314 reply_res = send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT,
315 auth_transaction, status, ies, ies_len);
6fc6879b
JM
316
317 sta = ap_get_sta(hapd, dst);
318 if (sta == NULL)
319 return;
320
bb598c3b
AB
321 if (sta->added_unassoc && (reply_res != WLAN_STATUS_SUCCESS ||
322 status != WLAN_STATUS_SUCCESS)) {
323 hostapd_drv_sta_remove(hapd, sta->addr);
324 sta->added_unassoc = 0;
325 return;
326 }
327
328 if (status != WLAN_STATUS_SUCCESS)
329 return;
330
6fc6879b
JM
331 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
332 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
333 sta->flags |= WLAN_STA_AUTH;
334 mlme_authenticate_indication(hapd, sta);
335}
336#endif /* CONFIG_IEEE80211R */
337
338
c10347f2 339#ifdef CONFIG_SAE
21af6d15 340
f3b8ad4d
BC
341#define dot11RSNASAESync 5 /* attempts */
342
343
e96da42b 344static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
872b7545 345 struct sta_info *sta, int update)
21af6d15
JM
346{
347 struct wpabuf *buf;
348
146f6c9a
JM
349 if (hapd->conf->ssid.wpa_passphrase == NULL) {
350 wpa_printf(MSG_DEBUG, "SAE: No password available");
351 return NULL;
352 }
353
872b7545
MH
354 if (update &&
355 sae_prepare_commit(hapd->own_addr, sta->addr,
8e31e955
JM
356 (u8 *) hapd->conf->ssid.wpa_passphrase,
357 os_strlen(hapd->conf->ssid.wpa_passphrase),
358 sta->sae) < 0) {
359 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
21af6d15 360 return NULL;
8e31e955 361 }
21af6d15 362
8e31e955
JM
363 buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
364 if (buf == NULL)
365 return NULL;
872b7545
MH
366 sae_write_commit(sta->sae, buf, sta->sae->tmp ?
367 sta->sae->tmp->anti_clogging_token : NULL);
21af6d15
JM
368
369 return buf;
370}
371
372
373static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
374 struct sta_info *sta)
375{
376 struct wpabuf *buf;
377
fb8fcc29 378 buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
21af6d15
JM
379 if (buf == NULL)
380 return NULL;
381
fb8fcc29 382 sae_write_confirm(sta->sae, buf);
21af6d15
JM
383
384 return buf;
385}
386
387
e96da42b
BC
388static int auth_sae_send_commit(struct hostapd_data *hapd,
389 struct sta_info *sta,
872b7545 390 const u8 *bssid, int update)
e96da42b
BC
391{
392 struct wpabuf *data;
bb598c3b 393 int reply_res;
e96da42b 394
872b7545 395 data = auth_build_sae_commit(hapd, sta, update);
e96da42b
BC
396 if (data == NULL)
397 return WLAN_STATUS_UNSPECIFIED_FAILURE;
398
bb598c3b
AB
399 reply_res = send_auth_reply(hapd, sta->addr, bssid, WLAN_AUTH_SAE, 1,
400 WLAN_STATUS_SUCCESS, wpabuf_head(data),
401 wpabuf_len(data));
e96da42b
BC
402
403 wpabuf_free(data);
404
bb598c3b 405 return reply_res;
e96da42b
BC
406}
407
408
409static int auth_sae_send_confirm(struct hostapd_data *hapd,
410 struct sta_info *sta,
411 const u8 *bssid)
412{
413 struct wpabuf *data;
bb598c3b 414 int reply_res;
e96da42b
BC
415
416 data = auth_build_sae_confirm(hapd, sta);
417 if (data == NULL)
418 return WLAN_STATUS_UNSPECIFIED_FAILURE;
419
bb598c3b
AB
420 reply_res = send_auth_reply(hapd, sta->addr, bssid, WLAN_AUTH_SAE, 2,
421 WLAN_STATUS_SUCCESS, wpabuf_head(data),
422 wpabuf_len(data));
e96da42b
BC
423
424 wpabuf_free(data);
425
bb598c3b 426 return reply_res;
e96da42b
BC
427}
428
429
d136c376
JM
430static int use_sae_anti_clogging(struct hostapd_data *hapd)
431{
432 struct sta_info *sta;
433 unsigned int open = 0;
434
435 if (hapd->conf->sae_anti_clogging_threshold == 0)
436 return 1;
437
438 for (sta = hapd->sta_list; sta; sta = sta->next) {
439 if (!sta->sae)
440 continue;
441 if (sta->sae->state != SAE_COMMITTED &&
442 sta->sae->state != SAE_CONFIRMED)
443 continue;
444 open++;
445 if (open >= hapd->conf->sae_anti_clogging_threshold)
446 return 1;
447 }
448
449 return 0;
450}
451
452
453static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
454 const u8 *token, size_t token_len)
455{
456 u8 mac[SHA256_MAC_LEN];
457
458 if (token_len != SHA256_MAC_LEN)
459 return -1;
460 if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
461 addr, ETH_ALEN, mac) < 0 ||
0233dcac 462 os_memcmp_const(token, mac, SHA256_MAC_LEN) != 0)
d136c376
JM
463 return -1;
464
465 return 0;
466}
467
468
469static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
a959a3b6 470 int group, const u8 *addr)
d136c376
JM
471{
472 struct wpabuf *buf;
473 u8 *token;
fe52c210 474 struct os_reltime now;
d136c376 475
fe52c210
JB
476 os_get_reltime(&now);
477 if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
478 os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60)) {
a50414c3
JM
479 if (random_get_bytes(hapd->sae_token_key,
480 sizeof(hapd->sae_token_key)) < 0)
481 return NULL;
d136c376
JM
482 wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
483 hapd->sae_token_key, sizeof(hapd->sae_token_key));
fe52c210 484 hapd->last_sae_token_key_update = now;
d136c376
JM
485 }
486
a959a3b6 487 buf = wpabuf_alloc(sizeof(le16) + SHA256_MAC_LEN);
d136c376
JM
488 if (buf == NULL)
489 return NULL;
490
a959a3b6
MH
491 wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
492
d136c376
JM
493 token = wpabuf_put(buf, SHA256_MAC_LEN);
494 hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
495 addr, ETH_ALEN, token);
496
497 return buf;
498}
499
500
f3b8ad4d
BC
501static int sae_check_big_sync(struct sta_info *sta)
502{
503 if (sta->sae->sync > dot11RSNASAESync) {
504 sta->sae->state = SAE_NOTHING;
505 sta->sae->sync = 0;
506 return -1;
507 }
508 return 0;
509}
510
511
512static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
513{
514 struct hostapd_data *hapd = eloop_ctx;
515 struct sta_info *sta = eloop_data;
516 int ret;
517
518 if (sae_check_big_sync(sta))
519 return;
520 sta->sae->sync++;
dad01292
JM
521 wpa_printf(MSG_DEBUG, "SAE: Auth SAE retransmit timer for " MACSTR
522 " (sync=%d state=%d)",
523 MAC2STR(sta->addr), sta->sae->sync, sta->sae->state);
f3b8ad4d
BC
524
525 switch (sta->sae->state) {
526 case SAE_COMMITTED:
527 ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
ecd40fef
MH
528 eloop_register_timeout(0,
529 hapd->dot11RSNASAERetransPeriod * 1000,
f3b8ad4d
BC
530 auth_sae_retransmit_timer, hapd, sta);
531 break;
532 case SAE_CONFIRMED:
533 ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
ecd40fef
MH
534 eloop_register_timeout(0,
535 hapd->dot11RSNASAERetransPeriod * 1000,
f3b8ad4d
BC
536 auth_sae_retransmit_timer, hapd, sta);
537 break;
538 default:
539 ret = -1;
540 break;
541 }
542
543 if (ret != WLAN_STATUS_SUCCESS)
544 wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
545}
546
547
548void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
549{
550 eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
551}
552
553
554static void sae_set_retransmit_timer(struct hostapd_data *hapd,
555 struct sta_info *sta)
556{
557 if (!(hapd->conf->mesh & MESH_ENABLED))
558 return;
559
560 eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
ecd40fef 561 eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
f3b8ad4d
BC
562 auth_sae_retransmit_timer, hapd, sta);
563}
564
565
9f2cf23e
MH
566void sae_accept_sta(struct hostapd_data *hapd, struct sta_info *sta)
567{
568 sta->flags |= WLAN_STA_AUTH;
569 sta->auth_alg = WLAN_AUTH_SAE;
570 mlme_authenticate_indication(hapd, sta);
571 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
572 sta->sae->state = SAE_ACCEPTED;
573 wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
574 sta->sae->pmk, sta->sae->pmkid);
575}
576
577
e96da42b
BC
578static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
579 const u8 *bssid, u8 auth_transaction)
580{
581 int ret;
582
583 if (auth_transaction != 1 && auth_transaction != 2)
584 return WLAN_STATUS_UNSPECIFIED_FAILURE;
585
586 switch (sta->sae->state) {
587 case SAE_NOTHING:
588 if (auth_transaction == 1) {
872b7545 589 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
e96da42b
BC
590 if (ret)
591 return ret;
592 sta->sae->state = SAE_COMMITTED;
593
594 if (sae_process_commit(sta->sae) < 0)
595 return WLAN_STATUS_UNSPECIFIED_FAILURE;
596
597 /*
598 * In mesh case, both Commit and Confirm can be sent
599 * immediately. In infrastructure BSS, only a single
600 * Authentication frame (Commit) is expected from the AP
601 * here and the second one (Confirm) will be sent once
602 * the STA has sent its second Authentication frame
603 * (Confirm).
604 */
605 if (hapd->conf->mesh & MESH_ENABLED) {
606 /*
607 * Send both Commit and Confirm immediately
608 * based on SAE finite state machine
609 * Nothing -> Confirm transition.
610 */
611 ret = auth_sae_send_confirm(hapd, sta, bssid);
612 if (ret)
613 return ret;
614 sta->sae->state = SAE_CONFIRMED;
615 } else {
616 /*
617 * For infrastructure BSS, send only the Commit
618 * message now to get alternating sequence of
619 * Authentication frames between the AP and STA.
620 * Confirm will be sent in
621 * Commited -> Confirmed/Accepted transition
622 * when receiving Confirm from STA.
623 */
624 }
f3b8ad4d
BC
625 sta->sae->sync = 0;
626 sae_set_retransmit_timer(hapd, sta);
e96da42b
BC
627 } else {
628 hostapd_logger(hapd, sta->addr,
629 HOSTAPD_MODULE_IEEE80211,
630 HOSTAPD_LEVEL_DEBUG,
631 "SAE confirm before commit");
632 }
633 break;
634 case SAE_COMMITTED:
f3b8ad4d 635 sae_clear_retransmit_timer(hapd, sta);
e96da42b
BC
636 if (auth_transaction == 1) {
637 if (sae_process_commit(sta->sae) < 0)
638 return WLAN_STATUS_UNSPECIFIED_FAILURE;
639
640 ret = auth_sae_send_confirm(hapd, sta, bssid);
641 if (ret)
642 return ret;
643 sta->sae->state = SAE_CONFIRMED;
f3b8ad4d
BC
644 sta->sae->sync = 0;
645 sae_set_retransmit_timer(hapd, sta);
e96da42b
BC
646 } else if (hapd->conf->mesh & MESH_ENABLED) {
647 /*
648 * In mesh case, follow SAE finite state machine and
f3b8ad4d 649 * send Commit now, if sync count allows.
e96da42b 650 */
f3b8ad4d
BC
651 if (sae_check_big_sync(sta))
652 return WLAN_STATUS_SUCCESS;
653 sta->sae->sync++;
654
fabc6dd8 655 ret = auth_sae_send_commit(hapd, sta, bssid, 0);
e96da42b
BC
656 if (ret)
657 return ret;
f3b8ad4d
BC
658
659 sae_set_retransmit_timer(hapd, sta);
e96da42b
BC
660 } else {
661 /*
662 * For instructure BSS, send the postponed Confirm from
663 * Nothing -> Confirmed transition that was reduced to
664 * Nothing -> Committed above.
665 */
666 ret = auth_sae_send_confirm(hapd, sta, bssid);
667 if (ret)
668 return ret;
669
670 sta->sae->state = SAE_CONFIRMED;
671
672 /*
673 * Since this was triggered on Confirm RX, run another
674 * step to get to Accepted without waiting for
675 * additional events.
676 */
677 return sae_sm_step(hapd, sta, bssid, auth_transaction);
678 }
679 break;
680 case SAE_CONFIRMED:
f3b8ad4d 681 sae_clear_retransmit_timer(hapd, sta);
e96da42b 682 if (auth_transaction == 1) {
f3b8ad4d
BC
683 if (sae_check_big_sync(sta))
684 return WLAN_STATUS_SUCCESS;
685 sta->sae->sync++;
686
872b7545 687 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
e96da42b
BC
688 if (ret)
689 return ret;
690
691 if (sae_process_commit(sta->sae) < 0)
692 return WLAN_STATUS_UNSPECIFIED_FAILURE;
693
694 ret = auth_sae_send_confirm(hapd, sta, bssid);
695 if (ret)
696 return ret;
f3b8ad4d
BC
697
698 sae_set_retransmit_timer(hapd, sta);
e96da42b 699 } else {
9f2cf23e 700 sae_accept_sta(hapd, sta);
e96da42b
BC
701 }
702 break;
703 case SAE_ACCEPTED:
704 if (auth_transaction == 1) {
705 wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
706 ") doing reauthentication",
707 MAC2STR(sta->addr));
708 ap_free_sta(hapd, sta);
9f2cf23e 709 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
e96da42b 710 } else {
f3b8ad4d
BC
711 if (sae_check_big_sync(sta))
712 return WLAN_STATUS_SUCCESS;
713 sta->sae->sync++;
714
e96da42b
BC
715 ret = auth_sae_send_confirm(hapd, sta, bssid);
716 sae_clear_temp_data(sta->sae);
717 if (ret)
718 return ret;
719 }
720 break;
721 default:
722 wpa_printf(MSG_ERROR, "SAE: invalid state %d",
723 sta->sae->state);
724 return WLAN_STATUS_UNSPECIFIED_FAILURE;
725 }
726 return WLAN_STATUS_SUCCESS;
727}
728
729
dad01292
JM
730static void sae_pick_next_group(struct hostapd_data *hapd, struct sta_info *sta)
731{
732 struct sae_data *sae = sta->sae;
733 int i, *groups = hapd->conf->sae_groups;
734
735 if (sae->state != SAE_COMMITTED)
736 return;
737
738 wpa_printf(MSG_DEBUG, "SAE: Previously selected group: %d", sae->group);
739
740 for (i = 0; groups && groups[i] > 0; i++) {
741 if (sae->group == groups[i])
742 break;
743 }
744
745 if (!groups || groups[i] <= 0) {
746 wpa_printf(MSG_DEBUG,
747 "SAE: Previously selected group not found from the current configuration");
748 return;
749 }
750
751 for (;;) {
752 i++;
753 if (groups[i] <= 0) {
754 wpa_printf(MSG_DEBUG,
755 "SAE: No alternative group enabled");
756 return;
757 }
758
759 if (sae_set_group(sae, groups[i]) < 0)
760 continue;
761
762 break;
763 }
764 wpa_printf(MSG_DEBUG, "SAE: Selected new group: %d", groups[i]);
765}
766
767
c10347f2
JM
768static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
769 const struct ieee80211_mgmt *mgmt, size_t len,
afa2ffb4 770 u16 auth_transaction, u16 status_code)
c10347f2 771{
bb598c3b 772 int resp = WLAN_STATUS_SUCCESS;
750efe6e 773 struct wpabuf *data = NULL;
c10347f2 774
98efcc41 775 if (!sta->sae) {
bb598c3b
AB
776 if (auth_transaction != 1 ||
777 status_code != WLAN_STATUS_SUCCESS) {
778 resp = -1;
779 goto remove_sta;
780 }
98efcc41 781 sta->sae = os_zalloc(sizeof(*sta->sae));
bb598c3b
AB
782 if (!sta->sae) {
783 resp = -1;
784 goto remove_sta;
785 }
dd43026a 786 sta->sae->state = SAE_NOTHING;
f3b8ad4d 787 sta->sae->sync = 0;
98efcc41
JM
788 }
789
9f2cf23e
MH
790 if (sta->mesh_sae_pmksa_caching) {
791 wpa_printf(MSG_DEBUG,
792 "SAE: Cancel use of mesh PMKSA caching because peer starts SAE authentication");
793 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
794 sta->mesh_sae_pmksa_caching = 0;
795 }
796
c10347f2 797 if (auth_transaction == 1) {
a959a3b6 798 const u8 *token = NULL, *pos, *end;
d136c376 799 size_t token_len = 0;
c10347f2
JM
800 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
801 HOSTAPD_LEVEL_DEBUG,
afa2ffb4
JM
802 "start SAE authentication (RX commit, status=%u)",
803 status_code);
872b7545
MH
804
805 if ((hapd->conf->mesh & MESH_ENABLED) &&
afa2ffb4
JM
806 status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
807 sta->sae->tmp) {
a959a3b6
MH
808 pos = mgmt->u.auth.variable;
809 end = ((const u8 *) mgmt) + len;
810 if (pos + sizeof(le16) > end) {
811 wpa_printf(MSG_ERROR,
812 "SAE: Too short anti-clogging token request");
813 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
814 goto reply;
815 }
816 resp = sae_group_allowed(sta->sae,
817 hapd->conf->sae_groups,
818 WPA_GET_LE16(pos));
819 if (resp != WLAN_STATUS_SUCCESS) {
820 wpa_printf(MSG_ERROR,
821 "SAE: Invalid group in anti-clogging token request");
822 goto reply;
823 }
824 pos += sizeof(le16);
825
872b7545
MH
826 wpabuf_free(sta->sae->tmp->anti_clogging_token);
827 sta->sae->tmp->anti_clogging_token =
a959a3b6 828 wpabuf_alloc_copy(pos, end - pos);
872b7545
MH
829 if (sta->sae->tmp->anti_clogging_token == NULL) {
830 wpa_printf(MSG_ERROR,
831 "SAE: Failed to alloc for anti-clogging token");
bb598c3b
AB
832 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
833 goto remove_sta;
872b7545
MH
834 }
835
836 /*
837 * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
838 * is 76, a new Commit Message shall be constructed
839 * with the Anti-Clogging Token from the received
840 * Authentication frame, and the commit-scalar and
841 * COMMIT-ELEMENT previously sent.
842 */
bb598c3b
AB
843 resp = auth_sae_send_commit(hapd, sta, mgmt->bssid, 0);
844 if (resp != WLAN_STATUS_SUCCESS) {
872b7545
MH
845 wpa_printf(MSG_ERROR,
846 "SAE: Failed to send commit message");
bb598c3b 847 goto remove_sta;
872b7545
MH
848 }
849 sta->sae->state = SAE_COMMITTED;
f3b8ad4d
BC
850 sta->sae->sync = 0;
851 sae_set_retransmit_timer(hapd, sta);
872b7545
MH
852 return;
853 }
854
dad01292
JM
855 if ((hapd->conf->mesh & MESH_ENABLED) &&
856 status_code ==
857 WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
858 sta->sae->tmp) {
859 wpa_printf(MSG_DEBUG,
860 "SAE: Peer did not accept our SAE group");
861 sae_pick_next_group(hapd, sta);
862 goto remove_sta;
863 }
864
afa2ffb4 865 if (status_code != WLAN_STATUS_SUCCESS)
bb598c3b 866 goto remove_sta;
afa2ffb4 867
146f6c9a
JM
868 resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
869 ((const u8 *) mgmt) + len -
d136c376 870 mgmt->u.auth.variable, &token,
625f202a 871 &token_len, hapd->conf->sae_groups);
6a58444d
JM
872 if (resp == SAE_SILENTLY_DISCARD) {
873 wpa_printf(MSG_DEBUG,
874 "SAE: Drop commit message from " MACSTR " due to reflection attack",
875 MAC2STR(sta->addr));
bb598c3b 876 goto remove_sta;
6a58444d 877 }
d136c376
JM
878 if (token && check_sae_token(hapd, sta->addr, token, token_len)
879 < 0) {
880 wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
881 "incorrect token from " MACSTR,
882 MAC2STR(sta->addr));
bb598c3b
AB
883 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
884 goto remove_sta;
d136c376
JM
885 }
886
e96da42b
BC
887 if (resp != WLAN_STATUS_SUCCESS)
888 goto reply;
889
890 if (!token && use_sae_anti_clogging(hapd)) {
891 wpa_printf(MSG_DEBUG,
892 "SAE: Request anti-clogging token from "
893 MACSTR, MAC2STR(sta->addr));
a959a3b6
MH
894 data = auth_build_token_req(hapd, sta->sae->group,
895 sta->addr);
e96da42b 896 resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
872b7545
MH
897 if (hapd->conf->mesh & MESH_ENABLED)
898 sta->sae->state = SAE_NOTHING;
e96da42b 899 goto reply;
750efe6e 900 }
e96da42b
BC
901
902 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
c10347f2 903 } else if (auth_transaction == 2) {
c10347f2
JM
904 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
905 HOSTAPD_LEVEL_DEBUG,
afa2ffb4
JM
906 "SAE authentication (RX confirm, status=%u)",
907 status_code);
908 if (status_code != WLAN_STATUS_SUCCESS)
bb598c3b 909 goto remove_sta;
e96da42b
BC
910 if (sta->sae->state >= SAE_CONFIRMED ||
911 !(hapd->conf->mesh & MESH_ENABLED)) {
912 if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
913 ((u8 *) mgmt) + len -
914 mgmt->u.auth.variable) < 0) {
750efe6e 915 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
e96da42b 916 goto reply;
b4fd3613 917 }
21af6d15 918 }
e96da42b 919 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
c10347f2
JM
920 } else {
921 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
922 HOSTAPD_LEVEL_DEBUG,
afa2ffb4
JM
923 "unexpected SAE authentication transaction %u (status=%u)",
924 auth_transaction, status_code);
925 if (status_code != WLAN_STATUS_SUCCESS)
bb598c3b 926 goto remove_sta;
c10347f2
JM
927 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
928 }
929
e96da42b
BC
930reply:
931 if (resp != WLAN_STATUS_SUCCESS) {
932 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
933 auth_transaction, resp,
934 data ? wpabuf_head(data) : (u8 *) "",
935 data ? wpabuf_len(data) : 0);
936 }
bb598c3b
AB
937
938remove_sta:
939 if (sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
940 status_code != WLAN_STATUS_SUCCESS)) {
941 hostapd_drv_sta_remove(hapd, sta->addr);
942 sta->added_unassoc = 0;
943 }
21af6d15 944 wpabuf_free(data);
c10347f2 945}
a206e2a1
BC
946
947
948/**
949 * auth_sae_init_committed - Send COMMIT and start SAE in committed state
950 * @hapd: BSS data for the device initiating the authentication
951 * @sta: the peer to which commit authentication frame is sent
952 *
953 * This function implements Init event handling (IEEE Std 802.11-2012,
954 * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
955 * sta->sae structure should be initialized appropriately via a call to
956 * sae_prepare_commit().
957 */
958int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
959{
960 int ret;
961
962 if (!sta->sae || !sta->sae->tmp)
963 return -1;
964
965 if (sta->sae->state != SAE_NOTHING)
966 return -1;
967
968 ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
969 if (ret)
970 return -1;
971
972 sta->sae->state = SAE_COMMITTED;
f3b8ad4d
BC
973 sta->sae->sync = 0;
974 sae_set_retransmit_timer(hapd, sta);
a206e2a1
BC
975
976 return 0;
977}
978
c10347f2
JM
979#endif /* CONFIG_SAE */
980
981
b57e086c
JM
982static void handle_auth(struct hostapd_data *hapd,
983 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
984{
985 u16 auth_alg, auth_transaction, status_code;
986 u16 resp = WLAN_STATUS_SUCCESS;
987 struct sta_info *sta = NULL;
bb598c3b 988 int res, reply_res;
6fc6879b 989 u16 fc;
b57e086c 990 const u8 *challenge = NULL;
6fc6879b 991 u32 session_timeout, acct_interim_interval;
1889af2e 992 struct vlan_description vlan_id;
2ad3e6c8 993 struct hostapd_sta_wpa_psk_short *psk = NULL;
6fc6879b
JM
994 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
995 size_t resp_ies_len = 0;
2092597f
MB
996 char *identity = NULL;
997 char *radius_cui = NULL;
38cb0a2d 998 u16 seq_ctrl;
6fc6879b 999
1889af2e
MB
1000 os_memset(&vlan_id, 0, sizeof(vlan_id));
1001
6fc6879b 1002 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
61323e70
JM
1003 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
1004 (unsigned long) len);
6fc6879b
JM
1005 return;
1006 }
1007
c2aff6b1 1008#ifdef CONFIG_TESTING_OPTIONS
06df2aa6 1009 if (hapd->iconf->ignore_auth_probability > 0.0 &&
c2aff6b1
JB
1010 drand48() < hapd->iconf->ignore_auth_probability) {
1011 wpa_printf(MSG_INFO,
1012 "TESTING: ignoring auth frame from " MACSTR,
1013 MAC2STR(mgmt->sa));
1014 return;
1015 }
1016#endif /* CONFIG_TESTING_OPTIONS */
1017
6fc6879b
JM
1018 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1019 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1020 status_code = le_to_host16(mgmt->u.auth.status_code);
1021 fc = le_to_host16(mgmt->frame_control);
38cb0a2d 1022 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
6fc6879b
JM
1023
1024 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
1025 2 + WLAN_AUTH_CHALLENGE_LEN &&
1026 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
1027 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
1028 challenge = &mgmt->u.auth.variable[2];
1029
1030 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
38cb0a2d
IP
1031 "auth_transaction=%d status_code=%d wep=%d%s "
1032 "seq_ctrl=0x%x%s",
6fc6879b
JM
1033 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
1034 status_code, !!(fc & WLAN_FC_ISWEP),
38cb0a2d
IP
1035 challenge ? " challenge" : "",
1036 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
6fc6879b 1037
7cb53ded
JM
1038#ifdef CONFIG_NO_RC4
1039 if (auth_alg == WLAN_AUTH_SHARED_KEY) {
1040 wpa_printf(MSG_INFO,
1041 "Unsupported authentication algorithm (%d)",
1042 auth_alg);
1043 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1044 goto fail;
1045 }
1046#endif /* CONFIG_NO_RC4 */
1047
6fc6879b
JM
1048 if (hapd->tkip_countermeasures) {
1049 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1050 goto fail;
1051 }
1052
1053 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
1054 auth_alg == WLAN_AUTH_OPEN) ||
1055#ifdef CONFIG_IEEE80211R
0bf927a0 1056 (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
6fc6879b
JM
1057 auth_alg == WLAN_AUTH_FT) ||
1058#endif /* CONFIG_IEEE80211R */
c10347f2
JM
1059#ifdef CONFIG_SAE
1060 (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
1061 auth_alg == WLAN_AUTH_SAE) ||
1062#endif /* CONFIG_SAE */
6fc6879b
JM
1063 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
1064 auth_alg == WLAN_AUTH_SHARED_KEY))) {
61323e70
JM
1065 wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
1066 auth_alg);
6fc6879b
JM
1067 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1068 goto fail;
1069 }
1070
c10347f2 1071 if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
6fc6879b 1072 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
61323e70
JM
1073 wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
1074 auth_transaction);
6fc6879b
JM
1075 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1076 goto fail;
1077 }
1078
1079 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
61323e70
JM
1080 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
1081 MAC2STR(mgmt->sa));
6fc6879b
JM
1082 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1083 goto fail;
1084 }
1085
0e2412d0
JM
1086 if (hapd->conf->no_auth_if_seen_on) {
1087 struct hostapd_data *other;
1088
1089 other = sta_track_seen_on(hapd->iface, mgmt->sa,
1090 hapd->conf->no_auth_if_seen_on);
1091 if (other) {
1092 u8 *pos;
1093 u32 info;
1094 u8 op_class, channel, phytype;
1095
1096 wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
1097 MACSTR " since STA has been seen on %s",
1098 hapd->conf->iface, MAC2STR(mgmt->sa),
1099 hapd->conf->no_auth_if_seen_on);
1100
1101 resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
1102 pos = &resp_ies[0];
1103 *pos++ = WLAN_EID_NEIGHBOR_REPORT;
1104 *pos++ = 13;
1105 os_memcpy(pos, other->own_addr, ETH_ALEN);
1106 pos += ETH_ALEN;
1107 info = 0; /* TODO: BSSID Information */
1108 WPA_PUT_LE32(pos, info);
1109 pos += 4;
1110 if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
1111 phytype = 8; /* dmg */
1112 else if (other->iconf->ieee80211ac)
1113 phytype = 9; /* vht */
1114 else if (other->iconf->ieee80211n)
1115 phytype = 7; /* ht */
1116 else if (other->iconf->hw_mode ==
1117 HOSTAPD_MODE_IEEE80211A)
1118 phytype = 4; /* ofdm */
1119 else if (other->iconf->hw_mode ==
1120 HOSTAPD_MODE_IEEE80211G)
1121 phytype = 6; /* erp */
1122 else
1123 phytype = 5; /* hrdsss */
1124 if (ieee80211_freq_to_channel_ext(
1125 hostapd_hw_get_freq(other,
1126 other->iconf->channel),
1127 other->iconf->secondary_channel,
1128 other->iconf->ieee80211ac,
1129 &op_class, &channel) == NUM_HOSTAPD_MODES) {
1130 op_class = 0;
1131 channel = other->iconf->channel;
1132 }
1133 *pos++ = op_class;
1134 *pos++ = channel;
1135 *pos++ = phytype;
1136 resp_ies_len = pos - &resp_ies[0];
1137 goto fail;
1138 }
1139 }
1140
6fc6879b
JM
1141 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
1142 &session_timeout,
05ab9712 1143 &acct_interim_interval, &vlan_id,
2ad3e6c8 1144 &psk, &identity, &radius_cui);
05ab9712 1145
6fc6879b 1146 if (res == HOSTAPD_ACL_REJECT) {
61323e70
JM
1147 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
1148 MAC2STR(mgmt->sa));
6fc6879b
JM
1149 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1150 goto fail;
1151 }
1152 if (res == HOSTAPD_ACL_PENDING) {
1153 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
1154 " waiting for an external authentication",
1155 MAC2STR(mgmt->sa));
1156 /* Authentication code will re-send the authentication frame
1157 * after it has received (and cached) information from the
1158 * external source. */
1159 return;
1160 }
1161
38cb0a2d
IP
1162 sta = ap_get_sta(hapd, mgmt->sa);
1163 if (sta) {
1164 if ((fc & WLAN_FC_RETRY) &&
1165 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1166 sta->last_seq_ctrl == seq_ctrl &&
1167 sta->last_subtype == WLAN_FC_STYPE_AUTH) {
1168 hostapd_logger(hapd, sta->addr,
1169 HOSTAPD_MODULE_IEEE80211,
1170 HOSTAPD_LEVEL_DEBUG,
1171 "Drop repeated authentication frame seq_ctrl=0x%x",
1172 seq_ctrl);
1173 return;
1174 }
09d96de0
MH
1175#ifdef CONFIG_MESH
1176 if ((hapd->conf->mesh & MESH_ENABLED) &&
1177 sta->plink_state == PLINK_BLOCKED) {
1178 wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
1179 " is blocked - drop Authentication frame",
1180 MAC2STR(mgmt->sa));
1181 return;
1182 }
1183#endif /* CONFIG_MESH */
38cb0a2d 1184 } else {
c50d94f1 1185#ifdef CONFIG_MESH
38cb0a2d
IP
1186 if (hapd->conf->mesh & MESH_ENABLED) {
1187 /* if the mesh peer is not available, we don't do auth.
1188 */
3a322496 1189 wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
09d96de0 1190 " not yet known - drop Authentication frame",
3a322496
JM
1191 MAC2STR(mgmt->sa));
1192 /*
1193 * Save a copy of the frame so that it can be processed
1194 * if a new peer entry is added shortly after this.
1195 */
1196 wpabuf_free(hapd->mesh_pending_auth);
1197 hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
1198 os_get_reltime(&hapd->mesh_pending_auth_time);
c50d94f1 1199 return;
38cb0a2d 1200 }
c50d94f1 1201#endif /* CONFIG_MESH */
38cb0a2d 1202
c50d94f1
BC
1203 sta = ap_sta_add(hapd, mgmt->sa);
1204 if (!sta) {
1205 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1206 goto fail;
1207 }
6fc6879b 1208 }
38cb0a2d
IP
1209 sta->last_seq_ctrl = seq_ctrl;
1210 sta->last_subtype = WLAN_FC_STYPE_AUTH;
6fc6879b 1211
8be640b7
MB
1212 if (vlan_id.notempty &&
1213 !hostapd_vlan_valid(hapd->conf->vlan, &vlan_id)) {
6fc6879b 1214 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
8be640b7
MB
1215 HOSTAPD_LEVEL_INFO,
1216 "Invalid VLAN %d%s received from RADIUS server",
1217 vlan_id.untagged,
1218 vlan_id.tagged[0] ? "+" : "");
1219 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1220 goto fail;
1221 }
1222 if (ap_sta_set_vlan(hapd, sta, &vlan_id) < 0) {
1223 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1224 goto fail;
6fc6879b 1225 }
8be640b7
MB
1226 if (sta->vlan_id)
1227 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1228 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
6fc6879b 1229
f2a14be7 1230 hostapd_free_psk_list(sta->psk);
5ee56c4e
MB
1231 if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
1232 sta->psk = psk;
1233 psk = NULL;
05ab9712 1234 } else {
05ab9712
MB
1235 sta->psk = NULL;
1236 }
1237
2092597f
MB
1238 sta->identity = identity;
1239 identity = NULL;
1240 sta->radius_cui = radius_cui;
1241 radius_cui = NULL;
1242
6fc6879b
JM
1243 sta->flags &= ~WLAN_STA_PREAUTH;
1244 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
1245
5843e1c9 1246 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
6fc6879b
JM
1247 sta->acct_interim_interval = acct_interim_interval;
1248 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
1249 ap_sta_session_timeout(hapd, sta, session_timeout);
1250 else
1251 ap_sta_no_session_timeout(hapd, sta);
1252
bb598c3b
AB
1253 /*
1254 * If the driver supports full AP client state, add a station to the
1255 * driver before sending authentication reply to make sure the driver
1256 * has resources, and not to go through the entire authentication and
1257 * association handshake, and fail it at the end.
1258 *
1259 * If this is not the first transaction, in a multi-step authentication
1260 * algorithm, the station already exists in the driver
1261 * (sta->added_unassoc = 1) so skip it.
1262 *
1263 * In mesh mode, the station was already added to the driver when the
1264 * NEW_PEER_CANDIDATE event is received.
1265 */
1266 if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
1267 !(hapd->conf->mesh & MESH_ENABLED) &&
1268 !(sta->added_unassoc)) {
1269 /*
1270 * If a station that is already associated to the AP, is trying
1271 * to authenticate again, remove the STA entry, in order to make
1272 * sure the STA PS state gets cleared and configuration gets
1273 * updated. To handle this, station's added_unassoc flag is
1274 * cleared once the station has completed association.
1275 */
1276 hostapd_drv_sta_remove(hapd, sta->addr);
1277 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH |
1278 WLAN_STA_AUTHORIZED);
1279
468b7b12 1280 if (hostapd_sta_add(hapd, sta->addr, 0, 0, NULL, 0, 0,
ae33239c 1281 NULL, NULL, sta->flags, 0, 0, 0, 0)) {
bb598c3b
AB
1282 hostapd_logger(hapd, sta->addr,
1283 HOSTAPD_MODULE_IEEE80211,
1284 HOSTAPD_LEVEL_NOTICE,
1285 "Could not add STA to kernel driver");
1286 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1287 goto fail;
1288 }
1289
1290 sta->added_unassoc = 1;
1291 }
1292
6fc6879b
JM
1293 switch (auth_alg) {
1294 case WLAN_AUTH_OPEN:
1295 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1296 HOSTAPD_LEVEL_DEBUG,
1297 "authentication OK (open system)");
6fc6879b
JM
1298 sta->flags |= WLAN_STA_AUTH;
1299 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1300 sta->auth_alg = WLAN_AUTH_OPEN;
1301 mlme_authenticate_indication(hapd, sta);
6fc6879b 1302 break;
7cb53ded 1303#ifndef CONFIG_NO_RC4
6fc6879b
JM
1304 case WLAN_AUTH_SHARED_KEY:
1305 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
1306 fc & WLAN_FC_ISWEP);
1307 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
1308 mlme_authenticate_indication(hapd, sta);
1309 if (sta->challenge && auth_transaction == 1) {
1310 resp_ies[0] = WLAN_EID_CHALLENGE;
1311 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
1312 os_memcpy(resp_ies + 2, sta->challenge,
1313 WLAN_AUTH_CHALLENGE_LEN);
1314 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
1315 }
1316 break;
7cb53ded 1317#endif /* CONFIG_NO_RC4 */
6fc6879b
JM
1318#ifdef CONFIG_IEEE80211R
1319 case WLAN_AUTH_FT:
1320 sta->auth_alg = WLAN_AUTH_FT;
1321 if (sta->wpa_sm == NULL)
1322 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e 1323 sta->addr, NULL);
6fc6879b
JM
1324 if (sta->wpa_sm == NULL) {
1325 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
1326 "state machine");
1327 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1328 goto fail;
1329 }
1330 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
1331 auth_transaction, mgmt->u.auth.variable,
1332 len - IEEE80211_HDRLEN -
1333 sizeof(mgmt->u.auth),
1334 handle_auth_ft_finish, hapd);
1335 /* handle_auth_ft_finish() callback will complete auth. */
1336 return;
1337#endif /* CONFIG_IEEE80211R */
c10347f2
JM
1338#ifdef CONFIG_SAE
1339 case WLAN_AUTH_SAE:
c50d94f1 1340#ifdef CONFIG_MESH
afa2ffb4
JM
1341 if (status_code == WLAN_STATUS_SUCCESS &&
1342 hapd->conf->mesh & MESH_ENABLED) {
c50d94f1
BC
1343 if (sta->wpa_sm == NULL)
1344 sta->wpa_sm =
1345 wpa_auth_sta_init(hapd->wpa_auth,
1346 sta->addr, NULL);
1347 if (sta->wpa_sm == NULL) {
1348 wpa_printf(MSG_DEBUG,
1349 "SAE: Failed to initialize WPA state machine");
1350 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1351 goto fail;
1352 }
1353 }
1354#endif /* CONFIG_MESH */
afa2ffb4
JM
1355 handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
1356 status_code);
c10347f2
JM
1357 return;
1358#endif /* CONFIG_SAE */
6fc6879b
JM
1359 }
1360
1361 fail:
2092597f
MB
1362 os_free(identity);
1363 os_free(radius_cui);
f2a14be7 1364 hostapd_free_psk_list(psk);
2092597f 1365
bb598c3b
AB
1366 reply_res = send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
1367 auth_transaction + 1, resp, resp_ies,
1368 resp_ies_len);
1369
1370 if (sta && sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
1371 reply_res != WLAN_STATUS_SUCCESS)) {
1372 hostapd_drv_sta_remove(hapd, sta->addr);
1373 sta->added_unassoc = 0;
1374 }
6fc6879b
JM
1375}
1376
1377
681753f2 1378int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
d42a62b3 1379{
2991469c
JM
1380 int i, j = 32, aid;
1381
d42a62b3
JM
1382 /* get a unique AID */
1383 if (sta->aid > 0) {
1384 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
1385 return 0;
1386 }
1387
d7358113
JM
1388 if (TEST_FAIL())
1389 return -1;
1390
2991469c
JM
1391 for (i = 0; i < AID_WORDS; i++) {
1392 if (hapd->sta_aid[i] == (u32) -1)
1393 continue;
1394 for (j = 0; j < 32; j++) {
1395 if (!(hapd->sta_aid[i] & BIT(j)))
1396 break;
1397 }
1398 if (j < 32)
d42a62b3 1399 break;
d42a62b3 1400 }
2991469c
JM
1401 if (j == 32)
1402 return -1;
1403 aid = i * 32 + j + 1;
1404 if (aid > 2007)
1405 return -1;
d42a62b3 1406
2991469c
JM
1407 sta->aid = aid;
1408 hapd->sta_aid[i] |= BIT(j);
d42a62b3
JM
1409 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
1410 return 0;
1411}
1412
1413
5586f500
JM
1414static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
1415 const u8 *ssid_ie, size_t ssid_ie_len)
6fc6879b 1416{
5586f500
JM
1417 if (ssid_ie == NULL)
1418 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b 1419
5586f500
JM
1420 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
1421 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
5586f500
JM
1422 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1423 HOSTAPD_LEVEL_INFO,
1424 "Station tried to associate with unknown SSID "
b37d582e 1425 "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
5586f500 1426 return WLAN_STATUS_UNSPECIFIED_FAILURE;
b0194fe0
JM
1427 }
1428
5586f500
JM
1429 return WLAN_STATUS_SUCCESS;
1430}
6fc6879b 1431
6fc6879b 1432
5586f500
JM
1433static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
1434 const u8 *wmm_ie, size_t wmm_ie_len)
1435{
3ae0800c 1436 sta->flags &= ~WLAN_STA_WMM;
5d061637 1437 sta->qosinfo = 0;
5586f500 1438 if (wmm_ie && hapd->conf->wmm_enabled) {
5f32f79c 1439 struct wmm_information_element *wmm;
5f32f79c 1440
c84b868a 1441 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
6fc6879b
JM
1442 hostapd_logger(hapd, sta->addr,
1443 HOSTAPD_MODULE_WPA,
1444 HOSTAPD_LEVEL_DEBUG,
3ae0800c 1445 "invalid WMM element in association "
6fc6879b 1446 "request");
5f32f79c
EP
1447 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1448 }
1449
1450 sta->flags |= WLAN_STA_WMM;
1451 wmm = (struct wmm_information_element *) wmm_ie;
5d061637 1452 sta->qosinfo = wmm->qos_info;
6fc6879b 1453 }
5586f500
JM
1454 return WLAN_STATUS_SUCCESS;
1455}
6fc6879b 1456
5586f500
JM
1457
1458static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
1459 struct ieee802_11_elems *elems)
1460{
1461 if (!elems->supp_rates) {
1462 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6fc6879b
JM
1463 HOSTAPD_LEVEL_DEBUG,
1464 "No supported rates element in AssocReq");
5586f500 1465 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
1466 }
1467
3489cfb0
JM
1468 if (elems->supp_rates_len + elems->ext_supp_rates_len >
1469 sizeof(sta->supported_rates)) {
5586f500 1470 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6fc6879b 1471 HOSTAPD_LEVEL_DEBUG,
3489cfb0
JM
1472 "Invalid supported rates element length %d+%d",
1473 elems->supp_rates_len,
1474 elems->ext_supp_rates_len);
5586f500 1475 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
1476 }
1477
3489cfb0
JM
1478 sta->supported_rates_len = merge_byte_arrays(
1479 sta->supported_rates, sizeof(sta->supported_rates),
1480 elems->supp_rates, elems->supp_rates_len,
1481 elems->ext_supp_rates, elems->ext_supp_rates_len);
6fc6879b 1482
5586f500
JM
1483 return WLAN_STATUS_SUCCESS;
1484}
1485
1486
c551700f
KP
1487static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
1488 const u8 *ext_capab_ie, size_t ext_capab_ie_len)
1489{
1490#ifdef CONFIG_INTERWORKING
1491 /* check for QoS Map support */
1492 if (ext_capab_ie_len >= 5) {
1493 if (ext_capab_ie[4] & 0x01)
1494 sta->qos_map_enabled = 1;
1495 }
1496#endif /* CONFIG_INTERWORKING */
1497
98b05081
AO
1498 if (ext_capab_ie_len > 0)
1499 sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
1500
c551700f
KP
1501 return WLAN_STATUS_SUCCESS;
1502}
1503
1504
5586f500 1505static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
b57e086c 1506 const u8 *ies, size_t ies_len, int reassoc)
5586f500
JM
1507{
1508 struct ieee802_11_elems elems;
1509 u16 resp;
ba091c06 1510 const u8 *wpa_ie;
5586f500 1511 size_t wpa_ie_len;
94ddef3e 1512 const u8 *p2p_dev_addr = NULL;
5586f500
JM
1513
1514 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
1515 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1516 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
1517 "association request");
1518 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1519 }
1520
1521 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
1522 if (resp != WLAN_STATUS_SUCCESS)
1523 return resp;
1524 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
c551700f
KP
1525 if (resp != WLAN_STATUS_SUCCESS)
1526 return resp;
1527 resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
5586f500
JM
1528 if (resp != WLAN_STATUS_SUCCESS)
1529 return resp;
1530 resp = copy_supp_rates(hapd, sta, &elems);
1531 if (resp != WLAN_STATUS_SUCCESS)
1532 return resp;
d45354be 1533#ifdef CONFIG_IEEE80211N
baae4cb9 1534 resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
5586f500
JM
1535 if (resp != WLAN_STATUS_SUCCESS)
1536 return resp;
29448243
JM
1537 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
1538 !(sta->flags & WLAN_STA_HT)) {
1539 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1540 HOSTAPD_LEVEL_INFO, "Station does not support "
1541 "mandatory HT PHY - reject association");
1542 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
1543 }
d45354be 1544#endif /* CONFIG_IEEE80211N */
5586f500 1545
de3cdf35 1546#ifdef CONFIG_IEEE80211AC
28ffd21c
ARN
1547 if (hapd->iconf->ieee80211ac) {
1548 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
1549 if (resp != WLAN_STATUS_SUCCESS)
1550 return resp;
8a458116 1551
28ffd21c
ARN
1552 resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
1553 if (resp != WLAN_STATUS_SUCCESS)
1554 return resp;
1555 }
8a458116 1556
140e850a
MP
1557 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
1558 !(sta->flags & WLAN_STA_VHT)) {
1559 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1560 HOSTAPD_LEVEL_INFO, "Station does not support "
1561 "mandatory VHT PHY - reject association");
13b24a76 1562 return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
140e850a 1563 }
e7d0e97b
YL
1564
1565 if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
1566 resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
1567 elems.vendor_vht_len);
1568 if (resp != WLAN_STATUS_SUCCESS)
1569 return resp;
1570 }
de3cdf35
MP
1571#endif /* CONFIG_IEEE80211AC */
1572
94ddef3e
JM
1573#ifdef CONFIG_P2P
1574 if (elems.p2p) {
1575 wpabuf_free(sta->p2p_ie);
1576 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1577 P2P_IE_VENDOR_TYPE);
1578 if (sta->p2p_ie)
1579 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
1580 } else {
1581 wpabuf_free(sta->p2p_ie);
1582 sta->p2p_ie = NULL;
1583 }
1584#endif /* CONFIG_P2P */
1585
6fc6879b
JM
1586 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1587 wpa_ie = elems.rsn_ie;
1588 wpa_ie_len = elems.rsn_ie_len;
1589 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1590 elems.wpa_ie) {
1591 wpa_ie = elems.wpa_ie;
1592 wpa_ie_len = elems.wpa_ie_len;
1593 } else {
1594 wpa_ie = NULL;
1595 wpa_ie_len = 0;
1596 }
5586f500 1597
ad08c363 1598#ifdef CONFIG_WPS
17f6b900 1599 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
c47cf42e
JM
1600 if (hapd->conf->wps_state && elems.wps_ie) {
1601 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
1602 "Request - assume WPS is used");
1603 sta->flags |= WLAN_STA_WPS;
1604 wpabuf_free(sta->wps_ie);
16e46ec0
JM
1605 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1606 WPS_IE_VENDOR_TYPE);
17f6b900
JM
1607 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
1608 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
1609 sta->flags |= WLAN_STA_WPS2;
1610 }
c47cf42e
JM
1611 wpa_ie = NULL;
1612 wpa_ie_len = 0;
54f489be
JM
1613 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
1614 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
1615 "(Re)Association Request - reject");
1616 return WLAN_STATUS_INVALID_IE;
1617 }
c47cf42e
JM
1618 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
1619 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
1620 "(Re)Association Request - possible WPS use");
1621 sta->flags |= WLAN_STA_MAYBE_WPS;
ad08c363
JM
1622 } else
1623#endif /* CONFIG_WPS */
6fc6879b 1624 if (hapd->conf->wpa && wpa_ie == NULL) {
5586f500
JM
1625 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1626 HOSTAPD_LEVEL_INFO,
1627 "No WPA/RSN IE in association request");
1628 return WLAN_STATUS_INVALID_IE;
6fc6879b
JM
1629 }
1630
1631 if (hapd->conf->wpa && wpa_ie) {
1632 int res;
1633 wpa_ie -= 2;
1634 wpa_ie_len += 2;
1635 if (sta->wpa_sm == NULL)
1636 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e
JM
1637 sta->addr,
1638 p2p_dev_addr);
6fc6879b 1639 if (sta->wpa_sm == NULL) {
5586f500
JM
1640 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1641 "state machine");
1642 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
1643 }
1644 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1645 wpa_ie, wpa_ie_len,
1646 elems.mdie, elems.mdie_len);
1647 if (res == WPA_INVALID_GROUP)
1648 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1649 else if (res == WPA_INVALID_PAIRWISE)
1650 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1651 else if (res == WPA_INVALID_AKMP)
1652 resp = WLAN_STATUS_AKMP_NOT_VALID;
1653 else if (res == WPA_ALLOC_FAIL)
1654 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1655#ifdef CONFIG_IEEE80211W
1656 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
9a9876bf 1657 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
6fc6879b 1658 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
9a9876bf 1659 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
6fc6879b
JM
1660#endif /* CONFIG_IEEE80211W */
1661 else if (res == WPA_INVALID_MDIE)
1662 resp = WLAN_STATUS_INVALID_MDIE;
1663 else if (res != WPA_IE_OK)
1664 resp = WLAN_STATUS_INVALID_IE;
1665 if (resp != WLAN_STATUS_SUCCESS)
5586f500 1666 return resp;
f3f7540e 1667#ifdef CONFIG_IEEE80211W
45c94154
JM
1668 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1669 sta->sa_query_count > 0)
1670 ap_check_sa_query_timeout(hapd, sta);
8f4617c6
JM
1671 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1672 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
5d22a1d5 1673 /*
93b76319
JM
1674 * STA has already been associated with MFP and SA
1675 * Query timeout has not been reached. Reject the
1676 * association attempt temporarily and start SA Query,
1677 * if one is not pending.
5d22a1d5
JM
1678 */
1679
93b76319
JM
1680 if (sta->sa_query_count == 0)
1681 ap_sta_start_sa_query(hapd, sta);
5d22a1d5 1682
5586f500 1683 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
5d22a1d5
JM
1684 }
1685
f3f7540e
JM
1686 if (wpa_auth_uses_mfp(sta->wpa_sm))
1687 sta->flags |= WLAN_STA_MFP;
1688 else
1689 sta->flags &= ~WLAN_STA_MFP;
1690#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
1691
1692#ifdef CONFIG_IEEE80211R
1693 if (sta->auth_alg == WLAN_AUTH_FT) {
1694 if (!reassoc) {
1695 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1696 "to use association (not "
1697 "re-association) with FT auth_alg",
1698 MAC2STR(sta->addr));
5586f500 1699 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
1700 }
1701
5586f500
JM
1702 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
1703 ies_len);
6fc6879b 1704 if (resp != WLAN_STATUS_SUCCESS)
5586f500 1705 return resp;
6fc6879b
JM
1706 }
1707#endif /* CONFIG_IEEE80211R */
5586f500 1708
c10347f2
JM
1709#ifdef CONFIG_SAE
1710 if (wpa_auth_uses_sae(sta->wpa_sm) &&
f2991170
JM
1711 sta->auth_alg == WLAN_AUTH_OPEN) {
1712 struct rsn_pmksa_cache_entry *sa;
1713 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1714 if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
1715 wpa_printf(MSG_DEBUG,
1716 "SAE: No PMKSA cache entry found for "
1717 MACSTR, MAC2STR(sta->addr));
1718 return WLAN_STATUS_INVALID_PMKID;
1719 }
1720 wpa_printf(MSG_DEBUG, "SAE: " MACSTR
1721 " using PMKSA caching", MAC2STR(sta->addr));
1722 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
1723 sta->auth_alg != WLAN_AUTH_SAE &&
1724 !(sta->auth_alg == WLAN_AUTH_FT &&
1725 wpa_auth_uses_ft_sae(sta->wpa_sm))) {
c10347f2
JM
1726 wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
1727 "SAE AKM after non-SAE auth_alg %u",
1728 MAC2STR(sta->addr), sta->auth_alg);
1729 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1730 }
1731#endif /* CONFIG_SAE */
1732
ff36ff00 1733#ifdef CONFIG_IEEE80211N
f0c7a986 1734 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
ff36ff00 1735 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
5586f500
JM
1736 hostapd_logger(hapd, sta->addr,
1737 HOSTAPD_MODULE_IEEE80211,
1738 HOSTAPD_LEVEL_INFO,
1739 "Station tried to use TKIP with HT "
1740 "association");
1741 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
ff36ff00
JM
1742 }
1743#endif /* CONFIG_IEEE80211N */
a14896e8
JM
1744#ifdef CONFIG_HS20
1745 } else if (hapd->conf->osen) {
1746 if (elems.osen == NULL) {
1747 hostapd_logger(
1748 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1749 HOSTAPD_LEVEL_INFO,
1750 "No HS 2.0 OSEN element in association request");
1751 return WLAN_STATUS_INVALID_IE;
1752 }
1753
1754 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
1755 if (sta->wpa_sm == NULL)
1756 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1757 sta->addr, NULL);
1758 if (sta->wpa_sm == NULL) {
1759 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1760 "state machine");
1761 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1762 }
1763 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
1764 elems.osen - 2, elems.osen_len + 2) < 0)
1765 return WLAN_STATUS_INVALID_IE;
1766#endif /* CONFIG_HS20 */
a8d05fca
JM
1767 } else
1768 wpa_auth_sta_no_wpa(sta->wpa_sm);
6fc6879b 1769
b305c684 1770#ifdef CONFIG_P2P
3f4ce13f 1771 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
b305c684
JM
1772#endif /* CONFIG_P2P */
1773
f403dcd6
JM
1774#ifdef CONFIG_HS20
1775 wpabuf_free(sta->hs20_ie);
1776 if (elems.hs20 && elems.hs20_len > 4) {
1777 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
1778 elems.hs20_len - 4);
1779 } else
1780 sta->hs20_ie = NULL;
1781#endif /* CONFIG_HS20 */
1782
ae667c08
AN
1783#ifdef CONFIG_FST
1784 wpabuf_free(sta->mb_ies);
1785 if (hapd->iface->fst)
1786 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
1787 else
1788 sta->mb_ies = NULL;
1789#endif /* CONFIG_FST */
1790
4c572281 1791#ifdef CONFIG_MBO
6332aaf3
JM
1792 mbo_ap_check_sta_assoc(hapd, sta, &elems);
1793
4c572281
JM
1794 if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
1795 elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
1796 hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1797 wpa_printf(MSG_INFO,
1798 "MBO: Reject WPA2 association without PMF");
1799 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1800 }
1801#endif /* CONFIG_MBO */
1802
adf0478e
JM
1803 ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
1804 elems.supp_op_classes_len);
1805
629e1804
DS
1806 if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
1807 elems.rrm_enabled &&
1808 elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
1809 os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
1810 sizeof(sta->rrm_enabled_capa));
1811
5586f500
JM
1812 return WLAN_STATUS_SUCCESS;
1813}
1814
1815
1816static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
1817 u16 reason_code)
1818{
1819 int send_len;
1820 struct ieee80211_mgmt reply;
1821
1822 os_memset(&reply, 0, sizeof(reply));
1823 reply.frame_control =
1824 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
1825 os_memcpy(reply.da, addr, ETH_ALEN);
1826 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
1827 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
1828
1829 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
1830 reply.u.deauth.reason_code = host_to_le16(reason_code);
1831
8cfa3527 1832 if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
5586f500
JM
1833 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
1834 strerror(errno));
1835}
1836
1837
3f81ac07
AO
1838static int add_associated_sta(struct hostapd_data *hapd,
1839 struct sta_info *sta)
1840{
1841 struct ieee80211_ht_capabilities ht_cap;
1842 struct ieee80211_vht_capabilities vht_cap;
1843
1844 /*
1845 * Remove the STA entry to ensure the STA PS state gets cleared and
1846 * configuration gets updated. This is relevant for cases, such as
1847 * FT-over-the-DS, where a station re-associates back to the same AP but
1848 * skips the authentication flow, or if working with a driver that
1849 * does not support full AP client state.
1850 */
1851 if (!sta->added_unassoc)
1852 hostapd_drv_sta_remove(hapd, sta->addr);
1853
1854#ifdef CONFIG_IEEE80211N
1855 if (sta->flags & WLAN_STA_HT)
1856 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1857#endif /* CONFIG_IEEE80211N */
1858#ifdef CONFIG_IEEE80211AC
1859 if (sta->flags & WLAN_STA_VHT)
1860 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
1861#endif /* CONFIG_IEEE80211AC */
1862
1863 /*
1864 * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
1865 * will be set when the ACK frame for the (Re)Association Response frame
1866 * is processed (TX status driver event).
1867 */
1868 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
1869 sta->supported_rates, sta->supported_rates_len,
1870 sta->listen_interval,
1871 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
1872 sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
1873 sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
ae33239c
AB
1874 sta->vht_opmode, sta->p2p_ie ? 1 : 0,
1875 sta->added_unassoc)) {
3f81ac07
AO
1876 hostapd_logger(hapd, sta->addr,
1877 HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
1878 "Could not %s STA to kernel driver",
1879 sta->added_unassoc ? "set" : "add");
1880
1881 if (sta->added_unassoc) {
1882 hostapd_drv_sta_remove(hapd, sta->addr);
1883 sta->added_unassoc = 0;
1884 }
1885
1886 return -1;
1887 }
1888
1889 sta->added_unassoc = 0;
1890
1891 return 0;
1892}
1893
1894
bb598c3b
AB
1895static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
1896 u16 status_code, int reassoc, const u8 *ies,
1897 size_t ies_len)
5586f500
JM
1898{
1899 int send_len;
1900 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
1901 struct ieee80211_mgmt *reply;
1902 u8 *p;
1903
1904 os_memset(buf, 0, sizeof(buf));
1905 reply = (struct ieee80211_mgmt *) buf;
1906 reply->frame_control =
1907 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1908 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1909 WLAN_FC_STYPE_ASSOC_RESP));
1910 os_memcpy(reply->da, sta->addr, ETH_ALEN);
1911 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1912 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
1913
1914 send_len = IEEE80211_HDRLEN;
1915 send_len += sizeof(reply->u.assoc_resp);
1916 reply->u.assoc_resp.capab_info =
f41ded6f 1917 host_to_le16(hostapd_own_capab_info(hapd));
5586f500 1918 reply->u.assoc_resp.status_code = host_to_le16(status_code);
93a1caec 1919 reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
5586f500
JM
1920 /* Supported rates */
1921 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1922 /* Extended supported rates */
1923 p = hostapd_eid_ext_supp_rates(hapd, p);
5586f500
JM
1924
1925#ifdef CONFIG_IEEE80211R
1926 if (status_code == WLAN_STATUS_SUCCESS) {
1927 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1928 * Transition Information, RSN, [RIC Response] */
1929 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1930 buf + sizeof(buf) - p,
1931 sta->auth_alg, ies, ies_len);
1932 }
1933#endif /* CONFIG_IEEE80211R */
1934
1935#ifdef CONFIG_IEEE80211W
1936 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1937 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1938#endif /* CONFIG_IEEE80211W */
1939
1bc774a1
JM
1940#ifdef CONFIG_IEEE80211N
1941 p = hostapd_eid_ht_capabilities(hapd, p);
1942 p = hostapd_eid_ht_operation(hapd, p);
1943#endif /* CONFIG_IEEE80211N */
1944
14708b50 1945#ifdef CONFIG_IEEE80211AC
e7d0e97b
YL
1946 if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
1947 p = hostapd_eid_vht_capabilities(hapd, p);
1948 p = hostapd_eid_vht_operation(hapd, p);
1949 }
14708b50
MP
1950#endif /* CONFIG_IEEE80211AC */
1951
1161ff1e 1952 p = hostapd_eid_ext_capab(hapd, p);
b6668734 1953 p = hostapd_eid_bss_max_idle_period(hapd, p);
c551700f
KP
1954 if (sta->qos_map_enabled)
1955 p = hostapd_eid_qos_map_set(hapd, p);
1161ff1e 1956
347827ff
AN
1957#ifdef CONFIG_FST
1958 if (hapd->iface->fst_ies) {
1959 os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
1960 wpabuf_len(hapd->iface->fst_ies));
1961 p += wpabuf_len(hapd->iface->fst_ies);
1962 }
1963#endif /* CONFIG_FST */
1964
e7d0e97b
YL
1965#ifdef CONFIG_IEEE80211AC
1966 if (hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
1967 p = hostapd_eid_vendor_vht(hapd, p);
1968#endif /* CONFIG_IEEE80211AC */
1969
1bc774a1
JM
1970 if (sta->flags & WLAN_STA_WMM)
1971 p = hostapd_eid_wmm(hapd, p);
1972
ed7a09f9 1973#ifdef CONFIG_WPS
71093e5e
JM
1974 if ((sta->flags & WLAN_STA_WPS) ||
1975 ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
ed7a09f9
JM
1976 struct wpabuf *wps = wps_build_assoc_resp_ie();
1977 if (wps) {
1978 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
1979 p += wpabuf_len(wps);
1980 wpabuf_free(wps);
1981 }
1982 }
1983#endif /* CONFIG_WPS */
1984
8ccbe415 1985#ifdef CONFIG_P2P
4f6cd3f4 1986 if (sta->p2p_ie && hapd->p2p_group) {
8ccbe415
JM
1987 struct wpabuf *p2p_resp_ie;
1988 enum p2p_status_code status;
1989 switch (status_code) {
1990 case WLAN_STATUS_SUCCESS:
1991 status = P2P_SC_SUCCESS;
1992 break;
1993 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
1994 status = P2P_SC_FAIL_LIMIT_REACHED;
1995 break;
1996 default:
1997 status = P2P_SC_FAIL_INVALID_PARAMS;
1998 break;
1999 }
2000 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
2001 if (p2p_resp_ie) {
2002 os_memcpy(p, wpabuf_head(p2p_resp_ie),
2003 wpabuf_len(p2p_resp_ie));
2004 p += wpabuf_len(p2p_resp_ie);
2005 wpabuf_free(p2p_resp_ie);
2006 }
2007 }
2008#endif /* CONFIG_P2P */
2009
962473c1
JM
2010#ifdef CONFIG_P2P_MANAGER
2011 if (hapd->conf->p2p & P2P_MANAGE)
2012 p = hostapd_eid_p2p_manage(hapd, p);
2013#endif /* CONFIG_P2P_MANAGER */
2014
fb9a1c3e
AS
2015 p = hostapd_eid_mbo(hapd, p, buf + sizeof(buf) - p);
2016
a9112270
BKB
2017 if (hapd->conf->assocresp_elements &&
2018 (size_t) (buf + sizeof(buf) - p) >=
2019 wpabuf_len(hapd->conf->assocresp_elements)) {
2020 os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
2021 wpabuf_len(hapd->conf->assocresp_elements));
2022 p += wpabuf_len(hapd->conf->assocresp_elements);
2023 }
2024
5586f500
JM
2025 send_len += p - reply->u.assoc_resp.variable;
2026
bb598c3b 2027 if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0) {
5586f500
JM
2028 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
2029 strerror(errno));
bb598c3b
AB
2030 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2031 }
2032
2033 return WLAN_STATUS_SUCCESS;
5586f500
JM
2034}
2035
2036
2037static void handle_assoc(struct hostapd_data *hapd,
b57e086c
JM
2038 const struct ieee80211_mgmt *mgmt, size_t len,
2039 int reassoc)
5586f500 2040{
38cb0a2d 2041 u16 capab_info, listen_interval, seq_ctrl, fc;
bb598c3b 2042 u16 resp = WLAN_STATUS_SUCCESS, reply_res;
b57e086c 2043 const u8 *pos;
5586f500
JM
2044 int left, i;
2045 struct sta_info *sta;
2046
2047 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
2048 sizeof(mgmt->u.assoc_req))) {
61323e70
JM
2049 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
2050 reassoc, (unsigned long) len);
5586f500
JM
2051 return;
2052 }
2053
c2aff6b1
JB
2054#ifdef CONFIG_TESTING_OPTIONS
2055 if (reassoc) {
06df2aa6 2056 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
c2aff6b1
JB
2057 drand48() < hapd->iconf->ignore_reassoc_probability) {
2058 wpa_printf(MSG_INFO,
2059 "TESTING: ignoring reassoc request from "
2060 MACSTR, MAC2STR(mgmt->sa));
2061 return;
2062 }
2063 } else {
06df2aa6 2064 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
c2aff6b1
JB
2065 drand48() < hapd->iconf->ignore_assoc_probability) {
2066 wpa_printf(MSG_INFO,
2067 "TESTING: ignoring assoc request from "
2068 MACSTR, MAC2STR(mgmt->sa));
2069 return;
2070 }
2071 }
2072#endif /* CONFIG_TESTING_OPTIONS */
2073
38cb0a2d
IP
2074 fc = le_to_host16(mgmt->frame_control);
2075 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
2076
5586f500
JM
2077 if (reassoc) {
2078 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
2079 listen_interval = le_to_host16(
2080 mgmt->u.reassoc_req.listen_interval);
2081 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
2082 " capab_info=0x%02x listen_interval=%d current_ap="
38cb0a2d 2083 MACSTR " seq_ctrl=0x%x%s",
5586f500 2084 MAC2STR(mgmt->sa), capab_info, listen_interval,
38cb0a2d
IP
2085 MAC2STR(mgmt->u.reassoc_req.current_ap),
2086 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
5586f500
JM
2087 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
2088 pos = mgmt->u.reassoc_req.variable;
2089 } else {
2090 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
2091 listen_interval = le_to_host16(
2092 mgmt->u.assoc_req.listen_interval);
2093 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
38cb0a2d
IP
2094 " capab_info=0x%02x listen_interval=%d "
2095 "seq_ctrl=0x%x%s",
2096 MAC2STR(mgmt->sa), capab_info, listen_interval,
2097 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
5586f500
JM
2098 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
2099 pos = mgmt->u.assoc_req.variable;
2100 }
2101
2102 sta = ap_get_sta(hapd, mgmt->sa);
2103#ifdef CONFIG_IEEE80211R
2104 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
2105 (sta->flags & WLAN_STA_AUTH) == 0) {
2106 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
2107 "prior to authentication since it is using "
2108 "over-the-DS FT", MAC2STR(mgmt->sa));
bb598c3b
AB
2109
2110 /*
2111 * Mark station as authenticated, to avoid adding station
2112 * entry in the driver as associated and not authenticated
2113 */
2114 sta->flags |= WLAN_STA_AUTH;
5586f500
JM
2115 } else
2116#endif /* CONFIG_IEEE80211R */
2117 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
2118 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2119 HOSTAPD_LEVEL_INFO, "Station tried to "
2120 "associate before authentication "
2121 "(aid=%d flags=0x%x)",
2122 sta ? sta->aid : -1,
2123 sta ? sta->flags : 0);
2124 send_deauth(hapd, mgmt->sa,
2125 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
2126 return;
2127 }
2128
38cb0a2d
IP
2129 if ((fc & WLAN_FC_RETRY) &&
2130 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2131 sta->last_seq_ctrl == seq_ctrl &&
2132 sta->last_subtype == reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
2133 WLAN_FC_STYPE_ASSOC_REQ) {
2134 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2135 HOSTAPD_LEVEL_DEBUG,
2136 "Drop repeated association frame seq_ctrl=0x%x",
2137 seq_ctrl);
2138 return;
2139 }
2140 sta->last_seq_ctrl = seq_ctrl;
2141 sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
2142 WLAN_FC_STYPE_ASSOC_REQ;
2143
5586f500
JM
2144 if (hapd->tkip_countermeasures) {
2145 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
2146 goto fail;
2147 }
2148
2149 if (listen_interval > hapd->conf->max_listen_interval) {
2150 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2151 HOSTAPD_LEVEL_DEBUG,
2152 "Too large Listen Interval (%d)",
2153 listen_interval);
2154 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
2155 goto fail;
2156 }
2157
fb9a1c3e
AS
2158#ifdef CONFIG_MBO
2159 if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
2160 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2161 goto fail;
2162 }
2163#endif /* CONFIG_MBO */
2164
629e1804
DS
2165 /*
2166 * sta->capability is used in check_assoc_ies() for RRM enabled
2167 * capability element.
2168 */
2169 sta->capability = capab_info;
2170
5586f500
JM
2171 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
2172 * is used */
2173 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
2174 if (resp != WLAN_STATUS_SUCCESS)
2175 goto fail;
2176
2177 if (hostapd_get_aid(hapd, sta) < 0) {
2178 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2179 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
2180 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2181 goto fail;
2182 }
2183
5586f500
JM
2184 sta->listen_interval = listen_interval;
2185
6fc6879b
JM
2186 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
2187 sta->flags |= WLAN_STA_NONERP;
2188 for (i = 0; i < sta->supported_rates_len; i++) {
2189 if ((sta->supported_rates[i] & 0x7f) > 22) {
2190 sta->flags &= ~WLAN_STA_NONERP;
2191 break;
2192 }
2193 }
2194 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
2195 sta->nonerp_set = 1;
2196 hapd->iface->num_sta_non_erp++;
2197 if (hapd->iface->num_sta_non_erp == 1)
2198 ieee802_11_set_beacons(hapd->iface);
2199 }
2200
2201 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
2202 !sta->no_short_slot_time_set) {
2203 sta->no_short_slot_time_set = 1;
2204 hapd->iface->num_sta_no_short_slot_time++;
2205 if (hapd->iface->current_mode->mode ==
2206 HOSTAPD_MODE_IEEE80211G &&
2207 hapd->iface->num_sta_no_short_slot_time == 1)
2208 ieee802_11_set_beacons(hapd->iface);
2209 }
2210
2211 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2212 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
2213 else
2214 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
2215
2216 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
2217 !sta->no_short_preamble_set) {
2218 sta->no_short_preamble_set = 1;
2219 hapd->iface->num_sta_no_short_preamble++;
2220 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
2221 && hapd->iface->num_sta_no_short_preamble == 1)
2222 ieee802_11_set_beacons(hapd->iface);
2223 }
2224
d45354be 2225#ifdef CONFIG_IEEE80211N
5586f500 2226 update_ht_state(hapd, sta);
d45354be 2227#endif /* CONFIG_IEEE80211N */
de9289c8 2228
6fc6879b
JM
2229 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2230 HOSTAPD_LEVEL_DEBUG,
2231 "association OK (aid %d)", sta->aid);
2232 /* Station will be marked associated, after it acknowledges AssocResp
2233 */
b8281964 2234 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
6fc6879b 2235
6f5c8dbd
JM
2236#ifdef CONFIG_IEEE80211W
2237 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
2238 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
2239 "SA Query procedure", reassoc ? "re" : "");
2240 /* TODO: Send a protected Disassociate frame to the STA using
2241 * the old key and Reason Code "Previous Authentication no
2242 * longer valid". Make sure this is only sent protected since
2243 * unprotected frame would be received by the STA that is now
2244 * trying to associate.
2245 */
2246 }
2247#endif /* CONFIG_IEEE80211W */
2248
6fc6879b
JM
2249 /* Make sure that the previously registered inactivity timer will not
2250 * remove the STA immediately. */
2251 sta->timeout_next = STA_NULLFUNC;
2252
2253 fail:
3f81ac07
AO
2254 /*
2255 * In case of a successful response, add the station to the driver.
2256 * Otherwise, the kernel may ignore Data frames before we process the
2257 * ACK frame (TX status). In case of a failure, this station will be
2258 * removed.
2259 *
2260 * Note that this is not compliant with the IEEE 802.11 standard that
2261 * states that a non-AP station should transition into the
2262 * authenticated/associated state only after the station acknowledges
2263 * the (Re)Association Response frame. However, still do this as:
2264 *
2265 * 1. In case the station does not acknowledge the (Re)Association
2266 * Response frame, it will be removed.
2267 * 2. Data frames will be dropped in the kernel until the station is
2268 * set into authorized state, and there are no significant known
2269 * issues with processing other non-Data Class 3 frames during this
2270 * window.
2271 */
2272 if (resp == WLAN_STATUS_SUCCESS && add_associated_sta(hapd, sta))
2273 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
2274
bb598c3b 2275 reply_res = send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
3f81ac07
AO
2276
2277 /*
2278 * Remove the station in case tranmission of a success response fails
2279 * (the STA was added associated to the driver) or if the station was
2280 * previously added unassociated.
2281 */
2282 if ((reply_res != WLAN_STATUS_SUCCESS &&
2283 resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc) {
bb598c3b
AB
2284 hostapd_drv_sta_remove(hapd, sta->addr);
2285 sta->added_unassoc = 0;
2286 }
6fc6879b
JM
2287}
2288
2289
6fc6879b 2290static void handle_disassoc(struct hostapd_data *hapd,
f8b1f695 2291 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
2292{
2293 struct sta_info *sta;
2294
2295 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
61323e70
JM
2296 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
2297 (unsigned long) len);
6fc6879b
JM
2298 return;
2299 }
2300
2301 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
2302 MAC2STR(mgmt->sa),
2303 le_to_host16(mgmt->u.disassoc.reason_code));
2304
6fc6879b
JM
2305 sta = ap_get_sta(hapd, mgmt->sa);
2306 if (sta == NULL) {
61323e70
JM
2307 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
2308 MAC2STR(mgmt->sa));
6fc6879b
JM
2309 return;
2310 }
2311
ae055af4 2312 ap_sta_set_authorized(hapd, sta, 0);
38cb0a2d 2313 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
b8281964 2314 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
6fc6879b
JM
2315 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
2316 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2317 HOSTAPD_LEVEL_INFO, "disassociated");
2318 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
2319 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
2320 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
2321 * authenticated. */
2322 accounting_sta_stop(hapd, sta);
d7c3347f 2323 ieee802_1x_free_station(hapd, sta);
7d597d46 2324 if (sta->ipaddr)
ed4ddb6d 2325 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
bd00c431 2326 ap_sta_ip6addr_del(hapd, sta);
51e2a27a 2327 hostapd_drv_sta_remove(hapd, sta->addr);
bb598c3b 2328 sta->added_unassoc = 0;
6fc6879b
JM
2329
2330 if (sta->timeout_next == STA_NULLFUNC ||
2331 sta->timeout_next == STA_DISASSOC) {
2332 sta->timeout_next = STA_DEAUTH;
2333 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2334 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
2335 hapd, sta);
2336 }
2337
2338 mlme_disassociate_indication(
2339 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
2340}
2341
2342
2343static void handle_deauth(struct hostapd_data *hapd,
f8b1f695 2344 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
2345{
2346 struct sta_info *sta;
2347
2348 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
3ec1e902
JM
2349 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
2350 "payload (len=%lu)", (unsigned long) len);
6fc6879b
JM
2351 return;
2352 }
2353
3ec1e902 2354 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
24d75245
BG
2355 " reason_code=%d",
2356 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
6fc6879b 2357
6fc6879b
JM
2358 sta = ap_get_sta(hapd, mgmt->sa);
2359 if (sta == NULL) {
3ec1e902
JM
2360 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
2361 "to deauthenticate, but it is not authenticated",
24d75245 2362 MAC2STR(mgmt->sa));
6fc6879b
JM
2363 return;
2364 }
2365
ae055af4 2366 ap_sta_set_authorized(hapd, sta, 0);
38cb0a2d 2367 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
b8281964
JM
2368 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
2369 WLAN_STA_ASSOC_REQ_OK);
6fc6879b
JM
2370 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
2371 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2372 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
2373 mlme_deauthenticate_indication(
2374 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
2375 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
2376 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
2377 ap_free_sta(hapd, sta);
2378}
2379
2380
2381static void handle_beacon(struct hostapd_data *hapd,
b57e086c 2382 const struct ieee80211_mgmt *mgmt, size_t len,
6fc6879b
JM
2383 struct hostapd_frame_info *fi)
2384{
2385 struct ieee802_11_elems elems;
2386
2387 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
61323e70
JM
2388 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
2389 (unsigned long) len);
6fc6879b
JM
2390 return;
2391 }
2392
3d536eb4 2393 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
6fc6879b
JM
2394 len - (IEEE80211_HDRLEN +
2395 sizeof(mgmt->u.beacon)), &elems,
2396 0);
2397
6fc6879b
JM
2398 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
2399}
2400
2401
5d22a1d5 2402#ifdef CONFIG_IEEE80211W
88b4b424 2403
912b34f0
JM
2404static int hostapd_sa_query_action(struct hostapd_data *hapd,
2405 const struct ieee80211_mgmt *mgmt,
2406 size_t len)
5d22a1d5 2407{
b57e086c 2408 const u8 *end;
5d22a1d5 2409
93b76319
JM
2410 end = mgmt->u.action.u.sa_query_resp.trans_id +
2411 WLAN_SA_QUERY_TR_ID_LEN;
5d22a1d5 2412 if (((u8 *) mgmt) + len < end) {
93b76319 2413 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
5d22a1d5 2414 "frame (len=%lu)", (unsigned long) len);
912b34f0 2415 return 0;
5d22a1d5
JM
2416 }
2417
d4370eac
MP
2418 ieee802_11_sa_query_action(hapd, mgmt->sa,
2419 mgmt->u.action.u.sa_query_resp.action,
2420 mgmt->u.action.u.sa_query_resp.trans_id);
912b34f0 2421 return 1;
5d22a1d5 2422}
5d22a1d5
JM
2423
2424
c4e281fd
JM
2425static int robust_action_frame(u8 category)
2426{
2427 return category != WLAN_ACTION_PUBLIC &&
2428 category != WLAN_ACTION_HT;
2429}
9f64b827 2430#endif /* CONFIG_IEEE80211W */
c4e281fd
JM
2431
2432
912b34f0
JM
2433static int handle_action(struct hostapd_data *hapd,
2434 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b 2435{
c4e281fd 2436 struct sta_info *sta;
46eeedac 2437 sta = ap_get_sta(hapd, mgmt->sa);
c4e281fd 2438
6fc6879b
JM
2439 if (len < IEEE80211_HDRLEN + 1) {
2440 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2441 HOSTAPD_LEVEL_DEBUG,
2442 "handle_action - too short payload (len=%lu)",
2443 (unsigned long) len);
912b34f0 2444 return 0;
6fc6879b
JM
2445 }
2446
c79938a5
JM
2447 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
2448 (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
2449 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
2450 "frame (category=%u) from unassociated STA " MACSTR,
2451 MAC2STR(mgmt->sa), mgmt->u.action.category);
912b34f0 2452 return 0;
c79938a5
JM
2453 }
2454
c4e281fd
JM
2455#ifdef CONFIG_IEEE80211W
2456 if (sta && (sta->flags & WLAN_STA_MFP) &&
7b2c42f8
JM
2457 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
2458 robust_action_frame(mgmt->u.action.category)) {
c4e281fd
JM
2459 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2460 HOSTAPD_LEVEL_DEBUG,
2461 "Dropped unprotected Robust Action frame from "
2462 "an MFP STA");
912b34f0 2463 return 0;
c4e281fd
JM
2464 }
2465#endif /* CONFIG_IEEE80211W */
2466
38cb0a2d
IP
2467 if (sta) {
2468 u16 fc = le_to_host16(mgmt->frame_control);
2469 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
2470
2471 if ((fc & WLAN_FC_RETRY) &&
2472 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
2473 sta->last_seq_ctrl == seq_ctrl &&
2474 sta->last_subtype == WLAN_FC_STYPE_ACTION) {
2475 hostapd_logger(hapd, sta->addr,
2476 HOSTAPD_MODULE_IEEE80211,
2477 HOSTAPD_LEVEL_DEBUG,
2478 "Drop repeated action frame seq_ctrl=0x%x",
2479 seq_ctrl);
2480 return 1;
2481 }
2482
2483 sta->last_seq_ctrl = seq_ctrl;
2484 sta->last_subtype = WLAN_FC_STYPE_ACTION;
2485 }
2486
6fc6879b
JM
2487 switch (mgmt->u.action.category) {
2488#ifdef CONFIG_IEEE80211R
2489 case WLAN_ACTION_FT:
d6c6b1fb
JM
2490 if (!sta ||
2491 wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
6fc6879b
JM
2492 len - IEEE80211_HDRLEN))
2493 break;
912b34f0 2494 return 1;
6fc6879b 2495#endif /* CONFIG_IEEE80211R */
c2a71408 2496 case WLAN_ACTION_WMM:
3ae0800c 2497 hostapd_wmm_action(hapd, mgmt, len);
912b34f0 2498 return 1;
5d22a1d5 2499#ifdef CONFIG_IEEE80211W
93b76319 2500 case WLAN_ACTION_SA_QUERY:
912b34f0 2501 return hostapd_sa_query_action(hapd, mgmt, len);
5d22a1d5 2502#endif /* CONFIG_IEEE80211W */
c79938a5
JM
2503#ifdef CONFIG_WNM
2504 case WLAN_ACTION_WNM:
dbfb8e82
JM
2505 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
2506 return 1;
c79938a5 2507#endif /* CONFIG_WNM */
037378ff
AN
2508#ifdef CONFIG_FST
2509 case WLAN_ACTION_FST:
2510 if (hapd->iface->fst)
2511 fst_rx_action(hapd->iface->fst, mgmt, len);
94edea89
JM
2512 else
2513 wpa_printf(MSG_DEBUG,
2514 "FST: Ignore FST Action frame - no FST attached");
037378ff
AN
2515 return 1;
2516#endif /* CONFIG_FST */
c706d5aa 2517 case WLAN_ACTION_PUBLIC:
5ce00d09 2518 case WLAN_ACTION_PROTECTED_DUAL:
587d60d2 2519#ifdef CONFIG_IEEE80211N
fd66aa63
JM
2520 if (len >= IEEE80211_HDRLEN + 2 &&
2521 mgmt->u.action.u.public_action.action ==
587d60d2
PX
2522 WLAN_PA_20_40_BSS_COEX) {
2523 wpa_printf(MSG_DEBUG,
2524 "HT20/40 coex mgmt frame received from STA "
2525 MACSTR, MAC2STR(mgmt->sa));
2526 hostapd_2040_coex_action(hapd, mgmt, len);
2527 }
2528#endif /* CONFIG_IEEE80211N */
c706d5aa
JM
2529 if (hapd->public_action_cb) {
2530 hapd->public_action_cb(hapd->public_action_cb_ctx,
2531 (u8 *) mgmt, len,
2532 hapd->iface->freq);
c706d5aa 2533 }
2d9ffe1e 2534 if (hapd->public_action_cb2) {
8dabf4bb 2535 hapd->public_action_cb2(hapd->public_action_cb2_ctx,
2d9ffe1e
JM
2536 (u8 *) mgmt, len,
2537 hapd->iface->freq);
2538 }
2539 if (hapd->public_action_cb || hapd->public_action_cb2)
912b34f0 2540 return 1;
c706d5aa 2541 break;
e44f8bf2
JM
2542 case WLAN_ACTION_VENDOR_SPECIFIC:
2543 if (hapd->vendor_action_cb) {
2544 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
2545 (u8 *) mgmt, len,
2546 hapd->iface->freq) == 0)
912b34f0 2547 return 1;
e44f8bf2
JM
2548 }
2549 break;
2572df34
DS
2550 case WLAN_ACTION_RADIO_MEASUREMENT:
2551 hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
2552 return 1;
6fc6879b
JM
2553 }
2554
2555 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2556 HOSTAPD_LEVEL_DEBUG,
2557 "handle_action - unknown action category %d or invalid "
2558 "frame",
2559 mgmt->u.action.category);
81372e34
JB
2560 if (!is_multicast_ether_addr(mgmt->da) &&
2561 !(mgmt->u.action.category & 0x80) &&
2562 !is_multicast_ether_addr(mgmt->sa)) {
b57e086c
JM
2563 struct ieee80211_mgmt *resp;
2564
6fc6879b
JM
2565 /*
2566 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
2567 * Return the Action frame to the source without change
2568 * except that MSB of the Category set to 1.
2569 */
2570 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
2571 "frame back to sender");
b57e086c
JM
2572 resp = os_malloc(len);
2573 if (resp == NULL)
912b34f0 2574 return 0;
b57e086c
JM
2575 os_memcpy(resp, mgmt, len);
2576 os_memcpy(resp->da, resp->sa, ETH_ALEN);
2577 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
2578 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
2579 resp->u.action.category |= 0x80;
2580
41fe8b42
JM
2581 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
2582 wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
2583 "Action frame");
2584 }
b57e086c 2585 os_free(resp);
6fc6879b 2586 }
912b34f0
JM
2587
2588 return 1;
6fc6879b
JM
2589}
2590
2591
2592/**
2593 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
2594 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
2595 * sent to)
2596 * @buf: management frame data (starting from IEEE 802.11 header)
2597 * @len: length of frame data in octets
a17df5fb 2598 * @fi: meta data about received frame (signal level, etc.)
6fc6879b
JM
2599 *
2600 * Process all incoming IEEE 802.11 management frames. This will be called for
2601 * each frame received from the kernel driver through wlan#ap interface. In
2602 * addition, it can be called to re-inserted pending frames (e.g., when using
2603 * external RADIUS server as an MAC ACL).
2604 */
912b34f0
JM
2605int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
2606 struct hostapd_frame_info *fi)
6fc6879b 2607{
f8b1f695 2608 struct ieee80211_mgmt *mgmt;
f8b1f695 2609 u16 fc, stype;
912b34f0 2610 int ret = 0;
f8b1f695 2611
cbcf92b4 2612 if (len < 24)
912b34f0 2613 return 0;
cbcf92b4 2614
f8b1f695
JM
2615 mgmt = (struct ieee80211_mgmt *) buf;
2616 fc = le_to_host16(mgmt->frame_control);
2617 stype = WLAN_FC_GET_STYPE(fc);
6fc6879b
JM
2618
2619 if (stype == WLAN_FC_STYPE_BEACON) {
2620 handle_beacon(hapd, mgmt, len, fi);
912b34f0 2621 return 1;
6fc6879b
JM
2622 }
2623
842c5af5 2624 if (!is_broadcast_ether_addr(mgmt->bssid) &&
3eeee931
AN
2625#ifdef CONFIG_P2P
2626 /* Invitation responses can be sent with the peer MAC as BSSID */
2627 !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
2628 stype == WLAN_FC_STYPE_ACTION) &&
2629#endif /* CONFIG_P2P */
f3e9899e
BC
2630#ifdef CONFIG_MESH
2631 !(hapd->conf->mesh & MESH_ENABLED) &&
2632#endif /* CONFIG_MESH */
fe2c5241 2633 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
61323e70
JM
2634 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
2635 MAC2STR(mgmt->bssid));
912b34f0 2636 return 0;
6fc6879b
JM
2637 }
2638
2639
2640 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
baf513d6 2641 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
912b34f0 2642 return 1;
6fc6879b
JM
2643 }
2644
2645 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
2646 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2647 HOSTAPD_LEVEL_DEBUG,
2648 "MGMT: DA=" MACSTR " not our address",
2649 MAC2STR(mgmt->da));
912b34f0 2650 return 0;
6fc6879b
JM
2651 }
2652
b308a304
JM
2653 if (hapd->iconf->track_sta_max_num)
2654 sta_track_add(hapd->iface, mgmt->sa);
2655
6fc6879b
JM
2656 switch (stype) {
2657 case WLAN_FC_STYPE_AUTH:
2658 wpa_printf(MSG_DEBUG, "mgmt::auth");
2659 handle_auth(hapd, mgmt, len);
912b34f0 2660 ret = 1;
6fc6879b
JM
2661 break;
2662 case WLAN_FC_STYPE_ASSOC_REQ:
2663 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
2664 handle_assoc(hapd, mgmt, len, 0);
912b34f0 2665 ret = 1;
6fc6879b 2666 break;
6fc6879b
JM
2667 case WLAN_FC_STYPE_REASSOC_REQ:
2668 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
2669 handle_assoc(hapd, mgmt, len, 1);
912b34f0 2670 ret = 1;
6fc6879b
JM
2671 break;
2672 case WLAN_FC_STYPE_DISASSOC:
2673 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
2674 handle_disassoc(hapd, mgmt, len);
912b34f0 2675 ret = 1;
6fc6879b
JM
2676 break;
2677 case WLAN_FC_STYPE_DEAUTH:
3ec1e902 2678 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
6fc6879b 2679 handle_deauth(hapd, mgmt, len);
912b34f0 2680 ret = 1;
6fc6879b
JM
2681 break;
2682 case WLAN_FC_STYPE_ACTION:
2683 wpa_printf(MSG_DEBUG, "mgmt::action");
912b34f0 2684 ret = handle_action(hapd, mgmt, len);
6fc6879b
JM
2685 break;
2686 default:
2687 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2688 HOSTAPD_LEVEL_DEBUG,
2689 "unknown mgmt frame subtype %d", stype);
2690 break;
2691 }
912b34f0
JM
2692
2693 return ret;
6fc6879b
JM
2694}
2695
2696
2697static void handle_auth_cb(struct hostapd_data *hapd,
f8b1f695 2698 const struct ieee80211_mgmt *mgmt,
6fc6879b
JM
2699 size_t len, int ok)
2700{
2701 u16 auth_alg, auth_transaction, status_code;
2702 struct sta_info *sta;
2703
bb598c3b
AB
2704 sta = ap_get_sta(hapd, mgmt->da);
2705 if (!sta) {
2706 wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
2707 MAC2STR(mgmt->da));
2708 return;
2709 }
2710
2711 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2712 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2713 status_code = le_to_host16(mgmt->u.auth.status_code);
2714
6fc6879b
JM
2715 if (!ok) {
2716 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2717 HOSTAPD_LEVEL_NOTICE,
2718 "did not acknowledge authentication response");
bb598c3b 2719 goto fail;
6fc6879b
JM
2720 }
2721
2722 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
61323e70
JM
2723 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
2724 (unsigned long) len);
bb598c3b 2725 goto fail;
6fc6879b
JM
2726 }
2727
2728 if (status_code == WLAN_STATUS_SUCCESS &&
2729 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
2730 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
2731 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2732 HOSTAPD_LEVEL_INFO, "authenticated");
2733 sta->flags |= WLAN_STA_AUTH;
bb598c3b
AB
2734 if (sta->added_unassoc)
2735 hostapd_set_sta_flags(hapd, sta);
2736 return;
2737 }
2738
2739fail:
2740 if (status_code != WLAN_STATUS_SUCCESS && sta->added_unassoc) {
2741 hostapd_drv_sta_remove(hapd, sta->addr);
2742 sta->added_unassoc = 0;
6fc6879b
JM
2743 }
2744}
2745
2746
69dd2967
SM
2747static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
2748 struct sta_info *sta,
2749 char *ifname_wds)
2750{
2751 int i;
f41ded6f 2752 struct hostapd_ssid *ssid = &hapd->conf->ssid;
69dd2967
SM
2753
2754 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
2755 return;
2756
2757 for (i = 0; i < 4; i++) {
2758 if (ssid->wep.key[i] &&
2759 hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
2760 i == ssid->wep.idx, NULL, 0,
2761 ssid->wep.key[i], ssid->wep.len[i])) {
2762 wpa_printf(MSG_WARNING,
2763 "Could not set WEP keys for WDS interface; %s",
2764 ifname_wds);
2765 break;
2766 }
2767 }
2768}
2769
2770
6fc6879b 2771static void handle_assoc_cb(struct hostapd_data *hapd,
f8b1f695 2772 const struct ieee80211_mgmt *mgmt,
6fc6879b
JM
2773 size_t len, int reassoc, int ok)
2774{
2775 u16 status;
2776 struct sta_info *sta;
2777 int new_assoc = 1;
2778
6fc6879b
JM
2779 sta = ap_get_sta(hapd, mgmt->da);
2780 if (!sta) {
61323e70
JM
2781 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
2782 MAC2STR(mgmt->da));
6fc6879b
JM
2783 return;
2784 }
2785
bb598c3b
AB
2786 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
2787 sizeof(mgmt->u.assoc_resp))) {
2788 wpa_printf(MSG_INFO,
2789 "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
2790 reassoc, (unsigned long) len);
3f81ac07
AO
2791 hostapd_drv_sta_remove(hapd, sta->addr);
2792 return;
bb598c3b
AB
2793 }
2794
3f81ac07
AO
2795 if (reassoc)
2796 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
2797 else
2798 status = le_to_host16(mgmt->u.assoc_resp.status_code);
2799
22b42372
FF
2800 if (!ok) {
2801 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2802 HOSTAPD_LEVEL_DEBUG,
2803 "did not acknowledge association response");
2804 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
3f81ac07
AO
2805 /* The STA is added only in case of SUCCESS */
2806 if (status == WLAN_STATUS_SUCCESS)
2807 hostapd_drv_sta_remove(hapd, sta->addr);
22b42372 2808
3f81ac07
AO
2809 return;
2810 }
22b42372 2811
6fc6879b 2812 if (status != WLAN_STATUS_SUCCESS)
5bdac4ab 2813 return;
6fc6879b
JM
2814
2815 /* Stop previous accounting session, if one is started, and allocate
2816 * new session id for the new session. */
2817 accounting_sta_stop(hapd, sta);
6fc6879b
JM
2818
2819 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2820 HOSTAPD_LEVEL_INFO,
2fc98d02
JM
2821 "associated (aid %d)",
2822 sta->aid);
6fc6879b
JM
2823
2824 if (sta->flags & WLAN_STA_ASSOC)
2825 new_assoc = 0;
2826 sta->flags |= WLAN_STA_ASSOC;
3578e665 2827 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
a14896e8 2828 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
ef580012
JM
2829 sta->auth_alg == WLAN_AUTH_FT) {
2830 /*
2831 * Open, static WEP, or FT protocol; no separate authorization
2832 * step.
2833 */
6905dcb1 2834 ap_sta_set_authorized(hapd, sta, 1);
515cf93f 2835 }
6fc6879b
JM
2836
2837 if (reassoc)
2838 mlme_reassociate_indication(hapd, sta);
2839 else
2840 mlme_associate_indication(hapd, sta);
2841
5d22a1d5 2842#ifdef CONFIG_IEEE80211W
93b76319 2843 sta->sa_query_timed_out = 0;
5d22a1d5
JM
2844#endif /* CONFIG_IEEE80211W */
2845
69dd2967
SM
2846 if (sta->flags & WLAN_STA_WDS) {
2847 int ret;
2848 char ifname_wds[IFNAMSIZ + 1];
2849
2850 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
2851 sta->aid, 1);
2852 if (!ret)
2853 hostapd_set_wds_encryption(hapd, sta, ifname_wds);
2854 }
7826ceae 2855
6fc6879b
JM
2856 if (sta->eapol_sm == NULL) {
2857 /*
2858 * This STA does not use RADIUS server for EAP authentication,
2859 * so bind it to the selected VLAN interface now, since the
2860 * interface selection is not going to change anymore.
2861 */
c8e6beab 2862 if (ap_sta_bind_vlan(hapd, sta) < 0)
5bdac4ab 2863 return;
6fc6879b
JM
2864 } else if (sta->vlan_id) {
2865 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
c8e6beab 2866 if (ap_sta_bind_vlan(hapd, sta) < 0)
5bdac4ab 2867 return;
6fc6879b 2868 }
eddd8010 2869
0e8a96a9 2870 hostapd_set_sta_flags(hapd, sta);
6fc6879b
JM
2871
2872 if (sta->auth_alg == WLAN_AUTH_FT)
2873 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
2874 else
2875 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
d24df7c3 2876 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
6fc6879b 2877 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
f2accfe7
EP
2878
2879 if (sta->pending_eapol_rx) {
2880 struct os_reltime now, age;
2881
2882 os_get_reltime(&now);
2883 os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
2884 if (age.sec == 0 && age.usec < 200000) {
2885 wpa_printf(MSG_DEBUG,
2886 "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
2887 MAC2STR(sta->addr));
2888 ieee802_1x_receive(
2889 hapd, mgmt->da,
2890 wpabuf_head(sta->pending_eapol_rx->buf),
2891 wpabuf_len(sta->pending_eapol_rx->buf));
2892 }
2893 wpabuf_free(sta->pending_eapol_rx->buf);
2894 os_free(sta->pending_eapol_rx);
2895 sta->pending_eapol_rx = NULL;
2896 }
6fc6879b
JM
2897}
2898
2899
4dc03726
JM
2900static void handle_deauth_cb(struct hostapd_data *hapd,
2901 const struct ieee80211_mgmt *mgmt,
2902 size_t len, int ok)
2903{
2904 struct sta_info *sta;
81372e34 2905 if (is_multicast_ether_addr(mgmt->da))
4dc03726
JM
2906 return;
2907 sta = ap_get_sta(hapd, mgmt->da);
2908 if (!sta) {
2909 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2910 " not found", MAC2STR(mgmt->da));
2911 return;
2912 }
2913 if (ok)
2914 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2915 MAC2STR(sta->addr));
2916 else
2917 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2918 "deauth", MAC2STR(sta->addr));
2919
2920 ap_sta_deauth_cb(hapd, sta);
2921}
2922
2923
2924static void handle_disassoc_cb(struct hostapd_data *hapd,
2925 const struct ieee80211_mgmt *mgmt,
2926 size_t len, int ok)
2927{
2928 struct sta_info *sta;
81372e34 2929 if (is_multicast_ether_addr(mgmt->da))
4dc03726
JM
2930 return;
2931 sta = ap_get_sta(hapd, mgmt->da);
2932 if (!sta) {
2933 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2934 " not found", MAC2STR(mgmt->da));
2935 return;
2936 }
2937 if (ok)
2938 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2939 MAC2STR(sta->addr));
2940 else
2941 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2942 "disassoc", MAC2STR(sta->addr));
2943
2944 ap_sta_disassoc_cb(hapd, sta);
2945}
2946
2947
1c6e69cc
JM
2948/**
2949 * ieee802_11_mgmt_cb - Process management frame TX status callback
2950 * @hapd: hostapd BSS data structure (the BSS from which the management frame
2951 * was sent from)
2952 * @buf: management frame data (starting from IEEE 802.11 header)
2953 * @len: length of frame data in octets
2954 * @stype: management frame subtype from frame control field
2955 * @ok: Whether the frame was ACK'ed
2956 */
f8b1f695 2957void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
6fc6879b
JM
2958 u16 stype, int ok)
2959{
f8b1f695
JM
2960 const struct ieee80211_mgmt *mgmt;
2961 mgmt = (const struct ieee80211_mgmt *) buf;
6fc6879b 2962
93827f45
JM
2963#ifdef CONFIG_TESTING_OPTIONS
2964 if (hapd->ext_mgmt_frame_handling) {
2965 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2966 stype, ok);
2967 return;
2968 }
2969#endif /* CONFIG_TESTING_OPTIONS */
2970
6fc6879b
JM
2971 switch (stype) {
2972 case WLAN_FC_STYPE_AUTH:
2973 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2974 handle_auth_cb(hapd, mgmt, len, ok);
2975 break;
2976 case WLAN_FC_STYPE_ASSOC_RESP:
2977 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2978 handle_assoc_cb(hapd, mgmt, len, 0, ok);
2979 break;
2980 case WLAN_FC_STYPE_REASSOC_RESP:
2981 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2982 handle_assoc_cb(hapd, mgmt, len, 1, ok);
2983 break;
2984 case WLAN_FC_STYPE_PROBE_RESP:
077dcfb8 2985 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
6fc6879b
JM
2986 break;
2987 case WLAN_FC_STYPE_DEAUTH:
4dc03726
JM
2988 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2989 handle_deauth_cb(hapd, mgmt, len, ok);
2990 break;
2991 case WLAN_FC_STYPE_DISASSOC:
2992 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2993 handle_disassoc_cb(hapd, mgmt, len, ok);
6fc6879b 2994 break;
5d22a1d5 2995 case WLAN_FC_STYPE_ACTION:
077dcfb8 2996 wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
5d22a1d5 2997 break;
6fc6879b 2998 default:
61323e70 2999 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
6fc6879b
JM
3000 break;
3001 }
3002}
3003
3004
6fc6879b
JM
3005int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
3006{
3007 /* TODO */
3008 return 0;
3009}
3010
3011
3012int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
3013 char *buf, size_t buflen)
3014{
3015 /* TODO */
3016 return 0;
3017}
3018
f8b1f695
JM
3019
3020void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
3021 const u8 *buf, size_t len, int ack)
3022{
3023 struct sta_info *sta;
3024 struct hostapd_iface *iface = hapd->iface;
3025
3026 sta = ap_get_sta(hapd, addr);
3027 if (sta == NULL && iface->num_bss > 1) {
3028 size_t j;
3029 for (j = 0; j < iface->num_bss; j++) {
3030 hapd = iface->bss[j];
3031 sta = ap_get_sta(hapd, addr);
3032 if (sta)
3033 break;
3034 }
3035 }
0c01d65d 3036 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
f8b1f695
JM
3037 return;
3038 if (sta->flags & WLAN_STA_PENDING_POLL) {
3039 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
3040 "activity poll", MAC2STR(sta->addr),
3041 ack ? "ACKed" : "did not ACK");
3042 if (ack)
3043 sta->flags &= ~WLAN_STA_PENDING_POLL;
3044 }
3045
3046 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
3047}
3048
3049
dd840f79
JB
3050void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
3051 const u8 *data, size_t len, int ack)
3052{
3053 struct sta_info *sta;
3054 struct hostapd_iface *iface = hapd->iface;
3055
3056 sta = ap_get_sta(hapd, dst);
3057 if (sta == NULL && iface->num_bss > 1) {
3058 size_t j;
3059 for (j = 0; j < iface->num_bss; j++) {
3060 hapd = iface->bss[j];
3061 sta = ap_get_sta(hapd, dst);
3062 if (sta)
3063 break;
3064 }
3065 }
d9a38716
JM
3066 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
3067 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
3068 MACSTR " that is not currently associated",
3069 MAC2STR(dst));
dd840f79 3070 return;
d9a38716 3071 }
dd840f79
JB
3072
3073 ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
3074}
3075
3076
bcf24348
JB
3077void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
3078{
3079 struct sta_info *sta;
3080 struct hostapd_iface *iface = hapd->iface;
3081
3082 sta = ap_get_sta(hapd, addr);
3083 if (sta == NULL && iface->num_bss > 1) {
3084 size_t j;
3085 for (j = 0; j < iface->num_bss; j++) {
3086 hapd = iface->bss[j];
3087 sta = ap_get_sta(hapd, addr);
3088 if (sta)
3089 break;
3090 }
3091 }
3092 if (sta == NULL)
3093 return;
1854eeca
JM
3094 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
3095 MAC2STR(sta->addr));
bcf24348
JB
3096 if (!(sta->flags & WLAN_STA_PENDING_POLL))
3097 return;
3098
3099 wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
3100 "activity poll", MAC2STR(sta->addr));
3101 sta->flags &= ~WLAN_STA_PENDING_POLL;
3102}
3103
3104
fbbfcbac
FF
3105void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
3106 int wds)
f8b1f695
JM
3107{
3108 struct sta_info *sta;
3109
3110 sta = ap_get_sta(hapd, src);
fbbfcbac 3111 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
99743811
FF
3112 if (!hapd->conf->wds_sta)
3113 return;
3114
fbbfcbac 3115 if (wds && !(sta->flags & WLAN_STA_WDS)) {
69dd2967
SM
3116 int ret;
3117 char ifname_wds[IFNAMSIZ + 1];
3118
fbbfcbac
FF
3119 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
3120 "STA " MACSTR " (aid %u)",
3121 MAC2STR(sta->addr), sta->aid);
3122 sta->flags |= WLAN_STA_WDS;
69dd2967
SM
3123 ret = hostapd_set_wds_sta(hapd, ifname_wds,
3124 sta->addr, sta->aid, 1);
3125 if (!ret)
3126 hostapd_set_wds_encryption(hapd, sta,
3127 ifname_wds);
fbbfcbac 3128 }
f8b1f695 3129 return;
fbbfcbac 3130 }
f8b1f695
JM
3131
3132 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
3133 MACSTR, MAC2STR(src));
81372e34 3134 if (is_multicast_ether_addr(src)) {
1df492df
JM
3135 /* Broadcast bit set in SA?! Ignore the frame silently. */
3136 return;
3137 }
3138
b8281964
JM
3139 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
3140 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
3141 "already been sent, but no TX status yet known - "
3142 "ignore Class 3 frame issue with " MACSTR,
3143 MAC2STR(src));
3144 return;
3145 }
3146
f8b1f695 3147 if (sta && (sta->flags & WLAN_STA_AUTH))
51e2a27a 3148 hostapd_drv_sta_disassoc(
f8b1f695
JM
3149 hapd, src,
3150 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
3151 else
51e2a27a 3152 hostapd_drv_sta_deauth(
f8b1f695
JM
3153 hapd, src,
3154 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
3155}
3156
3157
6fc6879b 3158#endif /* CONFIG_NATIVE_WINDOWS */