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