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