2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2008, 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.
17 #ifndef CONFIG_NATIVE_WINDOWS
23 #include "ieee802_11.h"
25 #include "hw_features.h"
26 #include "radius/radius.h"
27 #include "radius/radius_client.h"
28 #include "ieee802_11_auth.h"
31 #include "ieee802_1x.h"
35 #include "accounting.h"
37 #include "ieee802_11h.h"
41 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
46 if (hapd->iface->current_rates == NULL)
49 *pos++ = WLAN_EID_SUPP_RATES;
50 num = hapd->iface->num_rates;
52 /* rest of the rates are encoded in Extended supported
59 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
62 *pos = hapd->iface->current_rates[i].rate / 5;
63 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
72 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
77 if (hapd->iface->current_rates == NULL)
80 num = hapd->iface->num_rates;
85 *pos++ = WLAN_EID_EXT_SUPP_RATES;
88 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
92 continue; /* already in SuppRates IE */
93 *pos = hapd->iface->current_rates[i].rate / 5;
94 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
103 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
106 int capab = WLAN_CAPABILITY_ESS;
109 if (hapd->iface->num_sta_no_short_preamble == 0 &&
110 hapd->iconf->preamble == SHORT_PREAMBLE)
111 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
113 privacy = hapd->conf->ssid.wep.keys_set;
115 if (hapd->conf->ieee802_1x &&
116 (hapd->conf->default_wep_key_len ||
117 hapd->conf->individual_wep_key_len))
124 int policy, def_klen;
125 if (probe && sta->ssid_probe) {
126 policy = sta->ssid_probe->security_policy;
127 def_klen = sta->ssid_probe->wep.default_len;
129 policy = sta->ssid->security_policy;
130 def_klen = sta->ssid->wep.default_len;
132 privacy = policy != SECURITY_PLAINTEXT;
133 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
138 capab |= WLAN_CAPABILITY_PRIVACY;
140 if (hapd->iface->current_mode &&
141 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
142 hapd->iface->num_sta_no_short_slot_time == 0)
143 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
145 if (hapd->iface->dfs_enable)
146 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
152 #define OUI_MICROSOFT 0x0050f2 /* Microsoft (also used in Wi-Fi specs)
155 static int ieee802_11_parse_vendor_specific(struct hostapd_data *hapd,
156 u8 *pos, size_t elen,
157 struct ieee802_11_elems *elems,
162 /* first 3 bytes in vendor specific information element are the IEEE
163 * OUI of the vendor. The following byte is used a vendor specific
167 wpa_printf(MSG_MSGDUMP, "short vendor specific "
168 "information element ignored (len=%lu)",
169 (unsigned long) elen);
174 oui = WPA_GET_BE24(pos);
177 /* Microsoft/Wi-Fi information elements are further typed and
181 /* Microsoft OUI (00:50:F2) with OUI Type 1:
182 * real WPA information element */
184 elems->wpa_ie_len = elen;
186 case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
188 wpa_printf(MSG_MSGDUMP, "short WME "
189 "information element ignored "
191 (unsigned long) elen);
195 case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
196 case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
198 elems->wme_len = elen;
200 case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
201 elems->wme_tspec = pos;
202 elems->wme_tspec_len = elen;
205 wpa_printf(MSG_MSGDUMP, "unknown WME "
206 "information element ignored "
207 "(subtype=%d len=%lu)",
208 pos[4], (unsigned long) elen);
213 wpa_printf(MSG_MSGDUMP, "Unknown Microsoft "
214 "information element ignored "
215 "(type=%d len=%lu)\n",
216 pos[3], (unsigned long) elen);
222 wpa_printf(MSG_MSGDUMP, "unknown vendor specific information "
223 "element ignored (vendor OUI %02x:%02x:%02x "
225 pos[0], pos[1], pos[2], (unsigned long) elen);
233 ParseRes ieee802_11_parse_elems(struct hostapd_data *hapd, u8 *start,
235 struct ieee802_11_elems *elems,
242 os_memset(elems, 0, sizeof(*elems));
253 wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
254 "parse failed (id=%d elen=%d "
256 id, elen, (unsigned long) left);
257 wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
265 elems->ssid_len = elen;
267 case WLAN_EID_SUPP_RATES:
268 elems->supp_rates = pos;
269 elems->supp_rates_len = elen;
271 case WLAN_EID_FH_PARAMS:
272 elems->fh_params = pos;
273 elems->fh_params_len = elen;
275 case WLAN_EID_DS_PARAMS:
276 elems->ds_params = pos;
277 elems->ds_params_len = elen;
279 case WLAN_EID_CF_PARAMS:
280 elems->cf_params = pos;
281 elems->cf_params_len = elen;
285 elems->tim_len = elen;
287 case WLAN_EID_IBSS_PARAMS:
288 elems->ibss_params = pos;
289 elems->ibss_params_len = elen;
291 case WLAN_EID_CHALLENGE:
292 elems->challenge = pos;
293 elems->challenge_len = elen;
295 case WLAN_EID_ERP_INFO:
296 elems->erp_info = pos;
297 elems->erp_info_len = elen;
299 case WLAN_EID_EXT_SUPP_RATES:
300 elems->ext_supp_rates = pos;
301 elems->ext_supp_rates_len = elen;
303 case WLAN_EID_VENDOR_SPECIFIC:
304 if (ieee802_11_parse_vendor_specific(hapd, pos, elen,
311 elems->rsn_ie_len = elen;
313 case WLAN_EID_PWR_CAPABILITY:
314 elems->power_cap = pos;
315 elems->power_cap_len = elen;
317 case WLAN_EID_SUPPORTED_CHANNELS:
318 elems->supp_channels = pos;
319 elems->supp_channels_len = elen;
321 case WLAN_EID_MOBILITY_DOMAIN:
323 elems->mdie_len = elen;
325 case WLAN_EID_FAST_BSS_TRANSITION:
327 elems->ftie_len = elen;
333 wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
334 "ignored unknown element (id=%d elen=%d)",
346 return unknown ? ParseUnknown : ParseOK;
350 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
353 if (len > HOSTAPD_MAX_SSID_LEN)
354 len = HOSTAPD_MAX_SSID_LEN;
355 for (i = 0; i < len; i++) {
356 if (ssid[i] >= 32 && ssid[i] < 127)
365 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
367 struct ieee80211_mgmt mgmt;
370 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
372 "deauthenticate - reason %d", reason);
373 os_snprintf(buf, sizeof(buf), "SEND-DEAUTHENTICATE %d", reason);
374 os_memset(&mgmt, 0, sizeof(mgmt));
375 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
376 WLAN_FC_STYPE_DEAUTH);
377 os_memcpy(mgmt.da, addr, ETH_ALEN);
378 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
379 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
380 mgmt.u.deauth.reason_code = host_to_le16(reason);
381 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
382 sizeof(mgmt.u.deauth), 0) < 0)
383 perror("ieee802_11_send_deauth: send");
387 static void ieee802_11_sta_authenticate(void *eloop_ctx, void *timeout_ctx)
389 struct hostapd_data *hapd = eloop_ctx;
390 struct ieee80211_mgmt mgmt;
393 if (hapd->assoc_ap_state == WAIT_BEACON)
394 hapd->assoc_ap_state = AUTHENTICATE;
395 if (hapd->assoc_ap_state != AUTHENTICATE)
398 ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
399 hapd->assoc_ap_ssid_len);
400 printf("Authenticate with AP " MACSTR " SSID=%s (as station)\n",
401 MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
403 os_memset(&mgmt, 0, sizeof(mgmt));
404 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
406 /* Request TX callback */
407 mgmt.frame_control |= host_to_le16(BIT(1));
408 os_memcpy(mgmt.da, hapd->conf->assoc_ap_addr, ETH_ALEN);
409 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
410 os_memcpy(mgmt.bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
411 mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
412 mgmt.u.auth.auth_transaction = host_to_le16(1);
413 mgmt.u.auth.status_code = host_to_le16(0);
414 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
415 sizeof(mgmt.u.auth), 0) < 0)
416 perror("ieee802_11_sta_authenticate: send");
418 /* Try to authenticate again, if this attempt fails or times out. */
419 eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
423 static void ieee802_11_sta_associate(void *eloop_ctx, void *timeout_ctx)
425 struct hostapd_data *hapd = eloop_ctx;
427 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
431 if (hapd->assoc_ap_state == AUTHENTICATE)
432 hapd->assoc_ap_state = ASSOCIATE;
433 if (hapd->assoc_ap_state != ASSOCIATE)
436 ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
437 hapd->assoc_ap_ssid_len);
438 printf("Associate with AP " MACSTR " SSID=%s (as station)\n",
439 MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
441 os_memset(mgmt, 0, sizeof(*mgmt));
442 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
443 WLAN_FC_STYPE_ASSOC_REQ);
444 /* Request TX callback */
445 mgmt->frame_control |= host_to_le16(BIT(1));
446 os_memcpy(mgmt->da, hapd->conf->assoc_ap_addr, ETH_ALEN);
447 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
448 os_memcpy(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
449 mgmt->u.assoc_req.capab_info = host_to_le16(0);
450 mgmt->u.assoc_req.listen_interval = host_to_le16(1);
451 p = &mgmt->u.assoc_req.variable[0];
453 *p++ = WLAN_EID_SSID;
454 *p++ = hapd->assoc_ap_ssid_len;
455 os_memcpy(p, hapd->assoc_ap_ssid, hapd->assoc_ap_ssid_len);
456 p += hapd->assoc_ap_ssid_len;
458 p = hostapd_eid_supp_rates(hapd, p);
459 p = hostapd_eid_ext_supp_rates(hapd, p);
461 if (hostapd_send_mgmt_frame(hapd, mgmt, p - (u8 *) mgmt, 0) < 0)
462 perror("ieee802_11_sta_associate: send");
464 /* Try to authenticate again, if this attempt fails or times out. */
465 eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
469 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
470 u16 auth_transaction, u8 *challenge, int iswep)
472 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
474 "authentication (shared key, transaction %d)",
477 if (auth_transaction == 1) {
478 if (!sta->challenge) {
479 /* Generate a pseudo-random challenge */
483 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
484 if (sta->challenge == NULL)
485 return WLAN_STATUS_UNSPECIFIED_FAILURE;
489 os_memcpy(key, &now, 4);
490 os_memcpy(key + 4, &r, 4);
491 rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
497 if (auth_transaction != 3)
498 return WLAN_STATUS_UNSPECIFIED_FAILURE;
501 if (!iswep || !sta->challenge || !challenge ||
502 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
503 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
505 "shared key authentication - invalid "
506 "challenge-response");
507 return WLAN_STATUS_CHALLENGE_FAIL;
510 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
512 "authentication OK (shared key)");
513 #ifdef IEEE80211_REQUIRE_AUTH_ACK
514 /* Station will be marked authenticated if it ACKs the
515 * authentication reply. */
517 sta->flags |= WLAN_STA_AUTH;
518 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
520 os_free(sta->challenge);
521 sta->challenge = NULL;
527 static void send_auth_reply(struct hostapd_data *hapd,
528 const u8 *dst, const u8 *bssid,
529 u16 auth_alg, u16 auth_transaction, u16 resp,
530 const u8 *ies, size_t ies_len)
532 struct ieee80211_mgmt *reply;
536 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
537 buf = os_zalloc(rlen);
541 reply = (struct ieee80211_mgmt *) buf;
542 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
544 /* Request TX callback */
545 reply->frame_control |= host_to_le16(BIT(1));
546 os_memcpy(reply->da, dst, ETH_ALEN);
547 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
548 os_memcpy(reply->bssid, bssid, ETH_ALEN);
550 reply->u.auth.auth_alg = host_to_le16(auth_alg);
551 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
552 reply->u.auth.status_code = host_to_le16(resp);
555 os_memcpy(reply->u.auth.variable, ies, ies_len);
557 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
558 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
559 MAC2STR(dst), auth_alg, auth_transaction,
560 resp, (unsigned long) ies_len);
561 if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
562 perror("send_auth_reply: send");
568 #ifdef CONFIG_IEEE80211R
569 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
570 u16 auth_transaction, u16 status,
571 const u8 *ies, size_t ies_len)
573 struct hostapd_data *hapd = ctx;
574 struct sta_info *sta;
576 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
577 status, ies, ies_len);
579 if (status != WLAN_STATUS_SUCCESS)
582 sta = ap_get_sta(hapd, dst);
586 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
587 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
588 sta->flags |= WLAN_STA_AUTH;
589 mlme_authenticate_indication(hapd, sta);
591 #endif /* CONFIG_IEEE80211R */
594 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
597 u16 auth_alg, auth_transaction, status_code;
598 u16 resp = WLAN_STATUS_SUCCESS;
599 struct sta_info *sta = NULL;
602 u8 *challenge = NULL;
603 u32 session_timeout, acct_interim_interval;
605 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
606 size_t resp_ies_len = 0;
608 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
609 printf("handle_auth - too short payload (len=%lu)\n",
610 (unsigned long) len);
614 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
615 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
616 status_code = le_to_host16(mgmt->u.auth.status_code);
617 fc = le_to_host16(mgmt->frame_control);
619 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
620 2 + WLAN_AUTH_CHALLENGE_LEN &&
621 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
622 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
623 challenge = &mgmt->u.auth.variable[2];
625 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
626 "auth_transaction=%d status_code=%d wep=%d%s",
627 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
628 status_code, !!(fc & WLAN_FC_ISWEP),
629 challenge ? " challenge" : "");
631 if (hapd->assoc_ap_state == AUTHENTICATE && auth_transaction == 2 &&
632 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0 &&
633 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
634 if (status_code != 0) {
635 printf("Authentication (as station) with AP "
636 MACSTR " failed (status_code=%d)\n",
637 MAC2STR(hapd->conf->assoc_ap_addr),
641 printf("Authenticated (as station) with AP " MACSTR "\n",
642 MAC2STR(hapd->conf->assoc_ap_addr));
643 ieee802_11_sta_associate(hapd, NULL);
647 if (hapd->tkip_countermeasures) {
648 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
652 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
653 auth_alg == WLAN_AUTH_OPEN) ||
654 #ifdef CONFIG_IEEE80211R
656 (hapd->conf->wpa_key_mgmt &
657 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
658 auth_alg == WLAN_AUTH_FT) ||
659 #endif /* CONFIG_IEEE80211R */
660 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
661 auth_alg == WLAN_AUTH_SHARED_KEY))) {
662 printf("Unsupported authentication algorithm (%d)\n",
664 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
668 if (!(auth_transaction == 1 ||
669 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
670 printf("Unknown authentication transaction number (%d)\n",
672 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
676 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
677 printf("Station " MACSTR " not allowed to authenticate.\n",
679 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
683 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
685 &acct_interim_interval, &vlan_id);
686 if (res == HOSTAPD_ACL_REJECT) {
687 printf("Station " MACSTR " not allowed to authenticate.\n",
689 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
692 if (res == HOSTAPD_ACL_PENDING) {
693 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
694 " waiting for an external authentication",
696 /* Authentication code will re-send the authentication frame
697 * after it has received (and cached) information from the
698 * external source. */
702 sta = ap_sta_add(hapd, mgmt->sa);
704 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
709 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
710 sta->vlan_id) == NULL) {
711 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
712 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
713 "%d received from RADIUS server",
715 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
718 sta->vlan_id = vlan_id;
719 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
720 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
723 sta->flags &= ~WLAN_STA_PREAUTH;
724 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
726 if (hapd->conf->radius->acct_interim_interval == 0 &&
727 acct_interim_interval)
728 sta->acct_interim_interval = acct_interim_interval;
729 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
730 ap_sta_session_timeout(hapd, sta, session_timeout);
732 ap_sta_no_session_timeout(hapd, sta);
736 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
738 "authentication OK (open system)");
739 #ifdef IEEE80211_REQUIRE_AUTH_ACK
740 /* Station will be marked authenticated if it ACKs the
741 * authentication reply. */
743 sta->flags |= WLAN_STA_AUTH;
744 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
745 sta->auth_alg = WLAN_AUTH_OPEN;
746 mlme_authenticate_indication(hapd, sta);
749 case WLAN_AUTH_SHARED_KEY:
750 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
752 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
753 mlme_authenticate_indication(hapd, sta);
754 if (sta->challenge && auth_transaction == 1) {
755 resp_ies[0] = WLAN_EID_CHALLENGE;
756 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
757 os_memcpy(resp_ies + 2, sta->challenge,
758 WLAN_AUTH_CHALLENGE_LEN);
759 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
762 #ifdef CONFIG_IEEE80211R
764 sta->auth_alg = WLAN_AUTH_FT;
765 if (sta->wpa_sm == NULL)
766 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
768 if (sta->wpa_sm == NULL) {
769 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
771 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
774 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
775 auth_transaction, mgmt->u.auth.variable,
776 len - IEEE80211_HDRLEN -
777 sizeof(mgmt->u.auth),
778 handle_auth_ft_finish, hapd);
779 /* handle_auth_ft_finish() callback will complete auth. */
781 #endif /* CONFIG_IEEE80211R */
785 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
786 auth_transaction + 1, resp, resp_ies, resp_ies_len);
790 static void handle_assoc(struct hostapd_data *hapd,
791 struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
793 u16 capab_info, listen_interval;
794 u16 resp = WLAN_STATUS_SUCCESS;
797 int send_deauth = 0, send_len, left, i;
798 struct sta_info *sta;
799 struct ieee802_11_elems elems;
800 u8 buf[sizeof(struct ieee80211_mgmt) + 512];
801 struct ieee80211_mgmt *reply;
803 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
804 sizeof(mgmt->u.assoc_req))) {
805 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
806 "\n", reassoc, (unsigned long) len);
811 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
812 listen_interval = le_to_host16(
813 mgmt->u.reassoc_req.listen_interval);
814 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
815 " capab_info=0x%02x listen_interval=%d current_ap="
817 MAC2STR(mgmt->sa), capab_info, listen_interval,
818 MAC2STR(mgmt->u.reassoc_req.current_ap));
819 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
820 pos = mgmt->u.reassoc_req.variable;
822 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
823 listen_interval = le_to_host16(
824 mgmt->u.assoc_req.listen_interval);
825 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
826 " capab_info=0x%02x listen_interval=%d",
827 MAC2STR(mgmt->sa), capab_info, listen_interval);
828 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
829 pos = mgmt->u.assoc_req.variable;
832 sta = ap_get_sta(hapd, mgmt->sa);
833 #ifdef CONFIG_IEEE80211R
834 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
835 (sta->flags & WLAN_STA_AUTH) == 0) {
836 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
837 "prior to authentication since it is using "
838 "over-the-DS FT", MAC2STR(mgmt->sa));
840 #endif /* CONFIG_IEEE80211R */
841 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
842 printf("STA " MACSTR " trying to associate before "
843 "authentication\n", MAC2STR(mgmt->sa));
845 printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
846 MAC2STR(sta->addr), sta->aid, sta->flags);
849 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
853 if (hapd->tkip_countermeasures) {
854 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
859 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
863 sta->capability = capab_info;
865 /* followed by SSID and Supported rates */
866 if (ieee802_11_parse_elems(hapd, pos, left, &elems, 1) == ParseFailed
868 printf("STA " MACSTR " sent invalid association request\n",
870 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
874 if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
875 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
878 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
879 printf("Station " MACSTR " tried to associate with "
880 "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
881 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
885 sta->flags &= ~WLAN_STA_WME;
886 if (elems.wme && hapd->conf->wme_enabled) {
887 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
888 hostapd_logger(hapd, sta->addr,
891 "invalid WME element in association "
894 sta->flags |= WLAN_STA_WME;
897 if (!elems.supp_rates) {
898 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
900 "No supported rates element in AssocReq");
901 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
905 if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
906 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
908 "Invalid supported rates element length %d",
909 elems.supp_rates_len);
910 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
914 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
915 os_memcpy(sta->supported_rates, elems.supp_rates,
916 elems.supp_rates_len);
917 sta->supported_rates_len = elems.supp_rates_len;
919 if (elems.ext_supp_rates) {
920 if (elems.supp_rates_len + elems.ext_supp_rates_len >
921 sizeof(sta->supported_rates)) {
922 hostapd_logger(hapd, mgmt->sa,
923 HOSTAPD_MODULE_IEEE80211,
925 "Invalid supported rates element length"
926 " %d+%d", elems.supp_rates_len,
927 elems.ext_supp_rates_len);
928 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
932 os_memcpy(sta->supported_rates + elems.supp_rates_len,
933 elems.ext_supp_rates, elems.ext_supp_rates_len);
934 sta->supported_rates_len += elems.ext_supp_rates_len;
937 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
938 wpa_ie = elems.rsn_ie;
939 wpa_ie_len = elems.rsn_ie_len;
940 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
942 wpa_ie = elems.wpa_ie;
943 wpa_ie_len = elems.wpa_ie_len;
948 if (hapd->conf->wpa && wpa_ie == NULL) {
949 printf("STA " MACSTR ": No WPA/RSN IE in association "
950 "request\n", MAC2STR(sta->addr));
951 resp = WLAN_STATUS_INVALID_IE;
955 if (hapd->conf->wpa && wpa_ie) {
959 if (sta->wpa_sm == NULL)
960 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
962 if (sta->wpa_sm == NULL) {
963 printf("Failed to initialize WPA state machine\n");
964 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
967 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
969 elems.mdie, elems.mdie_len);
970 if (res == WPA_INVALID_GROUP)
971 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
972 else if (res == WPA_INVALID_PAIRWISE)
973 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
974 else if (res == WPA_INVALID_AKMP)
975 resp = WLAN_STATUS_AKMP_NOT_VALID;
976 else if (res == WPA_ALLOC_FAIL)
977 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
978 #ifdef CONFIG_IEEE80211W
979 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
980 resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
981 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
982 resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
983 #endif /* CONFIG_IEEE80211W */
984 else if (res == WPA_INVALID_MDIE)
985 resp = WLAN_STATUS_INVALID_MDIE;
986 else if (res != WPA_IE_OK)
987 resp = WLAN_STATUS_INVALID_IE;
988 if (resp != WLAN_STATUS_SUCCESS)
991 #ifdef CONFIG_IEEE80211R
992 if (sta->auth_alg == WLAN_AUTH_FT) {
994 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
995 "to use association (not "
996 "re-association) with FT auth_alg",
998 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1002 resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
1003 if (resp != WLAN_STATUS_SUCCESS)
1006 #endif /* CONFIG_IEEE80211R */
1009 if (hapd->iface->dfs_enable &&
1010 hapd->iconf->ieee80211h == SPECT_STRICT_BINDING) {
1011 if (hostapd_check_power_cap(hapd, elems.power_cap,
1012 elems.power_cap_len)) {
1013 resp = WLAN_STATUS_PWR_CAPABILITY_NOT_VALID;
1014 hostapd_logger(hapd, sta->addr,
1015 HOSTAPD_MODULE_IEEE80211,
1016 HOSTAPD_LEVEL_DEBUG,
1017 "Power capabilities of the station not "
1023 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1024 sta->flags |= WLAN_STA_NONERP;
1025 for (i = 0; i < sta->supported_rates_len; i++) {
1026 if ((sta->supported_rates[i] & 0x7f) > 22) {
1027 sta->flags &= ~WLAN_STA_NONERP;
1031 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1032 sta->nonerp_set = 1;
1033 hapd->iface->num_sta_non_erp++;
1034 if (hapd->iface->num_sta_non_erp == 1)
1035 ieee802_11_set_beacons(hapd->iface);
1038 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1039 !sta->no_short_slot_time_set) {
1040 sta->no_short_slot_time_set = 1;
1041 hapd->iface->num_sta_no_short_slot_time++;
1042 if (hapd->iface->current_mode->mode ==
1043 HOSTAPD_MODE_IEEE80211G &&
1044 hapd->iface->num_sta_no_short_slot_time == 1)
1045 ieee802_11_set_beacons(hapd->iface);
1048 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1049 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1051 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1053 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1054 !sta->no_short_preamble_set) {
1055 sta->no_short_preamble_set = 1;
1056 hapd->iface->num_sta_no_short_preamble++;
1057 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1058 && hapd->iface->num_sta_no_short_preamble == 1)
1059 ieee802_11_set_beacons(hapd->iface);
1062 /* get a unique AID */
1064 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
1066 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1067 if (hapd->sta_aid[sta->aid - 1] == NULL)
1069 if (sta->aid > MAX_AID_TABLE_SIZE) {
1071 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1072 wpa_printf(MSG_ERROR, " no room for more AIDs");
1075 hapd->sta_aid[sta->aid - 1] = sta;
1076 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
1080 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1081 HOSTAPD_LEVEL_DEBUG,
1082 "association OK (aid %d)", sta->aid);
1083 /* Station will be marked associated, after it acknowledges AssocResp
1086 if (sta->last_assoc_req)
1087 os_free(sta->last_assoc_req);
1088 sta->last_assoc_req = os_malloc(len);
1089 if (sta->last_assoc_req)
1090 os_memcpy(sta->last_assoc_req, mgmt, len);
1092 /* Make sure that the previously registered inactivity timer will not
1093 * remove the STA immediately. */
1094 sta->timeout_next = STA_NULLFUNC;
1097 os_memset(buf, 0, sizeof(buf));
1098 reply = (struct ieee80211_mgmt *) buf;
1099 reply->frame_control =
1100 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1101 (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1102 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1103 WLAN_FC_STYPE_ASSOC_RESP)));
1104 os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1105 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1106 os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1108 send_len = IEEE80211_HDRLEN;
1110 send_len += sizeof(reply->u.deauth);
1111 reply->u.deauth.reason_code = host_to_le16(resp);
1114 send_len += sizeof(reply->u.assoc_resp);
1115 reply->u.assoc_resp.capab_info =
1116 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1117 reply->u.assoc_resp.status_code = host_to_le16(resp);
1118 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1119 | BIT(14) | BIT(15));
1120 /* Supported rates */
1121 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1122 /* Extended supported rates */
1123 p = hostapd_eid_ext_supp_rates(hapd, p);
1124 if (sta->flags & WLAN_STA_WME)
1125 p = hostapd_eid_wme(hapd, p);
1127 #ifdef CONFIG_IEEE80211R
1128 if (resp == WLAN_STATUS_SUCCESS) {
1129 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1130 * Transition Information, RSN */
1131 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1132 buf + sizeof(buf) - p,
1135 #endif /* CONFIG_IEEE80211R */
1137 send_len += p - reply->u.assoc_resp.variable;
1139 /* Request TX callback */
1140 reply->frame_control |= host_to_le16(BIT(1));
1143 if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1144 perror("handle_assoc: send");
1148 static void handle_assoc_resp(struct hostapd_data *hapd,
1149 struct ieee80211_mgmt *mgmt, size_t len)
1151 u16 status_code, aid;
1153 if (hapd->assoc_ap_state != ASSOCIATE) {
1154 printf("Unexpected association response received from " MACSTR
1155 "\n", MAC2STR(mgmt->sa));
1159 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
1160 printf("handle_assoc_resp - too short payload (len=%lu)\n",
1161 (unsigned long) len);
1165 if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
1166 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
1167 printf("Received association response from unexpected address "
1168 "(SA=" MACSTR " BSSID=" MACSTR "\n",
1169 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
1173 status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
1174 aid = le_to_host16(mgmt->u.assoc_resp.aid);
1175 aid &= ~(BIT(14) | BIT(15));
1177 if (status_code != 0) {
1178 printf("Association (as station) with AP " MACSTR " failed "
1179 "(status_code=%d)\n",
1180 MAC2STR(hapd->conf->assoc_ap_addr), status_code);
1181 /* Try to authenticate again */
1182 hapd->assoc_ap_state = AUTHENTICATE;
1183 eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
1187 printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
1188 MAC2STR(hapd->conf->assoc_ap_addr), aid);
1189 hapd->assoc_ap_aid = aid;
1190 hapd->assoc_ap_state = ASSOCIATED;
1192 if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
1193 printf("Could not set associated AP address to kernel "
1199 static void handle_disassoc(struct hostapd_data *hapd,
1200 struct ieee80211_mgmt *mgmt, size_t len)
1202 struct sta_info *sta;
1204 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1205 printf("handle_disassoc - too short payload (len=%lu)\n",
1206 (unsigned long) len);
1210 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1212 le_to_host16(mgmt->u.disassoc.reason_code));
1214 if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1215 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1216 printf("Assoc AP " MACSTR " sent disassociation "
1217 "(reason_code=%d) - try to authenticate\n",
1218 MAC2STR(hapd->conf->assoc_ap_addr),
1219 le_to_host16(mgmt->u.disassoc.reason_code));
1220 hapd->assoc_ap_state = AUTHENTICATE;
1221 ieee802_11_sta_authenticate(hapd, NULL);
1222 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1227 sta = ap_get_sta(hapd, mgmt->sa);
1229 printf("Station " MACSTR " trying to disassociate, but it "
1230 "is not associated.\n", MAC2STR(mgmt->sa));
1234 sta->flags &= ~WLAN_STA_ASSOC;
1235 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1236 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1237 HOSTAPD_LEVEL_INFO, "disassociated");
1238 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1239 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1240 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1242 accounting_sta_stop(hapd, sta);
1243 ieee802_1x_free_station(sta);
1244 hostapd_sta_remove(hapd, sta->addr);
1246 if (sta->timeout_next == STA_NULLFUNC ||
1247 sta->timeout_next == STA_DISASSOC) {
1248 sta->timeout_next = STA_DEAUTH;
1249 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1250 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1254 mlme_disassociate_indication(
1255 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1259 static void handle_deauth(struct hostapd_data *hapd,
1260 struct ieee80211_mgmt *mgmt, size_t len)
1262 struct sta_info *sta;
1264 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1265 printf("handle_deauth - too short payload (len=%lu)\n",
1266 (unsigned long) len);
1270 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1273 le_to_host16(mgmt->u.deauth.reason_code));
1275 if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1276 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1277 printf("Assoc AP " MACSTR " sent deauthentication "
1278 "(reason_code=%d) - try to authenticate\n",
1279 MAC2STR(hapd->conf->assoc_ap_addr),
1280 le_to_host16(mgmt->u.deauth.reason_code));
1281 hapd->assoc_ap_state = AUTHENTICATE;
1282 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1287 sta = ap_get_sta(hapd, mgmt->sa);
1289 printf("Station " MACSTR " trying to deauthenticate, but it "
1290 "is not authenticated.\n", MAC2STR(mgmt->sa));
1294 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1295 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1296 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1297 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1298 mlme_deauthenticate_indication(
1299 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1300 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1301 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1302 ap_free_sta(hapd, sta);
1306 static void handle_beacon(struct hostapd_data *hapd,
1307 struct ieee80211_mgmt *mgmt, size_t len,
1308 struct hostapd_frame_info *fi)
1310 struct ieee802_11_elems elems;
1312 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1313 printf("handle_beacon - too short payload (len=%lu)\n",
1314 (unsigned long) len);
1318 (void) ieee802_11_parse_elems(hapd, mgmt->u.beacon.variable,
1319 len - (IEEE80211_HDRLEN +
1320 sizeof(mgmt->u.beacon)), &elems,
1323 if (hapd->assoc_ap_state == WAIT_BEACON &&
1324 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1325 if (elems.ssid && elems.ssid_len <= 32) {
1326 os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
1328 hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
1329 hapd->assoc_ap_ssid_len = elems.ssid_len;
1331 ieee802_11_sta_authenticate(hapd, NULL);
1334 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1338 static void handle_action(struct hostapd_data *hapd,
1339 struct ieee80211_mgmt *mgmt, size_t len)
1341 if (len < IEEE80211_HDRLEN + 1) {
1342 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1343 HOSTAPD_LEVEL_DEBUG,
1344 "handle_action - too short payload (len=%lu)",
1345 (unsigned long) len);
1349 switch (mgmt->u.action.category) {
1350 #ifdef CONFIG_IEEE80211R
1351 case WLAN_ACTION_FT:
1353 struct sta_info *sta;
1355 sta = ap_get_sta(hapd, mgmt->sa);
1356 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1357 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1358 "frame from unassociated STA " MACSTR,
1363 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1364 len - IEEE80211_HDRLEN))
1369 #endif /* CONFIG_IEEE80211R */
1370 case WME_ACTION_CATEGORY:
1371 hostapd_wme_action(hapd, mgmt, len);
1375 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1376 HOSTAPD_LEVEL_DEBUG,
1377 "handle_action - unknown action category %d or invalid "
1379 mgmt->u.action.category);
1380 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1381 !(mgmt->sa[0] & 0x01)) {
1383 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1384 * Return the Action frame to the source without change
1385 * except that MSB of the Category set to 1.
1387 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1388 "frame back to sender");
1389 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1390 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1391 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1392 mgmt->u.action.category |= 0x80;
1394 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1400 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1401 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1403 * @buf: management frame data (starting from IEEE 802.11 header)
1404 * @len: length of frame data in octets
1405 * @stype: management frame subtype from frame control field
1407 * Process all incoming IEEE 802.11 management frames. This will be called for
1408 * each frame received from the kernel driver through wlan#ap interface. In
1409 * addition, it can be called to re-inserted pending frames (e.g., when using
1410 * external RADIUS server as an MAC ACL).
1412 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1413 struct hostapd_frame_info *fi)
1415 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1418 if (stype == WLAN_FC_STYPE_BEACON) {
1419 handle_beacon(hapd, mgmt, len, fi);
1423 if (fi && fi->passive_scan)
1426 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1427 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1428 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1431 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
1432 (hapd->assoc_ap_state == DO_NOT_ASSOC ||
1433 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
1435 printf("MGMT: BSSID=" MACSTR " not our address\n",
1436 MAC2STR(mgmt->bssid));
1441 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1442 handle_probe_req(hapd, mgmt, len);
1446 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1447 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1448 HOSTAPD_LEVEL_DEBUG,
1449 "MGMT: DA=" MACSTR " not our address",
1455 case WLAN_FC_STYPE_AUTH:
1456 wpa_printf(MSG_DEBUG, "mgmt::auth");
1457 handle_auth(hapd, mgmt, len);
1459 case WLAN_FC_STYPE_ASSOC_REQ:
1460 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1461 handle_assoc(hapd, mgmt, len, 0);
1463 case WLAN_FC_STYPE_ASSOC_RESP:
1464 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
1465 handle_assoc_resp(hapd, mgmt, len);
1467 case WLAN_FC_STYPE_REASSOC_REQ:
1468 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1469 handle_assoc(hapd, mgmt, len, 1);
1471 case WLAN_FC_STYPE_DISASSOC:
1472 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1473 handle_disassoc(hapd, mgmt, len);
1475 case WLAN_FC_STYPE_DEAUTH:
1476 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1477 handle_deauth(hapd, mgmt, len);
1479 case WLAN_FC_STYPE_ACTION:
1480 wpa_printf(MSG_DEBUG, "mgmt::action");
1481 handle_action(hapd, mgmt, len);
1484 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1485 HOSTAPD_LEVEL_DEBUG,
1486 "unknown mgmt frame subtype %d", stype);
1492 static void handle_auth_cb(struct hostapd_data *hapd,
1493 struct ieee80211_mgmt *mgmt,
1496 u16 auth_alg, auth_transaction, status_code;
1497 struct sta_info *sta;
1500 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1501 HOSTAPD_LEVEL_NOTICE,
1502 "did not acknowledge authentication response");
1506 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1507 printf("handle_auth_cb - too short payload (len=%lu)\n",
1508 (unsigned long) len);
1512 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1513 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1514 status_code = le_to_host16(mgmt->u.auth.status_code);
1516 sta = ap_get_sta(hapd, mgmt->da);
1518 printf("handle_auth_cb: STA " MACSTR " not found\n",
1523 if (status_code == WLAN_STATUS_SUCCESS &&
1524 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1525 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1526 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1527 HOSTAPD_LEVEL_INFO, "authenticated");
1528 sta->flags |= WLAN_STA_AUTH;
1533 static void handle_assoc_cb(struct hostapd_data *hapd,
1534 struct ieee80211_mgmt *mgmt,
1535 size_t len, int reassoc, int ok)
1538 struct sta_info *sta;
1542 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1543 HOSTAPD_LEVEL_DEBUG,
1544 "did not acknowledge association response");
1548 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1549 sizeof(mgmt->u.assoc_resp))) {
1550 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1551 "(len=%lu)\n", reassoc, (unsigned long) len);
1556 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1558 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1560 sta = ap_get_sta(hapd, mgmt->da);
1562 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1567 if (status != WLAN_STATUS_SUCCESS)
1570 /* Stop previous accounting session, if one is started, and allocate
1571 * new session id for the new session. */
1572 accounting_sta_stop(hapd, sta);
1573 accounting_sta_get_id(hapd, sta);
1575 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1577 "associated (aid %d, accounting session %08X-%08X)",
1578 sta->aid, sta->acct_session_id_hi,
1579 sta->acct_session_id_lo);
1581 if (sta->flags & WLAN_STA_ASSOC)
1583 sta->flags |= WLAN_STA_ASSOC;
1586 mlme_reassociate_indication(hapd, sta);
1588 mlme_associate_indication(hapd, sta);
1590 if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1591 sta->capability, sta->supported_rates,
1592 sta->supported_rates_len, 0)) {
1593 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1594 HOSTAPD_LEVEL_NOTICE,
1595 "Could not add STA to kernel driver");
1598 if (sta->eapol_sm == NULL) {
1600 * This STA does not use RADIUS server for EAP authentication,
1601 * so bind it to the selected VLAN interface now, since the
1602 * interface selection is not going to change anymore.
1604 ap_sta_bind_vlan(hapd, sta, 0);
1605 } else if (sta->vlan_id) {
1606 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1607 ap_sta_bind_vlan(hapd, sta, 0);
1609 if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1610 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1611 WLAN_STA_SHORT_PREAMBLE, ~0);
1613 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1614 0, ~WLAN_STA_SHORT_PREAMBLE);
1617 if (sta->auth_alg == WLAN_AUTH_FT)
1618 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1620 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1621 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1623 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1626 /* Copy of the association request is not needed anymore */
1627 if (sta->last_assoc_req) {
1628 os_free(sta->last_assoc_req);
1629 sta->last_assoc_req = NULL;
1634 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1637 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1640 case WLAN_FC_STYPE_AUTH:
1641 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1642 handle_auth_cb(hapd, mgmt, len, ok);
1644 case WLAN_FC_STYPE_ASSOC_RESP:
1645 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1646 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1648 case WLAN_FC_STYPE_REASSOC_RESP:
1649 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1650 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1652 case WLAN_FC_STYPE_PROBE_RESP:
1653 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1655 case WLAN_FC_STYPE_DEAUTH:
1659 printf("unknown mgmt cb frame subtype %d\n", stype);
1665 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1668 struct hostapd_data *hapd = eloop_ctx;
1669 hapd->tkip_countermeasures = 0;
1670 hostapd_set_countermeasures(hapd, 0);
1671 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1672 HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1676 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1678 struct sta_info *sta;
1680 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1681 HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1683 wpa_auth_countermeasures_start(hapd->wpa_auth);
1684 hapd->tkip_countermeasures = 1;
1685 hostapd_set_countermeasures(hapd, 1);
1686 wpa_gtk_rekey(hapd->wpa_auth);
1687 eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
1688 eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
1690 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
1691 hostapd_sta_deauth(hapd, sta->addr,
1692 WLAN_REASON_MICHAEL_MIC_FAILURE);
1693 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1694 WLAN_STA_AUTHORIZED);
1695 hostapd_sta_remove(hapd, sta->addr);
1700 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
1705 if (addr && local) {
1706 struct sta_info *sta = ap_get_sta(hapd, addr);
1708 wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
1709 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1711 "Michael MIC failure detected in "
1713 mlme_michaelmicfailure_indication(hapd, addr);
1715 wpa_printf(MSG_DEBUG,
1716 "MLME-MICHAELMICFAILURE.indication "
1717 "for not associated STA (" MACSTR
1718 ") ignored", MAC2STR(addr));
1724 if (now > hapd->michael_mic_failure + 60) {
1725 hapd->michael_mic_failures = 1;
1727 hapd->michael_mic_failures++;
1728 if (hapd->michael_mic_failures > 1)
1729 ieee80211_tkip_countermeasures_start(hapd);
1731 hapd->michael_mic_failure = now;
1735 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1742 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1743 char *buf, size_t buflen)
1749 #endif /* CONFIG_NATIVE_WINDOWS */