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