]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/ieee802_11.c
SAE: Add Finite Cyclic Group negotiation and Send-Confirm
[thirdparty/hostap.git] / src / ap / ieee802_11.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / IEEE 802.11 Management
d9a38716 3 * Copyright (c) 2002-2012, 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"
2ce86d9d 16#include "drivers/driver.h"
81f4f619
JM
17#include "common/ieee802_11_defs.h"
18#include "common/ieee802_11_common.h"
03da66bd
JM
19#include "common/wpa_ctrl.h"
20#include "radius/radius.h"
21#include "radius/radius_client.h"
e44f8bf2 22#include "p2p/p2p.h"
ed7a09f9 23#include "wps/wps.h"
6fc6879b 24#include "hostapd.h"
6fc6879b 25#include "beacon.h"
6fc6879b
JM
26#include "ieee802_11_auth.h"
27#include "sta_info.h"
6fc6879b 28#include "ieee802_1x.h"
6226e38d 29#include "wpa_auth.h"
1057d78e 30#include "wmm.h"
6fc6879b
JM
31#include "ap_list.h"
32#include "accounting.h"
6226e38d
JM
33#include "ap_config.h"
34#include "ap_mlme.h"
dce044cc 35#include "p2p_hostapd.h"
cee7d66b 36#include "ap_drv_ops.h"
6226e38d 37#include "ieee802_11.h"
6fc6879b
JM
38
39
40u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
41{
42 u8 *pos = eid;
43 int i, num, count;
44
45 if (hapd->iface->current_rates == NULL)
46 return eid;
47
48 *pos++ = WLAN_EID_SUPP_RATES;
49 num = hapd->iface->num_rates;
29448243
JM
50 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
51 num++;
6fc6879b
JM
52 if (num > 8) {
53 /* rest of the rates are encoded in Extended supported
54 * rates element */
55 num = 8;
56 }
57
58 *pos++ = num;
59 count = 0;
60 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
61 i++) {
62 count++;
63 *pos = hapd->iface->current_rates[i].rate / 5;
64 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
65 *pos |= 0x80;
66 pos++;
67 }
68
29448243
JM
69 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
70 hapd->iface->num_rates < 8)
71 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
72
6fc6879b
JM
73 return pos;
74}
75
76
77u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
78{
79 u8 *pos = eid;
80 int i, num, count;
81
82 if (hapd->iface->current_rates == NULL)
83 return eid;
84
85 num = hapd->iface->num_rates;
29448243
JM
86 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
87 num++;
6fc6879b
JM
88 if (num <= 8)
89 return eid;
90 num -= 8;
91
92 *pos++ = WLAN_EID_EXT_SUPP_RATES;
93 *pos++ = num;
94 count = 0;
95 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
96 i++) {
97 count++;
98 if (count <= 8)
99 continue; /* already in SuppRates IE */
100 *pos = hapd->iface->current_rates[i].rate / 5;
101 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
102 *pos |= 0x80;
103 pos++;
104 }
105
29448243
JM
106 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
107 hapd->iface->num_rates >= 8)
108 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
109
6fc6879b
JM
110 return pos;
111}
112
113
114u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
115 int probe)
116{
117 int capab = WLAN_CAPABILITY_ESS;
118 int privacy;
119
120 if (hapd->iface->num_sta_no_short_preamble == 0 &&
121 hapd->iconf->preamble == SHORT_PREAMBLE)
122 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
123
124 privacy = hapd->conf->ssid.wep.keys_set;
125
126 if (hapd->conf->ieee802_1x &&
127 (hapd->conf->default_wep_key_len ||
128 hapd->conf->individual_wep_key_len))
129 privacy = 1;
130
131 if (hapd->conf->wpa)
132 privacy = 1;
133
134 if (sta) {
135 int policy, def_klen;
136 if (probe && sta->ssid_probe) {
137 policy = sta->ssid_probe->security_policy;
138 def_klen = sta->ssid_probe->wep.default_len;
139 } else {
140 policy = sta->ssid->security_policy;
141 def_klen = sta->ssid->wep.default_len;
142 }
143 privacy = policy != SECURITY_PLAINTEXT;
144 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
145 privacy = 0;
146 }
147
148 if (privacy)
149 capab |= WLAN_CAPABILITY_PRIVACY;
150
151 if (hapd->iface->current_mode &&
152 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
153 hapd->iface->num_sta_no_short_slot_time == 0)
154 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
155
6fc6879b
JM
156 return capab;
157}
158
159
6fc6879b
JM
160void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
161{
162 int i;
163 if (len > HOSTAPD_MAX_SSID_LEN)
164 len = HOSTAPD_MAX_SSID_LEN;
165 for (i = 0; i < len; i++) {
166 if (ssid[i] >= 32 && ssid[i] < 127)
167 buf[i] = ssid[i];
168 else
169 buf[i] = '.';
170 }
171 buf[len] = '\0';
172}
173
174
6fc6879b 175static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
b57e086c
JM
176 u16 auth_transaction, const u8 *challenge,
177 int iswep)
6fc6879b
JM
178{
179 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
180 HOSTAPD_LEVEL_DEBUG,
181 "authentication (shared key, transaction %d)",
182 auth_transaction);
183
184 if (auth_transaction == 1) {
185 if (!sta->challenge) {
186 /* Generate a pseudo-random challenge */
187 u8 key[8];
0b04889f 188 struct os_time now;
6fc6879b
JM
189 int r;
190 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
191 if (sta->challenge == NULL)
192 return WLAN_STATUS_UNSPECIFIED_FAILURE;
193
0b04889f
PE
194 os_get_time(&now);
195 r = os_random();
196 os_memcpy(key, &now.sec, 4);
6fc6879b 197 os_memcpy(key + 4, &r, 4);
8ef16831
JM
198 rc4_skip(key, sizeof(key), 0,
199 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
6fc6879b
JM
200 }
201 return 0;
202 }
203
204 if (auth_transaction != 3)
205 return WLAN_STATUS_UNSPECIFIED_FAILURE;
206
207 /* Transaction 3 */
208 if (!iswep || !sta->challenge || !challenge ||
209 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
210 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
211 HOSTAPD_LEVEL_INFO,
212 "shared key authentication - invalid "
213 "challenge-response");
214 return WLAN_STATUS_CHALLENGE_FAIL;
215 }
216
217 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
218 HOSTAPD_LEVEL_DEBUG,
219 "authentication OK (shared key)");
220#ifdef IEEE80211_REQUIRE_AUTH_ACK
221 /* Station will be marked authenticated if it ACKs the
222 * authentication reply. */
223#else
224 sta->flags |= WLAN_STA_AUTH;
225 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
226#endif
227 os_free(sta->challenge);
228 sta->challenge = NULL;
229
230 return 0;
231}
232
233
234static void send_auth_reply(struct hostapd_data *hapd,
235 const u8 *dst, const u8 *bssid,
236 u16 auth_alg, u16 auth_transaction, u16 resp,
237 const u8 *ies, size_t ies_len)
238{
239 struct ieee80211_mgmt *reply;
240 u8 *buf;
241 size_t rlen;
242
243 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
244 buf = os_zalloc(rlen);
245 if (buf == NULL)
246 return;
247
248 reply = (struct ieee80211_mgmt *) buf;
249 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
250 WLAN_FC_STYPE_AUTH);
6fc6879b
JM
251 os_memcpy(reply->da, dst, ETH_ALEN);
252 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
253 os_memcpy(reply->bssid, bssid, ETH_ALEN);
254
255 reply->u.auth.auth_alg = host_to_le16(auth_alg);
256 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
257 reply->u.auth.status_code = host_to_le16(resp);
258
259 if (ies && ies_len)
260 os_memcpy(reply->u.auth.variable, ies, ies_len);
261
262 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
263 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
264 MAC2STR(dst), auth_alg, auth_transaction,
265 resp, (unsigned long) ies_len);
8cfa3527 266 if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
6fc6879b
JM
267 perror("send_auth_reply: send");
268
269 os_free(buf);
270}
271
272
273#ifdef CONFIG_IEEE80211R
274static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
275 u16 auth_transaction, u16 status,
276 const u8 *ies, size_t ies_len)
277{
278 struct hostapd_data *hapd = ctx;
279 struct sta_info *sta;
280
281 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
282 status, ies, ies_len);
283
284 if (status != WLAN_STATUS_SUCCESS)
285 return;
286
287 sta = ap_get_sta(hapd, dst);
288 if (sta == NULL)
289 return;
290
291 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
292 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
293 sta->flags |= WLAN_STA_AUTH;
294 mlme_authenticate_indication(hapd, sta);
295}
296#endif /* CONFIG_IEEE80211R */
297
298
c10347f2 299#ifdef CONFIG_SAE
21af6d15
JM
300
301static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
302 struct sta_info *sta)
303{
304 struct wpabuf *buf;
305
306 buf = wpabuf_alloc(2);
307 if (buf == NULL)
308 return NULL;
309
310 wpabuf_put_le16(buf, 19); /* Finite Cyclic Group */
311 /* TODO: Anti-Clogging Token (if requested) */
312 /* TODO: Scalar */
313 /* TODO: Element */
314
315 return buf;
316}
317
318
319static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
320 struct sta_info *sta)
321{
322 struct wpabuf *buf;
323
324 buf = wpabuf_alloc(2);
325 if (buf == NULL)
326 return NULL;
327
328 wpabuf_put_le16(buf, sta->sae_send_confirm);
329 sta->sae_send_confirm++;
330 /* TODO: Confirm */
331
332 return buf;
333}
334
335
336static u16 handle_sae_commit(struct hostapd_data *hapd, struct sta_info *sta,
337 const u8 *data, size_t len)
338{
339 wpa_hexdump(MSG_DEBUG, "SAE commit fields", data, len);
340
341 /* Check Finite Cyclic Group */
342 if (len < 2)
343 return WLAN_STATUS_UNSPECIFIED_FAILURE;
344 if (WPA_GET_LE16(data) != 19) {
345 wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
346 WPA_GET_LE16(data));
347 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
348 }
349
350 return WLAN_STATUS_SUCCESS;
351}
352
353
354static u16 handle_sae_confirm(struct hostapd_data *hapd, struct sta_info *sta,
355 const u8 *data, size_t len)
356{
357 u16 rc;
358
359 wpa_hexdump(MSG_DEBUG, "SAE confirm fields", data, len);
360
361 if (len < 2)
362 return WLAN_STATUS_UNSPECIFIED_FAILURE;
363 rc = WPA_GET_LE16(data);
364 wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", rc);
365
366 return WLAN_STATUS_SUCCESS;
367}
368
369
c10347f2
JM
370static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
371 const struct ieee80211_mgmt *mgmt, size_t len,
372 u8 auth_transaction)
373{
374 u16 resp = WLAN_STATUS_SUCCESS;
21af6d15 375 struct wpabuf *data;
c10347f2
JM
376
377 if (auth_transaction == 1) {
378 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
379 HOSTAPD_LEVEL_DEBUG,
380 "start SAE authentication (RX commit)");
21af6d15
JM
381 resp = handle_sae_commit(hapd, sta, mgmt->u.auth.variable,
382 ((u8 *) mgmt) + len -
383 mgmt->u.auth.variable);
384 if (resp == WLAN_STATUS_SUCCESS)
385 sta->sae_state = SAE_COMMIT;
c10347f2
JM
386 } else if (auth_transaction == 2) {
387 if (sta->sae_state != SAE_COMMIT) {
388 hostapd_logger(hapd, sta->addr,
389 HOSTAPD_MODULE_IEEE80211,
390 HOSTAPD_LEVEL_DEBUG,
391 "SAE confirm before commit");
392 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
393 }
394 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
395 HOSTAPD_LEVEL_DEBUG,
396 "SAE authentication (RX confirm)");
21af6d15
JM
397 resp = handle_sae_confirm(hapd, sta, mgmt->u.auth.variable,
398 ((u8 *) mgmt) + len -
399 mgmt->u.auth.variable);
400 if (resp == WLAN_STATUS_SUCCESS) {
401 sta->flags |= WLAN_STA_AUTH;
402 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
403 sta->auth_alg = WLAN_AUTH_SAE;
404 mlme_authenticate_indication(hapd, sta);
405 }
c10347f2
JM
406 } else {
407 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
408 HOSTAPD_LEVEL_DEBUG,
409 "unexpected SAE authentication transaction %u",
410 auth_transaction);
411 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
412 }
413
414 sta->auth_alg = WLAN_AUTH_SAE;
415
21af6d15
JM
416 if (resp == WLAN_STATUS_SUCCESS) {
417 if (auth_transaction == 1)
418 data = auth_build_sae_commit(hapd, sta);
419 else
420 data = auth_build_sae_confirm(hapd, sta);
421 if (data == NULL)
422 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
423 } else
424 data = NULL;
425
c10347f2 426 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
21af6d15
JM
427 auth_transaction, resp,
428 data ? wpabuf_head(data) : (u8 *) "",
429 data ? wpabuf_len(data) : 0);
430 wpabuf_free(data);
c10347f2
JM
431}
432#endif /* CONFIG_SAE */
433
434
b57e086c
JM
435static void handle_auth(struct hostapd_data *hapd,
436 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
437{
438 u16 auth_alg, auth_transaction, status_code;
439 u16 resp = WLAN_STATUS_SUCCESS;
440 struct sta_info *sta = NULL;
441 int res;
442 u16 fc;
b57e086c 443 const u8 *challenge = NULL;
6fc6879b
JM
444 u32 session_timeout, acct_interim_interval;
445 int vlan_id = 0;
05ab9712
MB
446 u8 psk[PMK_LEN];
447 int has_psk = 0;
6fc6879b
JM
448 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
449 size_t resp_ies_len = 0;
2092597f
MB
450 char *identity = NULL;
451 char *radius_cui = NULL;
6fc6879b
JM
452
453 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
454 printf("handle_auth - too short payload (len=%lu)\n",
455 (unsigned long) len);
456 return;
457 }
458
459 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
460 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
461 status_code = le_to_host16(mgmt->u.auth.status_code);
462 fc = le_to_host16(mgmt->frame_control);
463
464 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
465 2 + WLAN_AUTH_CHALLENGE_LEN &&
466 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
467 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
468 challenge = &mgmt->u.auth.variable[2];
469
470 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
471 "auth_transaction=%d status_code=%d wep=%d%s",
472 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
473 status_code, !!(fc & WLAN_FC_ISWEP),
474 challenge ? " challenge" : "");
475
6fc6879b
JM
476 if (hapd->tkip_countermeasures) {
477 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
478 goto fail;
479 }
480
481 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
482 auth_alg == WLAN_AUTH_OPEN) ||
483#ifdef CONFIG_IEEE80211R
0bf927a0 484 (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
6fc6879b
JM
485 auth_alg == WLAN_AUTH_FT) ||
486#endif /* CONFIG_IEEE80211R */
c10347f2
JM
487#ifdef CONFIG_SAE
488 (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
489 auth_alg == WLAN_AUTH_SAE) ||
490#endif /* CONFIG_SAE */
6fc6879b
JM
491 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
492 auth_alg == WLAN_AUTH_SHARED_KEY))) {
493 printf("Unsupported authentication algorithm (%d)\n",
494 auth_alg);
495 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
496 goto fail;
497 }
498
c10347f2 499 if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
6fc6879b
JM
500 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
501 printf("Unknown authentication transaction number (%d)\n",
502 auth_transaction);
503 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
504 goto fail;
505 }
506
507 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
508 printf("Station " MACSTR " not allowed to authenticate.\n",
509 MAC2STR(mgmt->sa));
510 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
511 goto fail;
512 }
513
514 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
515 &session_timeout,
05ab9712 516 &acct_interim_interval, &vlan_id,
2092597f 517 psk, &has_psk, &identity, &radius_cui);
05ab9712 518
6fc6879b
JM
519 if (res == HOSTAPD_ACL_REJECT) {
520 printf("Station " MACSTR " not allowed to authenticate.\n",
521 MAC2STR(mgmt->sa));
522 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
523 goto fail;
524 }
525 if (res == HOSTAPD_ACL_PENDING) {
526 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
527 " waiting for an external authentication",
528 MAC2STR(mgmt->sa));
529 /* Authentication code will re-send the authentication frame
530 * after it has received (and cached) information from the
531 * external source. */
532 return;
533 }
534
535 sta = ap_sta_add(hapd, mgmt->sa);
536 if (!sta) {
537 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
538 goto fail;
539 }
540
541 if (vlan_id > 0) {
542 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1066c1ee 543 vlan_id) == NULL) {
6fc6879b
JM
544 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
545 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
546 "%d received from RADIUS server",
547 vlan_id);
548 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
549 goto fail;
550 }
551 sta->vlan_id = vlan_id;
552 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
553 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
554 }
555
05ab9712
MB
556 if (has_psk && hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
557 os_free(sta->psk);
558 sta->psk = os_malloc(PMK_LEN);
559 if (sta->psk)
560 os_memcpy(sta->psk, psk, PMK_LEN);
561 } else {
562 os_free(sta->psk);
563 sta->psk = NULL;
564 }
565
2092597f
MB
566 sta->identity = identity;
567 identity = NULL;
568 sta->radius_cui = radius_cui;
569 radius_cui = NULL;
570
6fc6879b
JM
571 sta->flags &= ~WLAN_STA_PREAUTH;
572 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
573
5843e1c9 574 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
6fc6879b
JM
575 sta->acct_interim_interval = acct_interim_interval;
576 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
577 ap_sta_session_timeout(hapd, sta, session_timeout);
578 else
579 ap_sta_no_session_timeout(hapd, sta);
580
581 switch (auth_alg) {
582 case WLAN_AUTH_OPEN:
583 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
584 HOSTAPD_LEVEL_DEBUG,
585 "authentication OK (open system)");
586#ifdef IEEE80211_REQUIRE_AUTH_ACK
587 /* Station will be marked authenticated if it ACKs the
588 * authentication reply. */
589#else
590 sta->flags |= WLAN_STA_AUTH;
591 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
592 sta->auth_alg = WLAN_AUTH_OPEN;
593 mlme_authenticate_indication(hapd, sta);
594#endif
595 break;
596 case WLAN_AUTH_SHARED_KEY:
597 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
598 fc & WLAN_FC_ISWEP);
599 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
600 mlme_authenticate_indication(hapd, sta);
601 if (sta->challenge && auth_transaction == 1) {
602 resp_ies[0] = WLAN_EID_CHALLENGE;
603 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
604 os_memcpy(resp_ies + 2, sta->challenge,
605 WLAN_AUTH_CHALLENGE_LEN);
606 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
607 }
608 break;
609#ifdef CONFIG_IEEE80211R
610 case WLAN_AUTH_FT:
611 sta->auth_alg = WLAN_AUTH_FT;
612 if (sta->wpa_sm == NULL)
613 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
614 sta->addr);
615 if (sta->wpa_sm == NULL) {
616 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
617 "state machine");
618 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
619 goto fail;
620 }
621 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
622 auth_transaction, mgmt->u.auth.variable,
623 len - IEEE80211_HDRLEN -
624 sizeof(mgmt->u.auth),
625 handle_auth_ft_finish, hapd);
626 /* handle_auth_ft_finish() callback will complete auth. */
627 return;
628#endif /* CONFIG_IEEE80211R */
c10347f2
JM
629#ifdef CONFIG_SAE
630 case WLAN_AUTH_SAE:
631 handle_auth_sae(hapd, sta, mgmt, len, auth_transaction);
632 return;
633#endif /* CONFIG_SAE */
6fc6879b
JM
634 }
635
636 fail:
2092597f
MB
637 os_free(identity);
638 os_free(radius_cui);
639
6fc6879b
JM
640 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
641 auth_transaction + 1, resp, resp_ies, resp_ies_len);
642}
643
644
d42a62b3
JM
645static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
646{
2991469c
JM
647 int i, j = 32, aid;
648
d42a62b3
JM
649 /* get a unique AID */
650 if (sta->aid > 0) {
651 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
652 return 0;
653 }
654
2991469c
JM
655 for (i = 0; i < AID_WORDS; i++) {
656 if (hapd->sta_aid[i] == (u32) -1)
657 continue;
658 for (j = 0; j < 32; j++) {
659 if (!(hapd->sta_aid[i] & BIT(j)))
660 break;
661 }
662 if (j < 32)
d42a62b3 663 break;
d42a62b3 664 }
2991469c
JM
665 if (j == 32)
666 return -1;
667 aid = i * 32 + j + 1;
668 if (aid > 2007)
669 return -1;
d42a62b3 670
2991469c
JM
671 sta->aid = aid;
672 hapd->sta_aid[i] |= BIT(j);
d42a62b3
JM
673 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
674 return 0;
675}
676
677
5586f500
JM
678static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
679 const u8 *ssid_ie, size_t ssid_ie_len)
6fc6879b 680{
5586f500
JM
681 if (ssid_ie == NULL)
682 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b 683
5586f500
JM
684 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
685 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
686 char ssid_txt[33];
687 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
688 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
689 HOSTAPD_LEVEL_INFO,
690 "Station tried to associate with unknown SSID "
691 "'%s'", ssid_txt);
692 return WLAN_STATUS_UNSPECIFIED_FAILURE;
b0194fe0
JM
693 }
694
5586f500
JM
695 return WLAN_STATUS_SUCCESS;
696}
6fc6879b 697
6fc6879b 698
5586f500
JM
699static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
700 const u8 *wmm_ie, size_t wmm_ie_len)
701{
3ae0800c 702 sta->flags &= ~WLAN_STA_WMM;
5d061637 703 sta->qosinfo = 0;
5586f500 704 if (wmm_ie && hapd->conf->wmm_enabled) {
5f32f79c 705 struct wmm_information_element *wmm;
5f32f79c 706
c84b868a 707 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
6fc6879b
JM
708 hostapd_logger(hapd, sta->addr,
709 HOSTAPD_MODULE_WPA,
710 HOSTAPD_LEVEL_DEBUG,
3ae0800c 711 "invalid WMM element in association "
6fc6879b 712 "request");
5f32f79c
EP
713 return WLAN_STATUS_UNSPECIFIED_FAILURE;
714 }
715
716 sta->flags |= WLAN_STA_WMM;
717 wmm = (struct wmm_information_element *) wmm_ie;
5d061637 718 sta->qosinfo = wmm->qos_info;
6fc6879b 719 }
5586f500
JM
720 return WLAN_STATUS_SUCCESS;
721}
6fc6879b 722
5586f500
JM
723
724static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
725 struct ieee802_11_elems *elems)
726{
727 if (!elems->supp_rates) {
728 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6fc6879b
JM
729 HOSTAPD_LEVEL_DEBUG,
730 "No supported rates element in AssocReq");
5586f500 731 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
732 }
733
3489cfb0
JM
734 if (elems->supp_rates_len + elems->ext_supp_rates_len >
735 sizeof(sta->supported_rates)) {
5586f500 736 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6fc6879b 737 HOSTAPD_LEVEL_DEBUG,
3489cfb0
JM
738 "Invalid supported rates element length %d+%d",
739 elems->supp_rates_len,
740 elems->ext_supp_rates_len);
5586f500 741 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
742 }
743
3489cfb0
JM
744 sta->supported_rates_len = merge_byte_arrays(
745 sta->supported_rates, sizeof(sta->supported_rates),
746 elems->supp_rates, elems->supp_rates_len,
747 elems->ext_supp_rates, elems->ext_supp_rates_len);
6fc6879b 748
5586f500
JM
749 return WLAN_STATUS_SUCCESS;
750}
751
752
5586f500 753static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
b57e086c 754 const u8 *ies, size_t ies_len, int reassoc)
5586f500
JM
755{
756 struct ieee802_11_elems elems;
757 u16 resp;
ba091c06 758 const u8 *wpa_ie;
5586f500
JM
759 size_t wpa_ie_len;
760
761 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
762 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
763 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
764 "association request");
765 return WLAN_STATUS_UNSPECIFIED_FAILURE;
766 }
767
768 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
769 if (resp != WLAN_STATUS_SUCCESS)
770 return resp;
771 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
772 if (resp != WLAN_STATUS_SUCCESS)
773 return resp;
774 resp = copy_supp_rates(hapd, sta, &elems);
775 if (resp != WLAN_STATUS_SUCCESS)
776 return resp;
d45354be 777#ifdef CONFIG_IEEE80211N
f39b07d7 778 resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
5586f500
JM
779 elems.ht_capabilities_len);
780 if (resp != WLAN_STATUS_SUCCESS)
781 return resp;
29448243
JM
782 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
783 !(sta->flags & WLAN_STA_HT)) {
784 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
785 HOSTAPD_LEVEL_INFO, "Station does not support "
786 "mandatory HT PHY - reject association");
787 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
788 }
d45354be 789#endif /* CONFIG_IEEE80211N */
5586f500 790
de3cdf35
MP
791#ifdef CONFIG_IEEE80211AC
792 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
793 elems.vht_capabilities_len);
794 if (resp != WLAN_STATUS_SUCCESS)
795 return resp;
140e850a
MP
796 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
797 !(sta->flags & WLAN_STA_VHT)) {
798 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
799 HOSTAPD_LEVEL_INFO, "Station does not support "
800 "mandatory VHT PHY - reject association");
801 return WLAN_STATUS_UNSPECIFIED_FAILURE;
802 }
de3cdf35
MP
803#endif /* CONFIG_IEEE80211AC */
804
6fc6879b
JM
805 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
806 wpa_ie = elems.rsn_ie;
807 wpa_ie_len = elems.rsn_ie_len;
808 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
809 elems.wpa_ie) {
810 wpa_ie = elems.wpa_ie;
811 wpa_ie_len = elems.wpa_ie_len;
812 } else {
813 wpa_ie = NULL;
814 wpa_ie_len = 0;
815 }
5586f500 816
ad08c363 817#ifdef CONFIG_WPS
17f6b900 818 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
c47cf42e
JM
819 if (hapd->conf->wps_state && elems.wps_ie) {
820 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
821 "Request - assume WPS is used");
822 sta->flags |= WLAN_STA_WPS;
823 wpabuf_free(sta->wps_ie);
16e46ec0
JM
824 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
825 WPS_IE_VENDOR_TYPE);
17f6b900
JM
826 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
827 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
828 sta->flags |= WLAN_STA_WPS2;
829 }
c47cf42e
JM
830 wpa_ie = NULL;
831 wpa_ie_len = 0;
54f489be
JM
832 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
833 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
834 "(Re)Association Request - reject");
835 return WLAN_STATUS_INVALID_IE;
836 }
c47cf42e
JM
837 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
838 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
839 "(Re)Association Request - possible WPS use");
840 sta->flags |= WLAN_STA_MAYBE_WPS;
ad08c363
JM
841 } else
842#endif /* CONFIG_WPS */
6fc6879b 843 if (hapd->conf->wpa && wpa_ie == NULL) {
5586f500
JM
844 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
845 HOSTAPD_LEVEL_INFO,
846 "No WPA/RSN IE in association request");
847 return WLAN_STATUS_INVALID_IE;
6fc6879b
JM
848 }
849
850 if (hapd->conf->wpa && wpa_ie) {
851 int res;
852 wpa_ie -= 2;
853 wpa_ie_len += 2;
854 if (sta->wpa_sm == NULL)
855 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
856 sta->addr);
857 if (sta->wpa_sm == NULL) {
5586f500
JM
858 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
859 "state machine");
860 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
861 }
862 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
863 wpa_ie, wpa_ie_len,
864 elems.mdie, elems.mdie_len);
865 if (res == WPA_INVALID_GROUP)
866 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
867 else if (res == WPA_INVALID_PAIRWISE)
868 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
869 else if (res == WPA_INVALID_AKMP)
870 resp = WLAN_STATUS_AKMP_NOT_VALID;
871 else if (res == WPA_ALLOC_FAIL)
872 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
873#ifdef CONFIG_IEEE80211W
874 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
9a9876bf 875 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
6fc6879b 876 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
9a9876bf 877 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
6fc6879b
JM
878#endif /* CONFIG_IEEE80211W */
879 else if (res == WPA_INVALID_MDIE)
880 resp = WLAN_STATUS_INVALID_MDIE;
881 else if (res != WPA_IE_OK)
882 resp = WLAN_STATUS_INVALID_IE;
883 if (resp != WLAN_STATUS_SUCCESS)
5586f500 884 return resp;
f3f7540e 885#ifdef CONFIG_IEEE80211W
45c94154
JM
886 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
887 sta->sa_query_count > 0)
888 ap_check_sa_query_timeout(hapd, sta);
8f4617c6
JM
889 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
890 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
5d22a1d5 891 /*
93b76319
JM
892 * STA has already been associated with MFP and SA
893 * Query timeout has not been reached. Reject the
894 * association attempt temporarily and start SA Query,
895 * if one is not pending.
5d22a1d5
JM
896 */
897
93b76319
JM
898 if (sta->sa_query_count == 0)
899 ap_sta_start_sa_query(hapd, sta);
5d22a1d5 900
5586f500 901 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
5d22a1d5
JM
902 }
903
f3f7540e
JM
904 if (wpa_auth_uses_mfp(sta->wpa_sm))
905 sta->flags |= WLAN_STA_MFP;
906 else
907 sta->flags &= ~WLAN_STA_MFP;
908#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
909
910#ifdef CONFIG_IEEE80211R
911 if (sta->auth_alg == WLAN_AUTH_FT) {
912 if (!reassoc) {
913 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
914 "to use association (not "
915 "re-association) with FT auth_alg",
916 MAC2STR(sta->addr));
5586f500 917 return WLAN_STATUS_UNSPECIFIED_FAILURE;
6fc6879b
JM
918 }
919
5586f500
JM
920 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
921 ies_len);
6fc6879b 922 if (resp != WLAN_STATUS_SUCCESS)
5586f500 923 return resp;
6fc6879b
JM
924 }
925#endif /* CONFIG_IEEE80211R */
5586f500 926
c10347f2
JM
927#ifdef CONFIG_SAE
928 if (wpa_auth_uses_sae(sta->wpa_sm) &&
929 sta->auth_alg != WLAN_AUTH_SAE) {
930 wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
931 "SAE AKM after non-SAE auth_alg %u",
932 MAC2STR(sta->addr), sta->auth_alg);
933 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
934 }
935#endif /* CONFIG_SAE */
936
ff36ff00 937#ifdef CONFIG_IEEE80211N
f0c7a986 938 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
ff36ff00 939 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
5586f500
JM
940 hostapd_logger(hapd, sta->addr,
941 HOSTAPD_MODULE_IEEE80211,
942 HOSTAPD_LEVEL_INFO,
943 "Station tried to use TKIP with HT "
944 "association");
945 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
ff36ff00
JM
946 }
947#endif /* CONFIG_IEEE80211N */
a8d05fca
JM
948 } else
949 wpa_auth_sta_no_wpa(sta->wpa_sm);
6fc6879b 950
b305c684
JM
951#ifdef CONFIG_P2P
952 if (elems.p2p) {
953 wpabuf_free(sta->p2p_ie);
954 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
955 P2P_IE_VENDOR_TYPE);
8ccbe415 956
b305c684
JM
957 } else {
958 wpabuf_free(sta->p2p_ie);
959 sta->p2p_ie = NULL;
960 }
3f4ce13f
JM
961
962 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
b305c684
JM
963#endif /* CONFIG_P2P */
964
5586f500
JM
965 return WLAN_STATUS_SUCCESS;
966}
967
968
969static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
970 u16 reason_code)
971{
972 int send_len;
973 struct ieee80211_mgmt reply;
974
975 os_memset(&reply, 0, sizeof(reply));
976 reply.frame_control =
977 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
978 os_memcpy(reply.da, addr, ETH_ALEN);
979 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
980 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
981
982 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
983 reply.u.deauth.reason_code = host_to_le16(reason_code);
984
8cfa3527 985 if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
5586f500
JM
986 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
987 strerror(errno));
988}
989
990
991static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
b57e086c 992 u16 status_code, int reassoc, const u8 *ies,
5586f500
JM
993 size_t ies_len)
994{
995 int send_len;
996 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
997 struct ieee80211_mgmt *reply;
998 u8 *p;
999
1000 os_memset(buf, 0, sizeof(buf));
1001 reply = (struct ieee80211_mgmt *) buf;
1002 reply->frame_control =
1003 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1004 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1005 WLAN_FC_STYPE_ASSOC_RESP));
1006 os_memcpy(reply->da, sta->addr, ETH_ALEN);
1007 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1008 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
1009
1010 send_len = IEEE80211_HDRLEN;
1011 send_len += sizeof(reply->u.assoc_resp);
1012 reply->u.assoc_resp.capab_info =
1013 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1014 reply->u.assoc_resp.status_code = host_to_le16(status_code);
1015 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1016 | BIT(14) | BIT(15));
1017 /* Supported rates */
1018 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1019 /* Extended supported rates */
1020 p = hostapd_eid_ext_supp_rates(hapd, p);
5586f500
JM
1021
1022#ifdef CONFIG_IEEE80211R
1023 if (status_code == WLAN_STATUS_SUCCESS) {
1024 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1025 * Transition Information, RSN, [RIC Response] */
1026 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1027 buf + sizeof(buf) - p,
1028 sta->auth_alg, ies, ies_len);
1029 }
1030#endif /* CONFIG_IEEE80211R */
1031
1032#ifdef CONFIG_IEEE80211W
1033 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1034 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1035#endif /* CONFIG_IEEE80211W */
1036
1bc774a1
JM
1037#ifdef CONFIG_IEEE80211N
1038 p = hostapd_eid_ht_capabilities(hapd, p);
1039 p = hostapd_eid_ht_operation(hapd, p);
1040#endif /* CONFIG_IEEE80211N */
1041
14708b50
MP
1042#ifdef CONFIG_IEEE80211AC
1043 p = hostapd_eid_vht_capabilities(hapd, p);
1044 p = hostapd_eid_vht_operation(hapd, p);
1045#endif /* CONFIG_IEEE80211AC */
1046
1161ff1e 1047 p = hostapd_eid_ext_capab(hapd, p);
b6668734 1048 p = hostapd_eid_bss_max_idle_period(hapd, p);
1161ff1e 1049
1bc774a1
JM
1050 if (sta->flags & WLAN_STA_WMM)
1051 p = hostapd_eid_wmm(hapd, p);
1052
ed7a09f9 1053#ifdef CONFIG_WPS
71093e5e
JM
1054 if ((sta->flags & WLAN_STA_WPS) ||
1055 ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
ed7a09f9
JM
1056 struct wpabuf *wps = wps_build_assoc_resp_ie();
1057 if (wps) {
1058 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
1059 p += wpabuf_len(wps);
1060 wpabuf_free(wps);
1061 }
1062 }
1063#endif /* CONFIG_WPS */
1064
8ccbe415
JM
1065#ifdef CONFIG_P2P
1066 if (sta->p2p_ie) {
1067 struct wpabuf *p2p_resp_ie;
1068 enum p2p_status_code status;
1069 switch (status_code) {
1070 case WLAN_STATUS_SUCCESS:
1071 status = P2P_SC_SUCCESS;
1072 break;
1073 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
1074 status = P2P_SC_FAIL_LIMIT_REACHED;
1075 break;
1076 default:
1077 status = P2P_SC_FAIL_INVALID_PARAMS;
1078 break;
1079 }
1080 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
1081 if (p2p_resp_ie) {
1082 os_memcpy(p, wpabuf_head(p2p_resp_ie),
1083 wpabuf_len(p2p_resp_ie));
1084 p += wpabuf_len(p2p_resp_ie);
1085 wpabuf_free(p2p_resp_ie);
1086 }
1087 }
1088#endif /* CONFIG_P2P */
1089
962473c1
JM
1090#ifdef CONFIG_P2P_MANAGER
1091 if (hapd->conf->p2p & P2P_MANAGE)
1092 p = hostapd_eid_p2p_manage(hapd, p);
1093#endif /* CONFIG_P2P_MANAGER */
1094
5586f500
JM
1095 send_len += p - reply->u.assoc_resp.variable;
1096
8cfa3527 1097 if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
5586f500
JM
1098 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
1099 strerror(errno));
1100}
1101
1102
1103static void handle_assoc(struct hostapd_data *hapd,
b57e086c
JM
1104 const struct ieee80211_mgmt *mgmt, size_t len,
1105 int reassoc)
5586f500
JM
1106{
1107 u16 capab_info, listen_interval;
1108 u16 resp = WLAN_STATUS_SUCCESS;
b57e086c 1109 const u8 *pos;
5586f500
JM
1110 int left, i;
1111 struct sta_info *sta;
1112
1113 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
1114 sizeof(mgmt->u.assoc_req))) {
1115 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
1116 "\n", reassoc, (unsigned long) len);
1117 return;
1118 }
1119
1120 if (reassoc) {
1121 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
1122 listen_interval = le_to_host16(
1123 mgmt->u.reassoc_req.listen_interval);
1124 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
1125 " capab_info=0x%02x listen_interval=%d current_ap="
1126 MACSTR,
1127 MAC2STR(mgmt->sa), capab_info, listen_interval,
1128 MAC2STR(mgmt->u.reassoc_req.current_ap));
1129 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
1130 pos = mgmt->u.reassoc_req.variable;
1131 } else {
1132 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
1133 listen_interval = le_to_host16(
1134 mgmt->u.assoc_req.listen_interval);
1135 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
1136 " capab_info=0x%02x listen_interval=%d",
1137 MAC2STR(mgmt->sa), capab_info, listen_interval);
1138 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
1139 pos = mgmt->u.assoc_req.variable;
1140 }
1141
1142 sta = ap_get_sta(hapd, mgmt->sa);
1143#ifdef CONFIG_IEEE80211R
1144 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
1145 (sta->flags & WLAN_STA_AUTH) == 0) {
1146 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
1147 "prior to authentication since it is using "
1148 "over-the-DS FT", MAC2STR(mgmt->sa));
1149 } else
1150#endif /* CONFIG_IEEE80211R */
1151 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1152 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1153 HOSTAPD_LEVEL_INFO, "Station tried to "
1154 "associate before authentication "
1155 "(aid=%d flags=0x%x)",
1156 sta ? sta->aid : -1,
1157 sta ? sta->flags : 0);
1158 send_deauth(hapd, mgmt->sa,
1159 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
1160 return;
1161 }
1162
1163 if (hapd->tkip_countermeasures) {
1164 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1165 goto fail;
1166 }
1167
1168 if (listen_interval > hapd->conf->max_listen_interval) {
1169 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1170 HOSTAPD_LEVEL_DEBUG,
1171 "Too large Listen Interval (%d)",
1172 listen_interval);
1173 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1174 goto fail;
1175 }
1176
1177 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1178 * is used */
1179 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1180 if (resp != WLAN_STATUS_SUCCESS)
1181 goto fail;
1182
1183 if (hostapd_get_aid(hapd, sta) < 0) {
1184 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1185 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1186 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1187 goto fail;
1188 }
1189
1190 sta->capability = capab_info;
1191 sta->listen_interval = listen_interval;
1192
6fc6879b
JM
1193 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1194 sta->flags |= WLAN_STA_NONERP;
1195 for (i = 0; i < sta->supported_rates_len; i++) {
1196 if ((sta->supported_rates[i] & 0x7f) > 22) {
1197 sta->flags &= ~WLAN_STA_NONERP;
1198 break;
1199 }
1200 }
1201 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1202 sta->nonerp_set = 1;
1203 hapd->iface->num_sta_non_erp++;
1204 if (hapd->iface->num_sta_non_erp == 1)
1205 ieee802_11_set_beacons(hapd->iface);
1206 }
1207
1208 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1209 !sta->no_short_slot_time_set) {
1210 sta->no_short_slot_time_set = 1;
1211 hapd->iface->num_sta_no_short_slot_time++;
1212 if (hapd->iface->current_mode->mode ==
1213 HOSTAPD_MODE_IEEE80211G &&
1214 hapd->iface->num_sta_no_short_slot_time == 1)
1215 ieee802_11_set_beacons(hapd->iface);
1216 }
1217
1218 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1219 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1220 else
1221 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1222
1223 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1224 !sta->no_short_preamble_set) {
1225 sta->no_short_preamble_set = 1;
1226 hapd->iface->num_sta_no_short_preamble++;
1227 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1228 && hapd->iface->num_sta_no_short_preamble == 1)
1229 ieee802_11_set_beacons(hapd->iface);
1230 }
1231
d45354be 1232#ifdef CONFIG_IEEE80211N
5586f500 1233 update_ht_state(hapd, sta);
d45354be 1234#endif /* CONFIG_IEEE80211N */
de9289c8 1235
6fc6879b
JM
1236 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1237 HOSTAPD_LEVEL_DEBUG,
1238 "association OK (aid %d)", sta->aid);
1239 /* Station will be marked associated, after it acknowledges AssocResp
1240 */
b8281964 1241 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
6fc6879b 1242
6f5c8dbd
JM
1243#ifdef CONFIG_IEEE80211W
1244 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1245 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1246 "SA Query procedure", reassoc ? "re" : "");
1247 /* TODO: Send a protected Disassociate frame to the STA using
1248 * the old key and Reason Code "Previous Authentication no
1249 * longer valid". Make sure this is only sent protected since
1250 * unprotected frame would be received by the STA that is now
1251 * trying to associate.
1252 */
1253 }
1254#endif /* CONFIG_IEEE80211W */
1255
1e858f69
JM
1256 if (reassoc) {
1257 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1258 ETH_ALEN);
1259 }
1260
6fc6879b
JM
1261 if (sta->last_assoc_req)
1262 os_free(sta->last_assoc_req);
1263 sta->last_assoc_req = os_malloc(len);
1264 if (sta->last_assoc_req)
1265 os_memcpy(sta->last_assoc_req, mgmt, len);
1266
1267 /* Make sure that the previously registered inactivity timer will not
1268 * remove the STA immediately. */
1269 sta->timeout_next = STA_NULLFUNC;
1270
1271 fail:
5586f500 1272 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
6fc6879b
JM
1273}
1274
1275
6fc6879b 1276static void handle_disassoc(struct hostapd_data *hapd,
f8b1f695 1277 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
1278{
1279 struct sta_info *sta;
1280
1281 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1282 printf("handle_disassoc - too short payload (len=%lu)\n",
1283 (unsigned long) len);
1284 return;
1285 }
1286
1287 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1288 MAC2STR(mgmt->sa),
1289 le_to_host16(mgmt->u.disassoc.reason_code));
1290
6fc6879b
JM
1291 sta = ap_get_sta(hapd, mgmt->sa);
1292 if (sta == NULL) {
1293 printf("Station " MACSTR " trying to disassociate, but it "
1294 "is not associated.\n", MAC2STR(mgmt->sa));
1295 return;
1296 }
1297
ae055af4 1298 ap_sta_set_authorized(hapd, sta, 0);
b8281964 1299 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
6fc6879b
JM
1300 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1301 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1302 HOSTAPD_LEVEL_INFO, "disassociated");
1303 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1304 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1305 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1306 * authenticated. */
1307 accounting_sta_stop(hapd, sta);
1308 ieee802_1x_free_station(sta);
51e2a27a 1309 hostapd_drv_sta_remove(hapd, sta->addr);
6fc6879b
JM
1310
1311 if (sta->timeout_next == STA_NULLFUNC ||
1312 sta->timeout_next == STA_DISASSOC) {
1313 sta->timeout_next = STA_DEAUTH;
1314 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1315 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1316 hapd, sta);
1317 }
1318
1319 mlme_disassociate_indication(
1320 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1321}
1322
1323
1324static void handle_deauth(struct hostapd_data *hapd,
f8b1f695 1325 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
1326{
1327 struct sta_info *sta;
1328
1329 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
3ec1e902
JM
1330 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
1331 "payload (len=%lu)", (unsigned long) len);
6fc6879b
JM
1332 return;
1333 }
1334
3ec1e902 1335 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
24d75245
BG
1336 " reason_code=%d",
1337 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
6fc6879b 1338
6fc6879b
JM
1339 sta = ap_get_sta(hapd, mgmt->sa);
1340 if (sta == NULL) {
3ec1e902
JM
1341 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
1342 "to deauthenticate, but it is not authenticated",
24d75245 1343 MAC2STR(mgmt->sa));
6fc6879b
JM
1344 return;
1345 }
1346
ae055af4 1347 ap_sta_set_authorized(hapd, sta, 0);
b8281964
JM
1348 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1349 WLAN_STA_ASSOC_REQ_OK);
6fc6879b
JM
1350 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1351 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1352 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1353 mlme_deauthenticate_indication(
1354 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1355 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1356 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1357 ap_free_sta(hapd, sta);
1358}
1359
1360
1361static void handle_beacon(struct hostapd_data *hapd,
b57e086c 1362 const struct ieee80211_mgmt *mgmt, size_t len,
6fc6879b
JM
1363 struct hostapd_frame_info *fi)
1364{
1365 struct ieee802_11_elems elems;
1366
1367 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1368 printf("handle_beacon - too short payload (len=%lu)\n",
1369 (unsigned long) len);
1370 return;
1371 }
1372
3d536eb4 1373 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
6fc6879b
JM
1374 len - (IEEE80211_HDRLEN +
1375 sizeof(mgmt->u.beacon)), &elems,
1376 0);
1377
6fc6879b
JM
1378 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1379}
1380
1381
5d22a1d5 1382#ifdef CONFIG_IEEE80211W
88b4b424 1383
93b76319 1384static void hostapd_sa_query_action(struct hostapd_data *hapd,
b57e086c
JM
1385 const struct ieee80211_mgmt *mgmt,
1386 size_t len)
5d22a1d5 1387{
b57e086c 1388 const u8 *end;
5d22a1d5 1389
93b76319
JM
1390 end = mgmt->u.action.u.sa_query_resp.trans_id +
1391 WLAN_SA_QUERY_TR_ID_LEN;
5d22a1d5 1392 if (((u8 *) mgmt) + len < end) {
93b76319 1393 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
5d22a1d5
JM
1394 "frame (len=%lu)", (unsigned long) len);
1395 return;
1396 }
1397
d4370eac
MP
1398 ieee802_11_sa_query_action(hapd, mgmt->sa,
1399 mgmt->u.action.u.sa_query_resp.action,
1400 mgmt->u.action.u.sa_query_resp.trans_id);
5d22a1d5 1401}
5d22a1d5
JM
1402
1403
c4e281fd
JM
1404static int robust_action_frame(u8 category)
1405{
1406 return category != WLAN_ACTION_PUBLIC &&
1407 category != WLAN_ACTION_HT;
1408}
9f64b827 1409#endif /* CONFIG_IEEE80211W */
c4e281fd
JM
1410
1411
6fc6879b 1412static void handle_action(struct hostapd_data *hapd,
b57e086c 1413 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b 1414{
46eeedac 1415#if defined(CONFIG_IEEE80211W) || defined(CONFIG_IEEE80211R)
c4e281fd 1416 struct sta_info *sta;
46eeedac
JM
1417 sta = ap_get_sta(hapd, mgmt->sa);
1418#endif /* CONFIG_IEEE80211W || CONFIG_IEEE80211R */
c4e281fd 1419
6fc6879b
JM
1420 if (len < IEEE80211_HDRLEN + 1) {
1421 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1422 HOSTAPD_LEVEL_DEBUG,
1423 "handle_action - too short payload (len=%lu)",
1424 (unsigned long) len);
1425 return;
1426 }
1427
c4e281fd
JM
1428#ifdef CONFIG_IEEE80211W
1429 if (sta && (sta->flags & WLAN_STA_MFP) &&
1430 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1431 robust_action_frame(mgmt->u.action.category))) {
1432 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1433 HOSTAPD_LEVEL_DEBUG,
1434 "Dropped unprotected Robust Action frame from "
1435 "an MFP STA");
1436 return;
1437 }
1438#endif /* CONFIG_IEEE80211W */
1439
6fc6879b
JM
1440 switch (mgmt->u.action.category) {
1441#ifdef CONFIG_IEEE80211R
1442 case WLAN_ACTION_FT:
1443 {
6fc6879b
JM
1444 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1445 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1446 "frame from unassociated STA " MACSTR,
1447 MAC2STR(mgmt->sa));
1448 return;
1449 }
1450
1451 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1452 len - IEEE80211_HDRLEN))
1453 break;
1454
1455 return;
1456 }
1457#endif /* CONFIG_IEEE80211R */
c2a71408 1458 case WLAN_ACTION_WMM:
3ae0800c 1459 hostapd_wmm_action(hapd, mgmt, len);
6fc6879b 1460 return;
5d22a1d5 1461#ifdef CONFIG_IEEE80211W
93b76319
JM
1462 case WLAN_ACTION_SA_QUERY:
1463 hostapd_sa_query_action(hapd, mgmt, len);
5d22a1d5
JM
1464 return;
1465#endif /* CONFIG_IEEE80211W */
c706d5aa
JM
1466 case WLAN_ACTION_PUBLIC:
1467 if (hapd->public_action_cb) {
1468 hapd->public_action_cb(hapd->public_action_cb_ctx,
1469 (u8 *) mgmt, len,
1470 hapd->iface->freq);
1471 return;
1472 }
1473 break;
e44f8bf2
JM
1474 case WLAN_ACTION_VENDOR_SPECIFIC:
1475 if (hapd->vendor_action_cb) {
1476 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1477 (u8 *) mgmt, len,
1478 hapd->iface->freq) == 0)
1479 return;
1480 }
1481 break;
6fc6879b
JM
1482 }
1483
1484 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1485 HOSTAPD_LEVEL_DEBUG,
1486 "handle_action - unknown action category %d or invalid "
1487 "frame",
1488 mgmt->u.action.category);
1489 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1490 !(mgmt->sa[0] & 0x01)) {
b57e086c
JM
1491 struct ieee80211_mgmt *resp;
1492
6fc6879b
JM
1493 /*
1494 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1495 * Return the Action frame to the source without change
1496 * except that MSB of the Category set to 1.
1497 */
1498 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1499 "frame back to sender");
b57e086c
JM
1500 resp = os_malloc(len);
1501 if (resp == NULL)
1502 return;
1503 os_memcpy(resp, mgmt, len);
1504 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1505 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1506 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1507 resp->u.action.category |= 0x80;
1508
41fe8b42
JM
1509 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
1510 wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
1511 "Action frame");
1512 }
b57e086c 1513 os_free(resp);
6fc6879b
JM
1514 }
1515}
1516
1517
1518/**
1519 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1520 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1521 * sent to)
1522 * @buf: management frame data (starting from IEEE 802.11 header)
1523 * @len: length of frame data in octets
a17df5fb 1524 * @fi: meta data about received frame (signal level, etc.)
6fc6879b
JM
1525 *
1526 * Process all incoming IEEE 802.11 management frames. This will be called for
1527 * each frame received from the kernel driver through wlan#ap interface. In
1528 * addition, it can be called to re-inserted pending frames (e.g., when using
1529 * external RADIUS server as an MAC ACL).
1530 */
b57e086c 1531void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
6fc6879b
JM
1532 struct hostapd_frame_info *fi)
1533{
f8b1f695 1534 struct ieee80211_mgmt *mgmt;
6fc6879b 1535 int broadcast;
f8b1f695
JM
1536 u16 fc, stype;
1537
cbcf92b4
JM
1538 if (len < 24)
1539 return;
1540
f8b1f695
JM
1541 mgmt = (struct ieee80211_mgmt *) buf;
1542 fc = le_to_host16(mgmt->frame_control);
1543 stype = WLAN_FC_GET_STYPE(fc);
6fc6879b
JM
1544
1545 if (stype == WLAN_FC_STYPE_BEACON) {
1546 handle_beacon(hapd, mgmt, len, fi);
1547 return;
1548 }
1549
6fc6879b
JM
1550 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1551 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1552 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1553
1554 if (!broadcast &&
3eeee931
AN
1555#ifdef CONFIG_P2P
1556 /* Invitation responses can be sent with the peer MAC as BSSID */
1557 !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
1558 stype == WLAN_FC_STYPE_ACTION) &&
1559#endif /* CONFIG_P2P */
fe2c5241 1560 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
6fc6879b
JM
1561 printf("MGMT: BSSID=" MACSTR " not our address\n",
1562 MAC2STR(mgmt->bssid));
1563 return;
1564 }
1565
1566
1567 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
baf513d6 1568 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
6fc6879b
JM
1569 return;
1570 }
1571
1572 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1573 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1574 HOSTAPD_LEVEL_DEBUG,
1575 "MGMT: DA=" MACSTR " not our address",
1576 MAC2STR(mgmt->da));
1577 return;
1578 }
1579
1580 switch (stype) {
1581 case WLAN_FC_STYPE_AUTH:
1582 wpa_printf(MSG_DEBUG, "mgmt::auth");
1583 handle_auth(hapd, mgmt, len);
1584 break;
1585 case WLAN_FC_STYPE_ASSOC_REQ:
1586 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1587 handle_assoc(hapd, mgmt, len, 0);
1588 break;
6fc6879b
JM
1589 case WLAN_FC_STYPE_REASSOC_REQ:
1590 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1591 handle_assoc(hapd, mgmt, len, 1);
1592 break;
1593 case WLAN_FC_STYPE_DISASSOC:
1594 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1595 handle_disassoc(hapd, mgmt, len);
1596 break;
1597 case WLAN_FC_STYPE_DEAUTH:
3ec1e902 1598 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
6fc6879b
JM
1599 handle_deauth(hapd, mgmt, len);
1600 break;
1601 case WLAN_FC_STYPE_ACTION:
1602 wpa_printf(MSG_DEBUG, "mgmt::action");
1603 handle_action(hapd, mgmt, len);
1604 break;
1605 default:
1606 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1607 HOSTAPD_LEVEL_DEBUG,
1608 "unknown mgmt frame subtype %d", stype);
1609 break;
1610 }
1611}
1612
1613
1614static void handle_auth_cb(struct hostapd_data *hapd,
f8b1f695 1615 const struct ieee80211_mgmt *mgmt,
6fc6879b
JM
1616 size_t len, int ok)
1617{
1618 u16 auth_alg, auth_transaction, status_code;
1619 struct sta_info *sta;
1620
1621 if (!ok) {
1622 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1623 HOSTAPD_LEVEL_NOTICE,
1624 "did not acknowledge authentication response");
1625 return;
1626 }
1627
1628 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1629 printf("handle_auth_cb - too short payload (len=%lu)\n",
1630 (unsigned long) len);
1631 return;
1632 }
1633
1634 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1635 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1636 status_code = le_to_host16(mgmt->u.auth.status_code);
1637
1638 sta = ap_get_sta(hapd, mgmt->da);
1639 if (!sta) {
1640 printf("handle_auth_cb: STA " MACSTR " not found\n",
1641 MAC2STR(mgmt->da));
1642 return;
1643 }
1644
1645 if (status_code == WLAN_STATUS_SUCCESS &&
1646 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1647 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1648 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1649 HOSTAPD_LEVEL_INFO, "authenticated");
1650 sta->flags |= WLAN_STA_AUTH;
1651 }
1652}
1653
1654
1655static void handle_assoc_cb(struct hostapd_data *hapd,
f8b1f695 1656 const struct ieee80211_mgmt *mgmt,
6fc6879b
JM
1657 size_t len, int reassoc, int ok)
1658{
1659 u16 status;
1660 struct sta_info *sta;
1661 int new_assoc = 1;
fc4e2d95 1662 struct ieee80211_ht_capabilities ht_cap;
6fc6879b 1663
6fc6879b
JM
1664 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1665 sizeof(mgmt->u.assoc_resp))) {
1666 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1667 "(len=%lu)\n", reassoc, (unsigned long) len);
1668 return;
1669 }
1670
6fc6879b
JM
1671 sta = ap_get_sta(hapd, mgmt->da);
1672 if (!sta) {
1673 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1674 MAC2STR(mgmt->da));
1675 return;
1676 }
1677
22b42372
FF
1678 if (!ok) {
1679 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1680 HOSTAPD_LEVEL_DEBUG,
1681 "did not acknowledge association response");
1682 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
1683 return;
1684 }
1685
1686 if (reassoc)
1687 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1688 else
1689 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1690
6fc6879b
JM
1691 if (status != WLAN_STATUS_SUCCESS)
1692 goto fail;
1693
1694 /* Stop previous accounting session, if one is started, and allocate
1695 * new session id for the new session. */
1696 accounting_sta_stop(hapd, sta);
6fc6879b
JM
1697
1698 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1699 HOSTAPD_LEVEL_INFO,
2fc98d02
JM
1700 "associated (aid %d)",
1701 sta->aid);
6fc6879b
JM
1702
1703 if (sta->flags & WLAN_STA_ASSOC)
1704 new_assoc = 0;
1705 sta->flags |= WLAN_STA_ASSOC;
ef580012
JM
1706 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1707 sta->auth_alg == WLAN_AUTH_FT) {
1708 /*
1709 * Open, static WEP, or FT protocol; no separate authorization
1710 * step.
1711 */
6905dcb1 1712 ap_sta_set_authorized(hapd, sta, 1);
515cf93f 1713 }
6fc6879b
JM
1714
1715 if (reassoc)
1716 mlme_reassociate_indication(hapd, sta);
1717 else
1718 mlme_associate_indication(hapd, sta);
1719
5d22a1d5 1720#ifdef CONFIG_IEEE80211W
93b76319 1721 sta->sa_query_timed_out = 0;
5d22a1d5
JM
1722#endif /* CONFIG_IEEE80211W */
1723
c140a228
JM
1724 /*
1725 * Remove the STA entry in order to make sure the STA PS state gets
1726 * cleared and configuration gets updated in case of reassociation back
1727 * to the same AP.
1728 */
51e2a27a 1729 hostapd_drv_sta_remove(hapd, sta->addr);
c140a228 1730
d45354be
JM
1731#ifdef CONFIG_IEEE80211N
1732 if (sta->flags & WLAN_STA_HT)
1733 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1734#endif /* CONFIG_IEEE80211N */
1735
0e8a96a9
JM
1736 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
1737 sta->supported_rates, sta->supported_rates_len,
1738 sta->listen_interval,
d83ab1fe 1739 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
5d061637 1740 sta->flags, sta->qosinfo)) {
6fc6879b
JM
1741 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1742 HOSTAPD_LEVEL_NOTICE,
1743 "Could not add STA to kernel driver");
8666585b
YAP
1744
1745 ap_sta_disconnect(hapd, sta, sta->addr,
1746 WLAN_REASON_DISASSOC_AP_BUSY);
1747
1748 goto fail;
6fc6879b
JM
1749 }
1750
7826ceae 1751 if (sta->flags & WLAN_STA_WDS)
0e8a96a9 1752 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
7826ceae 1753
6fc6879b
JM
1754 if (sta->eapol_sm == NULL) {
1755 /*
1756 * This STA does not use RADIUS server for EAP authentication,
1757 * so bind it to the selected VLAN interface now, since the
1758 * interface selection is not going to change anymore.
1759 */
4254100d
JM
1760 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1761 goto fail;
6fc6879b
JM
1762 } else if (sta->vlan_id) {
1763 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
4254100d
JM
1764 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1765 goto fail;
6fc6879b 1766 }
eddd8010 1767
0e8a96a9 1768 hostapd_set_sta_flags(hapd, sta);
6fc6879b
JM
1769
1770 if (sta->auth_alg == WLAN_AUTH_FT)
1771 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1772 else
1773 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
d24df7c3 1774 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
6fc6879b
JM
1775
1776 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1777
1778 fail:
1779 /* Copy of the association request is not needed anymore */
1780 if (sta->last_assoc_req) {
1781 os_free(sta->last_assoc_req);
1782 sta->last_assoc_req = NULL;
1783 }
1784}
1785
1786
4dc03726
JM
1787static void handle_deauth_cb(struct hostapd_data *hapd,
1788 const struct ieee80211_mgmt *mgmt,
1789 size_t len, int ok)
1790{
1791 struct sta_info *sta;
1792 if (mgmt->da[0] & 0x01)
1793 return;
1794 sta = ap_get_sta(hapd, mgmt->da);
1795 if (!sta) {
1796 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
1797 " not found", MAC2STR(mgmt->da));
1798 return;
1799 }
1800 if (ok)
1801 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
1802 MAC2STR(sta->addr));
1803 else
1804 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
1805 "deauth", MAC2STR(sta->addr));
1806
1807 ap_sta_deauth_cb(hapd, sta);
1808}
1809
1810
1811static void handle_disassoc_cb(struct hostapd_data *hapd,
1812 const struct ieee80211_mgmt *mgmt,
1813 size_t len, int ok)
1814{
1815 struct sta_info *sta;
1816 if (mgmt->da[0] & 0x01)
1817 return;
1818 sta = ap_get_sta(hapd, mgmt->da);
1819 if (!sta) {
1820 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
1821 " not found", MAC2STR(mgmt->da));
1822 return;
1823 }
1824 if (ok)
1825 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
1826 MAC2STR(sta->addr));
1827 else
1828 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
1829 "disassoc", MAC2STR(sta->addr));
1830
1831 ap_sta_disassoc_cb(hapd, sta);
1832}
1833
1834
1c6e69cc
JM
1835/**
1836 * ieee802_11_mgmt_cb - Process management frame TX status callback
1837 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1838 * was sent from)
1839 * @buf: management frame data (starting from IEEE 802.11 header)
1840 * @len: length of frame data in octets
1841 * @stype: management frame subtype from frame control field
1842 * @ok: Whether the frame was ACK'ed
1843 */
f8b1f695 1844void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
6fc6879b
JM
1845 u16 stype, int ok)
1846{
f8b1f695
JM
1847 const struct ieee80211_mgmt *mgmt;
1848 mgmt = (const struct ieee80211_mgmt *) buf;
6fc6879b
JM
1849
1850 switch (stype) {
1851 case WLAN_FC_STYPE_AUTH:
1852 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1853 handle_auth_cb(hapd, mgmt, len, ok);
1854 break;
1855 case WLAN_FC_STYPE_ASSOC_RESP:
1856 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1857 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1858 break;
1859 case WLAN_FC_STYPE_REASSOC_RESP:
1860 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1861 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1862 break;
1863 case WLAN_FC_STYPE_PROBE_RESP:
2d8bf732 1864 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
6fc6879b
JM
1865 break;
1866 case WLAN_FC_STYPE_DEAUTH:
4dc03726
JM
1867 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
1868 handle_deauth_cb(hapd, mgmt, len, ok);
1869 break;
1870 case WLAN_FC_STYPE_DISASSOC:
1871 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
1872 handle_disassoc_cb(hapd, mgmt, len, ok);
6fc6879b 1873 break;
5d22a1d5
JM
1874 case WLAN_FC_STYPE_ACTION:
1875 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1876 break;
6fc6879b
JM
1877 default:
1878 printf("unknown mgmt cb frame subtype %d\n", stype);
1879 break;
1880 }
1881}
1882
1883
6fc6879b
JM
1884int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1885{
1886 /* TODO */
1887 return 0;
1888}
1889
1890
1891int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1892 char *buf, size_t buflen)
1893{
1894 /* TODO */
1895 return 0;
1896}
1897
f8b1f695
JM
1898
1899void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
1900 const u8 *buf, size_t len, int ack)
1901{
1902 struct sta_info *sta;
1903 struct hostapd_iface *iface = hapd->iface;
1904
1905 sta = ap_get_sta(hapd, addr);
1906 if (sta == NULL && iface->num_bss > 1) {
1907 size_t j;
1908 for (j = 0; j < iface->num_bss; j++) {
1909 hapd = iface->bss[j];
1910 sta = ap_get_sta(hapd, addr);
1911 if (sta)
1912 break;
1913 }
1914 }
0c01d65d 1915 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
f8b1f695
JM
1916 return;
1917 if (sta->flags & WLAN_STA_PENDING_POLL) {
1918 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1919 "activity poll", MAC2STR(sta->addr),
1920 ack ? "ACKed" : "did not ACK");
1921 if (ack)
1922 sta->flags &= ~WLAN_STA_PENDING_POLL;
1923 }
1924
1925 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
1926}
1927
1928
dd840f79
JB
1929void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
1930 const u8 *data, size_t len, int ack)
1931{
1932 struct sta_info *sta;
1933 struct hostapd_iface *iface = hapd->iface;
1934
1935 sta = ap_get_sta(hapd, dst);
1936 if (sta == NULL && iface->num_bss > 1) {
1937 size_t j;
1938 for (j = 0; j < iface->num_bss; j++) {
1939 hapd = iface->bss[j];
1940 sta = ap_get_sta(hapd, dst);
1941 if (sta)
1942 break;
1943 }
1944 }
d9a38716
JM
1945 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1946 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
1947 MACSTR " that is not currently associated",
1948 MAC2STR(dst));
dd840f79 1949 return;
d9a38716 1950 }
dd840f79
JB
1951
1952 ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
1953}
1954
1955
bcf24348
JB
1956void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
1957{
1958 struct sta_info *sta;
1959 struct hostapd_iface *iface = hapd->iface;
1960
1961 sta = ap_get_sta(hapd, addr);
1962 if (sta == NULL && iface->num_bss > 1) {
1963 size_t j;
1964 for (j = 0; j < iface->num_bss; j++) {
1965 hapd = iface->bss[j];
1966 sta = ap_get_sta(hapd, addr);
1967 if (sta)
1968 break;
1969 }
1970 }
1971 if (sta == NULL)
1972 return;
1973 if (!(sta->flags & WLAN_STA_PENDING_POLL))
1974 return;
1975
1976 wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
1977 "activity poll", MAC2STR(sta->addr));
1978 sta->flags &= ~WLAN_STA_PENDING_POLL;
1979}
1980
1981
fbbfcbac
FF
1982void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
1983 int wds)
f8b1f695
JM
1984{
1985 struct sta_info *sta;
1986
1987 sta = ap_get_sta(hapd, src);
fbbfcbac 1988 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
99743811
FF
1989 if (!hapd->conf->wds_sta)
1990 return;
1991
fbbfcbac
FF
1992 if (wds && !(sta->flags & WLAN_STA_WDS)) {
1993 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
1994 "STA " MACSTR " (aid %u)",
1995 MAC2STR(sta->addr), sta->aid);
1996 sta->flags |= WLAN_STA_WDS;
0e8a96a9 1997 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
fbbfcbac 1998 }
f8b1f695 1999 return;
fbbfcbac 2000 }
f8b1f695
JM
2001
2002 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
2003 MACSTR, MAC2STR(src));
1df492df
JM
2004 if (src[0] & 0x01) {
2005 /* Broadcast bit set in SA?! Ignore the frame silently. */
2006 return;
2007 }
2008
b8281964
JM
2009 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
2010 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
2011 "already been sent, but no TX status yet known - "
2012 "ignore Class 3 frame issue with " MACSTR,
2013 MAC2STR(src));
2014 return;
2015 }
2016
f8b1f695 2017 if (sta && (sta->flags & WLAN_STA_AUTH))
51e2a27a 2018 hostapd_drv_sta_disassoc(
f8b1f695
JM
2019 hapd, src,
2020 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2021 else
51e2a27a 2022 hostapd_drv_sta_deauth(
f8b1f695
JM
2023 hapd, src,
2024 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2025}
2026
2027
6fc6879b 2028#endif /* CONFIG_NATIVE_WINDOWS */