2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #ifndef CONFIG_NATIVE_WINDOWS
19 #include "utils/common.h"
20 #include "utils/eloop.h"
21 #include "crypto/crypto.h"
22 #include "drivers/driver.h"
23 #include "common/ieee802_11_defs.h"
24 #include "common/ieee802_11_common.h"
25 #include "common/wpa_ctrl.h"
26 #include "radius/radius.h"
27 #include "radius/radius_client.h"
32 #include "ieee802_11_auth.h"
34 #include "ieee802_1x.h"
38 #include "accounting.h"
39 #include "ap_config.h"
41 #include "p2p_hostapd.h"
42 #include "ieee802_11.h"
45 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
50 if (hapd->iface->current_rates == NULL)
53 *pos++ = WLAN_EID_SUPP_RATES;
54 num = hapd->iface->num_rates;
56 /* rest of the rates are encoded in Extended supported
63 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
66 *pos = hapd->iface->current_rates[i].rate / 5;
67 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
76 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
81 if (hapd->iface->current_rates == NULL)
84 num = hapd->iface->num_rates;
89 *pos++ = WLAN_EID_EXT_SUPP_RATES;
92 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
96 continue; /* already in SuppRates IE */
97 *pos = hapd->iface->current_rates[i].rate / 5;
98 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
107 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
110 int capab = WLAN_CAPABILITY_ESS;
113 if (hapd->iface->num_sta_no_short_preamble == 0 &&
114 hapd->iconf->preamble == SHORT_PREAMBLE)
115 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
117 privacy = hapd->conf->ssid.wep.keys_set;
119 if (hapd->conf->ieee802_1x &&
120 (hapd->conf->default_wep_key_len ||
121 hapd->conf->individual_wep_key_len))
128 int policy, def_klen;
129 if (probe && sta->ssid_probe) {
130 policy = sta->ssid_probe->security_policy;
131 def_klen = sta->ssid_probe->wep.default_len;
133 policy = sta->ssid->security_policy;
134 def_klen = sta->ssid->wep.default_len;
136 privacy = policy != SECURITY_PLAINTEXT;
137 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
142 capab |= WLAN_CAPABILITY_PRIVACY;
144 if (hapd->iface->current_mode &&
145 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
146 hapd->iface->num_sta_no_short_slot_time == 0)
147 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
153 #ifdef CONFIG_IEEE80211W
154 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
155 struct sta_info *sta, u8 *eid)
159 struct os_time now, passed;
161 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
163 *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
165 os_time_sub(&now, &sta->sa_query_start, &passed);
166 tu = (passed.sec * 1000000 + passed.usec) / 1024;
167 if (hapd->conf->assoc_sa_query_max_timeout > tu)
168 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
171 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
172 timeout++; /* add some extra time for local timers */
173 WPA_PUT_LE32(pos, timeout);
178 #endif /* CONFIG_IEEE80211W */
181 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
184 if (len > HOSTAPD_MAX_SSID_LEN)
185 len = HOSTAPD_MAX_SSID_LEN;
186 for (i = 0; i < len; i++) {
187 if (ssid[i] >= 32 && ssid[i] < 127)
196 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
197 u16 auth_transaction, const u8 *challenge,
200 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
202 "authentication (shared key, transaction %d)",
205 if (auth_transaction == 1) {
206 if (!sta->challenge) {
207 /* Generate a pseudo-random challenge */
211 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
212 if (sta->challenge == NULL)
213 return WLAN_STATUS_UNSPECIFIED_FAILURE;
217 os_memcpy(key, &now, 4);
218 os_memcpy(key + 4, &r, 4);
219 rc4_skip(key, sizeof(key), 0,
220 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
225 if (auth_transaction != 3)
226 return WLAN_STATUS_UNSPECIFIED_FAILURE;
229 if (!iswep || !sta->challenge || !challenge ||
230 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
231 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
233 "shared key authentication - invalid "
234 "challenge-response");
235 return WLAN_STATUS_CHALLENGE_FAIL;
238 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
240 "authentication OK (shared key)");
241 #ifdef IEEE80211_REQUIRE_AUTH_ACK
242 /* Station will be marked authenticated if it ACKs the
243 * authentication reply. */
245 sta->flags |= WLAN_STA_AUTH;
246 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
248 os_free(sta->challenge);
249 sta->challenge = NULL;
255 static void send_auth_reply(struct hostapd_data *hapd,
256 const u8 *dst, const u8 *bssid,
257 u16 auth_alg, u16 auth_transaction, u16 resp,
258 const u8 *ies, size_t ies_len)
260 struct ieee80211_mgmt *reply;
264 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
265 buf = os_zalloc(rlen);
269 reply = (struct ieee80211_mgmt *) buf;
270 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
272 os_memcpy(reply->da, dst, ETH_ALEN);
273 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
274 os_memcpy(reply->bssid, bssid, ETH_ALEN);
276 reply->u.auth.auth_alg = host_to_le16(auth_alg);
277 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
278 reply->u.auth.status_code = host_to_le16(resp);
281 os_memcpy(reply->u.auth.variable, ies, ies_len);
283 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
284 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
285 MAC2STR(dst), auth_alg, auth_transaction,
286 resp, (unsigned long) ies_len);
287 if (hapd->drv.send_mgmt_frame(hapd, reply, rlen) < 0)
288 perror("send_auth_reply: send");
294 #ifdef CONFIG_IEEE80211R
295 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
296 u16 auth_transaction, u16 status,
297 const u8 *ies, size_t ies_len)
299 struct hostapd_data *hapd = ctx;
300 struct sta_info *sta;
302 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
303 status, ies, ies_len);
305 if (status != WLAN_STATUS_SUCCESS)
308 sta = ap_get_sta(hapd, dst);
312 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
313 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
314 sta->flags |= WLAN_STA_AUTH;
315 mlme_authenticate_indication(hapd, sta);
317 #endif /* CONFIG_IEEE80211R */
320 static void handle_auth(struct hostapd_data *hapd,
321 const struct ieee80211_mgmt *mgmt, size_t len)
323 u16 auth_alg, auth_transaction, status_code;
324 u16 resp = WLAN_STATUS_SUCCESS;
325 struct sta_info *sta = NULL;
328 const u8 *challenge = NULL;
329 u32 session_timeout, acct_interim_interval;
331 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
332 size_t resp_ies_len = 0;
334 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
335 printf("handle_auth - too short payload (len=%lu)\n",
336 (unsigned long) len);
340 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
341 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
342 status_code = le_to_host16(mgmt->u.auth.status_code);
343 fc = le_to_host16(mgmt->frame_control);
345 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
346 2 + WLAN_AUTH_CHALLENGE_LEN &&
347 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
348 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
349 challenge = &mgmt->u.auth.variable[2];
351 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
352 "auth_transaction=%d status_code=%d wep=%d%s",
353 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
354 status_code, !!(fc & WLAN_FC_ISWEP),
355 challenge ? " challenge" : "");
357 if (hapd->tkip_countermeasures) {
358 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
362 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
363 auth_alg == WLAN_AUTH_OPEN) ||
364 #ifdef CONFIG_IEEE80211R
366 (hapd->conf->wpa_key_mgmt &
367 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
368 auth_alg == WLAN_AUTH_FT) ||
369 #endif /* CONFIG_IEEE80211R */
370 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
371 auth_alg == WLAN_AUTH_SHARED_KEY))) {
372 printf("Unsupported authentication algorithm (%d)\n",
374 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
378 if (!(auth_transaction == 1 ||
379 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
380 printf("Unknown authentication transaction number (%d)\n",
382 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
386 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
387 printf("Station " MACSTR " not allowed to authenticate.\n",
389 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
393 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
395 &acct_interim_interval, &vlan_id);
396 if (res == HOSTAPD_ACL_REJECT) {
397 printf("Station " MACSTR " not allowed to authenticate.\n",
399 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
402 if (res == HOSTAPD_ACL_PENDING) {
403 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
404 " waiting for an external authentication",
406 /* Authentication code will re-send the authentication frame
407 * after it has received (and cached) information from the
408 * external source. */
412 sta = ap_sta_add(hapd, mgmt->sa);
414 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
419 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
421 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
422 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
423 "%d received from RADIUS server",
425 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
428 sta->vlan_id = vlan_id;
429 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
430 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
433 sta->flags &= ~WLAN_STA_PREAUTH;
434 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
436 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
437 sta->acct_interim_interval = acct_interim_interval;
438 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
439 ap_sta_session_timeout(hapd, sta, session_timeout);
441 ap_sta_no_session_timeout(hapd, sta);
445 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
447 "authentication OK (open system)");
448 #ifdef IEEE80211_REQUIRE_AUTH_ACK
449 /* Station will be marked authenticated if it ACKs the
450 * authentication reply. */
452 sta->flags |= WLAN_STA_AUTH;
453 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
454 sta->auth_alg = WLAN_AUTH_OPEN;
455 mlme_authenticate_indication(hapd, sta);
458 case WLAN_AUTH_SHARED_KEY:
459 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
461 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
462 mlme_authenticate_indication(hapd, sta);
463 if (sta->challenge && auth_transaction == 1) {
464 resp_ies[0] = WLAN_EID_CHALLENGE;
465 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
466 os_memcpy(resp_ies + 2, sta->challenge,
467 WLAN_AUTH_CHALLENGE_LEN);
468 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
471 #ifdef CONFIG_IEEE80211R
473 sta->auth_alg = WLAN_AUTH_FT;
474 if (sta->wpa_sm == NULL)
475 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
477 if (sta->wpa_sm == NULL) {
478 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
480 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
483 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
484 auth_transaction, mgmt->u.auth.variable,
485 len - IEEE80211_HDRLEN -
486 sizeof(mgmt->u.auth),
487 handle_auth_ft_finish, hapd);
488 /* handle_auth_ft_finish() callback will complete auth. */
490 #endif /* CONFIG_IEEE80211R */
494 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
495 auth_transaction + 1, resp, resp_ies, resp_ies_len);
499 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
503 /* get a unique AID */
505 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
509 for (i = 0; i < AID_WORDS; i++) {
510 if (hapd->sta_aid[i] == (u32) -1)
512 for (j = 0; j < 32; j++) {
513 if (!(hapd->sta_aid[i] & BIT(j)))
521 aid = i * 32 + j + 1;
526 hapd->sta_aid[i] |= BIT(j);
527 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
532 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
533 const u8 *ssid_ie, size_t ssid_ie_len)
536 return WLAN_STATUS_UNSPECIFIED_FAILURE;
538 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
539 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
541 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
542 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
544 "Station tried to associate with unknown SSID "
546 return WLAN_STATUS_UNSPECIFIED_FAILURE;
549 return WLAN_STATUS_SUCCESS;
553 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
554 const u8 *wmm_ie, size_t wmm_ie_len)
556 sta->flags &= ~WLAN_STA_WMM;
557 if (wmm_ie && hapd->conf->wmm_enabled) {
558 if (hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len))
559 hostapd_logger(hapd, sta->addr,
562 "invalid WMM element in association "
565 sta->flags |= WLAN_STA_WMM;
567 return WLAN_STATUS_SUCCESS;
571 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
572 struct ieee802_11_elems *elems)
574 if (!elems->supp_rates) {
575 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
577 "No supported rates element in AssocReq");
578 return WLAN_STATUS_UNSPECIFIED_FAILURE;
581 if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
582 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
584 "Invalid supported rates element length %d",
585 elems->supp_rates_len);
586 return WLAN_STATUS_UNSPECIFIED_FAILURE;
589 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
590 os_memcpy(sta->supported_rates, elems->supp_rates,
591 elems->supp_rates_len);
592 sta->supported_rates_len = elems->supp_rates_len;
594 if (elems->ext_supp_rates) {
595 if (elems->supp_rates_len + elems->ext_supp_rates_len >
596 sizeof(sta->supported_rates)) {
597 hostapd_logger(hapd, sta->addr,
598 HOSTAPD_MODULE_IEEE80211,
600 "Invalid supported rates element length"
601 " %d+%d", elems->supp_rates_len,
602 elems->ext_supp_rates_len);
603 return WLAN_STATUS_UNSPECIFIED_FAILURE;
606 os_memcpy(sta->supported_rates + elems->supp_rates_len,
607 elems->ext_supp_rates, elems->ext_supp_rates_len);
608 sta->supported_rates_len += elems->ext_supp_rates_len;
611 return WLAN_STATUS_SUCCESS;
615 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
616 const u8 *ies, size_t ies_len, int reassoc)
618 struct ieee802_11_elems elems;
623 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
624 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
625 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
626 "association request");
627 return WLAN_STATUS_UNSPECIFIED_FAILURE;
630 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
631 if (resp != WLAN_STATUS_SUCCESS)
633 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
634 if (resp != WLAN_STATUS_SUCCESS)
636 resp = copy_supp_rates(hapd, sta, &elems);
637 if (resp != WLAN_STATUS_SUCCESS)
639 #ifdef CONFIG_IEEE80211N
640 resp = copy_sta_ht_capab(sta, elems.ht_capabilities,
641 elems.ht_capabilities_len);
642 if (resp != WLAN_STATUS_SUCCESS)
644 #endif /* CONFIG_IEEE80211N */
646 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
647 wpa_ie = elems.rsn_ie;
648 wpa_ie_len = elems.rsn_ie_len;
649 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
651 wpa_ie = elems.wpa_ie;
652 wpa_ie_len = elems.wpa_ie_len;
659 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
660 if (hapd->conf->wps_state && elems.wps_ie) {
661 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
662 "Request - assume WPS is used");
663 sta->flags |= WLAN_STA_WPS;
664 wpabuf_free(sta->wps_ie);
665 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
669 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
670 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
671 "(Re)Association Request - reject");
672 return WLAN_STATUS_INVALID_IE;
674 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
675 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
676 "(Re)Association Request - possible WPS use");
677 sta->flags |= WLAN_STA_MAYBE_WPS;
679 #endif /* CONFIG_WPS */
680 if (hapd->conf->wpa && wpa_ie == NULL) {
681 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
683 "No WPA/RSN IE in association request");
684 return WLAN_STATUS_INVALID_IE;
687 if (hapd->conf->wpa && wpa_ie) {
691 if (sta->wpa_sm == NULL)
692 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
694 if (sta->wpa_sm == NULL) {
695 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
697 return WLAN_STATUS_UNSPECIFIED_FAILURE;
699 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
701 elems.mdie, elems.mdie_len);
702 if (res == WPA_INVALID_GROUP)
703 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
704 else if (res == WPA_INVALID_PAIRWISE)
705 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
706 else if (res == WPA_INVALID_AKMP)
707 resp = WLAN_STATUS_AKMP_NOT_VALID;
708 else if (res == WPA_ALLOC_FAIL)
709 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
710 #ifdef CONFIG_IEEE80211W
711 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
712 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
713 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
714 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
715 #endif /* CONFIG_IEEE80211W */
716 else if (res == WPA_INVALID_MDIE)
717 resp = WLAN_STATUS_INVALID_MDIE;
718 else if (res != WPA_IE_OK)
719 resp = WLAN_STATUS_INVALID_IE;
720 if (resp != WLAN_STATUS_SUCCESS)
722 #ifdef CONFIG_IEEE80211W
723 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
724 sta->sa_query_count > 0)
725 ap_check_sa_query_timeout(hapd, sta);
726 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
727 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
729 * STA has already been associated with MFP and SA
730 * Query timeout has not been reached. Reject the
731 * association attempt temporarily and start SA Query,
732 * if one is not pending.
735 if (sta->sa_query_count == 0)
736 ap_sta_start_sa_query(hapd, sta);
738 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
741 if (wpa_auth_uses_mfp(sta->wpa_sm))
742 sta->flags |= WLAN_STA_MFP;
744 sta->flags &= ~WLAN_STA_MFP;
745 #endif /* CONFIG_IEEE80211W */
747 #ifdef CONFIG_IEEE80211R
748 if (sta->auth_alg == WLAN_AUTH_FT) {
750 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
751 "to use association (not "
752 "re-association) with FT auth_alg",
754 return WLAN_STATUS_UNSPECIFIED_FAILURE;
757 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
759 if (resp != WLAN_STATUS_SUCCESS)
762 #endif /* CONFIG_IEEE80211R */
764 #ifdef CONFIG_IEEE80211N
765 if ((sta->flags & WLAN_STA_HT) &&
766 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
767 hostapd_logger(hapd, sta->addr,
768 HOSTAPD_MODULE_IEEE80211,
770 "Station tried to use TKIP with HT "
772 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
774 #endif /* CONFIG_IEEE80211N */
776 wpa_auth_sta_no_wpa(sta->wpa_sm);
780 wpabuf_free(sta->p2p_ie);
781 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
784 if (p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
786 wpa_printf(MSG_DEBUG, "P2P: Invalid P2P IE in "
787 "(Re)Association Request frame from "
788 MACSTR, MAC2STR(sta->addr));
789 return WLAN_STATUS_UNSPECIFIED_FAILURE;
792 wpabuf_free(sta->p2p_ie);
795 #endif /* CONFIG_P2P */
797 return WLAN_STATUS_SUCCESS;
801 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
805 struct ieee80211_mgmt reply;
807 os_memset(&reply, 0, sizeof(reply));
808 reply.frame_control =
809 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
810 os_memcpy(reply.da, addr, ETH_ALEN);
811 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
812 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
814 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
815 reply.u.deauth.reason_code = host_to_le16(reason_code);
817 if (hapd->drv.send_mgmt_frame(hapd, &reply, send_len) < 0)
818 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
823 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
824 u16 status_code, int reassoc, const u8 *ies,
828 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
829 struct ieee80211_mgmt *reply;
832 os_memset(buf, 0, sizeof(buf));
833 reply = (struct ieee80211_mgmt *) buf;
834 reply->frame_control =
835 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
836 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
837 WLAN_FC_STYPE_ASSOC_RESP));
838 os_memcpy(reply->da, sta->addr, ETH_ALEN);
839 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
840 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
842 send_len = IEEE80211_HDRLEN;
843 send_len += sizeof(reply->u.assoc_resp);
844 reply->u.assoc_resp.capab_info =
845 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
846 reply->u.assoc_resp.status_code = host_to_le16(status_code);
847 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
848 | BIT(14) | BIT(15));
849 /* Supported rates */
850 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
851 /* Extended supported rates */
852 p = hostapd_eid_ext_supp_rates(hapd, p);
854 #ifdef CONFIG_IEEE80211R
855 if (status_code == WLAN_STATUS_SUCCESS) {
856 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
857 * Transition Information, RSN, [RIC Response] */
858 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
859 buf + sizeof(buf) - p,
860 sta->auth_alg, ies, ies_len);
862 #endif /* CONFIG_IEEE80211R */
864 #ifdef CONFIG_IEEE80211W
865 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
866 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
867 #endif /* CONFIG_IEEE80211W */
869 #ifdef CONFIG_IEEE80211N
870 p = hostapd_eid_ht_capabilities(hapd, p);
871 p = hostapd_eid_ht_operation(hapd, p);
872 #endif /* CONFIG_IEEE80211N */
874 if (sta->flags & WLAN_STA_WMM)
875 p = hostapd_eid_wmm(hapd, p);
878 if (sta->flags & WLAN_STA_WPS) {
879 struct wpabuf *wps = wps_build_assoc_resp_ie();
881 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
882 p += wpabuf_len(wps);
886 #endif /* CONFIG_WPS */
890 struct wpabuf *p2p_resp_ie;
891 enum p2p_status_code status;
892 switch (status_code) {
893 case WLAN_STATUS_SUCCESS:
894 status = P2P_SC_SUCCESS;
896 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
897 status = P2P_SC_FAIL_LIMIT_REACHED;
900 status = P2P_SC_FAIL_INVALID_PARAMS;
903 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
905 os_memcpy(p, wpabuf_head(p2p_resp_ie),
906 wpabuf_len(p2p_resp_ie));
907 p += wpabuf_len(p2p_resp_ie);
908 wpabuf_free(p2p_resp_ie);
911 #endif /* CONFIG_P2P */
913 #ifdef CONFIG_P2P_MANAGER
914 if (hapd->conf->p2p & P2P_MANAGE)
915 p = hostapd_eid_p2p_manage(hapd, p);
916 #endif /* CONFIG_P2P_MANAGER */
918 send_len += p - reply->u.assoc_resp.variable;
920 if (hapd->drv.send_mgmt_frame(hapd, reply, send_len) < 0)
921 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
926 static void handle_assoc(struct hostapd_data *hapd,
927 const struct ieee80211_mgmt *mgmt, size_t len,
930 u16 capab_info, listen_interval;
931 u16 resp = WLAN_STATUS_SUCCESS;
934 struct sta_info *sta;
936 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
937 sizeof(mgmt->u.assoc_req))) {
938 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
939 "\n", reassoc, (unsigned long) len);
944 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
945 listen_interval = le_to_host16(
946 mgmt->u.reassoc_req.listen_interval);
947 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
948 " capab_info=0x%02x listen_interval=%d current_ap="
950 MAC2STR(mgmt->sa), capab_info, listen_interval,
951 MAC2STR(mgmt->u.reassoc_req.current_ap));
952 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
953 pos = mgmt->u.reassoc_req.variable;
955 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
956 listen_interval = le_to_host16(
957 mgmt->u.assoc_req.listen_interval);
958 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
959 " capab_info=0x%02x listen_interval=%d",
960 MAC2STR(mgmt->sa), capab_info, listen_interval);
961 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
962 pos = mgmt->u.assoc_req.variable;
965 sta = ap_get_sta(hapd, mgmt->sa);
966 #ifdef CONFIG_IEEE80211R
967 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
968 (sta->flags & WLAN_STA_AUTH) == 0) {
969 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
970 "prior to authentication since it is using "
971 "over-the-DS FT", MAC2STR(mgmt->sa));
973 #endif /* CONFIG_IEEE80211R */
974 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
975 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
976 HOSTAPD_LEVEL_INFO, "Station tried to "
977 "associate before authentication "
978 "(aid=%d flags=0x%x)",
980 sta ? sta->flags : 0);
981 send_deauth(hapd, mgmt->sa,
982 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
986 if (hapd->tkip_countermeasures) {
987 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
991 if (listen_interval > hapd->conf->max_listen_interval) {
992 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
994 "Too large Listen Interval (%d)",
996 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1000 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1002 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1003 if (resp != WLAN_STATUS_SUCCESS)
1006 if (hostapd_get_aid(hapd, sta) < 0) {
1007 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1008 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1009 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1013 sta->capability = capab_info;
1014 sta->listen_interval = listen_interval;
1016 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1017 sta->flags |= WLAN_STA_NONERP;
1018 for (i = 0; i < sta->supported_rates_len; i++) {
1019 if ((sta->supported_rates[i] & 0x7f) > 22) {
1020 sta->flags &= ~WLAN_STA_NONERP;
1024 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1025 sta->nonerp_set = 1;
1026 hapd->iface->num_sta_non_erp++;
1027 if (hapd->iface->num_sta_non_erp == 1)
1028 ieee802_11_set_beacons(hapd->iface);
1031 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1032 !sta->no_short_slot_time_set) {
1033 sta->no_short_slot_time_set = 1;
1034 hapd->iface->num_sta_no_short_slot_time++;
1035 if (hapd->iface->current_mode->mode ==
1036 HOSTAPD_MODE_IEEE80211G &&
1037 hapd->iface->num_sta_no_short_slot_time == 1)
1038 ieee802_11_set_beacons(hapd->iface);
1041 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1042 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1044 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1046 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1047 !sta->no_short_preamble_set) {
1048 sta->no_short_preamble_set = 1;
1049 hapd->iface->num_sta_no_short_preamble++;
1050 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1051 && hapd->iface->num_sta_no_short_preamble == 1)
1052 ieee802_11_set_beacons(hapd->iface);
1055 #ifdef CONFIG_IEEE80211N
1056 update_ht_state(hapd, sta);
1057 #endif /* CONFIG_IEEE80211N */
1059 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1060 HOSTAPD_LEVEL_DEBUG,
1061 "association OK (aid %d)", sta->aid);
1062 /* Station will be marked associated, after it acknowledges AssocResp
1065 #ifdef CONFIG_IEEE80211W
1066 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1067 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1068 "SA Query procedure", reassoc ? "re" : "");
1069 /* TODO: Send a protected Disassociate frame to the STA using
1070 * the old key and Reason Code "Previous Authentication no
1071 * longer valid". Make sure this is only sent protected since
1072 * unprotected frame would be received by the STA that is now
1073 * trying to associate.
1076 #endif /* CONFIG_IEEE80211W */
1079 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1083 if (sta->last_assoc_req)
1084 os_free(sta->last_assoc_req);
1085 sta->last_assoc_req = os_malloc(len);
1086 if (sta->last_assoc_req)
1087 os_memcpy(sta->last_assoc_req, mgmt, len);
1089 /* Make sure that the previously registered inactivity timer will not
1090 * remove the STA immediately. */
1091 sta->timeout_next = STA_NULLFUNC;
1094 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1098 static void handle_disassoc(struct hostapd_data *hapd,
1099 const struct ieee80211_mgmt *mgmt, size_t len)
1101 struct sta_info *sta;
1103 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1104 printf("handle_disassoc - too short payload (len=%lu)\n",
1105 (unsigned long) len);
1109 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1111 le_to_host16(mgmt->u.disassoc.reason_code));
1113 sta = ap_get_sta(hapd, mgmt->sa);
1115 printf("Station " MACSTR " trying to disassociate, but it "
1116 "is not associated.\n", MAC2STR(mgmt->sa));
1120 sta->flags &= ~WLAN_STA_ASSOC;
1121 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1122 MAC2STR(sta->addr));
1123 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1124 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1125 HOSTAPD_LEVEL_INFO, "disassociated");
1126 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1127 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1128 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1130 accounting_sta_stop(hapd, sta);
1131 ieee802_1x_free_station(sta);
1132 hapd->drv.sta_remove(hapd, sta->addr);
1134 if (sta->timeout_next == STA_NULLFUNC ||
1135 sta->timeout_next == STA_DISASSOC) {
1136 sta->timeout_next = STA_DEAUTH;
1137 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1138 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1142 mlme_disassociate_indication(
1143 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1147 static void handle_deauth(struct hostapd_data *hapd,
1148 const struct ieee80211_mgmt *mgmt, size_t len)
1150 struct sta_info *sta;
1152 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1153 printf("handle_deauth - too short payload (len=%lu)\n",
1154 (unsigned long) len);
1158 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1161 le_to_host16(mgmt->u.deauth.reason_code));
1163 sta = ap_get_sta(hapd, mgmt->sa);
1165 printf("Station " MACSTR " trying to deauthenticate, but it "
1166 "is not authenticated.\n", MAC2STR(mgmt->sa));
1170 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1171 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1172 MAC2STR(sta->addr));
1173 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1174 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1175 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1176 mlme_deauthenticate_indication(
1177 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1178 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1179 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1180 ap_free_sta(hapd, sta);
1184 static void handle_beacon(struct hostapd_data *hapd,
1185 const struct ieee80211_mgmt *mgmt, size_t len,
1186 struct hostapd_frame_info *fi)
1188 struct ieee802_11_elems elems;
1190 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1191 printf("handle_beacon - too short payload (len=%lu)\n",
1192 (unsigned long) len);
1196 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1197 len - (IEEE80211_HDRLEN +
1198 sizeof(mgmt->u.beacon)), &elems,
1201 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1205 #ifdef CONFIG_IEEE80211W
1207 /* MLME-SAQuery.request */
1208 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1209 const u8 *addr, const u8 *trans_id)
1211 struct ieee80211_mgmt mgmt;
1214 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
1215 MACSTR, MAC2STR(addr));
1216 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1217 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1219 os_memset(&mgmt, 0, sizeof(mgmt));
1220 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1221 WLAN_FC_STYPE_ACTION);
1222 os_memcpy(mgmt.da, addr, ETH_ALEN);
1223 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1224 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1225 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1226 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1227 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1228 WLAN_SA_QUERY_TR_ID_LEN);
1229 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1230 if (hapd->drv.send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
1231 perror("ieee802_11_send_sa_query_req: send");
1235 static void hostapd_sa_query_request(struct hostapd_data *hapd,
1236 const struct ieee80211_mgmt *mgmt)
1238 struct sta_info *sta;
1239 struct ieee80211_mgmt resp;
1242 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
1243 MACSTR, MAC2STR(mgmt->sa));
1244 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1245 mgmt->u.action.u.sa_query_resp.trans_id,
1246 WLAN_SA_QUERY_TR_ID_LEN);
1248 sta = ap_get_sta(hapd, mgmt->sa);
1249 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1250 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
1251 "from unassociated STA " MACSTR, MAC2STR(mgmt->sa));
1255 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
1256 MACSTR, MAC2STR(mgmt->sa));
1258 os_memset(&resp, 0, sizeof(resp));
1259 resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1260 WLAN_FC_STYPE_ACTION);
1261 os_memcpy(resp.da, mgmt->sa, ETH_ALEN);
1262 os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
1263 os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
1264 resp.u.action.category = WLAN_ACTION_SA_QUERY;
1265 resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
1266 os_memcpy(resp.u.action.u.sa_query_req.trans_id,
1267 mgmt->u.action.u.sa_query_req.trans_id,
1268 WLAN_SA_QUERY_TR_ID_LEN);
1269 end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1270 if (hapd->drv.send_mgmt_frame(hapd, &resp, end - (u8 *) &resp) < 0)
1271 perror("hostapd_sa_query_request: send");
1275 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1276 const struct ieee80211_mgmt *mgmt,
1279 struct sta_info *sta;
1283 end = mgmt->u.action.u.sa_query_resp.trans_id +
1284 WLAN_SA_QUERY_TR_ID_LEN;
1285 if (((u8 *) mgmt) + len < end) {
1286 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1287 "frame (len=%lu)", (unsigned long) len);
1291 if (mgmt->u.action.u.sa_query_resp.action == WLAN_SA_QUERY_REQUEST) {
1292 hostapd_sa_query_request(hapd, mgmt);
1296 if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1297 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1298 "Action %d", mgmt->u.action.u.sa_query_resp.action);
1302 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
1303 MACSTR, MAC2STR(mgmt->sa));
1304 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1305 mgmt->u.action.u.sa_query_resp.trans_id,
1306 WLAN_SA_QUERY_TR_ID_LEN);
1308 /* MLME-SAQuery.confirm */
1310 sta = ap_get_sta(hapd, mgmt->sa);
1311 if (sta == NULL || sta->sa_query_trans_id == NULL) {
1312 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1313 "pending SA Query request found");
1317 for (i = 0; i < sta->sa_query_count; i++) {
1318 if (os_memcmp(sta->sa_query_trans_id +
1319 i * WLAN_SA_QUERY_TR_ID_LEN,
1320 mgmt->u.action.u.sa_query_resp.trans_id,
1321 WLAN_SA_QUERY_TR_ID_LEN) == 0)
1325 if (i >= sta->sa_query_count) {
1326 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1327 "transaction identifier found");
1331 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1332 HOSTAPD_LEVEL_DEBUG,
1333 "Reply to pending SA Query received");
1334 ap_sta_stop_sa_query(hapd, sta);
1338 static int robust_action_frame(u8 category)
1340 return category != WLAN_ACTION_PUBLIC &&
1341 category != WLAN_ACTION_HT;
1343 #endif /* CONFIG_IEEE80211W */
1346 static void handle_action(struct hostapd_data *hapd,
1347 const struct ieee80211_mgmt *mgmt, size_t len)
1349 struct sta_info *sta;
1351 if (len < IEEE80211_HDRLEN + 1) {
1352 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1353 HOSTAPD_LEVEL_DEBUG,
1354 "handle_action - too short payload (len=%lu)",
1355 (unsigned long) len);
1359 sta = ap_get_sta(hapd, mgmt->sa);
1360 #ifdef CONFIG_IEEE80211W
1361 if (sta && (sta->flags & WLAN_STA_MFP) &&
1362 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1363 robust_action_frame(mgmt->u.action.category))) {
1364 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1365 HOSTAPD_LEVEL_DEBUG,
1366 "Dropped unprotected Robust Action frame from "
1370 #endif /* CONFIG_IEEE80211W */
1372 switch (mgmt->u.action.category) {
1373 #ifdef CONFIG_IEEE80211R
1374 case WLAN_ACTION_FT:
1376 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1377 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1378 "frame from unassociated STA " MACSTR,
1383 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1384 len - IEEE80211_HDRLEN))
1389 #endif /* CONFIG_IEEE80211R */
1390 case WLAN_ACTION_WMM:
1391 hostapd_wmm_action(hapd, mgmt, len);
1393 #ifdef CONFIG_IEEE80211W
1394 case WLAN_ACTION_SA_QUERY:
1395 hostapd_sa_query_action(hapd, mgmt, len);
1397 #endif /* CONFIG_IEEE80211W */
1398 case WLAN_ACTION_PUBLIC:
1399 if (hapd->public_action_cb) {
1400 hapd->public_action_cb(hapd->public_action_cb_ctx,
1406 case WLAN_ACTION_VENDOR_SPECIFIC:
1407 if (hapd->vendor_action_cb) {
1408 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1410 hapd->iface->freq) == 0)
1416 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1417 HOSTAPD_LEVEL_DEBUG,
1418 "handle_action - unknown action category %d or invalid "
1420 mgmt->u.action.category);
1421 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1422 !(mgmt->sa[0] & 0x01)) {
1423 struct ieee80211_mgmt *resp;
1426 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1427 * Return the Action frame to the source without change
1428 * except that MSB of the Category set to 1.
1430 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1431 "frame back to sender");
1432 resp = os_malloc(len);
1435 os_memcpy(resp, mgmt, len);
1436 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1437 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1438 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1439 resp->u.action.category |= 0x80;
1441 hapd->drv.send_mgmt_frame(hapd, resp, len);
1448 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1449 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1451 * @buf: management frame data (starting from IEEE 802.11 header)
1452 * @len: length of frame data in octets
1453 * @fi: meta data about received frame (signal level, etc.)
1455 * Process all incoming IEEE 802.11 management frames. This will be called for
1456 * each frame received from the kernel driver through wlan#ap interface. In
1457 * addition, it can be called to re-inserted pending frames (e.g., when using
1458 * external RADIUS server as an MAC ACL).
1460 void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1461 struct hostapd_frame_info *fi)
1463 struct ieee80211_mgmt *mgmt;
1467 mgmt = (struct ieee80211_mgmt *) buf;
1468 fc = le_to_host16(mgmt->frame_control);
1469 stype = WLAN_FC_GET_STYPE(fc);
1471 if (stype == WLAN_FC_STYPE_BEACON) {
1472 handle_beacon(hapd, mgmt, len, fi);
1476 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1477 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1478 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1481 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1482 printf("MGMT: BSSID=" MACSTR " not our address\n",
1483 MAC2STR(mgmt->bssid));
1488 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1489 handle_probe_req(hapd, mgmt, len);
1493 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1494 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1495 HOSTAPD_LEVEL_DEBUG,
1496 "MGMT: DA=" MACSTR " not our address",
1502 case WLAN_FC_STYPE_AUTH:
1503 wpa_printf(MSG_DEBUG, "mgmt::auth");
1504 handle_auth(hapd, mgmt, len);
1506 case WLAN_FC_STYPE_ASSOC_REQ:
1507 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1508 handle_assoc(hapd, mgmt, len, 0);
1510 case WLAN_FC_STYPE_REASSOC_REQ:
1511 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1512 handle_assoc(hapd, mgmt, len, 1);
1514 case WLAN_FC_STYPE_DISASSOC:
1515 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1516 handle_disassoc(hapd, mgmt, len);
1518 case WLAN_FC_STYPE_DEAUTH:
1519 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1520 handle_deauth(hapd, mgmt, len);
1522 case WLAN_FC_STYPE_ACTION:
1523 wpa_printf(MSG_DEBUG, "mgmt::action");
1524 handle_action(hapd, mgmt, len);
1527 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1528 HOSTAPD_LEVEL_DEBUG,
1529 "unknown mgmt frame subtype %d", stype);
1535 static void handle_auth_cb(struct hostapd_data *hapd,
1536 const struct ieee80211_mgmt *mgmt,
1539 u16 auth_alg, auth_transaction, status_code;
1540 struct sta_info *sta;
1543 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1544 HOSTAPD_LEVEL_NOTICE,
1545 "did not acknowledge authentication response");
1549 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1550 printf("handle_auth_cb - too short payload (len=%lu)\n",
1551 (unsigned long) len);
1555 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1556 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1557 status_code = le_to_host16(mgmt->u.auth.status_code);
1559 sta = ap_get_sta(hapd, mgmt->da);
1561 printf("handle_auth_cb: STA " MACSTR " not found\n",
1566 if (status_code == WLAN_STATUS_SUCCESS &&
1567 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1568 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1569 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1570 HOSTAPD_LEVEL_INFO, "authenticated");
1571 sta->flags |= WLAN_STA_AUTH;
1576 static void handle_assoc_cb(struct hostapd_data *hapd,
1577 const struct ieee80211_mgmt *mgmt,
1578 size_t len, int reassoc, int ok)
1581 struct sta_info *sta;
1583 struct ieee80211_ht_capabilities ht_cap;
1586 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1587 HOSTAPD_LEVEL_DEBUG,
1588 "did not acknowledge association response");
1592 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1593 sizeof(mgmt->u.assoc_resp))) {
1594 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1595 "(len=%lu)\n", reassoc, (unsigned long) len);
1600 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1602 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1604 sta = ap_get_sta(hapd, mgmt->da);
1606 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1611 if (status != WLAN_STATUS_SUCCESS)
1614 /* Stop previous accounting session, if one is started, and allocate
1615 * new session id for the new session. */
1616 accounting_sta_stop(hapd, sta);
1618 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1620 "associated (aid %d)",
1623 if (sta->flags & WLAN_STA_ASSOC)
1625 sta->flags |= WLAN_STA_ASSOC;
1626 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1627 sta->auth_alg == WLAN_AUTH_FT) {
1629 * Open, static WEP, or FT protocol; no separate authorization
1632 sta->flags |= WLAN_STA_AUTHORIZED;
1633 wpa_msg(hapd->msg_ctx, MSG_INFO,
1634 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
1638 mlme_reassociate_indication(hapd, sta);
1640 mlme_associate_indication(hapd, sta);
1642 #ifdef CONFIG_IEEE80211W
1643 sta->sa_query_timed_out = 0;
1644 #endif /* CONFIG_IEEE80211W */
1647 * Remove the STA entry in order to make sure the STA PS state gets
1648 * cleared and configuration gets updated in case of reassociation back
1651 hapd->drv.sta_remove(hapd, sta->addr);
1653 #ifdef CONFIG_IEEE80211N
1654 if (sta->flags & WLAN_STA_HT)
1655 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1656 #endif /* CONFIG_IEEE80211N */
1658 if (hapd->drv.sta_add(hapd, sta->addr, sta->aid, sta->capability,
1659 sta->supported_rates, sta->supported_rates_len,
1660 sta->listen_interval,
1661 sta->flags & WLAN_STA_HT ? &ht_cap : NULL)) {
1662 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1663 HOSTAPD_LEVEL_NOTICE,
1664 "Could not add STA to kernel driver");
1667 if (sta->eapol_sm == NULL) {
1669 * This STA does not use RADIUS server for EAP authentication,
1670 * so bind it to the selected VLAN interface now, since the
1671 * interface selection is not going to change anymore.
1673 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1675 } else if (sta->vlan_id) {
1676 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1677 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1681 hapd->drv.set_sta_flags(hapd, sta);
1683 if (sta->auth_alg == WLAN_AUTH_FT)
1684 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1686 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1687 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1689 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1692 /* Copy of the association request is not needed anymore */
1693 if (sta->last_assoc_req) {
1694 os_free(sta->last_assoc_req);
1695 sta->last_assoc_req = NULL;
1701 * ieee802_11_mgmt_cb - Process management frame TX status callback
1702 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1704 * @buf: management frame data (starting from IEEE 802.11 header)
1705 * @len: length of frame data in octets
1706 * @stype: management frame subtype from frame control field
1707 * @ok: Whether the frame was ACK'ed
1709 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
1712 const struct ieee80211_mgmt *mgmt;
1713 mgmt = (const struct ieee80211_mgmt *) buf;
1716 case WLAN_FC_STYPE_AUTH:
1717 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1718 handle_auth_cb(hapd, mgmt, len, ok);
1720 case WLAN_FC_STYPE_ASSOC_RESP:
1721 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1722 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1724 case WLAN_FC_STYPE_REASSOC_RESP:
1725 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1726 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1728 case WLAN_FC_STYPE_PROBE_RESP:
1729 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
1731 case WLAN_FC_STYPE_DEAUTH:
1734 case WLAN_FC_STYPE_ACTION:
1735 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1738 printf("unknown mgmt cb frame subtype %d\n", stype);
1744 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1751 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1752 char *buf, size_t buflen)
1759 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
1760 const u8 *buf, size_t len, int ack)
1762 struct sta_info *sta;
1763 struct hostapd_iface *iface = hapd->iface;
1765 sta = ap_get_sta(hapd, addr);
1766 if (sta == NULL && iface->num_bss > 1) {
1768 for (j = 0; j < iface->num_bss; j++) {
1769 hapd = iface->bss[j];
1770 sta = ap_get_sta(hapd, addr);
1777 if (sta->flags & WLAN_STA_PENDING_POLL) {
1778 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1779 "activity poll", MAC2STR(sta->addr),
1780 ack ? "ACKed" : "did not ACK");
1782 sta->flags &= ~WLAN_STA_PENDING_POLL;
1785 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
1789 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
1792 struct sta_info *sta;
1794 sta = ap_get_sta(hapd, src);
1795 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
1796 if (wds && !(sta->flags & WLAN_STA_WDS)) {
1797 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
1798 "STA " MACSTR " (aid %u)",
1799 MAC2STR(sta->addr), sta->aid);
1800 sta->flags |= WLAN_STA_WDS;
1801 hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 1);
1806 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
1807 MACSTR, MAC2STR(src));
1808 if (sta && (sta->flags & WLAN_STA_AUTH))
1809 hapd->drv.sta_disassoc(
1811 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1813 hapd->drv.sta_deauth(
1815 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1819 #endif /* CONFIG_NATIVE_WINDOWS */