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 "ap_drv_ops.h"
43 #include "ieee802_11.h"
46 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
51 if (hapd->iface->current_rates == NULL)
54 *pos++ = WLAN_EID_SUPP_RATES;
55 num = hapd->iface->num_rates;
56 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
59 /* rest of the rates are encoded in Extended supported
66 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
69 *pos = hapd->iface->current_rates[i].rate / 5;
70 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
75 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
76 hapd->iface->num_rates < 8)
77 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
83 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
88 if (hapd->iface->current_rates == NULL)
91 num = hapd->iface->num_rates;
92 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
98 *pos++ = WLAN_EID_EXT_SUPP_RATES;
101 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
105 continue; /* already in SuppRates IE */
106 *pos = hapd->iface->current_rates[i].rate / 5;
107 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
112 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
113 hapd->iface->num_rates >= 8)
114 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
120 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
123 int capab = WLAN_CAPABILITY_ESS;
126 if (hapd->iface->num_sta_no_short_preamble == 0 &&
127 hapd->iconf->preamble == SHORT_PREAMBLE)
128 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
130 privacy = hapd->conf->ssid.wep.keys_set;
132 if (hapd->conf->ieee802_1x &&
133 (hapd->conf->default_wep_key_len ||
134 hapd->conf->individual_wep_key_len))
141 int policy, def_klen;
142 if (probe && sta->ssid_probe) {
143 policy = sta->ssid_probe->security_policy;
144 def_klen = sta->ssid_probe->wep.default_len;
146 policy = sta->ssid->security_policy;
147 def_klen = sta->ssid->wep.default_len;
149 privacy = policy != SECURITY_PLAINTEXT;
150 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
155 capab |= WLAN_CAPABILITY_PRIVACY;
157 if (hapd->iface->current_mode &&
158 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
159 hapd->iface->num_sta_no_short_slot_time == 0)
160 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
166 u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid)
170 if ((hapd->conf->tdls & (TDLS_PROHIBIT | TDLS_PROHIBIT_CHAN_SWITCH)) ==
174 *pos++ = WLAN_EID_EXT_CAPAB;
181 if (hapd->conf->tdls & TDLS_PROHIBIT)
182 *pos |= 0x40; /* Bit 38 - TDLS Prohibited */
183 if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH)
184 *pos |= 0x80; /* Bit 39 - TDLS Channel Switching Prohibited */
191 #ifdef CONFIG_IEEE80211W
192 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
193 struct sta_info *sta, u8 *eid)
197 struct os_time now, passed;
199 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
201 *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
203 os_time_sub(&now, &sta->sa_query_start, &passed);
204 tu = (passed.sec * 1000000 + passed.usec) / 1024;
205 if (hapd->conf->assoc_sa_query_max_timeout > tu)
206 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
209 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
210 timeout++; /* add some extra time for local timers */
211 WPA_PUT_LE32(pos, timeout);
216 #endif /* CONFIG_IEEE80211W */
219 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
222 if (len > HOSTAPD_MAX_SSID_LEN)
223 len = HOSTAPD_MAX_SSID_LEN;
224 for (i = 0; i < len; i++) {
225 if (ssid[i] >= 32 && ssid[i] < 127)
234 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
235 u16 auth_transaction, const u8 *challenge,
238 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
240 "authentication (shared key, transaction %d)",
243 if (auth_transaction == 1) {
244 if (!sta->challenge) {
245 /* Generate a pseudo-random challenge */
249 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
250 if (sta->challenge == NULL)
251 return WLAN_STATUS_UNSPECIFIED_FAILURE;
255 os_memcpy(key, &now.sec, 4);
256 os_memcpy(key + 4, &r, 4);
257 rc4_skip(key, sizeof(key), 0,
258 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
263 if (auth_transaction != 3)
264 return WLAN_STATUS_UNSPECIFIED_FAILURE;
267 if (!iswep || !sta->challenge || !challenge ||
268 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
269 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
271 "shared key authentication - invalid "
272 "challenge-response");
273 return WLAN_STATUS_CHALLENGE_FAIL;
276 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
278 "authentication OK (shared key)");
279 #ifdef IEEE80211_REQUIRE_AUTH_ACK
280 /* Station will be marked authenticated if it ACKs the
281 * authentication reply. */
283 sta->flags |= WLAN_STA_AUTH;
284 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
286 os_free(sta->challenge);
287 sta->challenge = NULL;
293 static void send_auth_reply(struct hostapd_data *hapd,
294 const u8 *dst, const u8 *bssid,
295 u16 auth_alg, u16 auth_transaction, u16 resp,
296 const u8 *ies, size_t ies_len)
298 struct ieee80211_mgmt *reply;
302 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
303 buf = os_zalloc(rlen);
307 reply = (struct ieee80211_mgmt *) buf;
308 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
310 os_memcpy(reply->da, dst, ETH_ALEN);
311 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
312 os_memcpy(reply->bssid, bssid, ETH_ALEN);
314 reply->u.auth.auth_alg = host_to_le16(auth_alg);
315 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
316 reply->u.auth.status_code = host_to_le16(resp);
319 os_memcpy(reply->u.auth.variable, ies, ies_len);
321 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
322 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
323 MAC2STR(dst), auth_alg, auth_transaction,
324 resp, (unsigned long) ies_len);
325 if (hostapd_drv_send_mlme(hapd, reply, rlen) < 0)
326 perror("send_auth_reply: send");
332 #ifdef CONFIG_IEEE80211R
333 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
334 u16 auth_transaction, u16 status,
335 const u8 *ies, size_t ies_len)
337 struct hostapd_data *hapd = ctx;
338 struct sta_info *sta;
340 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
341 status, ies, ies_len);
343 if (status != WLAN_STATUS_SUCCESS)
346 sta = ap_get_sta(hapd, dst);
350 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
351 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
352 sta->flags |= WLAN_STA_AUTH;
353 mlme_authenticate_indication(hapd, sta);
355 #endif /* CONFIG_IEEE80211R */
358 static void handle_auth(struct hostapd_data *hapd,
359 const struct ieee80211_mgmt *mgmt, size_t len)
361 u16 auth_alg, auth_transaction, status_code;
362 u16 resp = WLAN_STATUS_SUCCESS;
363 struct sta_info *sta = NULL;
366 const u8 *challenge = NULL;
367 u32 session_timeout, acct_interim_interval;
369 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
370 size_t resp_ies_len = 0;
372 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
373 printf("handle_auth - too short payload (len=%lu)\n",
374 (unsigned long) len);
378 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
379 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
380 status_code = le_to_host16(mgmt->u.auth.status_code);
381 fc = le_to_host16(mgmt->frame_control);
383 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
384 2 + WLAN_AUTH_CHALLENGE_LEN &&
385 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
386 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
387 challenge = &mgmt->u.auth.variable[2];
389 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
390 "auth_transaction=%d status_code=%d wep=%d%s",
391 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
392 status_code, !!(fc & WLAN_FC_ISWEP),
393 challenge ? " challenge" : "");
395 if (hapd->tkip_countermeasures) {
396 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
400 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
401 auth_alg == WLAN_AUTH_OPEN) ||
402 #ifdef CONFIG_IEEE80211R
404 (hapd->conf->wpa_key_mgmt &
405 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
406 auth_alg == WLAN_AUTH_FT) ||
407 #endif /* CONFIG_IEEE80211R */
408 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
409 auth_alg == WLAN_AUTH_SHARED_KEY))) {
410 printf("Unsupported authentication algorithm (%d)\n",
412 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
416 if (!(auth_transaction == 1 ||
417 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
418 printf("Unknown authentication transaction number (%d)\n",
420 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
424 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
425 printf("Station " MACSTR " not allowed to authenticate.\n",
427 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
431 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
433 &acct_interim_interval, &vlan_id);
434 if (res == HOSTAPD_ACL_REJECT) {
435 printf("Station " MACSTR " not allowed to authenticate.\n",
437 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
440 if (res == HOSTAPD_ACL_PENDING) {
441 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
442 " waiting for an external authentication",
444 /* Authentication code will re-send the authentication frame
445 * after it has received (and cached) information from the
446 * external source. */
450 sta = ap_sta_add(hapd, mgmt->sa);
452 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
457 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
459 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
460 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
461 "%d received from RADIUS server",
463 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
466 sta->vlan_id = vlan_id;
467 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
468 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
471 sta->flags &= ~WLAN_STA_PREAUTH;
472 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
474 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
475 sta->acct_interim_interval = acct_interim_interval;
476 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
477 ap_sta_session_timeout(hapd, sta, session_timeout);
479 ap_sta_no_session_timeout(hapd, sta);
483 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
485 "authentication OK (open system)");
486 #ifdef IEEE80211_REQUIRE_AUTH_ACK
487 /* Station will be marked authenticated if it ACKs the
488 * authentication reply. */
490 sta->flags |= WLAN_STA_AUTH;
491 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
492 sta->auth_alg = WLAN_AUTH_OPEN;
493 mlme_authenticate_indication(hapd, sta);
496 case WLAN_AUTH_SHARED_KEY:
497 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
499 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
500 mlme_authenticate_indication(hapd, sta);
501 if (sta->challenge && auth_transaction == 1) {
502 resp_ies[0] = WLAN_EID_CHALLENGE;
503 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
504 os_memcpy(resp_ies + 2, sta->challenge,
505 WLAN_AUTH_CHALLENGE_LEN);
506 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
509 #ifdef CONFIG_IEEE80211R
511 sta->auth_alg = WLAN_AUTH_FT;
512 if (sta->wpa_sm == NULL)
513 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
515 if (sta->wpa_sm == NULL) {
516 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
518 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
521 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
522 auth_transaction, mgmt->u.auth.variable,
523 len - IEEE80211_HDRLEN -
524 sizeof(mgmt->u.auth),
525 handle_auth_ft_finish, hapd);
526 /* handle_auth_ft_finish() callback will complete auth. */
528 #endif /* CONFIG_IEEE80211R */
532 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
533 auth_transaction + 1, resp, resp_ies, resp_ies_len);
537 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
541 /* get a unique AID */
543 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
547 for (i = 0; i < AID_WORDS; i++) {
548 if (hapd->sta_aid[i] == (u32) -1)
550 for (j = 0; j < 32; j++) {
551 if (!(hapd->sta_aid[i] & BIT(j)))
559 aid = i * 32 + j + 1;
564 hapd->sta_aid[i] |= BIT(j);
565 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
570 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
571 const u8 *ssid_ie, size_t ssid_ie_len)
574 return WLAN_STATUS_UNSPECIFIED_FAILURE;
576 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
577 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
579 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
580 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
582 "Station tried to associate with unknown SSID "
584 return WLAN_STATUS_UNSPECIFIED_FAILURE;
587 return WLAN_STATUS_SUCCESS;
591 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
592 const u8 *wmm_ie, size_t wmm_ie_len)
594 sta->flags &= ~WLAN_STA_WMM;
595 if (wmm_ie && hapd->conf->wmm_enabled) {
596 if (hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len))
597 hostapd_logger(hapd, sta->addr,
600 "invalid WMM element in association "
603 sta->flags |= WLAN_STA_WMM;
605 return WLAN_STATUS_SUCCESS;
609 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
610 struct ieee802_11_elems *elems)
612 if (!elems->supp_rates) {
613 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
615 "No supported rates element in AssocReq");
616 return WLAN_STATUS_UNSPECIFIED_FAILURE;
619 if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
620 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
622 "Invalid supported rates element length %d",
623 elems->supp_rates_len);
624 return WLAN_STATUS_UNSPECIFIED_FAILURE;
627 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
628 os_memcpy(sta->supported_rates, elems->supp_rates,
629 elems->supp_rates_len);
630 sta->supported_rates_len = elems->supp_rates_len;
632 if (elems->ext_supp_rates) {
633 if (elems->supp_rates_len + elems->ext_supp_rates_len >
634 sizeof(sta->supported_rates)) {
635 hostapd_logger(hapd, sta->addr,
636 HOSTAPD_MODULE_IEEE80211,
638 "Invalid supported rates element length"
639 " %d+%d", elems->supp_rates_len,
640 elems->ext_supp_rates_len);
641 return WLAN_STATUS_UNSPECIFIED_FAILURE;
644 os_memcpy(sta->supported_rates + elems->supp_rates_len,
645 elems->ext_supp_rates, elems->ext_supp_rates_len);
646 sta->supported_rates_len += elems->ext_supp_rates_len;
649 return WLAN_STATUS_SUCCESS;
653 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
654 const u8 *ies, size_t ies_len, int reassoc)
656 struct ieee802_11_elems elems;
661 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
662 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
663 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
664 "association request");
665 return WLAN_STATUS_UNSPECIFIED_FAILURE;
668 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
669 if (resp != WLAN_STATUS_SUCCESS)
671 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
672 if (resp != WLAN_STATUS_SUCCESS)
674 resp = copy_supp_rates(hapd, sta, &elems);
675 if (resp != WLAN_STATUS_SUCCESS)
677 #ifdef CONFIG_IEEE80211N
678 resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
679 elems.ht_capabilities_len);
680 if (resp != WLAN_STATUS_SUCCESS)
682 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
683 !(sta->flags & WLAN_STA_HT)) {
684 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
685 HOSTAPD_LEVEL_INFO, "Station does not support "
686 "mandatory HT PHY - reject association");
687 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
689 #endif /* CONFIG_IEEE80211N */
691 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
692 wpa_ie = elems.rsn_ie;
693 wpa_ie_len = elems.rsn_ie_len;
694 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
696 wpa_ie = elems.wpa_ie;
697 wpa_ie_len = elems.wpa_ie_len;
704 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
705 if (hapd->conf->wps_state && elems.wps_ie) {
706 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
707 "Request - assume WPS is used");
708 sta->flags |= WLAN_STA_WPS;
709 wpabuf_free(sta->wps_ie);
710 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
712 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
713 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
714 sta->flags |= WLAN_STA_WPS2;
718 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
719 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
720 "(Re)Association Request - reject");
721 return WLAN_STATUS_INVALID_IE;
723 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
724 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
725 "(Re)Association Request - possible WPS use");
726 sta->flags |= WLAN_STA_MAYBE_WPS;
728 #endif /* CONFIG_WPS */
729 if (hapd->conf->wpa && wpa_ie == NULL) {
730 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
732 "No WPA/RSN IE in association request");
733 return WLAN_STATUS_INVALID_IE;
736 if (hapd->conf->wpa && wpa_ie) {
740 if (sta->wpa_sm == NULL)
741 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
743 if (sta->wpa_sm == NULL) {
744 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
746 return WLAN_STATUS_UNSPECIFIED_FAILURE;
748 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
750 elems.mdie, elems.mdie_len);
751 if (res == WPA_INVALID_GROUP)
752 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
753 else if (res == WPA_INVALID_PAIRWISE)
754 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
755 else if (res == WPA_INVALID_AKMP)
756 resp = WLAN_STATUS_AKMP_NOT_VALID;
757 else if (res == WPA_ALLOC_FAIL)
758 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
759 #ifdef CONFIG_IEEE80211W
760 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
761 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
762 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
763 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
764 #endif /* CONFIG_IEEE80211W */
765 else if (res == WPA_INVALID_MDIE)
766 resp = WLAN_STATUS_INVALID_MDIE;
767 else if (res != WPA_IE_OK)
768 resp = WLAN_STATUS_INVALID_IE;
769 if (resp != WLAN_STATUS_SUCCESS)
771 #ifdef CONFIG_IEEE80211W
772 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
773 sta->sa_query_count > 0)
774 ap_check_sa_query_timeout(hapd, sta);
775 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
776 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
778 * STA has already been associated with MFP and SA
779 * Query timeout has not been reached. Reject the
780 * association attempt temporarily and start SA Query,
781 * if one is not pending.
784 if (sta->sa_query_count == 0)
785 ap_sta_start_sa_query(hapd, sta);
787 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
790 if (wpa_auth_uses_mfp(sta->wpa_sm))
791 sta->flags |= WLAN_STA_MFP;
793 sta->flags &= ~WLAN_STA_MFP;
794 #endif /* CONFIG_IEEE80211W */
796 #ifdef CONFIG_IEEE80211R
797 if (sta->auth_alg == WLAN_AUTH_FT) {
799 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
800 "to use association (not "
801 "re-association) with FT auth_alg",
803 return WLAN_STATUS_UNSPECIFIED_FAILURE;
806 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
808 if (resp != WLAN_STATUS_SUCCESS)
811 #endif /* CONFIG_IEEE80211R */
813 #ifdef CONFIG_IEEE80211N
814 if ((sta->flags & WLAN_STA_HT) &&
815 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
816 hostapd_logger(hapd, sta->addr,
817 HOSTAPD_MODULE_IEEE80211,
819 "Station tried to use TKIP with HT "
821 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
823 #endif /* CONFIG_IEEE80211N */
825 wpa_auth_sta_no_wpa(sta->wpa_sm);
829 wpabuf_free(sta->p2p_ie);
830 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
834 wpabuf_free(sta->p2p_ie);
838 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
839 #endif /* CONFIG_P2P */
841 return WLAN_STATUS_SUCCESS;
845 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
849 struct ieee80211_mgmt reply;
851 os_memset(&reply, 0, sizeof(reply));
852 reply.frame_control =
853 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
854 os_memcpy(reply.da, addr, ETH_ALEN);
855 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
856 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
858 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
859 reply.u.deauth.reason_code = host_to_le16(reason_code);
861 if (hostapd_drv_send_mlme(hapd, &reply, send_len) < 0)
862 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
867 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
868 u16 status_code, int reassoc, const u8 *ies,
872 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
873 struct ieee80211_mgmt *reply;
876 os_memset(buf, 0, sizeof(buf));
877 reply = (struct ieee80211_mgmt *) buf;
878 reply->frame_control =
879 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
880 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
881 WLAN_FC_STYPE_ASSOC_RESP));
882 os_memcpy(reply->da, sta->addr, ETH_ALEN);
883 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
884 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
886 send_len = IEEE80211_HDRLEN;
887 send_len += sizeof(reply->u.assoc_resp);
888 reply->u.assoc_resp.capab_info =
889 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
890 reply->u.assoc_resp.status_code = host_to_le16(status_code);
891 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
892 | BIT(14) | BIT(15));
893 /* Supported rates */
894 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
895 /* Extended supported rates */
896 p = hostapd_eid_ext_supp_rates(hapd, p);
898 #ifdef CONFIG_IEEE80211R
899 if (status_code == WLAN_STATUS_SUCCESS) {
900 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
901 * Transition Information, RSN, [RIC Response] */
902 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
903 buf + sizeof(buf) - p,
904 sta->auth_alg, ies, ies_len);
906 #endif /* CONFIG_IEEE80211R */
908 #ifdef CONFIG_IEEE80211W
909 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
910 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
911 #endif /* CONFIG_IEEE80211W */
913 #ifdef CONFIG_IEEE80211N
914 p = hostapd_eid_ht_capabilities(hapd, p);
915 p = hostapd_eid_ht_operation(hapd, p);
916 #endif /* CONFIG_IEEE80211N */
918 p = hostapd_eid_ext_capab(hapd, p);
920 if (sta->flags & WLAN_STA_WMM)
921 p = hostapd_eid_wmm(hapd, p);
924 if (sta->flags & WLAN_STA_WPS) {
925 struct wpabuf *wps = wps_build_assoc_resp_ie();
927 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
928 p += wpabuf_len(wps);
932 #endif /* CONFIG_WPS */
936 struct wpabuf *p2p_resp_ie;
937 enum p2p_status_code status;
938 switch (status_code) {
939 case WLAN_STATUS_SUCCESS:
940 status = P2P_SC_SUCCESS;
942 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
943 status = P2P_SC_FAIL_LIMIT_REACHED;
946 status = P2P_SC_FAIL_INVALID_PARAMS;
949 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
951 os_memcpy(p, wpabuf_head(p2p_resp_ie),
952 wpabuf_len(p2p_resp_ie));
953 p += wpabuf_len(p2p_resp_ie);
954 wpabuf_free(p2p_resp_ie);
957 #endif /* CONFIG_P2P */
959 #ifdef CONFIG_P2P_MANAGER
960 if (hapd->conf->p2p & P2P_MANAGE)
961 p = hostapd_eid_p2p_manage(hapd, p);
962 #endif /* CONFIG_P2P_MANAGER */
964 send_len += p - reply->u.assoc_resp.variable;
966 if (hostapd_drv_send_mlme(hapd, reply, send_len) < 0)
967 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
972 static void handle_assoc(struct hostapd_data *hapd,
973 const struct ieee80211_mgmt *mgmt, size_t len,
976 u16 capab_info, listen_interval;
977 u16 resp = WLAN_STATUS_SUCCESS;
980 struct sta_info *sta;
982 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
983 sizeof(mgmt->u.assoc_req))) {
984 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
985 "\n", reassoc, (unsigned long) len);
990 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
991 listen_interval = le_to_host16(
992 mgmt->u.reassoc_req.listen_interval);
993 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
994 " capab_info=0x%02x listen_interval=%d current_ap="
996 MAC2STR(mgmt->sa), capab_info, listen_interval,
997 MAC2STR(mgmt->u.reassoc_req.current_ap));
998 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
999 pos = mgmt->u.reassoc_req.variable;
1001 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
1002 listen_interval = le_to_host16(
1003 mgmt->u.assoc_req.listen_interval);
1004 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
1005 " capab_info=0x%02x listen_interval=%d",
1006 MAC2STR(mgmt->sa), capab_info, listen_interval);
1007 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
1008 pos = mgmt->u.assoc_req.variable;
1011 sta = ap_get_sta(hapd, mgmt->sa);
1012 #ifdef CONFIG_IEEE80211R
1013 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
1014 (sta->flags & WLAN_STA_AUTH) == 0) {
1015 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
1016 "prior to authentication since it is using "
1017 "over-the-DS FT", MAC2STR(mgmt->sa));
1019 #endif /* CONFIG_IEEE80211R */
1020 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1021 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1022 HOSTAPD_LEVEL_INFO, "Station tried to "
1023 "associate before authentication "
1024 "(aid=%d flags=0x%x)",
1025 sta ? sta->aid : -1,
1026 sta ? sta->flags : 0);
1027 send_deauth(hapd, mgmt->sa,
1028 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
1032 if (hapd->tkip_countermeasures) {
1033 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1037 if (listen_interval > hapd->conf->max_listen_interval) {
1038 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1039 HOSTAPD_LEVEL_DEBUG,
1040 "Too large Listen Interval (%d)",
1042 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1046 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1048 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1049 if (resp != WLAN_STATUS_SUCCESS)
1052 if (hostapd_get_aid(hapd, sta) < 0) {
1053 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1054 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1055 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1059 sta->capability = capab_info;
1060 sta->listen_interval = listen_interval;
1062 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1063 sta->flags |= WLAN_STA_NONERP;
1064 for (i = 0; i < sta->supported_rates_len; i++) {
1065 if ((sta->supported_rates[i] & 0x7f) > 22) {
1066 sta->flags &= ~WLAN_STA_NONERP;
1070 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1071 sta->nonerp_set = 1;
1072 hapd->iface->num_sta_non_erp++;
1073 if (hapd->iface->num_sta_non_erp == 1)
1074 ieee802_11_set_beacons(hapd->iface);
1077 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1078 !sta->no_short_slot_time_set) {
1079 sta->no_short_slot_time_set = 1;
1080 hapd->iface->num_sta_no_short_slot_time++;
1081 if (hapd->iface->current_mode->mode ==
1082 HOSTAPD_MODE_IEEE80211G &&
1083 hapd->iface->num_sta_no_short_slot_time == 1)
1084 ieee802_11_set_beacons(hapd->iface);
1087 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1088 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1090 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1092 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1093 !sta->no_short_preamble_set) {
1094 sta->no_short_preamble_set = 1;
1095 hapd->iface->num_sta_no_short_preamble++;
1096 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1097 && hapd->iface->num_sta_no_short_preamble == 1)
1098 ieee802_11_set_beacons(hapd->iface);
1101 #ifdef CONFIG_IEEE80211N
1102 update_ht_state(hapd, sta);
1103 #endif /* CONFIG_IEEE80211N */
1105 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1106 HOSTAPD_LEVEL_DEBUG,
1107 "association OK (aid %d)", sta->aid);
1108 /* Station will be marked associated, after it acknowledges AssocResp
1110 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
1112 #ifdef CONFIG_IEEE80211W
1113 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1114 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1115 "SA Query procedure", reassoc ? "re" : "");
1116 /* TODO: Send a protected Disassociate frame to the STA using
1117 * the old key and Reason Code "Previous Authentication no
1118 * longer valid". Make sure this is only sent protected since
1119 * unprotected frame would be received by the STA that is now
1120 * trying to associate.
1123 #endif /* CONFIG_IEEE80211W */
1126 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1130 if (sta->last_assoc_req)
1131 os_free(sta->last_assoc_req);
1132 sta->last_assoc_req = os_malloc(len);
1133 if (sta->last_assoc_req)
1134 os_memcpy(sta->last_assoc_req, mgmt, len);
1136 /* Make sure that the previously registered inactivity timer will not
1137 * remove the STA immediately. */
1138 sta->timeout_next = STA_NULLFUNC;
1141 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1145 static void handle_disassoc(struct hostapd_data *hapd,
1146 const struct ieee80211_mgmt *mgmt, size_t len)
1148 struct sta_info *sta;
1150 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1151 printf("handle_disassoc - too short payload (len=%lu)\n",
1152 (unsigned long) len);
1156 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1158 le_to_host16(mgmt->u.disassoc.reason_code));
1160 sta = ap_get_sta(hapd, mgmt->sa);
1162 printf("Station " MACSTR " trying to disassociate, but it "
1163 "is not associated.\n", MAC2STR(mgmt->sa));
1167 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
1168 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1169 MAC2STR(sta->addr));
1170 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1171 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1172 HOSTAPD_LEVEL_INFO, "disassociated");
1173 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1174 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1175 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1177 accounting_sta_stop(hapd, sta);
1178 ieee802_1x_free_station(sta);
1179 hostapd_drv_sta_remove(hapd, sta->addr);
1181 if (sta->timeout_next == STA_NULLFUNC ||
1182 sta->timeout_next == STA_DISASSOC) {
1183 sta->timeout_next = STA_DEAUTH;
1184 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1185 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1189 mlme_disassociate_indication(
1190 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1194 static void handle_deauth(struct hostapd_data *hapd,
1195 const struct ieee80211_mgmt *mgmt, size_t len)
1197 struct sta_info *sta;
1199 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1200 wpa_msg(hapd, MSG_DEBUG, "handle_deauth - too short payload "
1201 "(len=%lu)", (unsigned long) len);
1205 wpa_msg(hapd, MSG_DEBUG, "deauthentication: STA=" MACSTR
1207 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
1209 sta = ap_get_sta(hapd, mgmt->sa);
1211 wpa_msg(hapd, MSG_DEBUG, "Station " MACSTR " trying to "
1212 "deauthenticate, but it is not authenticated",
1217 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1218 WLAN_STA_ASSOC_REQ_OK);
1219 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1220 MAC2STR(sta->addr));
1221 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1222 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1223 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1224 mlme_deauthenticate_indication(
1225 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1226 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1227 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1228 ap_free_sta(hapd, sta);
1232 static void handle_beacon(struct hostapd_data *hapd,
1233 const struct ieee80211_mgmt *mgmt, size_t len,
1234 struct hostapd_frame_info *fi)
1236 struct ieee802_11_elems elems;
1238 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1239 printf("handle_beacon - too short payload (len=%lu)\n",
1240 (unsigned long) len);
1244 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1245 len - (IEEE80211_HDRLEN +
1246 sizeof(mgmt->u.beacon)), &elems,
1249 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1253 #ifdef CONFIG_IEEE80211W
1255 /* MLME-SAQuery.request */
1256 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1257 const u8 *addr, const u8 *trans_id)
1259 struct ieee80211_mgmt mgmt;
1262 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
1263 MACSTR, MAC2STR(addr));
1264 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1265 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1267 os_memset(&mgmt, 0, sizeof(mgmt));
1268 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1269 WLAN_FC_STYPE_ACTION);
1270 os_memcpy(mgmt.da, addr, ETH_ALEN);
1271 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1272 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1273 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1274 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1275 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1276 WLAN_SA_QUERY_TR_ID_LEN);
1277 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1278 if (hostapd_drv_send_mlme(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
1279 perror("ieee802_11_send_sa_query_req: send");
1283 static void hostapd_sa_query_request(struct hostapd_data *hapd,
1284 const struct ieee80211_mgmt *mgmt)
1286 struct sta_info *sta;
1287 struct ieee80211_mgmt resp;
1290 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
1291 MACSTR, MAC2STR(mgmt->sa));
1292 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1293 mgmt->u.action.u.sa_query_resp.trans_id,
1294 WLAN_SA_QUERY_TR_ID_LEN);
1296 sta = ap_get_sta(hapd, mgmt->sa);
1297 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1298 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
1299 "from unassociated STA " MACSTR, MAC2STR(mgmt->sa));
1303 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
1304 MACSTR, MAC2STR(mgmt->sa));
1306 os_memset(&resp, 0, sizeof(resp));
1307 resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1308 WLAN_FC_STYPE_ACTION);
1309 os_memcpy(resp.da, mgmt->sa, ETH_ALEN);
1310 os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
1311 os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
1312 resp.u.action.category = WLAN_ACTION_SA_QUERY;
1313 resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
1314 os_memcpy(resp.u.action.u.sa_query_req.trans_id,
1315 mgmt->u.action.u.sa_query_req.trans_id,
1316 WLAN_SA_QUERY_TR_ID_LEN);
1317 end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1318 if (hostapd_drv_send_mlme(hapd, &resp, end - (u8 *) &resp) < 0)
1319 perror("hostapd_sa_query_request: send");
1323 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1324 const struct ieee80211_mgmt *mgmt,
1327 struct sta_info *sta;
1331 end = mgmt->u.action.u.sa_query_resp.trans_id +
1332 WLAN_SA_QUERY_TR_ID_LEN;
1333 if (((u8 *) mgmt) + len < end) {
1334 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1335 "frame (len=%lu)", (unsigned long) len);
1339 if (mgmt->u.action.u.sa_query_resp.action == WLAN_SA_QUERY_REQUEST) {
1340 hostapd_sa_query_request(hapd, mgmt);
1344 if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1345 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1346 "Action %d", mgmt->u.action.u.sa_query_resp.action);
1350 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
1351 MACSTR, MAC2STR(mgmt->sa));
1352 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1353 mgmt->u.action.u.sa_query_resp.trans_id,
1354 WLAN_SA_QUERY_TR_ID_LEN);
1356 /* MLME-SAQuery.confirm */
1358 sta = ap_get_sta(hapd, mgmt->sa);
1359 if (sta == NULL || sta->sa_query_trans_id == NULL) {
1360 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1361 "pending SA Query request found");
1365 for (i = 0; i < sta->sa_query_count; i++) {
1366 if (os_memcmp(sta->sa_query_trans_id +
1367 i * WLAN_SA_QUERY_TR_ID_LEN,
1368 mgmt->u.action.u.sa_query_resp.trans_id,
1369 WLAN_SA_QUERY_TR_ID_LEN) == 0)
1373 if (i >= sta->sa_query_count) {
1374 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1375 "transaction identifier found");
1379 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1380 HOSTAPD_LEVEL_DEBUG,
1381 "Reply to pending SA Query received");
1382 ap_sta_stop_sa_query(hapd, sta);
1386 static int robust_action_frame(u8 category)
1388 return category != WLAN_ACTION_PUBLIC &&
1389 category != WLAN_ACTION_HT;
1391 #endif /* CONFIG_IEEE80211W */
1394 static void handle_action(struct hostapd_data *hapd,
1395 const struct ieee80211_mgmt *mgmt, size_t len)
1397 struct sta_info *sta;
1399 if (len < IEEE80211_HDRLEN + 1) {
1400 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1401 HOSTAPD_LEVEL_DEBUG,
1402 "handle_action - too short payload (len=%lu)",
1403 (unsigned long) len);
1407 sta = ap_get_sta(hapd, mgmt->sa);
1408 #ifdef CONFIG_IEEE80211W
1409 if (sta && (sta->flags & WLAN_STA_MFP) &&
1410 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1411 robust_action_frame(mgmt->u.action.category))) {
1412 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1413 HOSTAPD_LEVEL_DEBUG,
1414 "Dropped unprotected Robust Action frame from "
1418 #endif /* CONFIG_IEEE80211W */
1420 switch (mgmt->u.action.category) {
1421 #ifdef CONFIG_IEEE80211R
1422 case WLAN_ACTION_FT:
1424 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1425 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1426 "frame from unassociated STA " MACSTR,
1431 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1432 len - IEEE80211_HDRLEN))
1437 #endif /* CONFIG_IEEE80211R */
1438 case WLAN_ACTION_WMM:
1439 hostapd_wmm_action(hapd, mgmt, len);
1441 #ifdef CONFIG_IEEE80211W
1442 case WLAN_ACTION_SA_QUERY:
1443 hostapd_sa_query_action(hapd, mgmt, len);
1445 #endif /* CONFIG_IEEE80211W */
1446 case WLAN_ACTION_PUBLIC:
1447 if (hapd->public_action_cb) {
1448 hapd->public_action_cb(hapd->public_action_cb_ctx,
1454 case WLAN_ACTION_VENDOR_SPECIFIC:
1455 if (hapd->vendor_action_cb) {
1456 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1458 hapd->iface->freq) == 0)
1464 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1465 HOSTAPD_LEVEL_DEBUG,
1466 "handle_action - unknown action category %d or invalid "
1468 mgmt->u.action.category);
1469 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1470 !(mgmt->sa[0] & 0x01)) {
1471 struct ieee80211_mgmt *resp;
1474 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1475 * Return the Action frame to the source without change
1476 * except that MSB of the Category set to 1.
1478 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1479 "frame back to sender");
1480 resp = os_malloc(len);
1483 os_memcpy(resp, mgmt, len);
1484 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1485 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1486 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1487 resp->u.action.category |= 0x80;
1489 hostapd_drv_send_mlme(hapd, resp, len);
1496 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1497 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1499 * @buf: management frame data (starting from IEEE 802.11 header)
1500 * @len: length of frame data in octets
1501 * @fi: meta data about received frame (signal level, etc.)
1503 * Process all incoming IEEE 802.11 management frames. This will be called for
1504 * each frame received from the kernel driver through wlan#ap interface. In
1505 * addition, it can be called to re-inserted pending frames (e.g., when using
1506 * external RADIUS server as an MAC ACL).
1508 void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1509 struct hostapd_frame_info *fi)
1511 struct ieee80211_mgmt *mgmt;
1518 mgmt = (struct ieee80211_mgmt *) buf;
1519 fc = le_to_host16(mgmt->frame_control);
1520 stype = WLAN_FC_GET_STYPE(fc);
1522 if (stype == WLAN_FC_STYPE_BEACON) {
1523 handle_beacon(hapd, mgmt, len, fi);
1527 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1528 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1529 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1532 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1533 printf("MGMT: BSSID=" MACSTR " not our address\n",
1534 MAC2STR(mgmt->bssid));
1539 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1540 handle_probe_req(hapd, mgmt, len);
1544 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1545 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1546 HOSTAPD_LEVEL_DEBUG,
1547 "MGMT: DA=" MACSTR " not our address",
1553 case WLAN_FC_STYPE_AUTH:
1554 wpa_printf(MSG_DEBUG, "mgmt::auth");
1555 handle_auth(hapd, mgmt, len);
1557 case WLAN_FC_STYPE_ASSOC_REQ:
1558 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1559 handle_assoc(hapd, mgmt, len, 0);
1561 case WLAN_FC_STYPE_REASSOC_REQ:
1562 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1563 handle_assoc(hapd, mgmt, len, 1);
1565 case WLAN_FC_STYPE_DISASSOC:
1566 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1567 handle_disassoc(hapd, mgmt, len);
1569 case WLAN_FC_STYPE_DEAUTH:
1570 wpa_msg(hapd, MSG_DEBUG, "mgmt::deauth");
1571 handle_deauth(hapd, mgmt, len);
1573 case WLAN_FC_STYPE_ACTION:
1574 wpa_printf(MSG_DEBUG, "mgmt::action");
1575 handle_action(hapd, mgmt, len);
1578 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1579 HOSTAPD_LEVEL_DEBUG,
1580 "unknown mgmt frame subtype %d", stype);
1586 static void handle_auth_cb(struct hostapd_data *hapd,
1587 const struct ieee80211_mgmt *mgmt,
1590 u16 auth_alg, auth_transaction, status_code;
1591 struct sta_info *sta;
1594 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1595 HOSTAPD_LEVEL_NOTICE,
1596 "did not acknowledge authentication response");
1600 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1601 printf("handle_auth_cb - too short payload (len=%lu)\n",
1602 (unsigned long) len);
1606 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1607 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1608 status_code = le_to_host16(mgmt->u.auth.status_code);
1610 sta = ap_get_sta(hapd, mgmt->da);
1612 printf("handle_auth_cb: STA " MACSTR " not found\n",
1617 if (status_code == WLAN_STATUS_SUCCESS &&
1618 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1619 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1620 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1621 HOSTAPD_LEVEL_INFO, "authenticated");
1622 sta->flags |= WLAN_STA_AUTH;
1627 static void handle_assoc_cb(struct hostapd_data *hapd,
1628 const struct ieee80211_mgmt *mgmt,
1629 size_t len, int reassoc, int ok)
1632 struct sta_info *sta;
1634 struct ieee80211_ht_capabilities ht_cap;
1637 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1638 HOSTAPD_LEVEL_DEBUG,
1639 "did not acknowledge association response");
1643 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1644 sizeof(mgmt->u.assoc_resp))) {
1645 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1646 "(len=%lu)\n", reassoc, (unsigned long) len);
1651 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1653 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1655 sta = ap_get_sta(hapd, mgmt->da);
1657 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1662 if (status != WLAN_STATUS_SUCCESS)
1665 /* Stop previous accounting session, if one is started, and allocate
1666 * new session id for the new session. */
1667 accounting_sta_stop(hapd, sta);
1669 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1671 "associated (aid %d)",
1674 if (sta->flags & WLAN_STA_ASSOC)
1676 sta->flags |= WLAN_STA_ASSOC;
1677 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1678 sta->auth_alg == WLAN_AUTH_FT) {
1680 * Open, static WEP, or FT protocol; no separate authorization
1683 ap_sta_set_authorized(hapd, sta, 1);
1684 wpa_msg(hapd->msg_ctx, MSG_INFO,
1685 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
1689 mlme_reassociate_indication(hapd, sta);
1691 mlme_associate_indication(hapd, sta);
1693 #ifdef CONFIG_IEEE80211W
1694 sta->sa_query_timed_out = 0;
1695 #endif /* CONFIG_IEEE80211W */
1698 * Remove the STA entry in order to make sure the STA PS state gets
1699 * cleared and configuration gets updated in case of reassociation back
1702 hostapd_drv_sta_remove(hapd, sta->addr);
1704 #ifdef CONFIG_IEEE80211N
1705 if (sta->flags & WLAN_STA_HT)
1706 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1707 #endif /* CONFIG_IEEE80211N */
1709 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
1710 sta->supported_rates, sta->supported_rates_len,
1711 sta->listen_interval,
1712 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
1714 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1715 HOSTAPD_LEVEL_NOTICE,
1716 "Could not add STA to kernel driver");
1719 if (sta->flags & WLAN_STA_WDS)
1720 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
1722 if (sta->eapol_sm == NULL) {
1724 * This STA does not use RADIUS server for EAP authentication,
1725 * so bind it to the selected VLAN interface now, since the
1726 * interface selection is not going to change anymore.
1728 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1730 } else if (sta->vlan_id) {
1731 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1732 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1736 hostapd_set_sta_flags(hapd, sta);
1738 if (sta->auth_alg == WLAN_AUTH_FT)
1739 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1741 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1742 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1744 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1747 /* Copy of the association request is not needed anymore */
1748 if (sta->last_assoc_req) {
1749 os_free(sta->last_assoc_req);
1750 sta->last_assoc_req = NULL;
1756 * ieee802_11_mgmt_cb - Process management frame TX status callback
1757 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1759 * @buf: management frame data (starting from IEEE 802.11 header)
1760 * @len: length of frame data in octets
1761 * @stype: management frame subtype from frame control field
1762 * @ok: Whether the frame was ACK'ed
1764 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
1767 const struct ieee80211_mgmt *mgmt;
1768 mgmt = (const struct ieee80211_mgmt *) buf;
1771 case WLAN_FC_STYPE_AUTH:
1772 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1773 handle_auth_cb(hapd, mgmt, len, ok);
1775 case WLAN_FC_STYPE_ASSOC_RESP:
1776 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1777 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1779 case WLAN_FC_STYPE_REASSOC_RESP:
1780 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1781 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1783 case WLAN_FC_STYPE_PROBE_RESP:
1784 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
1786 case WLAN_FC_STYPE_DEAUTH:
1789 case WLAN_FC_STYPE_ACTION:
1790 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1793 printf("unknown mgmt cb frame subtype %d\n", stype);
1799 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1806 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1807 char *buf, size_t buflen)
1814 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
1815 const u8 *buf, size_t len, int ack)
1817 struct sta_info *sta;
1818 struct hostapd_iface *iface = hapd->iface;
1820 sta = ap_get_sta(hapd, addr);
1821 if (sta == NULL && iface->num_bss > 1) {
1823 for (j = 0; j < iface->num_bss; j++) {
1824 hapd = iface->bss[j];
1825 sta = ap_get_sta(hapd, addr);
1832 if (sta->flags & WLAN_STA_PENDING_POLL) {
1833 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1834 "activity poll", MAC2STR(sta->addr),
1835 ack ? "ACKed" : "did not ACK");
1837 sta->flags &= ~WLAN_STA_PENDING_POLL;
1840 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
1844 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
1847 struct sta_info *sta;
1849 sta = ap_get_sta(hapd, src);
1850 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
1851 if (wds && !(sta->flags & WLAN_STA_WDS)) {
1852 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
1853 "STA " MACSTR " (aid %u)",
1854 MAC2STR(sta->addr), sta->aid);
1855 sta->flags |= WLAN_STA_WDS;
1856 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
1861 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
1862 MACSTR, MAC2STR(src));
1863 if (src[0] & 0x01) {
1864 /* Broadcast bit set in SA?! Ignore the frame silently. */
1868 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
1869 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
1870 "already been sent, but no TX status yet known - "
1871 "ignore Class 3 frame issue with " MACSTR,
1876 if (sta && (sta->flags & WLAN_STA_AUTH))
1877 hostapd_drv_sta_disassoc(
1879 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1881 hostapd_drv_sta_deauth(
1883 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1887 #endif /* CONFIG_NATIVE_WINDOWS */