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 HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
155 if (hapd->iconf->secondary_channel == -1)
156 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
157 HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
159 pos += sizeof(*oper);
162 #else /* CONFIG_IEEE80211N */
164 #endif /* CONFIG_IEEE80211N */
168 #ifdef CONFIG_IEEE80211N
172 Set to 0 (HT pure) under the followign conditions
173 - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
174 - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
175 Set to 1 (HT non-member protection) if there may be non-HT STAs
176 in both the primary and the secondary channel
177 Set to 2 if only HT STAs are associated in BSS,
178 however and at least one 20 MHz HT STA is associated
179 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
180 (currently non-GF HT station is considered as non-HT STA also)
182 int hostapd_ht_operation_update(struct hostapd_iface *iface)
184 u16 cur_op_mode, new_op_mode;
185 int op_mode_changes = 0;
187 if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
190 wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
191 __func__, iface->ht_op_mode);
193 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
194 && iface->num_sta_ht_no_gf) {
196 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
198 } else if ((iface->ht_op_mode &
199 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
200 iface->num_sta_ht_no_gf == 0) {
202 ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
206 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
207 (iface->num_sta_no_ht || iface->olbc_ht)) {
208 iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
210 } else if ((iface->ht_op_mode &
211 HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
212 (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
214 ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
218 /* Note: currently we switch to the MIXED op mode if HT non-greenfield
219 * station is associated. Probably it's a theoretical case, since
220 * it looks like all known HT STAs support greenfield.
223 if (iface->num_sta_no_ht ||
224 (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
225 new_op_mode = OP_MODE_MIXED;
226 else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
227 && iface->num_sta_ht_20mhz)
228 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
229 else if (iface->olbc_ht)
230 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
232 new_op_mode = OP_MODE_PURE;
234 cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
235 if (cur_op_mode != new_op_mode) {
236 iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
237 iface->ht_op_mode |= new_op_mode;
241 wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
242 __func__, iface->ht_op_mode, op_mode_changes);
244 return op_mode_changes;
247 #endif /* CONFIG_IEEE80211N */
250 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
253 int capab = WLAN_CAPABILITY_ESS;
256 if (hapd->iface->num_sta_no_short_preamble == 0 &&
257 hapd->iconf->preamble == SHORT_PREAMBLE)
258 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
260 privacy = hapd->conf->ssid.wep.keys_set;
262 if (hapd->conf->ieee802_1x &&
263 (hapd->conf->default_wep_key_len ||
264 hapd->conf->individual_wep_key_len))
271 int policy, def_klen;
272 if (probe && sta->ssid_probe) {
273 policy = sta->ssid_probe->security_policy;
274 def_klen = sta->ssid_probe->wep.default_len;
276 policy = sta->ssid->security_policy;
277 def_klen = sta->ssid->wep.default_len;
279 privacy = policy != SECURITY_PLAINTEXT;
280 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
285 capab |= WLAN_CAPABILITY_PRIVACY;
287 if (hapd->iface->current_mode &&
288 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
289 hapd->iface->num_sta_no_short_slot_time == 0)
290 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
296 #ifdef CONFIG_IEEE80211W
297 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
298 struct sta_info *sta, u8 *eid)
302 struct os_time now, passed;
304 *pos++ = WLAN_EID_ASSOC_COMEBACK_TIME;
307 os_time_sub(&now, &sta->sa_query_start, &passed);
308 tu = (passed.sec * 1000000 + passed.usec) / 1024;
309 if (hapd->conf->assoc_sa_query_max_timeout > tu)
310 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
313 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
314 timeout++; /* add some extra time for local timers */
315 WPA_PUT_LE32(pos, timeout);
320 #endif /* CONFIG_IEEE80211W */
323 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
326 if (len > HOSTAPD_MAX_SSID_LEN)
327 len = HOSTAPD_MAX_SSID_LEN;
328 for (i = 0; i < len; i++) {
329 if (ssid[i] >= 32 && ssid[i] < 127)
338 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
340 struct ieee80211_mgmt mgmt;
342 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
344 "deauthenticate - reason %d", reason);
345 os_memset(&mgmt, 0, sizeof(mgmt));
346 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
347 WLAN_FC_STYPE_DEAUTH);
348 os_memcpy(mgmt.da, addr, ETH_ALEN);
349 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
350 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
351 mgmt.u.deauth.reason_code = host_to_le16(reason);
352 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
353 sizeof(mgmt.u.deauth), 0) < 0)
354 perror("ieee802_11_send_deauth: send");
358 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
359 u16 auth_transaction, u8 *challenge, int iswep)
361 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
363 "authentication (shared key, transaction %d)",
366 if (auth_transaction == 1) {
367 if (!sta->challenge) {
368 /* Generate a pseudo-random challenge */
372 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
373 if (sta->challenge == NULL)
374 return WLAN_STATUS_UNSPECIFIED_FAILURE;
378 os_memcpy(key, &now, 4);
379 os_memcpy(key + 4, &r, 4);
380 rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
386 if (auth_transaction != 3)
387 return WLAN_STATUS_UNSPECIFIED_FAILURE;
390 if (!iswep || !sta->challenge || !challenge ||
391 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
392 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
394 "shared key authentication - invalid "
395 "challenge-response");
396 return WLAN_STATUS_CHALLENGE_FAIL;
399 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
401 "authentication OK (shared key)");
402 #ifdef IEEE80211_REQUIRE_AUTH_ACK
403 /* Station will be marked authenticated if it ACKs the
404 * authentication reply. */
406 sta->flags |= WLAN_STA_AUTH;
407 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
409 os_free(sta->challenge);
410 sta->challenge = NULL;
416 static void send_auth_reply(struct hostapd_data *hapd,
417 const u8 *dst, const u8 *bssid,
418 u16 auth_alg, u16 auth_transaction, u16 resp,
419 const u8 *ies, size_t ies_len)
421 struct ieee80211_mgmt *reply;
425 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
426 buf = os_zalloc(rlen);
430 reply = (struct ieee80211_mgmt *) buf;
431 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
433 os_memcpy(reply->da, dst, ETH_ALEN);
434 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
435 os_memcpy(reply->bssid, bssid, ETH_ALEN);
437 reply->u.auth.auth_alg = host_to_le16(auth_alg);
438 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
439 reply->u.auth.status_code = host_to_le16(resp);
442 os_memcpy(reply->u.auth.variable, ies, ies_len);
444 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
445 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
446 MAC2STR(dst), auth_alg, auth_transaction,
447 resp, (unsigned long) ies_len);
448 if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
449 perror("send_auth_reply: send");
455 #ifdef CONFIG_IEEE80211R
456 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
457 u16 auth_transaction, u16 status,
458 const u8 *ies, size_t ies_len)
460 struct hostapd_data *hapd = ctx;
461 struct sta_info *sta;
463 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
464 status, ies, ies_len);
466 if (status != WLAN_STATUS_SUCCESS)
469 sta = ap_get_sta(hapd, dst);
473 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
474 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
475 sta->flags |= WLAN_STA_AUTH;
476 mlme_authenticate_indication(hapd, sta);
478 #endif /* CONFIG_IEEE80211R */
481 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
484 u16 auth_alg, auth_transaction, status_code;
485 u16 resp = WLAN_STATUS_SUCCESS;
486 struct sta_info *sta = NULL;
489 u8 *challenge = NULL;
490 u32 session_timeout, acct_interim_interval;
492 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
493 size_t resp_ies_len = 0;
495 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
496 printf("handle_auth - too short payload (len=%lu)\n",
497 (unsigned long) len);
501 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
502 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
503 status_code = le_to_host16(mgmt->u.auth.status_code);
504 fc = le_to_host16(mgmt->frame_control);
506 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
507 2 + WLAN_AUTH_CHALLENGE_LEN &&
508 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
509 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
510 challenge = &mgmt->u.auth.variable[2];
512 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
513 "auth_transaction=%d status_code=%d wep=%d%s",
514 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
515 status_code, !!(fc & WLAN_FC_ISWEP),
516 challenge ? " challenge" : "");
518 if (hapd->tkip_countermeasures) {
519 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
523 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
524 auth_alg == WLAN_AUTH_OPEN) ||
525 #ifdef CONFIG_IEEE80211R
527 (hapd->conf->wpa_key_mgmt &
528 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
529 auth_alg == WLAN_AUTH_FT) ||
530 #endif /* CONFIG_IEEE80211R */
531 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
532 auth_alg == WLAN_AUTH_SHARED_KEY))) {
533 printf("Unsupported authentication algorithm (%d)\n",
535 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
539 if (!(auth_transaction == 1 ||
540 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
541 printf("Unknown authentication transaction number (%d)\n",
543 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
547 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
548 printf("Station " MACSTR " not allowed to authenticate.\n",
550 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
554 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
556 &acct_interim_interval, &vlan_id);
557 if (res == HOSTAPD_ACL_REJECT) {
558 printf("Station " MACSTR " not allowed to authenticate.\n",
560 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
563 if (res == HOSTAPD_ACL_PENDING) {
564 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
565 " waiting for an external authentication",
567 /* Authentication code will re-send the authentication frame
568 * after it has received (and cached) information from the
569 * external source. */
573 sta = ap_sta_add(hapd, mgmt->sa);
575 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
580 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
581 sta->vlan_id) == NULL) {
582 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
583 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
584 "%d received from RADIUS server",
586 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
589 sta->vlan_id = vlan_id;
590 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
591 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
594 sta->flags &= ~WLAN_STA_PREAUTH;
595 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
597 if (hapd->conf->radius->acct_interim_interval == 0 &&
598 acct_interim_interval)
599 sta->acct_interim_interval = acct_interim_interval;
600 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
601 ap_sta_session_timeout(hapd, sta, session_timeout);
603 ap_sta_no_session_timeout(hapd, sta);
607 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
609 "authentication OK (open system)");
610 #ifdef IEEE80211_REQUIRE_AUTH_ACK
611 /* Station will be marked authenticated if it ACKs the
612 * authentication reply. */
614 sta->flags |= WLAN_STA_AUTH;
615 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
616 sta->auth_alg = WLAN_AUTH_OPEN;
617 mlme_authenticate_indication(hapd, sta);
620 case WLAN_AUTH_SHARED_KEY:
621 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
623 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
624 mlme_authenticate_indication(hapd, sta);
625 if (sta->challenge && auth_transaction == 1) {
626 resp_ies[0] = WLAN_EID_CHALLENGE;
627 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
628 os_memcpy(resp_ies + 2, sta->challenge,
629 WLAN_AUTH_CHALLENGE_LEN);
630 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
633 #ifdef CONFIG_IEEE80211R
635 sta->auth_alg = WLAN_AUTH_FT;
636 if (sta->wpa_sm == NULL)
637 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
639 if (sta->wpa_sm == NULL) {
640 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
642 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
645 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
646 auth_transaction, mgmt->u.auth.variable,
647 len - IEEE80211_HDRLEN -
648 sizeof(mgmt->u.auth),
649 handle_auth_ft_finish, hapd);
650 /* handle_auth_ft_finish() callback will complete auth. */
652 #endif /* CONFIG_IEEE80211R */
656 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
657 auth_transaction + 1, resp, resp_ies, resp_ies_len);
661 static void handle_assoc(struct hostapd_data *hapd,
662 struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
664 u16 capab_info, listen_interval;
665 u16 resp = WLAN_STATUS_SUCCESS;
668 int send_deauth = 0, send_len, left, i;
669 struct sta_info *sta;
670 struct ieee802_11_elems elems;
671 u8 buf[sizeof(struct ieee80211_mgmt) + 512];
672 struct ieee80211_mgmt *reply;
674 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
675 sizeof(mgmt->u.assoc_req))) {
676 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
677 "\n", reassoc, (unsigned long) len);
682 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
683 listen_interval = le_to_host16(
684 mgmt->u.reassoc_req.listen_interval);
685 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
686 " capab_info=0x%02x listen_interval=%d current_ap="
688 MAC2STR(mgmt->sa), capab_info, listen_interval,
689 MAC2STR(mgmt->u.reassoc_req.current_ap));
690 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
691 pos = mgmt->u.reassoc_req.variable;
693 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
694 listen_interval = le_to_host16(
695 mgmt->u.assoc_req.listen_interval);
696 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
697 " capab_info=0x%02x listen_interval=%d",
698 MAC2STR(mgmt->sa), capab_info, listen_interval);
699 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
700 pos = mgmt->u.assoc_req.variable;
703 sta = ap_get_sta(hapd, mgmt->sa);
704 #ifdef CONFIG_IEEE80211R
705 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
706 (sta->flags & WLAN_STA_AUTH) == 0) {
707 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
708 "prior to authentication since it is using "
709 "over-the-DS FT", MAC2STR(mgmt->sa));
711 #endif /* CONFIG_IEEE80211R */
712 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
713 printf("STA " MACSTR " trying to associate before "
714 "authentication\n", MAC2STR(mgmt->sa));
716 printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
717 MAC2STR(sta->addr), sta->aid, sta->flags);
720 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
724 if (hapd->tkip_countermeasures) {
725 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
729 if (listen_interval > hapd->conf->max_listen_interval) {
730 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
732 "Too large Listen Interval (%d)",
734 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
738 sta->capability = capab_info;
739 sta->listen_interval = listen_interval;
741 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
743 if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
745 printf("STA " MACSTR " sent invalid association request\n",
747 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
751 if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
752 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
755 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
756 printf("Station " MACSTR " tried to associate with "
757 "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
758 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
762 sta->flags &= ~WLAN_STA_WME;
763 if (elems.wme && hapd->conf->wme_enabled) {
764 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
765 hostapd_logger(hapd, sta->addr,
768 "invalid WME element in association "
771 sta->flags |= WLAN_STA_WME;
774 if (!elems.supp_rates) {
775 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
777 "No supported rates element in AssocReq");
778 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
782 if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
783 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
785 "Invalid supported rates element length %d",
786 elems.supp_rates_len);
787 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
791 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
792 os_memcpy(sta->supported_rates, elems.supp_rates,
793 elems.supp_rates_len);
794 sta->supported_rates_len = elems.supp_rates_len;
796 if (elems.ext_supp_rates) {
797 if (elems.supp_rates_len + elems.ext_supp_rates_len >
798 sizeof(sta->supported_rates)) {
799 hostapd_logger(hapd, mgmt->sa,
800 HOSTAPD_MODULE_IEEE80211,
802 "Invalid supported rates element length"
803 " %d+%d", elems.supp_rates_len,
804 elems.ext_supp_rates_len);
805 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
809 os_memcpy(sta->supported_rates + elems.supp_rates_len,
810 elems.ext_supp_rates, elems.ext_supp_rates_len);
811 sta->supported_rates_len += elems.ext_supp_rates_len;
814 #ifdef CONFIG_IEEE80211N
815 /* save HT capabilities in the sta object */
816 os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
817 if (elems.ht_capabilities &&
818 elems.ht_capabilities_len >=
819 sizeof(struct ieee80211_ht_capability)) {
820 sta->flags |= WLAN_STA_HT;
821 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
822 sta->ht_capabilities.length =
823 sizeof(struct ieee80211_ht_capability);
824 os_memcpy(&sta->ht_capabilities.data,
825 elems.ht_capabilities,
826 sizeof(struct ieee80211_ht_capability));
828 sta->flags &= ~WLAN_STA_HT;
829 #endif /* CONFIG_IEEE80211N */
831 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
832 wpa_ie = elems.rsn_ie;
833 wpa_ie_len = elems.rsn_ie_len;
834 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
836 wpa_ie = elems.wpa_ie;
837 wpa_ie_len = elems.wpa_ie_len;
843 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
844 if (hapd->conf->wps_state && wpa_ie == NULL) {
846 wpa_printf(MSG_DEBUG, "STA included WPS IE in "
847 "(Re)Association Request - assume WPS is "
849 sta->flags |= WLAN_STA_WPS;
850 wpabuf_free(sta->wps_ie);
851 sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
852 elems.wps_ie_len - 4);
854 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
855 "in (Re)Association Request - possible WPS "
857 sta->flags |= WLAN_STA_MAYBE_WPS;
860 #endif /* CONFIG_WPS */
861 if (hapd->conf->wpa && wpa_ie == NULL) {
862 printf("STA " MACSTR ": No WPA/RSN IE in association "
863 "request\n", MAC2STR(sta->addr));
864 resp = WLAN_STATUS_INVALID_IE;
868 if (hapd->conf->wpa && wpa_ie) {
872 if (sta->wpa_sm == NULL)
873 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
875 if (sta->wpa_sm == NULL) {
876 printf("Failed to initialize WPA state machine\n");
877 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
880 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
882 elems.mdie, elems.mdie_len);
883 if (res == WPA_INVALID_GROUP)
884 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
885 else if (res == WPA_INVALID_PAIRWISE)
886 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
887 else if (res == WPA_INVALID_AKMP)
888 resp = WLAN_STATUS_AKMP_NOT_VALID;
889 else if (res == WPA_ALLOC_FAIL)
890 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
891 #ifdef CONFIG_IEEE80211W
892 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
893 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
894 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
895 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
896 #endif /* CONFIG_IEEE80211W */
897 else if (res == WPA_INVALID_MDIE)
898 resp = WLAN_STATUS_INVALID_MDIE;
899 else if (res != WPA_IE_OK)
900 resp = WLAN_STATUS_INVALID_IE;
901 if (resp != WLAN_STATUS_SUCCESS)
903 #ifdef CONFIG_IEEE80211W
904 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
905 sta->sa_query_count > 0)
906 ap_check_sa_query_timeout(hapd, sta);
907 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
908 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
910 * STA has already been associated with MFP and SA
911 * Query timeout has not been reached. Reject the
912 * association attempt temporarily and start SA Query,
913 * if one is not pending.
916 if (sta->sa_query_count == 0)
917 ap_sta_start_sa_query(hapd, sta);
919 resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
923 if (wpa_auth_uses_mfp(sta->wpa_sm))
924 sta->flags |= WLAN_STA_MFP;
926 sta->flags &= ~WLAN_STA_MFP;
927 #endif /* CONFIG_IEEE80211W */
929 #ifdef CONFIG_IEEE80211R
930 if (sta->auth_alg == WLAN_AUTH_FT) {
932 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
933 "to use association (not "
934 "re-association) with FT auth_alg",
936 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
940 resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
941 if (resp != WLAN_STATUS_SUCCESS)
944 #endif /* CONFIG_IEEE80211R */
945 #ifdef CONFIG_IEEE80211N
946 if ((sta->flags & WLAN_STA_HT) &&
947 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
948 wpa_printf(MSG_DEBUG, "HT: " MACSTR " tried to "
949 "use TKIP with HT association",
951 resp = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
954 #endif /* CONFIG_IEEE80211N */
956 wpa_auth_sta_no_wpa(sta->wpa_sm);
958 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
959 sta->flags |= WLAN_STA_NONERP;
960 for (i = 0; i < sta->supported_rates_len; i++) {
961 if ((sta->supported_rates[i] & 0x7f) > 22) {
962 sta->flags &= ~WLAN_STA_NONERP;
966 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
968 hapd->iface->num_sta_non_erp++;
969 if (hapd->iface->num_sta_non_erp == 1)
970 ieee802_11_set_beacons(hapd->iface);
973 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
974 !sta->no_short_slot_time_set) {
975 sta->no_short_slot_time_set = 1;
976 hapd->iface->num_sta_no_short_slot_time++;
977 if (hapd->iface->current_mode->mode ==
978 HOSTAPD_MODE_IEEE80211G &&
979 hapd->iface->num_sta_no_short_slot_time == 1)
980 ieee802_11_set_beacons(hapd->iface);
983 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
984 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
986 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
988 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
989 !sta->no_short_preamble_set) {
990 sta->no_short_preamble_set = 1;
991 hapd->iface->num_sta_no_short_preamble++;
992 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
993 && hapd->iface->num_sta_no_short_preamble == 1)
994 ieee802_11_set_beacons(hapd->iface);
997 #ifdef CONFIG_IEEE80211N
998 if (sta->flags & WLAN_STA_HT) {
999 u16 ht_capab = le_to_host16(
1000 sta->ht_capabilities.data.capabilities_info);
1001 wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
1002 "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
1003 if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
1004 if (!sta->no_ht_gf_set) {
1005 sta->no_ht_gf_set = 1;
1006 hapd->iface->num_sta_ht_no_gf++;
1008 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1009 "greenfield, num of non-gf stations %d",
1010 __func__, MAC2STR(sta->addr),
1011 hapd->iface->num_sta_ht_no_gf);
1013 if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1014 if (!sta->ht_20mhz_set) {
1015 sta->ht_20mhz_set = 1;
1016 hapd->iface->num_sta_ht_20mhz++;
1018 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1019 "num of 20MHz HT STAs %d",
1020 __func__, MAC2STR(sta->addr),
1021 hapd->iface->num_sta_ht_20mhz);
1024 if (!sta->no_ht_set) {
1026 hapd->iface->num_sta_no_ht++;
1028 if (hapd->iconf->ieee80211n) {
1029 wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1030 " - no HT, num of non-HT stations %d",
1031 __func__, MAC2STR(sta->addr),
1032 hapd->iface->num_sta_no_ht);
1036 if (hostapd_ht_operation_update(hapd->iface) > 0)
1037 ieee802_11_set_beacons(hapd->iface);
1038 #endif /* CONFIG_IEEE80211N */
1040 /* get a unique AID */
1042 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
1044 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1045 if (hapd->sta_aid[sta->aid - 1] == NULL)
1047 if (sta->aid > MAX_AID_TABLE_SIZE) {
1049 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1050 wpa_printf(MSG_ERROR, " no room for more AIDs");
1053 hapd->sta_aid[sta->aid - 1] = sta;
1054 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
1058 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1059 HOSTAPD_LEVEL_DEBUG,
1060 "association OK (aid %d)", sta->aid);
1061 /* Station will be marked associated, after it acknowledges AssocResp
1064 #ifdef CONFIG_IEEE80211W
1065 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1066 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1067 "SA Query procedure", reassoc ? "re" : "");
1068 /* TODO: Send a protected Disassociate frame to the STA using
1069 * the old key and Reason Code "Previous Authentication no
1070 * longer valid". Make sure this is only sent protected since
1071 * unprotected frame would be received by the STA that is now
1072 * trying to associate.
1075 #endif /* CONFIG_IEEE80211W */
1078 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1082 if (sta->last_assoc_req)
1083 os_free(sta->last_assoc_req);
1084 sta->last_assoc_req = os_malloc(len);
1085 if (sta->last_assoc_req)
1086 os_memcpy(sta->last_assoc_req, mgmt, len);
1088 /* Make sure that the previously registered inactivity timer will not
1089 * remove the STA immediately. */
1090 sta->timeout_next = STA_NULLFUNC;
1093 os_memset(buf, 0, sizeof(buf));
1094 reply = (struct ieee80211_mgmt *) buf;
1095 reply->frame_control =
1096 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1097 (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1098 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1099 WLAN_FC_STYPE_ASSOC_RESP)));
1100 os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1101 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1102 os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1104 send_len = IEEE80211_HDRLEN;
1106 send_len += sizeof(reply->u.deauth);
1107 reply->u.deauth.reason_code = host_to_le16(resp);
1110 send_len += sizeof(reply->u.assoc_resp);
1111 reply->u.assoc_resp.capab_info =
1112 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1113 reply->u.assoc_resp.status_code = host_to_le16(resp);
1114 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1115 | BIT(14) | BIT(15));
1116 /* Supported rates */
1117 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1118 /* Extended supported rates */
1119 p = hostapd_eid_ext_supp_rates(hapd, p);
1120 if (sta->flags & WLAN_STA_WME)
1121 p = hostapd_eid_wme(hapd, p);
1123 p = hostapd_eid_ht_capabilities_info(hapd, p);
1124 p = hostapd_eid_ht_operation(hapd, p);
1126 #ifdef CONFIG_IEEE80211R
1127 if (resp == WLAN_STATUS_SUCCESS) {
1128 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1129 * Transition Information, RSN */
1130 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1131 buf + sizeof(buf) - p,
1134 #endif /* CONFIG_IEEE80211R */
1136 #ifdef CONFIG_IEEE80211W
1137 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1138 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1139 #endif /* CONFIG_IEEE80211W */
1141 send_len += p - reply->u.assoc_resp.variable;
1144 if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1145 perror("handle_assoc: send");
1149 static void handle_disassoc(struct hostapd_data *hapd,
1150 struct ieee80211_mgmt *mgmt, size_t len)
1152 struct sta_info *sta;
1154 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1155 printf("handle_disassoc - too short payload (len=%lu)\n",
1156 (unsigned long) len);
1160 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1162 le_to_host16(mgmt->u.disassoc.reason_code));
1164 sta = ap_get_sta(hapd, mgmt->sa);
1166 printf("Station " MACSTR " trying to disassociate, but it "
1167 "is not associated.\n", MAC2STR(mgmt->sa));
1171 sta->flags &= ~WLAN_STA_ASSOC;
1172 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1173 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1174 HOSTAPD_LEVEL_INFO, "disassociated");
1175 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1176 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1177 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1179 accounting_sta_stop(hapd, sta);
1180 ieee802_1x_free_station(sta);
1181 hostapd_sta_remove(hapd, sta->addr);
1183 if (sta->timeout_next == STA_NULLFUNC ||
1184 sta->timeout_next == STA_DISASSOC) {
1185 sta->timeout_next = STA_DEAUTH;
1186 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1187 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1191 mlme_disassociate_indication(
1192 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1196 static void handle_deauth(struct hostapd_data *hapd,
1197 struct ieee80211_mgmt *mgmt, size_t len)
1199 struct sta_info *sta;
1201 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1202 printf("handle_deauth - too short payload (len=%lu)\n",
1203 (unsigned long) len);
1207 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1210 le_to_host16(mgmt->u.deauth.reason_code));
1212 sta = ap_get_sta(hapd, mgmt->sa);
1214 printf("Station " MACSTR " trying to deauthenticate, but it "
1215 "is not authenticated.\n", MAC2STR(mgmt->sa));
1219 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1220 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1221 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1222 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1223 mlme_deauthenticate_indication(
1224 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1225 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1226 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1227 ap_free_sta(hapd, sta);
1231 static void handle_beacon(struct hostapd_data *hapd,
1232 struct ieee80211_mgmt *mgmt, size_t len,
1233 struct hostapd_frame_info *fi)
1235 struct ieee802_11_elems elems;
1237 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1238 printf("handle_beacon - too short payload (len=%lu)\n",
1239 (unsigned long) len);
1243 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1244 len - (IEEE80211_HDRLEN +
1245 sizeof(mgmt->u.beacon)), &elems,
1248 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1252 #ifdef CONFIG_IEEE80211W
1254 /* MLME-SAQuery.request */
1255 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1256 const u8 *addr, const u8 *trans_id)
1258 struct ieee80211_mgmt mgmt;
1261 os_memset(&mgmt, 0, sizeof(mgmt));
1262 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1263 WLAN_FC_STYPE_ACTION);
1264 os_memcpy(mgmt.da, addr, ETH_ALEN);
1265 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1266 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1267 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1268 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1269 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1270 WLAN_SA_QUERY_TR_ID_LEN);
1271 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1272 if (hostapd_send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt, 0) < 0)
1273 perror("ieee802_11_send_sa_query_req: send");
1277 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1278 struct ieee80211_mgmt *mgmt, size_t len)
1280 struct sta_info *sta;
1284 end = mgmt->u.action.u.sa_query_resp.trans_id +
1285 WLAN_SA_QUERY_TR_ID_LEN;
1286 if (((u8 *) mgmt) + len < end) {
1287 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1288 "frame (len=%lu)", (unsigned long) len);
1292 if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1293 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1294 "Action %d", mgmt->u.action.u.sa_query_resp.action);
1298 /* MLME-SAQuery.confirm */
1300 sta = ap_get_sta(hapd, mgmt->sa);
1301 if (sta == NULL || sta->sa_query_trans_id == NULL) {
1302 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1303 "pending SA Query request found");
1307 for (i = 0; i < sta->sa_query_count; i++) {
1308 if (os_memcmp(sta->sa_query_trans_id +
1309 i * WLAN_SA_QUERY_TR_ID_LEN,
1310 mgmt->u.action.u.sa_query_resp.trans_id,
1311 WLAN_SA_QUERY_TR_ID_LEN) == 0)
1315 if (i >= sta->sa_query_count) {
1316 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1317 "transaction identifier found");
1321 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1322 HOSTAPD_LEVEL_DEBUG,
1323 "Reply to pending SA Query received");
1324 ap_sta_stop_sa_query(hapd, sta);
1326 #endif /* CONFIG_IEEE80211W */
1329 static void handle_action(struct hostapd_data *hapd,
1330 struct ieee80211_mgmt *mgmt, size_t len)
1332 if (len < IEEE80211_HDRLEN + 1) {
1333 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1334 HOSTAPD_LEVEL_DEBUG,
1335 "handle_action - too short payload (len=%lu)",
1336 (unsigned long) len);
1340 switch (mgmt->u.action.category) {
1341 #ifdef CONFIG_IEEE80211R
1342 case WLAN_ACTION_FT:
1344 struct sta_info *sta;
1346 sta = ap_get_sta(hapd, mgmt->sa);
1347 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1348 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1349 "frame from unassociated STA " MACSTR,
1354 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1355 len - IEEE80211_HDRLEN))
1360 #endif /* CONFIG_IEEE80211R */
1361 case WLAN_ACTION_WMM:
1362 hostapd_wme_action(hapd, mgmt, len);
1364 #ifdef CONFIG_IEEE80211W
1365 case WLAN_ACTION_SA_QUERY:
1366 hostapd_sa_query_action(hapd, mgmt, len);
1368 #endif /* CONFIG_IEEE80211W */
1371 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1372 HOSTAPD_LEVEL_DEBUG,
1373 "handle_action - unknown action category %d or invalid "
1375 mgmt->u.action.category);
1376 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1377 !(mgmt->sa[0] & 0x01)) {
1379 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1380 * Return the Action frame to the source without change
1381 * except that MSB of the Category set to 1.
1383 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1384 "frame back to sender");
1385 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1386 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1387 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1388 mgmt->u.action.category |= 0x80;
1390 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1396 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1397 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1399 * @buf: management frame data (starting from IEEE 802.11 header)
1400 * @len: length of frame data in octets
1401 * @stype: management frame subtype from frame control field
1403 * Process all incoming IEEE 802.11 management frames. This will be called for
1404 * each frame received from the kernel driver through wlan#ap interface. In
1405 * addition, it can be called to re-inserted pending frames (e.g., when using
1406 * external RADIUS server as an MAC ACL).
1408 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1409 struct hostapd_frame_info *fi)
1411 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1414 if (stype == WLAN_FC_STYPE_BEACON) {
1415 handle_beacon(hapd, mgmt, len, fi);
1419 if (fi && fi->passive_scan)
1422 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1423 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1424 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1427 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1428 printf("MGMT: BSSID=" MACSTR " not our address\n",
1429 MAC2STR(mgmt->bssid));
1434 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1435 handle_probe_req(hapd, mgmt, len);
1439 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1440 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1441 HOSTAPD_LEVEL_DEBUG,
1442 "MGMT: DA=" MACSTR " not our address",
1448 case WLAN_FC_STYPE_AUTH:
1449 wpa_printf(MSG_DEBUG, "mgmt::auth");
1450 handle_auth(hapd, mgmt, len);
1452 case WLAN_FC_STYPE_ASSOC_REQ:
1453 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1454 handle_assoc(hapd, mgmt, len, 0);
1456 case WLAN_FC_STYPE_REASSOC_REQ:
1457 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1458 handle_assoc(hapd, mgmt, len, 1);
1460 case WLAN_FC_STYPE_DISASSOC:
1461 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1462 handle_disassoc(hapd, mgmt, len);
1464 case WLAN_FC_STYPE_DEAUTH:
1465 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1466 handle_deauth(hapd, mgmt, len);
1468 case WLAN_FC_STYPE_ACTION:
1469 wpa_printf(MSG_DEBUG, "mgmt::action");
1470 handle_action(hapd, mgmt, len);
1473 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1474 HOSTAPD_LEVEL_DEBUG,
1475 "unknown mgmt frame subtype %d", stype);
1481 static void handle_auth_cb(struct hostapd_data *hapd,
1482 struct ieee80211_mgmt *mgmt,
1485 u16 auth_alg, auth_transaction, status_code;
1486 struct sta_info *sta;
1489 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1490 HOSTAPD_LEVEL_NOTICE,
1491 "did not acknowledge authentication response");
1495 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1496 printf("handle_auth_cb - too short payload (len=%lu)\n",
1497 (unsigned long) len);
1501 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1502 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1503 status_code = le_to_host16(mgmt->u.auth.status_code);
1505 sta = ap_get_sta(hapd, mgmt->da);
1507 printf("handle_auth_cb: STA " MACSTR " not found\n",
1512 if (status_code == WLAN_STATUS_SUCCESS &&
1513 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1514 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1515 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1516 HOSTAPD_LEVEL_INFO, "authenticated");
1517 sta->flags |= WLAN_STA_AUTH;
1522 static void handle_assoc_cb(struct hostapd_data *hapd,
1523 struct ieee80211_mgmt *mgmt,
1524 size_t len, int reassoc, int ok)
1527 struct sta_info *sta;
1529 struct ht_cap_ie *ht_cap = NULL;
1532 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1533 HOSTAPD_LEVEL_DEBUG,
1534 "did not acknowledge association response");
1538 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1539 sizeof(mgmt->u.assoc_resp))) {
1540 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1541 "(len=%lu)\n", reassoc, (unsigned long) len);
1546 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1548 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1550 sta = ap_get_sta(hapd, mgmt->da);
1552 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1557 if (status != WLAN_STATUS_SUCCESS)
1560 /* Stop previous accounting session, if one is started, and allocate
1561 * new session id for the new session. */
1562 accounting_sta_stop(hapd, sta);
1564 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1566 "associated (aid %d)",
1569 if (sta->flags & WLAN_STA_ASSOC)
1571 sta->flags |= WLAN_STA_ASSOC;
1574 mlme_reassociate_indication(hapd, sta);
1576 mlme_associate_indication(hapd, sta);
1578 #ifdef CONFIG_IEEE80211N
1579 if (sta->flags & WLAN_STA_HT)
1580 ht_cap = &sta->ht_capabilities;
1581 #endif /* CONFIG_IEEE80211N */
1583 #ifdef CONFIG_IEEE80211W
1584 sta->sa_query_timed_out = 0;
1585 #endif /* CONFIG_IEEE80211W */
1587 if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1588 sta->capability, sta->supported_rates,
1589 sta->supported_rates_len, 0, sta->listen_interval,
1592 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1593 HOSTAPD_LEVEL_NOTICE,
1594 "Could not add STA to kernel driver");
1597 if (sta->eapol_sm == NULL) {
1599 * This STA does not use RADIUS server for EAP authentication,
1600 * so bind it to the selected VLAN interface now, since the
1601 * interface selection is not going to change anymore.
1603 ap_sta_bind_vlan(hapd, sta, 0);
1604 } else if (sta->vlan_id) {
1605 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1606 ap_sta_bind_vlan(hapd, sta, 0);
1608 if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1609 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1610 WLAN_STA_SHORT_PREAMBLE, ~0);
1612 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1613 0, ~WLAN_STA_SHORT_PREAMBLE);
1616 if (sta->auth_alg == WLAN_AUTH_FT)
1617 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1619 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1620 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1622 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1625 /* Copy of the association request is not needed anymore */
1626 if (sta->last_assoc_req) {
1627 os_free(sta->last_assoc_req);
1628 sta->last_assoc_req = NULL;
1633 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1636 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1639 case WLAN_FC_STYPE_AUTH:
1640 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1641 handle_auth_cb(hapd, mgmt, len, ok);
1643 case WLAN_FC_STYPE_ASSOC_RESP:
1644 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1645 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1647 case WLAN_FC_STYPE_REASSOC_RESP:
1648 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1649 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1651 case WLAN_FC_STYPE_PROBE_RESP:
1652 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1654 case WLAN_FC_STYPE_DEAUTH:
1657 case WLAN_FC_STYPE_ACTION:
1658 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1661 printf("unknown mgmt cb frame subtype %d\n", stype);
1667 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1670 struct hostapd_data *hapd = eloop_ctx;
1671 hapd->tkip_countermeasures = 0;
1672 hostapd_set_countermeasures(hapd, 0);
1673 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1674 HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1678 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1680 struct sta_info *sta;
1682 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1683 HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1685 wpa_auth_countermeasures_start(hapd->wpa_auth);
1686 hapd->tkip_countermeasures = 1;
1687 hostapd_set_countermeasures(hapd, 1);
1688 wpa_gtk_rekey(hapd->wpa_auth);
1689 eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
1690 eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
1692 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
1693 hostapd_sta_deauth(hapd, sta->addr,
1694 WLAN_REASON_MICHAEL_MIC_FAILURE);
1695 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1696 WLAN_STA_AUTHORIZED);
1697 hostapd_sta_remove(hapd, sta->addr);
1702 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
1707 if (addr && local) {
1708 struct sta_info *sta = ap_get_sta(hapd, addr);
1710 wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
1711 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1713 "Michael MIC failure detected in "
1715 mlme_michaelmicfailure_indication(hapd, addr);
1717 wpa_printf(MSG_DEBUG,
1718 "MLME-MICHAELMICFAILURE.indication "
1719 "for not associated STA (" MACSTR
1720 ") ignored", MAC2STR(addr));
1726 if (now > hapd->michael_mic_failure + 60) {
1727 hapd->michael_mic_failures = 1;
1729 hapd->michael_mic_failures++;
1730 if (hapd->michael_mic_failures > 1)
1731 ieee80211_tkip_countermeasures_start(hapd);
1733 hapd->michael_mic_failure = now;
1737 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1744 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1745 char *buf, size_t buflen)
1751 #endif /* CONFIG_NATIVE_WINDOWS */