2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2007-2008, Intel Corporation
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.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
18 #ifndef CONFIG_NATIVE_WINDOWS
24 #include "ieee802_11.h"
26 #include "hw_features.h"
27 #include "radius/radius.h"
28 #include "radius/radius_client.h"
29 #include "ieee802_11_auth.h"
32 #include "ieee802_1x.h"
36 #include "accounting.h"
41 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
46 if (hapd->iface->current_rates == NULL)
49 *pos++ = WLAN_EID_SUPP_RATES;
50 num = hapd->iface->num_rates;
52 /* rest of the rates are encoded in Extended supported
59 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
62 *pos = hapd->iface->current_rates[i].rate / 5;
63 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
72 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
77 if (hapd->iface->current_rates == NULL)
80 num = hapd->iface->num_rates;
85 *pos++ = WLAN_EID_EXT_SUPP_RATES;
88 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
92 continue; /* already in SuppRates IE */
93 *pos = hapd->iface->current_rates[i].rate / 5;
94 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
103 u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
105 #ifdef CONFIG_IEEE80211N
106 struct ieee80211_ht_capability *cap;
109 if (!hapd->iconf->ieee80211n)
112 *pos++ = WLAN_EID_HT_CAP;
113 *pos++ = sizeof(*cap);
115 cap = (struct ieee80211_ht_capability *) pos;
116 os_memset(cap, 0, sizeof(*cap));
117 SET_2BIT_U8(&cap->mac_ht_params_info,
118 MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
119 MAX_RX_AMPDU_FACTOR_64KB);
121 cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
123 cap->supported_mcs_set[0] = 0xff;
124 cap->supported_mcs_set[1] = 0xff;
129 #else /* CONFIG_IEEE80211N */
131 #endif /* CONFIG_IEEE80211N */
135 u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
137 #ifdef CONFIG_IEEE80211N
138 struct ieee80211_ht_operation *oper;
141 if (!hapd->iconf->ieee80211n)
144 *pos++ = WLAN_EID_HT_OPERATION;
145 *pos++ = sizeof(*oper);
147 oper = (struct ieee80211_ht_operation *) pos;
148 os_memset(oper, 0, sizeof(*oper));
150 oper->control_chan = hapd->iconf->channel;
151 oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
152 if (hapd->iconf->secondary_channel == 1)
153 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE;
154 if (hapd->iconf->secondary_channel == -1)
155 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW;
157 pos += sizeof(*oper);
160 #else /* CONFIG_IEEE80211N */
162 #endif /* CONFIG_IEEE80211N */
166 #ifdef CONFIG_IEEE80211N
170 Set to 0 (HT pure) under the followign conditions
171 - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
172 - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
173 Set to 1 (HT non-member protection) if there may be non-HT STAs
174 in both the primary and the secondary channel
175 Set to 2 if only HT STAs are associated in BSS,
176 however and at least one 20 MHz HT STA is associated
177 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
178 (currently non-GF HT station is considered as non-HT STA also)
180 int hostapd_ht_operation_update(struct hostapd_iface *iface)
182 u16 cur_op_mode, new_op_mode;
183 int op_mode_changes = 0;
185 if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
188 wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
189 __func__, iface->ht_op_mode);
191 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
192 && iface->num_sta_ht_no_gf) {
194 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
196 } else if ((iface->ht_op_mode &
197 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
198 iface->num_sta_ht_no_gf == 0) {
200 ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
204 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
205 (iface->num_sta_no_ht || iface->olbc_ht)) {
206 iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
208 } else if ((iface->ht_op_mode &
209 HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
210 (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
212 ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
216 /* Note: currently we switch to the MIXED op mode if HT non-greenfield
217 * station is associated. Probably it's a theoretical case, since
218 * it looks like all known HT STAs support greenfield.
221 if (iface->num_sta_no_ht ||
222 (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
223 new_op_mode = OP_MODE_MIXED;
224 else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
225 && iface->num_sta_ht_20mhz)
226 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
227 else if (iface->olbc_ht)
228 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
230 new_op_mode = OP_MODE_PURE;
232 cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
233 if (cur_op_mode != new_op_mode) {
234 iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
235 iface->ht_op_mode |= new_op_mode;
239 wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
240 __func__, iface->ht_op_mode, op_mode_changes);
242 return op_mode_changes;
245 #endif /* CONFIG_IEEE80211N */
248 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
251 int capab = WLAN_CAPABILITY_ESS;
254 if (hapd->iface->num_sta_no_short_preamble == 0 &&
255 hapd->iconf->preamble == SHORT_PREAMBLE)
256 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
258 privacy = hapd->conf->ssid.wep.keys_set;
260 if (hapd->conf->ieee802_1x &&
261 (hapd->conf->default_wep_key_len ||
262 hapd->conf->individual_wep_key_len))
269 int policy, def_klen;
270 if (probe && sta->ssid_probe) {
271 policy = sta->ssid_probe->security_policy;
272 def_klen = sta->ssid_probe->wep.default_len;
274 policy = sta->ssid->security_policy;
275 def_klen = sta->ssid->wep.default_len;
277 privacy = policy != SECURITY_PLAINTEXT;
278 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
283 capab |= WLAN_CAPABILITY_PRIVACY;
285 if (hapd->iface->current_mode &&
286 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
287 hapd->iface->num_sta_no_short_slot_time == 0)
288 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
294 #ifdef CONFIG_IEEE80211W
295 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
296 struct sta_info *sta, u8 *eid)
301 *pos++ = WLAN_EID_ASSOC_COMEBACK_TIME;
303 timeout = (hapd->conf->assoc_ping_attempts - sta->ping_count + 1) *
304 hapd->conf->assoc_ping_timeout;
305 WPA_PUT_LE32(pos, timeout);
310 #endif /* CONFIG_IEEE80211W */
313 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
316 if (len > HOSTAPD_MAX_SSID_LEN)
317 len = HOSTAPD_MAX_SSID_LEN;
318 for (i = 0; i < len; i++) {
319 if (ssid[i] >= 32 && ssid[i] < 127)
328 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
330 struct ieee80211_mgmt mgmt;
332 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
334 "deauthenticate - reason %d", reason);
335 os_memset(&mgmt, 0, sizeof(mgmt));
336 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
337 WLAN_FC_STYPE_DEAUTH);
338 os_memcpy(mgmt.da, addr, ETH_ALEN);
339 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
340 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
341 mgmt.u.deauth.reason_code = host_to_le16(reason);
342 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
343 sizeof(mgmt.u.deauth), 0) < 0)
344 perror("ieee802_11_send_deauth: send");
348 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
349 u16 auth_transaction, u8 *challenge, int iswep)
351 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
353 "authentication (shared key, transaction %d)",
356 if (auth_transaction == 1) {
357 if (!sta->challenge) {
358 /* Generate a pseudo-random challenge */
362 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
363 if (sta->challenge == NULL)
364 return WLAN_STATUS_UNSPECIFIED_FAILURE;
368 os_memcpy(key, &now, 4);
369 os_memcpy(key + 4, &r, 4);
370 rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
376 if (auth_transaction != 3)
377 return WLAN_STATUS_UNSPECIFIED_FAILURE;
380 if (!iswep || !sta->challenge || !challenge ||
381 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
382 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
384 "shared key authentication - invalid "
385 "challenge-response");
386 return WLAN_STATUS_CHALLENGE_FAIL;
389 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
391 "authentication OK (shared key)");
392 #ifdef IEEE80211_REQUIRE_AUTH_ACK
393 /* Station will be marked authenticated if it ACKs the
394 * authentication reply. */
396 sta->flags |= WLAN_STA_AUTH;
397 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
399 os_free(sta->challenge);
400 sta->challenge = NULL;
406 static void send_auth_reply(struct hostapd_data *hapd,
407 const u8 *dst, const u8 *bssid,
408 u16 auth_alg, u16 auth_transaction, u16 resp,
409 const u8 *ies, size_t ies_len)
411 struct ieee80211_mgmt *reply;
415 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
416 buf = os_zalloc(rlen);
420 reply = (struct ieee80211_mgmt *) buf;
421 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
423 os_memcpy(reply->da, dst, ETH_ALEN);
424 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
425 os_memcpy(reply->bssid, bssid, ETH_ALEN);
427 reply->u.auth.auth_alg = host_to_le16(auth_alg);
428 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
429 reply->u.auth.status_code = host_to_le16(resp);
432 os_memcpy(reply->u.auth.variable, ies, ies_len);
434 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
435 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
436 MAC2STR(dst), auth_alg, auth_transaction,
437 resp, (unsigned long) ies_len);
438 if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
439 perror("send_auth_reply: send");
445 #ifdef CONFIG_IEEE80211R
446 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
447 u16 auth_transaction, u16 status,
448 const u8 *ies, size_t ies_len)
450 struct hostapd_data *hapd = ctx;
451 struct sta_info *sta;
453 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
454 status, ies, ies_len);
456 if (status != WLAN_STATUS_SUCCESS)
459 sta = ap_get_sta(hapd, dst);
463 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
464 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
465 sta->flags |= WLAN_STA_AUTH;
466 mlme_authenticate_indication(hapd, sta);
468 #endif /* CONFIG_IEEE80211R */
471 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
474 u16 auth_alg, auth_transaction, status_code;
475 u16 resp = WLAN_STATUS_SUCCESS;
476 struct sta_info *sta = NULL;
479 u8 *challenge = NULL;
480 u32 session_timeout, acct_interim_interval;
482 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
483 size_t resp_ies_len = 0;
485 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
486 printf("handle_auth - too short payload (len=%lu)\n",
487 (unsigned long) len);
491 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
492 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
493 status_code = le_to_host16(mgmt->u.auth.status_code);
494 fc = le_to_host16(mgmt->frame_control);
496 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
497 2 + WLAN_AUTH_CHALLENGE_LEN &&
498 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
499 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
500 challenge = &mgmt->u.auth.variable[2];
502 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
503 "auth_transaction=%d status_code=%d wep=%d%s",
504 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
505 status_code, !!(fc & WLAN_FC_ISWEP),
506 challenge ? " challenge" : "");
508 if (hapd->tkip_countermeasures) {
509 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
513 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
514 auth_alg == WLAN_AUTH_OPEN) ||
515 #ifdef CONFIG_IEEE80211R
517 (hapd->conf->wpa_key_mgmt &
518 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
519 auth_alg == WLAN_AUTH_FT) ||
520 #endif /* CONFIG_IEEE80211R */
521 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
522 auth_alg == WLAN_AUTH_SHARED_KEY))) {
523 printf("Unsupported authentication algorithm (%d)\n",
525 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
529 if (!(auth_transaction == 1 ||
530 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
531 printf("Unknown authentication transaction number (%d)\n",
533 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
537 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
538 printf("Station " MACSTR " not allowed to authenticate.\n",
540 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
544 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
546 &acct_interim_interval, &vlan_id);
547 if (res == HOSTAPD_ACL_REJECT) {
548 printf("Station " MACSTR " not allowed to authenticate.\n",
550 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
553 if (res == HOSTAPD_ACL_PENDING) {
554 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
555 " waiting for an external authentication",
557 /* Authentication code will re-send the authentication frame
558 * after it has received (and cached) information from the
559 * external source. */
563 sta = ap_sta_add(hapd, mgmt->sa);
565 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
570 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
571 sta->vlan_id) == NULL) {
572 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
573 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
574 "%d received from RADIUS server",
576 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
579 sta->vlan_id = vlan_id;
580 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
581 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
584 sta->flags &= ~WLAN_STA_PREAUTH;
585 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
587 if (hapd->conf->radius->acct_interim_interval == 0 &&
588 acct_interim_interval)
589 sta->acct_interim_interval = acct_interim_interval;
590 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
591 ap_sta_session_timeout(hapd, sta, session_timeout);
593 ap_sta_no_session_timeout(hapd, sta);
597 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
599 "authentication OK (open system)");
600 #ifdef IEEE80211_REQUIRE_AUTH_ACK
601 /* Station will be marked authenticated if it ACKs the
602 * authentication reply. */
604 sta->flags |= WLAN_STA_AUTH;
605 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
606 sta->auth_alg = WLAN_AUTH_OPEN;
607 mlme_authenticate_indication(hapd, sta);
610 case WLAN_AUTH_SHARED_KEY:
611 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
613 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
614 mlme_authenticate_indication(hapd, sta);
615 if (sta->challenge && auth_transaction == 1) {
616 resp_ies[0] = WLAN_EID_CHALLENGE;
617 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
618 os_memcpy(resp_ies + 2, sta->challenge,
619 WLAN_AUTH_CHALLENGE_LEN);
620 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
623 #ifdef CONFIG_IEEE80211R
625 sta->auth_alg = WLAN_AUTH_FT;
626 if (sta->wpa_sm == NULL)
627 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
629 if (sta->wpa_sm == NULL) {
630 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
632 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
635 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
636 auth_transaction, mgmt->u.auth.variable,
637 len - IEEE80211_HDRLEN -
638 sizeof(mgmt->u.auth),
639 handle_auth_ft_finish, hapd);
640 /* handle_auth_ft_finish() callback will complete auth. */
642 #endif /* CONFIG_IEEE80211R */
646 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
647 auth_transaction + 1, resp, resp_ies, resp_ies_len);
651 static void handle_assoc(struct hostapd_data *hapd,
652 struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
654 u16 capab_info, listen_interval;
655 u16 resp = WLAN_STATUS_SUCCESS;
658 int send_deauth = 0, send_len, left, i;
659 struct sta_info *sta;
660 struct ieee802_11_elems elems;
661 u8 buf[sizeof(struct ieee80211_mgmt) + 512];
662 struct ieee80211_mgmt *reply;
664 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
665 sizeof(mgmt->u.assoc_req))) {
666 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
667 "\n", reassoc, (unsigned long) len);
672 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
673 listen_interval = le_to_host16(
674 mgmt->u.reassoc_req.listen_interval);
675 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
676 " capab_info=0x%02x listen_interval=%d current_ap="
678 MAC2STR(mgmt->sa), capab_info, listen_interval,
679 MAC2STR(mgmt->u.reassoc_req.current_ap));
680 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
681 pos = mgmt->u.reassoc_req.variable;
683 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
684 listen_interval = le_to_host16(
685 mgmt->u.assoc_req.listen_interval);
686 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
687 " capab_info=0x%02x listen_interval=%d",
688 MAC2STR(mgmt->sa), capab_info, listen_interval);
689 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
690 pos = mgmt->u.assoc_req.variable;
693 sta = ap_get_sta(hapd, mgmt->sa);
694 #ifdef CONFIG_IEEE80211R
695 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
696 (sta->flags & WLAN_STA_AUTH) == 0) {
697 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
698 "prior to authentication since it is using "
699 "over-the-DS FT", MAC2STR(mgmt->sa));
701 #endif /* CONFIG_IEEE80211R */
702 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
703 printf("STA " MACSTR " trying to associate before "
704 "authentication\n", MAC2STR(mgmt->sa));
706 printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
707 MAC2STR(sta->addr), sta->aid, sta->flags);
710 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
714 if (hapd->tkip_countermeasures) {
715 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
719 if (listen_interval > hapd->conf->max_listen_interval) {
720 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
722 "Too large Listen Interval (%d)",
724 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
728 sta->capability = capab_info;
729 sta->listen_interval = listen_interval;
731 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
733 if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
735 printf("STA " MACSTR " sent invalid association request\n",
737 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
741 if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
742 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
745 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
746 printf("Station " MACSTR " tried to associate with "
747 "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
748 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
752 sta->flags &= ~WLAN_STA_WME;
753 if (elems.wme && hapd->conf->wme_enabled) {
754 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
755 hostapd_logger(hapd, sta->addr,
758 "invalid WME element in association "
761 sta->flags |= WLAN_STA_WME;
764 if (!elems.supp_rates) {
765 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
767 "No supported rates element in AssocReq");
768 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
772 if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
773 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
775 "Invalid supported rates element length %d",
776 elems.supp_rates_len);
777 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
781 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
782 os_memcpy(sta->supported_rates, elems.supp_rates,
783 elems.supp_rates_len);
784 sta->supported_rates_len = elems.supp_rates_len;
786 if (elems.ext_supp_rates) {
787 if (elems.supp_rates_len + elems.ext_supp_rates_len >
788 sizeof(sta->supported_rates)) {
789 hostapd_logger(hapd, mgmt->sa,
790 HOSTAPD_MODULE_IEEE80211,
792 "Invalid supported rates element length"
793 " %d+%d", elems.supp_rates_len,
794 elems.ext_supp_rates_len);
795 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
799 os_memcpy(sta->supported_rates + elems.supp_rates_len,
800 elems.ext_supp_rates, elems.ext_supp_rates_len);
801 sta->supported_rates_len += elems.ext_supp_rates_len;
804 #ifdef CONFIG_IEEE80211N
805 /* save HT capabilities in the sta object */
806 os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
807 if (elems.ht_capabilities &&
808 elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_capability)
809 && (sta->flags & WLAN_STA_WME)) {
810 /* note: without WMM capability, treat the sta as non-HT */
811 sta->flags |= WLAN_STA_HT;
812 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
813 sta->ht_capabilities.length =
814 sizeof(struct ieee80211_ht_capability);
815 os_memcpy(&sta->ht_capabilities.data,
816 elems.ht_capabilities,
817 sizeof(struct ieee80211_ht_capability));
819 sta->flags &= ~WLAN_STA_HT;
820 #endif /* CONFIG_IEEE80211N */
822 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
823 wpa_ie = elems.rsn_ie;
824 wpa_ie_len = elems.rsn_ie_len;
825 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
827 wpa_ie = elems.wpa_ie;
828 wpa_ie_len = elems.wpa_ie_len;
834 if (hapd->conf->wps_state && wpa_ie == NULL) {
836 wpa_printf(MSG_DEBUG, "STA included WPS IE in "
837 "(Re)Association Request - assume WPS is "
839 sta->flags |= WLAN_STA_WPS;
841 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
842 "in (Re)Association Request - possible WPS "
844 sta->flags |= WLAN_STA_MAYBE_WPS;
847 #endif /* CONFIG_WPS */
848 if (hapd->conf->wpa && wpa_ie == NULL) {
849 printf("STA " MACSTR ": No WPA/RSN IE in association "
850 "request\n", MAC2STR(sta->addr));
851 resp = WLAN_STATUS_INVALID_IE;
855 if (hapd->conf->wpa && wpa_ie) {
859 if (sta->wpa_sm == NULL)
860 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
862 if (sta->wpa_sm == NULL) {
863 printf("Failed to initialize WPA state machine\n");
864 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
867 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
869 elems.mdie, elems.mdie_len);
870 if (res == WPA_INVALID_GROUP)
871 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
872 else if (res == WPA_INVALID_PAIRWISE)
873 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
874 else if (res == WPA_INVALID_AKMP)
875 resp = WLAN_STATUS_AKMP_NOT_VALID;
876 else if (res == WPA_ALLOC_FAIL)
877 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
878 #ifdef CONFIG_IEEE80211W
879 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
880 resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
881 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
882 resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
883 #endif /* CONFIG_IEEE80211W */
884 else if (res == WPA_INVALID_MDIE)
885 resp = WLAN_STATUS_INVALID_MDIE;
886 else if (res != WPA_IE_OK)
887 resp = WLAN_STATUS_INVALID_IE;
888 if (resp != WLAN_STATUS_SUCCESS)
890 #ifdef CONFIG_IEEE80211W
891 if ((sta->flags & WLAN_STA_MFP) && !sta->ping_timed_out) {
893 * STA has already been associated with MFP and ping
894 * timeout has not been reached. Reject the
895 * association attempt temporarily and start ping, if
896 * one is not pending.
899 if (sta->ping_count == 0)
900 ap_sta_start_ping(hapd, sta);
902 resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
906 if (wpa_auth_uses_mfp(sta->wpa_sm))
907 sta->flags |= WLAN_STA_MFP;
909 sta->flags &= ~WLAN_STA_MFP;
910 #endif /* CONFIG_IEEE80211W */
912 #ifdef CONFIG_IEEE80211R
913 if (sta->auth_alg == WLAN_AUTH_FT) {
915 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
916 "to use association (not "
917 "re-association) with FT auth_alg",
919 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
923 resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
924 if (resp != WLAN_STATUS_SUCCESS)
927 #endif /* CONFIG_IEEE80211R */
930 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
931 sta->flags |= WLAN_STA_NONERP;
932 for (i = 0; i < sta->supported_rates_len; i++) {
933 if ((sta->supported_rates[i] & 0x7f) > 22) {
934 sta->flags &= ~WLAN_STA_NONERP;
938 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
940 hapd->iface->num_sta_non_erp++;
941 if (hapd->iface->num_sta_non_erp == 1)
942 ieee802_11_set_beacons(hapd->iface);
945 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
946 !sta->no_short_slot_time_set) {
947 sta->no_short_slot_time_set = 1;
948 hapd->iface->num_sta_no_short_slot_time++;
949 if (hapd->iface->current_mode->mode ==
950 HOSTAPD_MODE_IEEE80211G &&
951 hapd->iface->num_sta_no_short_slot_time == 1)
952 ieee802_11_set_beacons(hapd->iface);
955 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
956 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
958 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
960 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
961 !sta->no_short_preamble_set) {
962 sta->no_short_preamble_set = 1;
963 hapd->iface->num_sta_no_short_preamble++;
964 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
965 && hapd->iface->num_sta_no_short_preamble == 1)
966 ieee802_11_set_beacons(hapd->iface);
969 #ifdef CONFIG_IEEE80211N
970 if (sta->flags & WLAN_STA_HT) {
971 u16 ht_capab = le_to_host16(
972 sta->ht_capabilities.data.capabilities_info);
973 wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
974 "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
975 if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
976 hapd->iface->num_sta_ht_no_gf++;
977 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
978 "greenfield, num of non-gf stations %d",
979 __func__, MAC2STR(sta->addr),
980 hapd->iface->num_sta_ht_no_gf);
982 if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
983 hapd->iface->num_sta_ht_20mhz++;
984 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
985 "num of 20MHz HT STAs %d",
986 __func__, MAC2STR(sta->addr),
987 hapd->iface->num_sta_ht_20mhz);
990 hapd->iface->num_sta_no_ht++;
991 if (hapd->iconf->ieee80211n) {
992 wpa_printf(MSG_DEBUG, "%s STA " MACSTR
993 " - no HT, num of non-HT stations %d",
994 __func__, MAC2STR(sta->addr),
995 hapd->iface->num_sta_no_ht);
999 if (hostapd_ht_operation_update(hapd->iface) > 0)
1000 ieee802_11_set_beacons(hapd->iface);
1001 #endif /* CONFIG_IEEE80211N */
1003 /* get a unique AID */
1005 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
1007 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1008 if (hapd->sta_aid[sta->aid - 1] == NULL)
1010 if (sta->aid > MAX_AID_TABLE_SIZE) {
1012 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1013 wpa_printf(MSG_ERROR, " no room for more AIDs");
1016 hapd->sta_aid[sta->aid - 1] = sta;
1017 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
1021 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1022 HOSTAPD_LEVEL_DEBUG,
1023 "association OK (aid %d)", sta->aid);
1024 /* Station will be marked associated, after it acknowledges AssocResp
1028 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1032 if (sta->last_assoc_req)
1033 os_free(sta->last_assoc_req);
1034 sta->last_assoc_req = os_malloc(len);
1035 if (sta->last_assoc_req)
1036 os_memcpy(sta->last_assoc_req, mgmt, len);
1038 /* Make sure that the previously registered inactivity timer will not
1039 * remove the STA immediately. */
1040 sta->timeout_next = STA_NULLFUNC;
1043 os_memset(buf, 0, sizeof(buf));
1044 reply = (struct ieee80211_mgmt *) buf;
1045 reply->frame_control =
1046 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1047 (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1048 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1049 WLAN_FC_STYPE_ASSOC_RESP)));
1050 os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1051 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1052 os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1054 send_len = IEEE80211_HDRLEN;
1056 send_len += sizeof(reply->u.deauth);
1057 reply->u.deauth.reason_code = host_to_le16(resp);
1060 send_len += sizeof(reply->u.assoc_resp);
1061 reply->u.assoc_resp.capab_info =
1062 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1063 reply->u.assoc_resp.status_code = host_to_le16(resp);
1064 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1065 | BIT(14) | BIT(15));
1066 /* Supported rates */
1067 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1068 /* Extended supported rates */
1069 p = hostapd_eid_ext_supp_rates(hapd, p);
1070 if (sta->flags & WLAN_STA_WME)
1071 p = hostapd_eid_wme(hapd, p);
1073 p = hostapd_eid_ht_capabilities_info(hapd, p);
1074 p = hostapd_eid_ht_operation(hapd, p);
1076 #ifdef CONFIG_IEEE80211R
1077 if (resp == WLAN_STATUS_SUCCESS) {
1078 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1079 * Transition Information, RSN */
1080 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1081 buf + sizeof(buf) - p,
1084 #endif /* CONFIG_IEEE80211R */
1086 #ifdef CONFIG_IEEE80211W
1087 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1088 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1089 #endif /* CONFIG_IEEE80211W */
1091 send_len += p - reply->u.assoc_resp.variable;
1094 if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1095 perror("handle_assoc: send");
1099 static void handle_disassoc(struct hostapd_data *hapd,
1100 struct ieee80211_mgmt *mgmt, size_t len)
1102 struct sta_info *sta;
1104 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1105 printf("handle_disassoc - too short payload (len=%lu)\n",
1106 (unsigned long) len);
1110 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1112 le_to_host16(mgmt->u.disassoc.reason_code));
1114 sta = ap_get_sta(hapd, mgmt->sa);
1116 printf("Station " MACSTR " trying to disassociate, but it "
1117 "is not associated.\n", MAC2STR(mgmt->sa));
1121 sta->flags &= ~WLAN_STA_ASSOC;
1122 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1123 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1124 HOSTAPD_LEVEL_INFO, "disassociated");
1125 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1126 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1127 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1129 accounting_sta_stop(hapd, sta);
1130 ieee802_1x_free_station(sta);
1131 hostapd_sta_remove(hapd, sta->addr);
1133 if (sta->timeout_next == STA_NULLFUNC ||
1134 sta->timeout_next == STA_DISASSOC) {
1135 sta->timeout_next = STA_DEAUTH;
1136 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1137 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1141 mlme_disassociate_indication(
1142 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1146 static void handle_deauth(struct hostapd_data *hapd,
1147 struct ieee80211_mgmt *mgmt, size_t len)
1149 struct sta_info *sta;
1151 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1152 printf("handle_deauth - too short payload (len=%lu)\n",
1153 (unsigned long) len);
1157 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1160 le_to_host16(mgmt->u.deauth.reason_code));
1162 sta = ap_get_sta(hapd, mgmt->sa);
1164 printf("Station " MACSTR " trying to deauthenticate, but it "
1165 "is not authenticated.\n", MAC2STR(mgmt->sa));
1169 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1170 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1171 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1172 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1173 mlme_deauthenticate_indication(
1174 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1175 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1176 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1177 ap_free_sta(hapd, sta);
1181 static void handle_beacon(struct hostapd_data *hapd,
1182 struct ieee80211_mgmt *mgmt, size_t len,
1183 struct hostapd_frame_info *fi)
1185 struct ieee802_11_elems elems;
1187 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1188 printf("handle_beacon - too short payload (len=%lu)\n",
1189 (unsigned long) len);
1193 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1194 len - (IEEE80211_HDRLEN +
1195 sizeof(mgmt->u.beacon)), &elems,
1198 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1202 #ifdef CONFIG_IEEE80211W
1203 static void hostapd_ping_action(struct hostapd_data *hapd,
1204 struct ieee80211_mgmt *mgmt, size_t len)
1206 struct sta_info *sta;
1210 end = mgmt->u.action.u.ping_resp.trans_id + WLAN_PING_TRANS_ID_LEN;
1211 if (((u8 *) mgmt) + len < end) {
1212 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short Ping Action "
1213 "frame (len=%lu)", (unsigned long) len);
1217 if (mgmt->u.action.u.ping_resp.action != WLAN_PING_RESPONSE) {
1218 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected Ping Action %d",
1219 mgmt->u.action.u.ping_resp.action);
1223 /* MLME-PING.confirm */
1225 sta = ap_get_sta(hapd, mgmt->sa);
1226 if (sta == NULL || sta->ping_trans_id == NULL) {
1227 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1228 "pending ping request found");
1232 for (i = 0; i < sta->ping_count; i++) {
1233 if (os_memcmp(sta->ping_trans_id + i * WLAN_PING_TRANS_ID_LEN,
1234 mgmt->u.action.u.ping_resp.trans_id,
1235 WLAN_PING_TRANS_ID_LEN) == 0)
1239 if (i >= sta->ping_count) {
1240 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching ping "
1241 "transaction identifier found");
1245 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1246 HOSTAPD_LEVEL_DEBUG, "Reply to pending ping received");
1247 ap_sta_stop_ping(hapd, sta);
1249 #endif /* CONFIG_IEEE80211W */
1252 static void handle_action(struct hostapd_data *hapd,
1253 struct ieee80211_mgmt *mgmt, size_t len)
1255 if (len < IEEE80211_HDRLEN + 1) {
1256 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1257 HOSTAPD_LEVEL_DEBUG,
1258 "handle_action - too short payload (len=%lu)",
1259 (unsigned long) len);
1263 switch (mgmt->u.action.category) {
1264 #ifdef CONFIG_IEEE80211R
1265 case WLAN_ACTION_FT:
1267 struct sta_info *sta;
1269 sta = ap_get_sta(hapd, mgmt->sa);
1270 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1271 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1272 "frame from unassociated STA " MACSTR,
1277 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1278 len - IEEE80211_HDRLEN))
1283 #endif /* CONFIG_IEEE80211R */
1284 case WLAN_ACTION_WMM:
1285 hostapd_wme_action(hapd, mgmt, len);
1287 #ifdef CONFIG_IEEE80211W
1288 case WLAN_ACTION_PING:
1289 hostapd_ping_action(hapd, mgmt, len);
1291 #endif /* CONFIG_IEEE80211W */
1294 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1295 HOSTAPD_LEVEL_DEBUG,
1296 "handle_action - unknown action category %d or invalid "
1298 mgmt->u.action.category);
1299 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1300 !(mgmt->sa[0] & 0x01)) {
1302 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1303 * Return the Action frame to the source without change
1304 * except that MSB of the Category set to 1.
1306 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1307 "frame back to sender");
1308 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1309 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1310 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1311 mgmt->u.action.category |= 0x80;
1313 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1319 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1320 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1322 * @buf: management frame data (starting from IEEE 802.11 header)
1323 * @len: length of frame data in octets
1324 * @stype: management frame subtype from frame control field
1326 * Process all incoming IEEE 802.11 management frames. This will be called for
1327 * each frame received from the kernel driver through wlan#ap interface. In
1328 * addition, it can be called to re-inserted pending frames (e.g., when using
1329 * external RADIUS server as an MAC ACL).
1331 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1332 struct hostapd_frame_info *fi)
1334 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1337 if (stype == WLAN_FC_STYPE_BEACON) {
1338 handle_beacon(hapd, mgmt, len, fi);
1342 if (fi && fi->passive_scan)
1345 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1346 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1347 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1350 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1351 printf("MGMT: BSSID=" MACSTR " not our address\n",
1352 MAC2STR(mgmt->bssid));
1357 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1358 handle_probe_req(hapd, mgmt, len);
1362 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1363 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1364 HOSTAPD_LEVEL_DEBUG,
1365 "MGMT: DA=" MACSTR " not our address",
1371 case WLAN_FC_STYPE_AUTH:
1372 wpa_printf(MSG_DEBUG, "mgmt::auth");
1373 handle_auth(hapd, mgmt, len);
1375 case WLAN_FC_STYPE_ASSOC_REQ:
1376 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1377 handle_assoc(hapd, mgmt, len, 0);
1379 case WLAN_FC_STYPE_REASSOC_REQ:
1380 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1381 handle_assoc(hapd, mgmt, len, 1);
1383 case WLAN_FC_STYPE_DISASSOC:
1384 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1385 handle_disassoc(hapd, mgmt, len);
1387 case WLAN_FC_STYPE_DEAUTH:
1388 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1389 handle_deauth(hapd, mgmt, len);
1391 case WLAN_FC_STYPE_ACTION:
1392 wpa_printf(MSG_DEBUG, "mgmt::action");
1393 handle_action(hapd, mgmt, len);
1396 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1397 HOSTAPD_LEVEL_DEBUG,
1398 "unknown mgmt frame subtype %d", stype);
1404 static void handle_auth_cb(struct hostapd_data *hapd,
1405 struct ieee80211_mgmt *mgmt,
1408 u16 auth_alg, auth_transaction, status_code;
1409 struct sta_info *sta;
1412 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1413 HOSTAPD_LEVEL_NOTICE,
1414 "did not acknowledge authentication response");
1418 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1419 printf("handle_auth_cb - too short payload (len=%lu)\n",
1420 (unsigned long) len);
1424 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1425 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1426 status_code = le_to_host16(mgmt->u.auth.status_code);
1428 sta = ap_get_sta(hapd, mgmt->da);
1430 printf("handle_auth_cb: STA " MACSTR " not found\n",
1435 if (status_code == WLAN_STATUS_SUCCESS &&
1436 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1437 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1438 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1439 HOSTAPD_LEVEL_INFO, "authenticated");
1440 sta->flags |= WLAN_STA_AUTH;
1445 static void handle_assoc_cb(struct hostapd_data *hapd,
1446 struct ieee80211_mgmt *mgmt,
1447 size_t len, int reassoc, int ok)
1450 struct sta_info *sta;
1452 struct ht_cap_ie *ht_cap = NULL;
1455 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1456 HOSTAPD_LEVEL_DEBUG,
1457 "did not acknowledge association response");
1461 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1462 sizeof(mgmt->u.assoc_resp))) {
1463 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1464 "(len=%lu)\n", reassoc, (unsigned long) len);
1469 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1471 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1473 sta = ap_get_sta(hapd, mgmt->da);
1475 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1480 if (status != WLAN_STATUS_SUCCESS)
1483 /* Stop previous accounting session, if one is started, and allocate
1484 * new session id for the new session. */
1485 accounting_sta_stop(hapd, sta);
1487 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1489 "associated (aid %d)",
1492 if (sta->flags & WLAN_STA_ASSOC)
1494 sta->flags |= WLAN_STA_ASSOC;
1497 mlme_reassociate_indication(hapd, sta);
1499 mlme_associate_indication(hapd, sta);
1501 #ifdef CONFIG_IEEE80211N
1502 if (sta->flags & WLAN_STA_HT)
1503 ht_cap = &sta->ht_capabilities;
1504 #endif /* CONFIG_IEEE80211N */
1506 #ifdef CONFIG_IEEE80211W
1507 sta->ping_timed_out = 0;
1508 #endif /* CONFIG_IEEE80211W */
1510 if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1511 sta->capability, sta->supported_rates,
1512 sta->supported_rates_len, 0, sta->listen_interval,
1515 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1516 HOSTAPD_LEVEL_NOTICE,
1517 "Could not add STA to kernel driver");
1520 if (sta->eapol_sm == NULL) {
1522 * This STA does not use RADIUS server for EAP authentication,
1523 * so bind it to the selected VLAN interface now, since the
1524 * interface selection is not going to change anymore.
1526 ap_sta_bind_vlan(hapd, sta, 0);
1527 } else if (sta->vlan_id) {
1528 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1529 ap_sta_bind_vlan(hapd, sta, 0);
1531 if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1532 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1533 WLAN_STA_SHORT_PREAMBLE, ~0);
1535 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1536 0, ~WLAN_STA_SHORT_PREAMBLE);
1539 if (sta->auth_alg == WLAN_AUTH_FT)
1540 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1542 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1543 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1545 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1548 /* Copy of the association request is not needed anymore */
1549 if (sta->last_assoc_req) {
1550 os_free(sta->last_assoc_req);
1551 sta->last_assoc_req = NULL;
1556 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1559 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1562 case WLAN_FC_STYPE_AUTH:
1563 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1564 handle_auth_cb(hapd, mgmt, len, ok);
1566 case WLAN_FC_STYPE_ASSOC_RESP:
1567 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1568 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1570 case WLAN_FC_STYPE_REASSOC_RESP:
1571 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1572 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1574 case WLAN_FC_STYPE_PROBE_RESP:
1575 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1577 case WLAN_FC_STYPE_DEAUTH:
1580 case WLAN_FC_STYPE_ACTION:
1581 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1584 printf("unknown mgmt cb frame subtype %d\n", stype);
1590 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1593 struct hostapd_data *hapd = eloop_ctx;
1594 hapd->tkip_countermeasures = 0;
1595 hostapd_set_countermeasures(hapd, 0);
1596 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1597 HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1601 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1603 struct sta_info *sta;
1605 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1606 HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1608 wpa_auth_countermeasures_start(hapd->wpa_auth);
1609 hapd->tkip_countermeasures = 1;
1610 hostapd_set_countermeasures(hapd, 1);
1611 wpa_gtk_rekey(hapd->wpa_auth);
1612 eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
1613 eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
1615 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
1616 hostapd_sta_deauth(hapd, sta->addr,
1617 WLAN_REASON_MICHAEL_MIC_FAILURE);
1618 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1619 WLAN_STA_AUTHORIZED);
1620 hostapd_sta_remove(hapd, sta->addr);
1625 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
1630 if (addr && local) {
1631 struct sta_info *sta = ap_get_sta(hapd, addr);
1633 wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
1634 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1636 "Michael MIC failure detected in "
1638 mlme_michaelmicfailure_indication(hapd, addr);
1640 wpa_printf(MSG_DEBUG,
1641 "MLME-MICHAELMICFAILURE.indication "
1642 "for not associated STA (" MACSTR
1643 ") ignored", MAC2STR(addr));
1649 if (now > hapd->michael_mic_failure + 60) {
1650 hapd->michael_mic_failures = 1;
1652 hapd->michael_mic_failures++;
1653 if (hapd->michael_mic_failures > 1)
1654 ieee80211_tkip_countermeasures_start(hapd);
1656 hapd->michael_mic_failure = now;
1660 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1667 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1668 char *buf, size_t buflen)
1674 #endif /* CONFIG_NATIVE_WINDOWS */