2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2009, 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"
42 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
47 if (hapd->iface->current_rates == NULL)
50 *pos++ = WLAN_EID_SUPP_RATES;
51 num = hapd->iface->num_rates;
53 /* rest of the rates are encoded in Extended supported
60 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
63 *pos = hapd->iface->current_rates[i].rate / 5;
64 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
73 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
78 if (hapd->iface->current_rates == NULL)
81 num = hapd->iface->num_rates;
86 *pos++ = WLAN_EID_EXT_SUPP_RATES;
89 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 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)
104 u8 * hostapd_eid_ht_capabilities(struct hostapd_data *hapd, u8 *eid)
106 #ifdef CONFIG_IEEE80211N
107 struct ieee80211_ht_capabilities *cap;
110 if (!hapd->iconf->ieee80211n)
113 *pos++ = WLAN_EID_HT_CAP;
114 *pos++ = sizeof(*cap);
116 cap = (struct ieee80211_ht_capabilities *) pos;
117 os_memset(cap, 0, sizeof(*cap));
118 cap->ht_capabilities_info = host_to_le16(hapd->iconf->ht_capab);
119 cap->a_mpdu_params = hapd->iface->current_mode->a_mpdu_params;
120 os_memcpy(cap->supported_mcs_set, hapd->iface->current_mode->mcs_set,
123 /* TODO: ht_extended_capabilities (now fully disabled) */
124 /* TODO: tx_bf_capability_info (now fully disabled) */
125 /* TODO: asel_capabilities (now fully disabled) */
130 #else /* CONFIG_IEEE80211N */
132 #endif /* CONFIG_IEEE80211N */
136 u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
138 #ifdef CONFIG_IEEE80211N
139 struct ieee80211_ht_operation *oper;
142 if (!hapd->iconf->ieee80211n)
145 *pos++ = WLAN_EID_HT_OPERATION;
146 *pos++ = sizeof(*oper);
148 oper = (struct ieee80211_ht_operation *) pos;
149 os_memset(oper, 0, sizeof(*oper));
151 oper->control_chan = hapd->iconf->channel;
152 oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
153 if (hapd->iconf->secondary_channel == 1)
154 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
155 HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
156 if (hapd->iconf->secondary_channel == -1)
157 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
158 HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
160 pos += sizeof(*oper);
163 #else /* CONFIG_IEEE80211N */
165 #endif /* CONFIG_IEEE80211N */
169 #ifdef CONFIG_IEEE80211N
173 Set to 0 (HT pure) under the followign conditions
174 - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
175 - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
176 Set to 1 (HT non-member protection) if there may be non-HT STAs
177 in both the primary and the secondary channel
178 Set to 2 if only HT STAs are associated in BSS,
179 however and at least one 20 MHz HT STA is associated
180 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
181 (currently non-GF HT station is considered as non-HT STA also)
183 int hostapd_ht_operation_update(struct hostapd_iface *iface)
185 u16 cur_op_mode, new_op_mode;
186 int op_mode_changes = 0;
188 if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
191 wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
192 __func__, iface->ht_op_mode);
194 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
195 && iface->num_sta_ht_no_gf) {
197 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
199 } else if ((iface->ht_op_mode &
200 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
201 iface->num_sta_ht_no_gf == 0) {
203 ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
207 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
208 (iface->num_sta_no_ht || iface->olbc_ht)) {
209 iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
211 } else if ((iface->ht_op_mode &
212 HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
213 (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
215 ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
219 /* Note: currently we switch to the MIXED op mode if HT non-greenfield
220 * station is associated. Probably it's a theoretical case, since
221 * it looks like all known HT STAs support greenfield.
224 if (iface->num_sta_no_ht ||
225 (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
226 new_op_mode = OP_MODE_MIXED;
227 else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
228 && iface->num_sta_ht_20mhz)
229 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
230 else if (iface->olbc_ht)
231 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
233 new_op_mode = OP_MODE_PURE;
235 cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
236 if (cur_op_mode != new_op_mode) {
237 iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
238 iface->ht_op_mode |= new_op_mode;
242 wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
243 __func__, iface->ht_op_mode, op_mode_changes);
245 return op_mode_changes;
248 #endif /* CONFIG_IEEE80211N */
251 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
254 int capab = WLAN_CAPABILITY_ESS;
257 if (hapd->iface->num_sta_no_short_preamble == 0 &&
258 hapd->iconf->preamble == SHORT_PREAMBLE)
259 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
261 privacy = hapd->conf->ssid.wep.keys_set;
263 if (hapd->conf->ieee802_1x &&
264 (hapd->conf->default_wep_key_len ||
265 hapd->conf->individual_wep_key_len))
272 int policy, def_klen;
273 if (probe && sta->ssid_probe) {
274 policy = sta->ssid_probe->security_policy;
275 def_klen = sta->ssid_probe->wep.default_len;
277 policy = sta->ssid->security_policy;
278 def_klen = sta->ssid->wep.default_len;
280 privacy = policy != SECURITY_PLAINTEXT;
281 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
286 capab |= WLAN_CAPABILITY_PRIVACY;
288 if (hapd->iface->current_mode &&
289 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
290 hapd->iface->num_sta_no_short_slot_time == 0)
291 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
297 #ifdef CONFIG_IEEE80211W
298 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
299 struct sta_info *sta, u8 *eid)
303 struct os_time now, passed;
305 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
307 *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
309 os_time_sub(&now, &sta->sa_query_start, &passed);
310 tu = (passed.sec * 1000000 + passed.usec) / 1024;
311 if (hapd->conf->assoc_sa_query_max_timeout > tu)
312 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
315 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
316 timeout++; /* add some extra time for local timers */
317 WPA_PUT_LE32(pos, timeout);
322 #endif /* CONFIG_IEEE80211W */
325 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
328 if (len > HOSTAPD_MAX_SSID_LEN)
329 len = HOSTAPD_MAX_SSID_LEN;
330 for (i = 0; i < len; i++) {
331 if (ssid[i] >= 32 && ssid[i] < 127)
341 * ieee802_11_send_deauth - Send Deauthentication frame
342 * @hapd: hostapd BSS data
343 * @addr: Address of the destination STA
344 * @reason: Reason code for Deauthentication
346 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
348 struct ieee80211_mgmt mgmt;
350 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
352 "deauthenticate - reason %d", reason);
353 os_memset(&mgmt, 0, sizeof(mgmt));
354 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
355 WLAN_FC_STYPE_DEAUTH);
356 os_memcpy(mgmt.da, addr, ETH_ALEN);
357 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
358 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
359 mgmt.u.deauth.reason_code = host_to_le16(reason);
360 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
361 sizeof(mgmt.u.deauth)) < 0)
362 perror("ieee802_11_send_deauth: send");
366 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
367 u16 auth_transaction, u8 *challenge, int iswep)
369 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
371 "authentication (shared key, transaction %d)",
374 if (auth_transaction == 1) {
375 if (!sta->challenge) {
376 /* Generate a pseudo-random challenge */
380 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
381 if (sta->challenge == NULL)
382 return WLAN_STATUS_UNSPECIFIED_FAILURE;
386 os_memcpy(key, &now, 4);
387 os_memcpy(key + 4, &r, 4);
388 rc4_skip(key, sizeof(key), 0,
389 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
394 if (auth_transaction != 3)
395 return WLAN_STATUS_UNSPECIFIED_FAILURE;
398 if (!iswep || !sta->challenge || !challenge ||
399 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
400 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
402 "shared key authentication - invalid "
403 "challenge-response");
404 return WLAN_STATUS_CHALLENGE_FAIL;
407 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
409 "authentication OK (shared key)");
410 #ifdef IEEE80211_REQUIRE_AUTH_ACK
411 /* Station will be marked authenticated if it ACKs the
412 * authentication reply. */
414 sta->flags |= WLAN_STA_AUTH;
415 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
417 os_free(sta->challenge);
418 sta->challenge = NULL;
424 static void send_auth_reply(struct hostapd_data *hapd,
425 const u8 *dst, const u8 *bssid,
426 u16 auth_alg, u16 auth_transaction, u16 resp,
427 const u8 *ies, size_t ies_len)
429 struct ieee80211_mgmt *reply;
433 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
434 buf = os_zalloc(rlen);
438 reply = (struct ieee80211_mgmt *) buf;
439 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
441 os_memcpy(reply->da, dst, ETH_ALEN);
442 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
443 os_memcpy(reply->bssid, bssid, ETH_ALEN);
445 reply->u.auth.auth_alg = host_to_le16(auth_alg);
446 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
447 reply->u.auth.status_code = host_to_le16(resp);
450 os_memcpy(reply->u.auth.variable, ies, ies_len);
452 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
453 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
454 MAC2STR(dst), auth_alg, auth_transaction,
455 resp, (unsigned long) ies_len);
456 if (hostapd_send_mgmt_frame(hapd, reply, rlen) < 0)
457 perror("send_auth_reply: send");
463 #ifdef CONFIG_IEEE80211R
464 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
465 u16 auth_transaction, u16 status,
466 const u8 *ies, size_t ies_len)
468 struct hostapd_data *hapd = ctx;
469 struct sta_info *sta;
471 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
472 status, ies, ies_len);
474 if (status != WLAN_STATUS_SUCCESS)
477 sta = ap_get_sta(hapd, dst);
481 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
482 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
483 sta->flags |= WLAN_STA_AUTH;
484 mlme_authenticate_indication(hapd, sta);
486 #endif /* CONFIG_IEEE80211R */
489 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
492 u16 auth_alg, auth_transaction, status_code;
493 u16 resp = WLAN_STATUS_SUCCESS;
494 struct sta_info *sta = NULL;
497 u8 *challenge = NULL;
498 u32 session_timeout, acct_interim_interval;
500 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
501 size_t resp_ies_len = 0;
503 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
504 printf("handle_auth - too short payload (len=%lu)\n",
505 (unsigned long) len);
509 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
510 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
511 status_code = le_to_host16(mgmt->u.auth.status_code);
512 fc = le_to_host16(mgmt->frame_control);
514 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
515 2 + WLAN_AUTH_CHALLENGE_LEN &&
516 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
517 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
518 challenge = &mgmt->u.auth.variable[2];
520 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
521 "auth_transaction=%d status_code=%d wep=%d%s",
522 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
523 status_code, !!(fc & WLAN_FC_ISWEP),
524 challenge ? " challenge" : "");
526 if (hapd->tkip_countermeasures) {
527 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
531 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
532 auth_alg == WLAN_AUTH_OPEN) ||
533 #ifdef CONFIG_IEEE80211R
535 (hapd->conf->wpa_key_mgmt &
536 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
537 auth_alg == WLAN_AUTH_FT) ||
538 #endif /* CONFIG_IEEE80211R */
539 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
540 auth_alg == WLAN_AUTH_SHARED_KEY))) {
541 printf("Unsupported authentication algorithm (%d)\n",
543 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
547 if (!(auth_transaction == 1 ||
548 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
549 printf("Unknown authentication transaction number (%d)\n",
551 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
555 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
556 printf("Station " MACSTR " not allowed to authenticate.\n",
558 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
562 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
564 &acct_interim_interval, &vlan_id);
565 if (res == HOSTAPD_ACL_REJECT) {
566 printf("Station " MACSTR " not allowed to authenticate.\n",
568 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
571 if (res == HOSTAPD_ACL_PENDING) {
572 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
573 " waiting for an external authentication",
575 /* Authentication code will re-send the authentication frame
576 * after it has received (and cached) information from the
577 * external source. */
581 sta = ap_sta_add(hapd, mgmt->sa);
583 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
588 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
590 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
591 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
592 "%d received from RADIUS server",
594 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
597 sta->vlan_id = vlan_id;
598 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
599 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
602 sta->flags &= ~WLAN_STA_PREAUTH;
603 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
605 if (hapd->conf->acct_interim_interval == 0 && 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);
610 ap_sta_no_session_timeout(hapd, sta);
614 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
616 "authentication OK (open system)");
617 #ifdef IEEE80211_REQUIRE_AUTH_ACK
618 /* Station will be marked authenticated if it ACKs the
619 * authentication reply. */
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);
627 case WLAN_AUTH_SHARED_KEY:
628 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
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;
640 #ifdef CONFIG_IEEE80211R
642 sta->auth_alg = WLAN_AUTH_FT;
643 if (sta->wpa_sm == NULL)
644 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
646 if (sta->wpa_sm == NULL) {
647 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
649 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
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. */
659 #endif /* CONFIG_IEEE80211R */
663 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
664 auth_transaction + 1, resp, resp_ies, resp_ies_len);
668 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
672 /* get a unique AID */
674 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
678 for (i = 0; i < AID_WORDS; i++) {
679 if (hapd->sta_aid[i] == (u32) -1)
681 for (j = 0; j < 32; j++) {
682 if (!(hapd->sta_aid[i] & BIT(j)))
690 aid = i * 32 + j + 1;
695 hapd->sta_aid[i] |= BIT(j);
696 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
701 static void handle_assoc(struct hostapd_data *hapd,
702 struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
704 u16 capab_info, listen_interval;
705 u16 resp = WLAN_STATUS_SUCCESS;
708 int send_deauth = 0, send_len, left, i;
709 struct sta_info *sta;
710 struct ieee802_11_elems elems;
711 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
712 struct ieee80211_mgmt *reply;
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);
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="
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;
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;
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));
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));
756 printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
757 MAC2STR(sta->addr), sta->aid, sta->flags);
760 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
764 if (hapd->tkip_countermeasures) {
765 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
769 if (listen_interval > hapd->conf->max_listen_interval) {
770 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
772 "Too large Listen Interval (%d)",
774 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
778 sta->capability = capab_info;
779 sta->listen_interval = listen_interval;
781 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
783 if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
785 printf("STA " MACSTR " sent invalid association request\n",
787 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
791 if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
792 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
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;
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))
805 hostapd_logger(hapd, sta->addr,
808 "invalid WMM element in association "
811 sta->flags |= WLAN_STA_WMM;
814 if (!elems.supp_rates) {
815 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
817 "No supported rates element in AssocReq");
818 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
822 if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
823 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
825 "Invalid supported rates element length %d",
826 elems.supp_rates_len);
827 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
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;
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,
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;
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;
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 &&
858 elems.ht_capabilities_len >=
859 sizeof(struct ieee80211_ht_capabilities)) {
860 sta->flags |= WLAN_STA_HT;
861 os_memcpy(&sta->ht_capabilities, elems.ht_capabilities,
862 sizeof(struct ieee80211_ht_capabilities));
864 sta->flags &= ~WLAN_STA_HT;
865 #endif /* CONFIG_IEEE80211N */
867 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
868 wpa_ie = elems.rsn_ie;
869 wpa_ie_len = elems.rsn_ie_len;
870 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
872 wpa_ie = elems.wpa_ie;
873 wpa_ie_len = elems.wpa_ie_len;
879 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
880 if (hapd->conf->wps_state && wpa_ie == NULL) {
882 wpa_printf(MSG_DEBUG, "STA included WPS IE in "
883 "(Re)Association Request - assume WPS is "
885 sta->flags |= WLAN_STA_WPS;
886 wpabuf_free(sta->wps_ie);
887 sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
888 elems.wps_ie_len - 4);
890 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
891 "in (Re)Association Request - possible WPS "
893 sta->flags |= WLAN_STA_MAYBE_WPS;
896 #endif /* CONFIG_WPS */
897 if (hapd->conf->wpa && wpa_ie == NULL) {
898 printf("STA " MACSTR ": No WPA/RSN IE in association "
899 "request\n", MAC2STR(sta->addr));
900 resp = WLAN_STATUS_INVALID_IE;
904 if (hapd->conf->wpa && wpa_ie) {
908 if (sta->wpa_sm == NULL)
909 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
911 if (sta->wpa_sm == NULL) {
912 printf("Failed to initialize WPA state machine\n");
913 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
916 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
918 elems.mdie, elems.mdie_len);
919 if (res == WPA_INVALID_GROUP)
920 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
921 else if (res == WPA_INVALID_PAIRWISE)
922 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
923 else if (res == WPA_INVALID_AKMP)
924 resp = WLAN_STATUS_AKMP_NOT_VALID;
925 else if (res == WPA_ALLOC_FAIL)
926 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
927 #ifdef CONFIG_IEEE80211W
928 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
929 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
930 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
931 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
932 #endif /* CONFIG_IEEE80211W */
933 else if (res == WPA_INVALID_MDIE)
934 resp = WLAN_STATUS_INVALID_MDIE;
935 else if (res != WPA_IE_OK)
936 resp = WLAN_STATUS_INVALID_IE;
937 if (resp != WLAN_STATUS_SUCCESS)
939 #ifdef CONFIG_IEEE80211W
940 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
941 sta->sa_query_count > 0)
942 ap_check_sa_query_timeout(hapd, sta);
943 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
944 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
946 * STA has already been associated with MFP and SA
947 * Query timeout has not been reached. Reject the
948 * association attempt temporarily and start SA Query,
949 * if one is not pending.
952 if (sta->sa_query_count == 0)
953 ap_sta_start_sa_query(hapd, sta);
955 resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
959 if (wpa_auth_uses_mfp(sta->wpa_sm))
960 sta->flags |= WLAN_STA_MFP;
962 sta->flags &= ~WLAN_STA_MFP;
963 #endif /* CONFIG_IEEE80211W */
965 #ifdef CONFIG_IEEE80211R
966 if (sta->auth_alg == WLAN_AUTH_FT) {
968 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
969 "to use association (not "
970 "re-association) with FT auth_alg",
972 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
976 resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
977 if (resp != WLAN_STATUS_SUCCESS)
980 #endif /* CONFIG_IEEE80211R */
981 #ifdef CONFIG_IEEE80211N
982 if ((sta->flags & WLAN_STA_HT) &&
983 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
984 wpa_printf(MSG_DEBUG, "HT: " MACSTR " tried to "
985 "use TKIP with HT association",
987 resp = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
990 #endif /* CONFIG_IEEE80211N */
992 wpa_auth_sta_no_wpa(sta->wpa_sm);
994 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
995 sta->flags |= WLAN_STA_NONERP;
996 for (i = 0; i < sta->supported_rates_len; i++) {
997 if ((sta->supported_rates[i] & 0x7f) > 22) {
998 sta->flags &= ~WLAN_STA_NONERP;
1002 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1003 sta->nonerp_set = 1;
1004 hapd->iface->num_sta_non_erp++;
1005 if (hapd->iface->num_sta_non_erp == 1)
1006 ieee802_11_set_beacons(hapd->iface);
1009 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1010 !sta->no_short_slot_time_set) {
1011 sta->no_short_slot_time_set = 1;
1012 hapd->iface->num_sta_no_short_slot_time++;
1013 if (hapd->iface->current_mode->mode ==
1014 HOSTAPD_MODE_IEEE80211G &&
1015 hapd->iface->num_sta_no_short_slot_time == 1)
1016 ieee802_11_set_beacons(hapd->iface);
1019 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1020 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1022 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1024 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1025 !sta->no_short_preamble_set) {
1026 sta->no_short_preamble_set = 1;
1027 hapd->iface->num_sta_no_short_preamble++;
1028 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1029 && hapd->iface->num_sta_no_short_preamble == 1)
1030 ieee802_11_set_beacons(hapd->iface);
1033 #ifdef CONFIG_IEEE80211N
1034 if (sta->flags & WLAN_STA_HT) {
1035 u16 ht_capab = le_to_host16(
1036 sta->ht_capabilities.ht_capabilities_info);
1037 wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
1038 "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
1039 if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
1040 if (!sta->no_ht_gf_set) {
1041 sta->no_ht_gf_set = 1;
1042 hapd->iface->num_sta_ht_no_gf++;
1044 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1045 "greenfield, num of non-gf stations %d",
1046 __func__, MAC2STR(sta->addr),
1047 hapd->iface->num_sta_ht_no_gf);
1049 if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1050 if (!sta->ht_20mhz_set) {
1051 sta->ht_20mhz_set = 1;
1052 hapd->iface->num_sta_ht_20mhz++;
1054 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1055 "num of 20MHz HT STAs %d",
1056 __func__, MAC2STR(sta->addr),
1057 hapd->iface->num_sta_ht_20mhz);
1060 if (!sta->no_ht_set) {
1062 hapd->iface->num_sta_no_ht++;
1064 if (hapd->iconf->ieee80211n) {
1065 wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1066 " - no HT, num of non-HT stations %d",
1067 __func__, MAC2STR(sta->addr),
1068 hapd->iface->num_sta_no_ht);
1072 if (hostapd_ht_operation_update(hapd->iface) > 0)
1073 ieee802_11_set_beacons(hapd->iface);
1074 #endif /* CONFIG_IEEE80211N */
1076 if (hostapd_get_aid(hapd, sta) < 0) {
1077 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1078 wpa_printf(MSG_ERROR, " no room for more AIDs");
1082 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1083 HOSTAPD_LEVEL_DEBUG,
1084 "association OK (aid %d)", sta->aid);
1085 /* Station will be marked associated, after it acknowledges AssocResp
1088 #ifdef CONFIG_IEEE80211W
1089 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1090 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1091 "SA Query procedure", reassoc ? "re" : "");
1092 /* TODO: Send a protected Disassociate frame to the STA using
1093 * the old key and Reason Code "Previous Authentication no
1094 * longer valid". Make sure this is only sent protected since
1095 * unprotected frame would be received by the STA that is now
1096 * trying to associate.
1099 #endif /* CONFIG_IEEE80211W */
1102 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1106 if (sta->last_assoc_req)
1107 os_free(sta->last_assoc_req);
1108 sta->last_assoc_req = os_malloc(len);
1109 if (sta->last_assoc_req)
1110 os_memcpy(sta->last_assoc_req, mgmt, len);
1112 /* Make sure that the previously registered inactivity timer will not
1113 * remove the STA immediately. */
1114 sta->timeout_next = STA_NULLFUNC;
1117 os_memset(buf, 0, sizeof(buf));
1118 reply = (struct ieee80211_mgmt *) buf;
1119 reply->frame_control =
1120 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1121 (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1122 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1123 WLAN_FC_STYPE_ASSOC_RESP)));
1124 os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1125 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1126 os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1128 send_len = IEEE80211_HDRLEN;
1130 send_len += sizeof(reply->u.deauth);
1131 reply->u.deauth.reason_code = host_to_le16(resp);
1134 send_len += sizeof(reply->u.assoc_resp);
1135 reply->u.assoc_resp.capab_info =
1136 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1137 reply->u.assoc_resp.status_code = host_to_le16(resp);
1138 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1139 | BIT(14) | BIT(15));
1140 /* Supported rates */
1141 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1142 /* Extended supported rates */
1143 p = hostapd_eid_ext_supp_rates(hapd, p);
1144 if (sta->flags & WLAN_STA_WMM)
1145 p = hostapd_eid_wmm(hapd, p);
1147 p = hostapd_eid_ht_capabilities(hapd, p);
1148 p = hostapd_eid_ht_operation(hapd, p);
1150 #ifdef CONFIG_IEEE80211R
1151 if (resp == WLAN_STATUS_SUCCESS) {
1152 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1153 * Transition Information, RSN, [RIC Response] */
1154 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1155 buf + sizeof(buf) - p,
1159 #endif /* CONFIG_IEEE80211R */
1161 #ifdef CONFIG_IEEE80211W
1162 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1163 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1164 #endif /* CONFIG_IEEE80211W */
1166 send_len += p - reply->u.assoc_resp.variable;
1169 if (hostapd_send_mgmt_frame(hapd, reply, send_len) < 0)
1170 perror("handle_assoc: send");
1174 static void handle_disassoc(struct hostapd_data *hapd,
1175 struct ieee80211_mgmt *mgmt, size_t len)
1177 struct sta_info *sta;
1179 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1180 printf("handle_disassoc - too short payload (len=%lu)\n",
1181 (unsigned long) len);
1185 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1187 le_to_host16(mgmt->u.disassoc.reason_code));
1189 sta = ap_get_sta(hapd, mgmt->sa);
1191 printf("Station " MACSTR " trying to disassociate, but it "
1192 "is not associated.\n", MAC2STR(mgmt->sa));
1196 sta->flags &= ~WLAN_STA_ASSOC;
1197 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1198 MAC2STR(sta->addr));
1199 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1200 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1201 HOSTAPD_LEVEL_INFO, "disassociated");
1202 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1203 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1204 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1206 accounting_sta_stop(hapd, sta);
1207 ieee802_1x_free_station(sta);
1208 hostapd_sta_remove(hapd, sta->addr);
1210 if (sta->timeout_next == STA_NULLFUNC ||
1211 sta->timeout_next == STA_DISASSOC) {
1212 sta->timeout_next = STA_DEAUTH;
1213 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1214 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1218 mlme_disassociate_indication(
1219 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1223 static void handle_deauth(struct hostapd_data *hapd,
1224 struct ieee80211_mgmt *mgmt, size_t len)
1226 struct sta_info *sta;
1228 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1229 printf("handle_deauth - too short payload (len=%lu)\n",
1230 (unsigned long) len);
1234 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1237 le_to_host16(mgmt->u.deauth.reason_code));
1239 sta = ap_get_sta(hapd, mgmt->sa);
1241 printf("Station " MACSTR " trying to deauthenticate, but it "
1242 "is not authenticated.\n", MAC2STR(mgmt->sa));
1246 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1247 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1248 MAC2STR(sta->addr));
1249 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1250 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1251 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1252 mlme_deauthenticate_indication(
1253 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1254 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1255 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1256 ap_free_sta(hapd, sta);
1260 static void handle_beacon(struct hostapd_data *hapd,
1261 struct ieee80211_mgmt *mgmt, size_t len,
1262 struct hostapd_frame_info *fi)
1264 struct ieee802_11_elems elems;
1266 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1267 printf("handle_beacon - too short payload (len=%lu)\n",
1268 (unsigned long) len);
1272 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1273 len - (IEEE80211_HDRLEN +
1274 sizeof(mgmt->u.beacon)), &elems,
1277 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1281 #ifdef CONFIG_IEEE80211W
1283 /* MLME-SAQuery.request */
1284 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1285 const u8 *addr, const u8 *trans_id)
1287 struct ieee80211_mgmt mgmt;
1290 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
1291 MACSTR, MAC2STR(addr));
1292 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1293 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1295 os_memset(&mgmt, 0, sizeof(mgmt));
1296 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1297 WLAN_FC_STYPE_ACTION);
1298 os_memcpy(mgmt.da, addr, ETH_ALEN);
1299 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1300 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1301 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1302 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1303 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1304 WLAN_SA_QUERY_TR_ID_LEN);
1305 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1306 if (hostapd_send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
1307 perror("ieee802_11_send_sa_query_req: send");
1311 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1312 struct ieee80211_mgmt *mgmt, size_t len)
1314 struct sta_info *sta;
1318 end = mgmt->u.action.u.sa_query_resp.trans_id +
1319 WLAN_SA_QUERY_TR_ID_LEN;
1320 if (((u8 *) mgmt) + len < end) {
1321 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1322 "frame (len=%lu)", (unsigned long) len);
1326 if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1327 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1328 "Action %d", mgmt->u.action.u.sa_query_resp.action);
1332 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
1333 MACSTR, MAC2STR(mgmt->sa));
1334 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1335 mgmt->u.action.u.sa_query_resp.trans_id,
1336 WLAN_SA_QUERY_TR_ID_LEN);
1338 /* MLME-SAQuery.confirm */
1340 sta = ap_get_sta(hapd, mgmt->sa);
1341 if (sta == NULL || sta->sa_query_trans_id == NULL) {
1342 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1343 "pending SA Query request found");
1347 for (i = 0; i < sta->sa_query_count; i++) {
1348 if (os_memcmp(sta->sa_query_trans_id +
1349 i * WLAN_SA_QUERY_TR_ID_LEN,
1350 mgmt->u.action.u.sa_query_resp.trans_id,
1351 WLAN_SA_QUERY_TR_ID_LEN) == 0)
1355 if (i >= sta->sa_query_count) {
1356 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1357 "transaction identifier found");
1361 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1362 HOSTAPD_LEVEL_DEBUG,
1363 "Reply to pending SA Query received");
1364 ap_sta_stop_sa_query(hapd, sta);
1368 static int robust_action_frame(u8 category)
1370 return category != WLAN_ACTION_PUBLIC &&
1371 category != WLAN_ACTION_HT;
1373 #endif /* CONFIG_IEEE80211W */
1376 static void handle_action(struct hostapd_data *hapd,
1377 struct ieee80211_mgmt *mgmt, size_t len)
1379 struct sta_info *sta;
1381 if (len < IEEE80211_HDRLEN + 1) {
1382 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1383 HOSTAPD_LEVEL_DEBUG,
1384 "handle_action - too short payload (len=%lu)",
1385 (unsigned long) len);
1389 sta = ap_get_sta(hapd, mgmt->sa);
1390 #ifdef CONFIG_IEEE80211W
1391 if (sta && (sta->flags & WLAN_STA_MFP) &&
1392 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1393 robust_action_frame(mgmt->u.action.category))) {
1394 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1395 HOSTAPD_LEVEL_DEBUG,
1396 "Dropped unprotected Robust Action frame from "
1400 #endif /* CONFIG_IEEE80211W */
1402 switch (mgmt->u.action.category) {
1403 #ifdef CONFIG_IEEE80211R
1404 case WLAN_ACTION_FT:
1406 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1407 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1408 "frame from unassociated STA " MACSTR,
1413 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1414 len - IEEE80211_HDRLEN))
1419 #endif /* CONFIG_IEEE80211R */
1420 case WLAN_ACTION_WMM:
1421 hostapd_wmm_action(hapd, mgmt, len);
1423 #ifdef CONFIG_IEEE80211W
1424 case WLAN_ACTION_SA_QUERY:
1425 hostapd_sa_query_action(hapd, mgmt, len);
1427 #endif /* CONFIG_IEEE80211W */
1430 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1431 HOSTAPD_LEVEL_DEBUG,
1432 "handle_action - unknown action category %d or invalid "
1434 mgmt->u.action.category);
1435 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1436 !(mgmt->sa[0] & 0x01)) {
1438 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1439 * Return the Action frame to the source without change
1440 * except that MSB of the Category set to 1.
1442 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1443 "frame back to sender");
1444 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1445 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1446 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1447 mgmt->u.action.category |= 0x80;
1449 hostapd_send_mgmt_frame(hapd, mgmt, len);
1455 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1456 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1458 * @buf: management frame data (starting from IEEE 802.11 header)
1459 * @len: length of frame data in octets
1460 * @stype: management frame subtype from frame control field
1461 * @fi: meta data about received frame (signal level, etc.)
1463 * Process all incoming IEEE 802.11 management frames. This will be called for
1464 * each frame received from the kernel driver through wlan#ap interface. In
1465 * addition, it can be called to re-inserted pending frames (e.g., when using
1466 * external RADIUS server as an MAC ACL).
1468 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1469 struct hostapd_frame_info *fi)
1471 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1474 if (stype == WLAN_FC_STYPE_BEACON) {
1475 handle_beacon(hapd, mgmt, len, fi);
1479 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1480 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1481 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1484 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1485 printf("MGMT: BSSID=" MACSTR " not our address\n",
1486 MAC2STR(mgmt->bssid));
1491 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1492 handle_probe_req(hapd, mgmt, len);
1496 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1497 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1498 HOSTAPD_LEVEL_DEBUG,
1499 "MGMT: DA=" MACSTR " not our address",
1505 case WLAN_FC_STYPE_AUTH:
1506 wpa_printf(MSG_DEBUG, "mgmt::auth");
1507 handle_auth(hapd, mgmt, len);
1509 case WLAN_FC_STYPE_ASSOC_REQ:
1510 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1511 handle_assoc(hapd, mgmt, len, 0);
1513 case WLAN_FC_STYPE_REASSOC_REQ:
1514 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1515 handle_assoc(hapd, mgmt, len, 1);
1517 case WLAN_FC_STYPE_DISASSOC:
1518 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1519 handle_disassoc(hapd, mgmt, len);
1521 case WLAN_FC_STYPE_DEAUTH:
1522 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1523 handle_deauth(hapd, mgmt, len);
1525 case WLAN_FC_STYPE_ACTION:
1526 wpa_printf(MSG_DEBUG, "mgmt::action");
1527 handle_action(hapd, mgmt, len);
1530 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1531 HOSTAPD_LEVEL_DEBUG,
1532 "unknown mgmt frame subtype %d", stype);
1538 static void handle_auth_cb(struct hostapd_data *hapd,
1539 struct ieee80211_mgmt *mgmt,
1542 u16 auth_alg, auth_transaction, status_code;
1543 struct sta_info *sta;
1546 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1547 HOSTAPD_LEVEL_NOTICE,
1548 "did not acknowledge authentication response");
1552 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1553 printf("handle_auth_cb - too short payload (len=%lu)\n",
1554 (unsigned long) len);
1558 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1559 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1560 status_code = le_to_host16(mgmt->u.auth.status_code);
1562 sta = ap_get_sta(hapd, mgmt->da);
1564 printf("handle_auth_cb: STA " MACSTR " not found\n",
1569 if (status_code == WLAN_STATUS_SUCCESS &&
1570 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1571 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1572 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1573 HOSTAPD_LEVEL_INFO, "authenticated");
1574 sta->flags |= WLAN_STA_AUTH;
1579 #ifdef CONFIG_IEEE80211N
1581 hostapd_get_ht_capab(struct hostapd_data *hapd,
1582 struct ieee80211_ht_capabilities *ht_cap,
1583 struct ieee80211_ht_capabilities *neg_ht_cap)
1587 os_memcpy(neg_ht_cap, ht_cap, sizeof(*neg_ht_cap));
1588 cap = le_to_host16(neg_ht_cap->ht_capabilities_info);
1589 cap &= hapd->iconf->ht_capab;
1590 cap |= (hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_DISABLED);
1592 /* FIXME: Rx STBC needs to be handled specially */
1593 cap |= (hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK);
1594 neg_ht_cap->ht_capabilities_info = host_to_le16(cap);
1596 #endif /* CONFIG_IEEE80211N */
1599 static void handle_assoc_cb(struct hostapd_data *hapd,
1600 struct ieee80211_mgmt *mgmt,
1601 size_t len, int reassoc, int ok)
1604 struct sta_info *sta;
1606 #ifdef CONFIG_IEEE80211N
1607 struct ieee80211_ht_capabilities ht_cap;
1608 #endif /* CONFIG_IEEE80211N */
1609 struct ieee80211_ht_capabilities *ht_cap_ptr = NULL;
1610 int set_flags, flags_and, flags_or;
1613 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1614 HOSTAPD_LEVEL_DEBUG,
1615 "did not acknowledge association response");
1619 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1620 sizeof(mgmt->u.assoc_resp))) {
1621 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1622 "(len=%lu)\n", reassoc, (unsigned long) len);
1627 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1629 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1631 sta = ap_get_sta(hapd, mgmt->da);
1633 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1638 if (status != WLAN_STATUS_SUCCESS)
1641 /* Stop previous accounting session, if one is started, and allocate
1642 * new session id for the new session. */
1643 accounting_sta_stop(hapd, sta);
1645 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1647 "associated (aid %d)",
1650 if (sta->flags & WLAN_STA_ASSOC)
1652 sta->flags |= WLAN_STA_ASSOC;
1653 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1654 /* Open or static WEP; no separate authorization */
1655 sta->flags |= WLAN_STA_AUTHORIZED;
1656 wpa_msg(hapd->msg_ctx, MSG_INFO,
1657 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
1661 mlme_reassociate_indication(hapd, sta);
1663 mlme_associate_indication(hapd, sta);
1665 #ifdef CONFIG_IEEE80211N
1666 if (sta->flags & WLAN_STA_HT) {
1667 ht_cap_ptr = &ht_cap;
1668 hostapd_get_ht_capab(hapd, &sta->ht_capabilities, ht_cap_ptr);
1670 #endif /* CONFIG_IEEE80211N */
1672 #ifdef CONFIG_IEEE80211W
1673 sta->sa_query_timed_out = 0;
1674 #endif /* CONFIG_IEEE80211W */
1677 * Remove the STA entry in order to make sure the STA PS state gets
1678 * cleared and configuration gets updated in case of reassociation back
1681 hostapd_sta_remove(hapd, sta->addr);
1683 if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1684 sta->capability, sta->supported_rates,
1685 sta->supported_rates_len, 0, sta->listen_interval,
1688 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1689 HOSTAPD_LEVEL_NOTICE,
1690 "Could not add STA to kernel driver");
1693 if (sta->eapol_sm == NULL) {
1695 * This STA does not use RADIUS server for EAP authentication,
1696 * so bind it to the selected VLAN interface now, since the
1697 * interface selection is not going to change anymore.
1699 ap_sta_bind_vlan(hapd, sta, 0);
1700 } else if (sta->vlan_id) {
1701 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1702 ap_sta_bind_vlan(hapd, sta, 0);
1705 set_flags = WLAN_STA_SHORT_PREAMBLE | WLAN_STA_WMM | WLAN_STA_MFP;
1706 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
1707 sta->flags & WLAN_STA_AUTHORIZED)
1708 set_flags |= WLAN_STA_AUTHORIZED;
1709 flags_or = sta->flags & set_flags;
1710 flags_and = sta->flags | ~set_flags;
1711 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1712 flags_or, flags_and);
1714 if (sta->auth_alg == WLAN_AUTH_FT)
1715 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1717 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1718 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1720 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1723 /* Copy of the association request is not needed anymore */
1724 if (sta->last_assoc_req) {
1725 os_free(sta->last_assoc_req);
1726 sta->last_assoc_req = NULL;
1732 * ieee802_11_mgmt_cb - Process management frame TX status callback
1733 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1735 * @buf: management frame data (starting from IEEE 802.11 header)
1736 * @len: length of frame data in octets
1737 * @stype: management frame subtype from frame control field
1738 * @ok: Whether the frame was ACK'ed
1740 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1743 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1746 case WLAN_FC_STYPE_AUTH:
1747 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1748 handle_auth_cb(hapd, mgmt, len, ok);
1750 case WLAN_FC_STYPE_ASSOC_RESP:
1751 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1752 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1754 case WLAN_FC_STYPE_REASSOC_RESP:
1755 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1756 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1758 case WLAN_FC_STYPE_PROBE_RESP:
1759 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1761 case WLAN_FC_STYPE_DEAUTH:
1764 case WLAN_FC_STYPE_ACTION:
1765 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1768 printf("unknown mgmt cb frame subtype %d\n", stype);
1774 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1781 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1782 char *buf, size_t buflen)
1788 #endif /* CONFIG_NATIVE_WINDOWS */