2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2007-2008, Intel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
18 #ifndef CONFIG_NATIVE_WINDOWS
24 #include "ieee802_11.h"
26 #include "hw_features.h"
27 #include "radius/radius.h"
28 #include "radius/radius_client.h"
29 #include "ieee802_11_auth.h"
32 #include "ieee802_1x.h"
36 #include "accounting.h"
38 #include "ieee802_11h.h"
42 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
47 if (hapd->iface->current_rates == NULL)
50 *pos++ = WLAN_EID_SUPP_RATES;
51 num = hapd->iface->num_rates;
53 /* rest of the rates are encoded in Extended supported
60 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
63 *pos = hapd->iface->current_rates[i].rate / 5;
64 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
73 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
78 if (hapd->iface->current_rates == NULL)
81 num = hapd->iface->num_rates;
86 *pos++ = WLAN_EID_EXT_SUPP_RATES;
89 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
93 continue; /* already in SuppRates IE */
94 *pos = hapd->iface->current_rates[i].rate / 5;
95 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
104 u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
106 #ifdef CONFIG_IEEE80211N
107 struct ieee80211_ht_capability *cap;
110 if (!hapd->iconf->ieee80211n)
113 *pos++ = WLAN_EID_HT_CAP;
114 *pos++ = sizeof(*cap);
116 cap = (struct ieee80211_ht_capability *) pos;
117 os_memset(cap, 0, sizeof(*cap));
118 SET_2BIT_U8(&cap->mac_ht_params_info,
119 MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
120 MAX_RX_AMPDU_FACTOR_64KB);
122 cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
124 cap->supported_mcs_set[0] = 0xff;
125 cap->supported_mcs_set[1] = 0xff;
130 #else /* CONFIG_IEEE80211N */
132 #endif /* CONFIG_IEEE80211N */
136 u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
138 #ifdef CONFIG_IEEE80211N
139 struct ieee80211_ht_operation *oper;
142 if (!hapd->iconf->ieee80211n)
145 *pos++ = WLAN_EID_HT_OPERATION;
146 *pos++ = sizeof(*oper);
148 oper = (struct ieee80211_ht_operation *) pos;
149 os_memset(oper, 0, sizeof(*oper));
151 oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
153 pos += sizeof(*oper);
156 #else /* CONFIG_IEEE80211N */
158 #endif /* CONFIG_IEEE80211N */
162 #ifdef CONFIG_IEEE80211N
166 Set to 0 (HT pure) under the followign conditions
167 - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
168 - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
169 Set to 1 (HT non-member protection) if there may be non-HT STAs
170 in both the primary and the secondary channel
171 Set to 2 if only HT STAs are associated in BSS,
172 however and at least one 20 MHz HT STA is associated
173 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
174 (currently non-GF HT station is considered as non-HT STA also)
176 int hostapd_ht_operation_update(struct hostapd_iface *iface)
178 u16 cur_op_mode, new_op_mode;
179 int op_mode_changes = 0;
181 if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
184 wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
185 __func__, iface->ht_op_mode);
187 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
188 && iface->num_sta_ht_no_gf) {
190 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
192 } else if ((iface->ht_op_mode &
193 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
194 iface->num_sta_ht_no_gf == 0) {
196 ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
200 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
201 (iface->num_sta_no_ht || iface->olbc_ht)) {
202 iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
204 } else if ((iface->ht_op_mode &
205 HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
206 (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
208 ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
212 /* Note: currently we switch to the MIXED op mode if HT non-greenfield
213 * station is associated. Probably it's a theoretical case, since
214 * it looks like all known HT STAs support greenfield.
217 if (iface->num_sta_no_ht ||
218 (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
219 new_op_mode = OP_MODE_MIXED;
220 else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
221 && iface->num_sta_ht_20mhz)
222 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
223 else if (iface->olbc_ht)
224 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
226 new_op_mode = OP_MODE_PURE;
228 cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
229 if (cur_op_mode != new_op_mode) {
230 iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
231 iface->ht_op_mode |= new_op_mode;
235 wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
236 __func__, iface->ht_op_mode, op_mode_changes);
238 return op_mode_changes;
241 #endif /* CONFIG_IEEE80211N */
244 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
247 int capab = WLAN_CAPABILITY_ESS;
250 if (hapd->iface->num_sta_no_short_preamble == 0 &&
251 hapd->iconf->preamble == SHORT_PREAMBLE)
252 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
254 privacy = hapd->conf->ssid.wep.keys_set;
256 if (hapd->conf->ieee802_1x &&
257 (hapd->conf->default_wep_key_len ||
258 hapd->conf->individual_wep_key_len))
265 int policy, def_klen;
266 if (probe && sta->ssid_probe) {
267 policy = sta->ssid_probe->security_policy;
268 def_klen = sta->ssid_probe->wep.default_len;
270 policy = sta->ssid->security_policy;
271 def_klen = sta->ssid->wep.default_len;
273 privacy = policy != SECURITY_PLAINTEXT;
274 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
279 capab |= WLAN_CAPABILITY_PRIVACY;
281 if (hapd->iface->current_mode &&
282 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
283 hapd->iface->num_sta_no_short_slot_time == 0)
284 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
286 if (hapd->iface->dfs_enable)
287 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
293 #define OUI_MICROSOFT 0x0050f2 /* Microsoft (also used in Wi-Fi specs)
296 static int ieee802_11_parse_vendor_specific(struct hostapd_data *hapd,
297 u8 *pos, size_t elen,
298 struct ieee802_11_elems *elems,
303 /* first 3 bytes in vendor specific information element are the IEEE
304 * OUI of the vendor. The following byte is used a vendor specific
308 wpa_printf(MSG_MSGDUMP, "short vendor specific "
309 "information element ignored (len=%lu)",
310 (unsigned long) elen);
315 oui = WPA_GET_BE24(pos);
318 /* Microsoft/Wi-Fi information elements are further typed and
322 /* Microsoft OUI (00:50:F2) with OUI Type 1:
323 * real WPA information element */
325 elems->wpa_ie_len = elen;
327 case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
329 wpa_printf(MSG_MSGDUMP, "short WME "
330 "information element ignored "
332 (unsigned long) elen);
336 case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
337 case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
339 elems->wme_len = elen;
341 case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
342 elems->wme_tspec = pos;
343 elems->wme_tspec_len = elen;
346 wpa_printf(MSG_MSGDUMP, "unknown WME "
347 "information element ignored "
348 "(subtype=%d len=%lu)",
349 pos[4], (unsigned long) elen);
354 wpa_printf(MSG_MSGDUMP, "Unknown Microsoft "
355 "information element ignored "
356 "(type=%d len=%lu)\n",
357 pos[3], (unsigned long) elen);
363 wpa_printf(MSG_MSGDUMP, "unknown vendor specific information "
364 "element ignored (vendor OUI %02x:%02x:%02x "
366 pos[0], pos[1], pos[2], (unsigned long) elen);
374 ParseRes ieee802_11_parse_elems(struct hostapd_data *hapd, u8 *start,
376 struct ieee802_11_elems *elems,
383 os_memset(elems, 0, sizeof(*elems));
394 wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
395 "parse failed (id=%d elen=%d "
397 id, elen, (unsigned long) left);
398 wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
406 elems->ssid_len = elen;
408 case WLAN_EID_SUPP_RATES:
409 elems->supp_rates = pos;
410 elems->supp_rates_len = elen;
412 case WLAN_EID_FH_PARAMS:
413 elems->fh_params = pos;
414 elems->fh_params_len = elen;
416 case WLAN_EID_DS_PARAMS:
417 elems->ds_params = pos;
418 elems->ds_params_len = elen;
420 case WLAN_EID_CF_PARAMS:
421 elems->cf_params = pos;
422 elems->cf_params_len = elen;
426 elems->tim_len = elen;
428 case WLAN_EID_IBSS_PARAMS:
429 elems->ibss_params = pos;
430 elems->ibss_params_len = elen;
432 case WLAN_EID_CHALLENGE:
433 elems->challenge = pos;
434 elems->challenge_len = elen;
436 case WLAN_EID_ERP_INFO:
437 elems->erp_info = pos;
438 elems->erp_info_len = elen;
440 case WLAN_EID_EXT_SUPP_RATES:
441 elems->ext_supp_rates = pos;
442 elems->ext_supp_rates_len = elen;
444 case WLAN_EID_VENDOR_SPECIFIC:
445 if (ieee802_11_parse_vendor_specific(hapd, pos, elen,
452 elems->rsn_ie_len = elen;
454 case WLAN_EID_PWR_CAPABILITY:
455 elems->power_cap = pos;
456 elems->power_cap_len = elen;
458 case WLAN_EID_SUPPORTED_CHANNELS:
459 elems->supp_channels = pos;
460 elems->supp_channels_len = elen;
462 case WLAN_EID_MOBILITY_DOMAIN:
464 elems->mdie_len = elen;
466 case WLAN_EID_FAST_BSS_TRANSITION:
468 elems->ftie_len = elen;
470 case WLAN_EID_HT_CAP:
471 elems->ht_capabilities = pos;
472 elems->ht_capabilities_len = elen;
474 case WLAN_EID_HT_OPERATION:
475 elems->ht_operation = pos;
476 elems->ht_operation_len = elen;
482 wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
483 "ignored unknown element (id=%d elen=%d)",
495 return unknown ? ParseUnknown : ParseOK;
499 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
502 if (len > HOSTAPD_MAX_SSID_LEN)
503 len = HOSTAPD_MAX_SSID_LEN;
504 for (i = 0; i < len; i++) {
505 if (ssid[i] >= 32 && ssid[i] < 127)
514 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
516 struct ieee80211_mgmt mgmt;
519 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
521 "deauthenticate - reason %d", reason);
522 os_snprintf(buf, sizeof(buf), "SEND-DEAUTHENTICATE %d", reason);
523 os_memset(&mgmt, 0, sizeof(mgmt));
524 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
525 WLAN_FC_STYPE_DEAUTH);
526 os_memcpy(mgmt.da, addr, ETH_ALEN);
527 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
528 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
529 mgmt.u.deauth.reason_code = host_to_le16(reason);
530 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
531 sizeof(mgmt.u.deauth), 0) < 0)
532 perror("ieee802_11_send_deauth: send");
536 static void ieee802_11_sta_authenticate(void *eloop_ctx, void *timeout_ctx)
538 struct hostapd_data *hapd = eloop_ctx;
539 struct ieee80211_mgmt mgmt;
542 if (hapd->assoc_ap_state == WAIT_BEACON)
543 hapd->assoc_ap_state = AUTHENTICATE;
544 if (hapd->assoc_ap_state != AUTHENTICATE)
547 ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
548 hapd->assoc_ap_ssid_len);
549 printf("Authenticate with AP " MACSTR " SSID=%s (as station)\n",
550 MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
552 os_memset(&mgmt, 0, sizeof(mgmt));
553 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
555 /* Request TX callback */
556 mgmt.frame_control |= host_to_le16(BIT(1));
557 os_memcpy(mgmt.da, hapd->conf->assoc_ap_addr, ETH_ALEN);
558 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
559 os_memcpy(mgmt.bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
560 mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
561 mgmt.u.auth.auth_transaction = host_to_le16(1);
562 mgmt.u.auth.status_code = host_to_le16(0);
563 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
564 sizeof(mgmt.u.auth), 0) < 0)
565 perror("ieee802_11_sta_authenticate: send");
567 /* Try to authenticate again, if this attempt fails or times out. */
568 eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
572 static void ieee802_11_sta_associate(void *eloop_ctx, void *timeout_ctx)
574 struct hostapd_data *hapd = eloop_ctx;
576 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
580 if (hapd->assoc_ap_state == AUTHENTICATE)
581 hapd->assoc_ap_state = ASSOCIATE;
582 if (hapd->assoc_ap_state != ASSOCIATE)
585 ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
586 hapd->assoc_ap_ssid_len);
587 printf("Associate with AP " MACSTR " SSID=%s (as station)\n",
588 MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
590 os_memset(mgmt, 0, sizeof(*mgmt));
591 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
592 WLAN_FC_STYPE_ASSOC_REQ);
593 /* Request TX callback */
594 mgmt->frame_control |= host_to_le16(BIT(1));
595 os_memcpy(mgmt->da, hapd->conf->assoc_ap_addr, ETH_ALEN);
596 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
597 os_memcpy(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
598 mgmt->u.assoc_req.capab_info = host_to_le16(0);
599 mgmt->u.assoc_req.listen_interval = host_to_le16(1);
600 p = &mgmt->u.assoc_req.variable[0];
602 *p++ = WLAN_EID_SSID;
603 *p++ = hapd->assoc_ap_ssid_len;
604 os_memcpy(p, hapd->assoc_ap_ssid, hapd->assoc_ap_ssid_len);
605 p += hapd->assoc_ap_ssid_len;
607 p = hostapd_eid_supp_rates(hapd, p);
608 p = hostapd_eid_ext_supp_rates(hapd, p);
610 if (hostapd_send_mgmt_frame(hapd, mgmt, p - (u8 *) mgmt, 0) < 0)
611 perror("ieee802_11_sta_associate: send");
613 /* Try to authenticate again, if this attempt fails or times out. */
614 eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
618 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
619 u16 auth_transaction, u8 *challenge, int iswep)
621 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
623 "authentication (shared key, transaction %d)",
626 if (auth_transaction == 1) {
627 if (!sta->challenge) {
628 /* Generate a pseudo-random challenge */
632 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
633 if (sta->challenge == NULL)
634 return WLAN_STATUS_UNSPECIFIED_FAILURE;
638 os_memcpy(key, &now, 4);
639 os_memcpy(key + 4, &r, 4);
640 rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
646 if (auth_transaction != 3)
647 return WLAN_STATUS_UNSPECIFIED_FAILURE;
650 if (!iswep || !sta->challenge || !challenge ||
651 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
652 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
654 "shared key authentication - invalid "
655 "challenge-response");
656 return WLAN_STATUS_CHALLENGE_FAIL;
659 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
661 "authentication OK (shared key)");
662 #ifdef IEEE80211_REQUIRE_AUTH_ACK
663 /* Station will be marked authenticated if it ACKs the
664 * authentication reply. */
666 sta->flags |= WLAN_STA_AUTH;
667 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
669 os_free(sta->challenge);
670 sta->challenge = NULL;
676 static void send_auth_reply(struct hostapd_data *hapd,
677 const u8 *dst, const u8 *bssid,
678 u16 auth_alg, u16 auth_transaction, u16 resp,
679 const u8 *ies, size_t ies_len)
681 struct ieee80211_mgmt *reply;
685 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
686 buf = os_zalloc(rlen);
690 reply = (struct ieee80211_mgmt *) buf;
691 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
693 /* Request TX callback */
694 reply->frame_control |= host_to_le16(BIT(1));
695 os_memcpy(reply->da, dst, ETH_ALEN);
696 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
697 os_memcpy(reply->bssid, bssid, ETH_ALEN);
699 reply->u.auth.auth_alg = host_to_le16(auth_alg);
700 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
701 reply->u.auth.status_code = host_to_le16(resp);
704 os_memcpy(reply->u.auth.variable, ies, ies_len);
706 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
707 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
708 MAC2STR(dst), auth_alg, auth_transaction,
709 resp, (unsigned long) ies_len);
710 if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
711 perror("send_auth_reply: send");
717 #ifdef CONFIG_IEEE80211R
718 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
719 u16 auth_transaction, u16 status,
720 const u8 *ies, size_t ies_len)
722 struct hostapd_data *hapd = ctx;
723 struct sta_info *sta;
725 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
726 status, ies, ies_len);
728 if (status != WLAN_STATUS_SUCCESS)
731 sta = ap_get_sta(hapd, dst);
735 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
736 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
737 sta->flags |= WLAN_STA_AUTH;
738 mlme_authenticate_indication(hapd, sta);
740 #endif /* CONFIG_IEEE80211R */
743 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
746 u16 auth_alg, auth_transaction, status_code;
747 u16 resp = WLAN_STATUS_SUCCESS;
748 struct sta_info *sta = NULL;
751 u8 *challenge = NULL;
752 u32 session_timeout, acct_interim_interval;
754 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
755 size_t resp_ies_len = 0;
757 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
758 printf("handle_auth - too short payload (len=%lu)\n",
759 (unsigned long) len);
763 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
764 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
765 status_code = le_to_host16(mgmt->u.auth.status_code);
766 fc = le_to_host16(mgmt->frame_control);
768 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
769 2 + WLAN_AUTH_CHALLENGE_LEN &&
770 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
771 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
772 challenge = &mgmt->u.auth.variable[2];
774 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
775 "auth_transaction=%d status_code=%d wep=%d%s",
776 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
777 status_code, !!(fc & WLAN_FC_ISWEP),
778 challenge ? " challenge" : "");
780 if (hapd->assoc_ap_state == AUTHENTICATE && auth_transaction == 2 &&
781 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0 &&
782 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
783 if (status_code != 0) {
784 printf("Authentication (as station) with AP "
785 MACSTR " failed (status_code=%d)\n",
786 MAC2STR(hapd->conf->assoc_ap_addr),
790 printf("Authenticated (as station) with AP " MACSTR "\n",
791 MAC2STR(hapd->conf->assoc_ap_addr));
792 ieee802_11_sta_associate(hapd, NULL);
796 if (hapd->tkip_countermeasures) {
797 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
801 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
802 auth_alg == WLAN_AUTH_OPEN) ||
803 #ifdef CONFIG_IEEE80211R
805 (hapd->conf->wpa_key_mgmt &
806 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
807 auth_alg == WLAN_AUTH_FT) ||
808 #endif /* CONFIG_IEEE80211R */
809 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
810 auth_alg == WLAN_AUTH_SHARED_KEY))) {
811 printf("Unsupported authentication algorithm (%d)\n",
813 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
817 if (!(auth_transaction == 1 ||
818 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
819 printf("Unknown authentication transaction number (%d)\n",
821 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
825 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
826 printf("Station " MACSTR " not allowed to authenticate.\n",
828 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
832 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
834 &acct_interim_interval, &vlan_id);
835 if (res == HOSTAPD_ACL_REJECT) {
836 printf("Station " MACSTR " not allowed to authenticate.\n",
838 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
841 if (res == HOSTAPD_ACL_PENDING) {
842 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
843 " waiting for an external authentication",
845 /* Authentication code will re-send the authentication frame
846 * after it has received (and cached) information from the
847 * external source. */
851 sta = ap_sta_add(hapd, mgmt->sa);
853 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
858 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
859 sta->vlan_id) == NULL) {
860 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
861 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
862 "%d received from RADIUS server",
864 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
867 sta->vlan_id = vlan_id;
868 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
869 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
872 sta->flags &= ~WLAN_STA_PREAUTH;
873 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
875 if (hapd->conf->radius->acct_interim_interval == 0 &&
876 acct_interim_interval)
877 sta->acct_interim_interval = acct_interim_interval;
878 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
879 ap_sta_session_timeout(hapd, sta, session_timeout);
881 ap_sta_no_session_timeout(hapd, sta);
885 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
887 "authentication OK (open system)");
888 #ifdef IEEE80211_REQUIRE_AUTH_ACK
889 /* Station will be marked authenticated if it ACKs the
890 * authentication reply. */
892 sta->flags |= WLAN_STA_AUTH;
893 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
894 sta->auth_alg = WLAN_AUTH_OPEN;
895 mlme_authenticate_indication(hapd, sta);
898 case WLAN_AUTH_SHARED_KEY:
899 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
901 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
902 mlme_authenticate_indication(hapd, sta);
903 if (sta->challenge && auth_transaction == 1) {
904 resp_ies[0] = WLAN_EID_CHALLENGE;
905 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
906 os_memcpy(resp_ies + 2, sta->challenge,
907 WLAN_AUTH_CHALLENGE_LEN);
908 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
911 #ifdef CONFIG_IEEE80211R
913 sta->auth_alg = WLAN_AUTH_FT;
914 if (sta->wpa_sm == NULL)
915 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
917 if (sta->wpa_sm == NULL) {
918 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
920 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
923 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
924 auth_transaction, mgmt->u.auth.variable,
925 len - IEEE80211_HDRLEN -
926 sizeof(mgmt->u.auth),
927 handle_auth_ft_finish, hapd);
928 /* handle_auth_ft_finish() callback will complete auth. */
930 #endif /* CONFIG_IEEE80211R */
934 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
935 auth_transaction + 1, resp, resp_ies, resp_ies_len);
939 static void handle_assoc(struct hostapd_data *hapd,
940 struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
942 u16 capab_info, listen_interval;
943 u16 resp = WLAN_STATUS_SUCCESS;
946 int send_deauth = 0, send_len, left, i;
947 struct sta_info *sta;
948 struct ieee802_11_elems elems;
949 u8 buf[sizeof(struct ieee80211_mgmt) + 512];
950 struct ieee80211_mgmt *reply;
952 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
953 sizeof(mgmt->u.assoc_req))) {
954 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
955 "\n", reassoc, (unsigned long) len);
960 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
961 listen_interval = le_to_host16(
962 mgmt->u.reassoc_req.listen_interval);
963 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
964 " capab_info=0x%02x listen_interval=%d current_ap="
966 MAC2STR(mgmt->sa), capab_info, listen_interval,
967 MAC2STR(mgmt->u.reassoc_req.current_ap));
968 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
969 pos = mgmt->u.reassoc_req.variable;
971 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
972 listen_interval = le_to_host16(
973 mgmt->u.assoc_req.listen_interval);
974 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
975 " capab_info=0x%02x listen_interval=%d",
976 MAC2STR(mgmt->sa), capab_info, listen_interval);
977 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
978 pos = mgmt->u.assoc_req.variable;
981 sta = ap_get_sta(hapd, mgmt->sa);
982 #ifdef CONFIG_IEEE80211R
983 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
984 (sta->flags & WLAN_STA_AUTH) == 0) {
985 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
986 "prior to authentication since it is using "
987 "over-the-DS FT", MAC2STR(mgmt->sa));
989 #endif /* CONFIG_IEEE80211R */
990 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
991 printf("STA " MACSTR " trying to associate before "
992 "authentication\n", MAC2STR(mgmt->sa));
994 printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
995 MAC2STR(sta->addr), sta->aid, sta->flags);
998 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1002 if (hapd->tkip_countermeasures) {
1003 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1007 if (listen_interval > hapd->conf->max_listen_interval) {
1008 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1009 HOSTAPD_LEVEL_DEBUG,
1010 "Too large Listen Interval (%d)",
1012 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1017 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1021 sta->capability = capab_info;
1023 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1025 if (ieee802_11_parse_elems(hapd, pos, left, &elems, 1) == ParseFailed
1027 printf("STA " MACSTR " sent invalid association request\n",
1028 MAC2STR(sta->addr));
1029 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1033 if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1034 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
1037 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
1038 printf("Station " MACSTR " tried to associate with "
1039 "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
1040 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1044 sta->flags &= ~WLAN_STA_WME;
1045 if (elems.wme && hapd->conf->wme_enabled) {
1046 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
1047 hostapd_logger(hapd, sta->addr,
1049 HOSTAPD_LEVEL_DEBUG,
1050 "invalid WME element in association "
1053 sta->flags |= WLAN_STA_WME;
1056 if (!elems.supp_rates) {
1057 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1058 HOSTAPD_LEVEL_DEBUG,
1059 "No supported rates element in AssocReq");
1060 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1064 if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
1065 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1066 HOSTAPD_LEVEL_DEBUG,
1067 "Invalid supported rates element length %d",
1068 elems.supp_rates_len);
1069 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1073 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
1074 os_memcpy(sta->supported_rates, elems.supp_rates,
1075 elems.supp_rates_len);
1076 sta->supported_rates_len = elems.supp_rates_len;
1078 if (elems.ext_supp_rates) {
1079 if (elems.supp_rates_len + elems.ext_supp_rates_len >
1080 sizeof(sta->supported_rates)) {
1081 hostapd_logger(hapd, mgmt->sa,
1082 HOSTAPD_MODULE_IEEE80211,
1083 HOSTAPD_LEVEL_DEBUG,
1084 "Invalid supported rates element length"
1085 " %d+%d", elems.supp_rates_len,
1086 elems.ext_supp_rates_len);
1087 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1091 os_memcpy(sta->supported_rates + elems.supp_rates_len,
1092 elems.ext_supp_rates, elems.ext_supp_rates_len);
1093 sta->supported_rates_len += elems.ext_supp_rates_len;
1096 #ifdef CONFIG_IEEE80211N
1097 /* save HT capabilities in the sta object */
1098 os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
1099 if (elems.ht_capabilities &&
1100 elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_capability)
1101 && (sta->flags & WLAN_STA_WME)) {
1102 /* note: without WMM capability, treat the sta as non-HT */
1103 sta->flags |= WLAN_STA_HT;
1104 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
1105 sta->ht_capabilities.length =
1106 sizeof(struct ieee80211_ht_capability);
1107 os_memcpy(&sta->ht_capabilities.data,
1108 elems.ht_capabilities,
1109 sizeof(struct ieee80211_ht_capability));
1111 sta->flags &= ~WLAN_STA_HT;
1112 #endif /* CONFIG_IEEE80211N */
1114 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1115 wpa_ie = elems.rsn_ie;
1116 wpa_ie_len = elems.rsn_ie_len;
1117 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1119 wpa_ie = elems.wpa_ie;
1120 wpa_ie_len = elems.wpa_ie_len;
1125 if (hapd->conf->wpa && wpa_ie == NULL) {
1126 printf("STA " MACSTR ": No WPA/RSN IE in association "
1127 "request\n", MAC2STR(sta->addr));
1128 resp = WLAN_STATUS_INVALID_IE;
1132 if (hapd->conf->wpa && wpa_ie) {
1136 if (sta->wpa_sm == NULL)
1137 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1139 if (sta->wpa_sm == NULL) {
1140 printf("Failed to initialize WPA state machine\n");
1141 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1144 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1146 elems.mdie, elems.mdie_len);
1147 if (res == WPA_INVALID_GROUP)
1148 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1149 else if (res == WPA_INVALID_PAIRWISE)
1150 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1151 else if (res == WPA_INVALID_AKMP)
1152 resp = WLAN_STATUS_AKMP_NOT_VALID;
1153 else if (res == WPA_ALLOC_FAIL)
1154 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1155 #ifdef CONFIG_IEEE80211W
1156 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1157 resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
1158 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1159 resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
1160 #endif /* CONFIG_IEEE80211W */
1161 else if (res == WPA_INVALID_MDIE)
1162 resp = WLAN_STATUS_INVALID_MDIE;
1163 else if (res != WPA_IE_OK)
1164 resp = WLAN_STATUS_INVALID_IE;
1165 if (resp != WLAN_STATUS_SUCCESS)
1167 #ifdef CONFIG_IEEE80211W
1168 if (wpa_auth_uses_mfp(sta->wpa_sm))
1169 sta->flags |= WLAN_STA_MFP;
1171 sta->flags &= ~WLAN_STA_MFP;
1172 #endif /* CONFIG_IEEE80211W */
1174 #ifdef CONFIG_IEEE80211R
1175 if (sta->auth_alg == WLAN_AUTH_FT) {
1177 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1178 "to use association (not "
1179 "re-association) with FT auth_alg",
1180 MAC2STR(sta->addr));
1181 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1185 resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
1186 if (resp != WLAN_STATUS_SUCCESS)
1189 #endif /* CONFIG_IEEE80211R */
1192 if (hapd->iface->dfs_enable &&
1193 hapd->iconf->ieee80211h == SPECT_STRICT_BINDING) {
1194 if (hostapd_check_power_cap(hapd, elems.power_cap,
1195 elems.power_cap_len)) {
1196 resp = WLAN_STATUS_PWR_CAPABILITY_NOT_VALID;
1197 hostapd_logger(hapd, sta->addr,
1198 HOSTAPD_MODULE_IEEE80211,
1199 HOSTAPD_LEVEL_DEBUG,
1200 "Power capabilities of the station not "
1206 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1207 sta->flags |= WLAN_STA_NONERP;
1208 for (i = 0; i < sta->supported_rates_len; i++) {
1209 if ((sta->supported_rates[i] & 0x7f) > 22) {
1210 sta->flags &= ~WLAN_STA_NONERP;
1214 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1215 sta->nonerp_set = 1;
1216 hapd->iface->num_sta_non_erp++;
1217 if (hapd->iface->num_sta_non_erp == 1)
1218 ieee802_11_set_beacons(hapd->iface);
1221 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1222 !sta->no_short_slot_time_set) {
1223 sta->no_short_slot_time_set = 1;
1224 hapd->iface->num_sta_no_short_slot_time++;
1225 if (hapd->iface->current_mode->mode ==
1226 HOSTAPD_MODE_IEEE80211G &&
1227 hapd->iface->num_sta_no_short_slot_time == 1)
1228 ieee802_11_set_beacons(hapd->iface);
1231 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1232 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1234 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1236 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1237 !sta->no_short_preamble_set) {
1238 sta->no_short_preamble_set = 1;
1239 hapd->iface->num_sta_no_short_preamble++;
1240 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1241 && hapd->iface->num_sta_no_short_preamble == 1)
1242 ieee802_11_set_beacons(hapd->iface);
1245 #ifdef CONFIG_IEEE80211N
1246 if (sta->flags & WLAN_STA_HT) {
1247 if ((sta->ht_capabilities.data.capabilities_info &
1248 HT_CAP_INFO_GREEN_FIELD) == 0) {
1249 hapd->iface->num_sta_ht_no_gf++;
1250 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1251 "greenfield, num of non-gf stations %d",
1252 __func__, MAC2STR(sta->addr),
1253 hapd->iface->num_sta_ht_no_gf);
1255 if ((sta->ht_capabilities.data.capabilities_info &
1256 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1257 hapd->iface->num_sta_ht_20mhz++;
1258 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1259 "num of 20MHz HT STAs %d",
1260 __func__, MAC2STR(sta->addr),
1261 hapd->iface->num_sta_ht_20mhz);
1264 hapd->iface->num_sta_no_ht++;
1265 if (hapd->iconf->ieee80211n) {
1266 wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1267 " - no HT, num of non-HT stations %d",
1268 __func__, MAC2STR(sta->addr),
1269 hapd->iface->num_sta_no_ht);
1273 if (hostapd_ht_operation_update(hapd->iface) > 0)
1274 ieee802_11_set_beacons(hapd->iface);
1275 #endif /* CONFIG_IEEE80211N */
1277 /* get a unique AID */
1279 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
1281 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1282 if (hapd->sta_aid[sta->aid - 1] == NULL)
1284 if (sta->aid > MAX_AID_TABLE_SIZE) {
1286 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1287 wpa_printf(MSG_ERROR, " no room for more AIDs");
1290 hapd->sta_aid[sta->aid - 1] = sta;
1291 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
1295 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1296 HOSTAPD_LEVEL_DEBUG,
1297 "association OK (aid %d)", sta->aid);
1298 /* Station will be marked associated, after it acknowledges AssocResp
1301 if (sta->last_assoc_req)
1302 os_free(sta->last_assoc_req);
1303 sta->last_assoc_req = os_malloc(len);
1304 if (sta->last_assoc_req)
1305 os_memcpy(sta->last_assoc_req, mgmt, len);
1307 /* Make sure that the previously registered inactivity timer will not
1308 * remove the STA immediately. */
1309 sta->timeout_next = STA_NULLFUNC;
1312 os_memset(buf, 0, sizeof(buf));
1313 reply = (struct ieee80211_mgmt *) buf;
1314 reply->frame_control =
1315 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1316 (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1317 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1318 WLAN_FC_STYPE_ASSOC_RESP)));
1319 os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1320 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1321 os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1323 send_len = IEEE80211_HDRLEN;
1325 send_len += sizeof(reply->u.deauth);
1326 reply->u.deauth.reason_code = host_to_le16(resp);
1329 send_len += sizeof(reply->u.assoc_resp);
1330 reply->u.assoc_resp.capab_info =
1331 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1332 reply->u.assoc_resp.status_code = host_to_le16(resp);
1333 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1334 | BIT(14) | BIT(15));
1335 /* Supported rates */
1336 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1337 /* Extended supported rates */
1338 p = hostapd_eid_ext_supp_rates(hapd, p);
1339 if (sta->flags & WLAN_STA_WME)
1340 p = hostapd_eid_wme(hapd, p);
1342 p = hostapd_eid_ht_capabilities_info(hapd, p);
1343 p = hostapd_eid_ht_operation(hapd, p);
1345 #ifdef CONFIG_IEEE80211R
1346 if (resp == WLAN_STATUS_SUCCESS) {
1347 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1348 * Transition Information, RSN */
1349 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1350 buf + sizeof(buf) - p,
1353 #endif /* CONFIG_IEEE80211R */
1355 send_len += p - reply->u.assoc_resp.variable;
1357 /* Request TX callback */
1358 reply->frame_control |= host_to_le16(BIT(1));
1361 if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1362 perror("handle_assoc: send");
1366 static void handle_assoc_resp(struct hostapd_data *hapd,
1367 struct ieee80211_mgmt *mgmt, size_t len)
1369 u16 status_code, aid;
1371 if (hapd->assoc_ap_state != ASSOCIATE) {
1372 printf("Unexpected association response received from " MACSTR
1373 "\n", MAC2STR(mgmt->sa));
1377 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
1378 printf("handle_assoc_resp - too short payload (len=%lu)\n",
1379 (unsigned long) len);
1383 if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
1384 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
1385 printf("Received association response from unexpected address "
1386 "(SA=" MACSTR " BSSID=" MACSTR "\n",
1387 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
1391 status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
1392 aid = le_to_host16(mgmt->u.assoc_resp.aid);
1393 aid &= ~(BIT(14) | BIT(15));
1395 if (status_code != 0) {
1396 printf("Association (as station) with AP " MACSTR " failed "
1397 "(status_code=%d)\n",
1398 MAC2STR(hapd->conf->assoc_ap_addr), status_code);
1399 /* Try to authenticate again */
1400 hapd->assoc_ap_state = AUTHENTICATE;
1401 eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
1405 printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
1406 MAC2STR(hapd->conf->assoc_ap_addr), aid);
1407 hapd->assoc_ap_aid = aid;
1408 hapd->assoc_ap_state = ASSOCIATED;
1410 if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
1411 printf("Could not set associated AP address to kernel "
1417 static void handle_disassoc(struct hostapd_data *hapd,
1418 struct ieee80211_mgmt *mgmt, size_t len)
1420 struct sta_info *sta;
1422 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1423 printf("handle_disassoc - too short payload (len=%lu)\n",
1424 (unsigned long) len);
1428 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1430 le_to_host16(mgmt->u.disassoc.reason_code));
1432 if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1433 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1434 printf("Assoc AP " MACSTR " sent disassociation "
1435 "(reason_code=%d) - try to authenticate\n",
1436 MAC2STR(hapd->conf->assoc_ap_addr),
1437 le_to_host16(mgmt->u.disassoc.reason_code));
1438 hapd->assoc_ap_state = AUTHENTICATE;
1439 ieee802_11_sta_authenticate(hapd, NULL);
1440 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1445 sta = ap_get_sta(hapd, mgmt->sa);
1447 printf("Station " MACSTR " trying to disassociate, but it "
1448 "is not associated.\n", MAC2STR(mgmt->sa));
1452 sta->flags &= ~WLAN_STA_ASSOC;
1453 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1454 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1455 HOSTAPD_LEVEL_INFO, "disassociated");
1456 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1457 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1458 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1460 accounting_sta_stop(hapd, sta);
1461 ieee802_1x_free_station(sta);
1462 hostapd_sta_remove(hapd, sta->addr);
1464 if (sta->timeout_next == STA_NULLFUNC ||
1465 sta->timeout_next == STA_DISASSOC) {
1466 sta->timeout_next = STA_DEAUTH;
1467 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1468 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1472 mlme_disassociate_indication(
1473 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1477 static void handle_deauth(struct hostapd_data *hapd,
1478 struct ieee80211_mgmt *mgmt, size_t len)
1480 struct sta_info *sta;
1482 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1483 printf("handle_deauth - too short payload (len=%lu)\n",
1484 (unsigned long) len);
1488 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1491 le_to_host16(mgmt->u.deauth.reason_code));
1493 if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1494 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1495 printf("Assoc AP " MACSTR " sent deauthentication "
1496 "(reason_code=%d) - try to authenticate\n",
1497 MAC2STR(hapd->conf->assoc_ap_addr),
1498 le_to_host16(mgmt->u.deauth.reason_code));
1499 hapd->assoc_ap_state = AUTHENTICATE;
1500 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1505 sta = ap_get_sta(hapd, mgmt->sa);
1507 printf("Station " MACSTR " trying to deauthenticate, but it "
1508 "is not authenticated.\n", MAC2STR(mgmt->sa));
1512 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1513 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1514 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1515 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1516 mlme_deauthenticate_indication(
1517 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1518 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1519 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1520 ap_free_sta(hapd, sta);
1524 static void handle_beacon(struct hostapd_data *hapd,
1525 struct ieee80211_mgmt *mgmt, size_t len,
1526 struct hostapd_frame_info *fi)
1528 struct ieee802_11_elems elems;
1530 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1531 printf("handle_beacon - too short payload (len=%lu)\n",
1532 (unsigned long) len);
1536 (void) ieee802_11_parse_elems(hapd, mgmt->u.beacon.variable,
1537 len - (IEEE80211_HDRLEN +
1538 sizeof(mgmt->u.beacon)), &elems,
1541 if (hapd->assoc_ap_state == WAIT_BEACON &&
1542 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1543 if (elems.ssid && elems.ssid_len <= 32) {
1544 os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
1546 hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
1547 hapd->assoc_ap_ssid_len = elems.ssid_len;
1549 ieee802_11_sta_authenticate(hapd, NULL);
1552 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1556 static void handle_action(struct hostapd_data *hapd,
1557 struct ieee80211_mgmt *mgmt, size_t len)
1559 if (len < IEEE80211_HDRLEN + 1) {
1560 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1561 HOSTAPD_LEVEL_DEBUG,
1562 "handle_action - too short payload (len=%lu)",
1563 (unsigned long) len);
1567 switch (mgmt->u.action.category) {
1568 #ifdef CONFIG_IEEE80211R
1569 case WLAN_ACTION_FT:
1571 struct sta_info *sta;
1573 sta = ap_get_sta(hapd, mgmt->sa);
1574 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1575 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1576 "frame from unassociated STA " MACSTR,
1581 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1582 len - IEEE80211_HDRLEN))
1587 #endif /* CONFIG_IEEE80211R */
1588 case WME_ACTION_CATEGORY:
1589 hostapd_wme_action(hapd, mgmt, len);
1593 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1594 HOSTAPD_LEVEL_DEBUG,
1595 "handle_action - unknown action category %d or invalid "
1597 mgmt->u.action.category);
1598 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1599 !(mgmt->sa[0] & 0x01)) {
1601 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1602 * Return the Action frame to the source without change
1603 * except that MSB of the Category set to 1.
1605 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1606 "frame back to sender");
1607 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1608 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1609 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1610 mgmt->u.action.category |= 0x80;
1612 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1618 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1619 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1621 * @buf: management frame data (starting from IEEE 802.11 header)
1622 * @len: length of frame data in octets
1623 * @stype: management frame subtype from frame control field
1625 * Process all incoming IEEE 802.11 management frames. This will be called for
1626 * each frame received from the kernel driver through wlan#ap interface. In
1627 * addition, it can be called to re-inserted pending frames (e.g., when using
1628 * external RADIUS server as an MAC ACL).
1630 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1631 struct hostapd_frame_info *fi)
1633 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1636 if (stype == WLAN_FC_STYPE_BEACON) {
1637 handle_beacon(hapd, mgmt, len, fi);
1641 if (fi && fi->passive_scan)
1644 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1645 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1646 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1649 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
1650 (hapd->assoc_ap_state == DO_NOT_ASSOC ||
1651 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
1653 printf("MGMT: BSSID=" MACSTR " not our address\n",
1654 MAC2STR(mgmt->bssid));
1659 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1660 handle_probe_req(hapd, mgmt, len);
1664 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1665 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1666 HOSTAPD_LEVEL_DEBUG,
1667 "MGMT: DA=" MACSTR " not our address",
1673 case WLAN_FC_STYPE_AUTH:
1674 wpa_printf(MSG_DEBUG, "mgmt::auth");
1675 handle_auth(hapd, mgmt, len);
1677 case WLAN_FC_STYPE_ASSOC_REQ:
1678 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1679 handle_assoc(hapd, mgmt, len, 0);
1681 case WLAN_FC_STYPE_ASSOC_RESP:
1682 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
1683 handle_assoc_resp(hapd, mgmt, len);
1685 case WLAN_FC_STYPE_REASSOC_REQ:
1686 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1687 handle_assoc(hapd, mgmt, len, 1);
1689 case WLAN_FC_STYPE_DISASSOC:
1690 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1691 handle_disassoc(hapd, mgmt, len);
1693 case WLAN_FC_STYPE_DEAUTH:
1694 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1695 handle_deauth(hapd, mgmt, len);
1697 case WLAN_FC_STYPE_ACTION:
1698 wpa_printf(MSG_DEBUG, "mgmt::action");
1699 handle_action(hapd, mgmt, len);
1702 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1703 HOSTAPD_LEVEL_DEBUG,
1704 "unknown mgmt frame subtype %d", stype);
1710 static void handle_auth_cb(struct hostapd_data *hapd,
1711 struct ieee80211_mgmt *mgmt,
1714 u16 auth_alg, auth_transaction, status_code;
1715 struct sta_info *sta;
1718 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1719 HOSTAPD_LEVEL_NOTICE,
1720 "did not acknowledge authentication response");
1724 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1725 printf("handle_auth_cb - too short payload (len=%lu)\n",
1726 (unsigned long) len);
1730 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1731 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1732 status_code = le_to_host16(mgmt->u.auth.status_code);
1734 sta = ap_get_sta(hapd, mgmt->da);
1736 printf("handle_auth_cb: STA " MACSTR " not found\n",
1741 if (status_code == WLAN_STATUS_SUCCESS &&
1742 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1743 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1744 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1745 HOSTAPD_LEVEL_INFO, "authenticated");
1746 sta->flags |= WLAN_STA_AUTH;
1751 static void handle_assoc_cb(struct hostapd_data *hapd,
1752 struct ieee80211_mgmt *mgmt,
1753 size_t len, int reassoc, int ok)
1756 struct sta_info *sta;
1758 struct ht_cap_ie *ht_cap = NULL;
1761 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1762 HOSTAPD_LEVEL_DEBUG,
1763 "did not acknowledge association response");
1767 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1768 sizeof(mgmt->u.assoc_resp))) {
1769 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1770 "(len=%lu)\n", reassoc, (unsigned long) len);
1775 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1777 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1779 sta = ap_get_sta(hapd, mgmt->da);
1781 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1786 if (status != WLAN_STATUS_SUCCESS)
1789 /* Stop previous accounting session, if one is started, and allocate
1790 * new session id for the new session. */
1791 accounting_sta_stop(hapd, sta);
1792 accounting_sta_get_id(hapd, sta);
1794 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1796 "associated (aid %d, accounting session %08X-%08X)",
1797 sta->aid, sta->acct_session_id_hi,
1798 sta->acct_session_id_lo);
1800 if (sta->flags & WLAN_STA_ASSOC)
1802 sta->flags |= WLAN_STA_ASSOC;
1805 mlme_reassociate_indication(hapd, sta);
1807 mlme_associate_indication(hapd, sta);
1809 #ifdef CONFIG_IEEE80211N
1810 if (sta->flags & WLAN_STA_HT)
1811 ht_cap = &sta->ht_capabilities;
1812 #endif /* CONFIG_IEEE80211N */
1814 if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1815 sta->capability, sta->supported_rates,
1816 sta->supported_rates_len, 0, sta->listen_interval,
1819 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1820 HOSTAPD_LEVEL_NOTICE,
1821 "Could not add STA to kernel driver");
1824 if (sta->eapol_sm == NULL) {
1826 * This STA does not use RADIUS server for EAP authentication,
1827 * so bind it to the selected VLAN interface now, since the
1828 * interface selection is not going to change anymore.
1830 ap_sta_bind_vlan(hapd, sta, 0);
1831 } else if (sta->vlan_id) {
1832 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1833 ap_sta_bind_vlan(hapd, sta, 0);
1835 if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1836 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1837 WLAN_STA_SHORT_PREAMBLE, ~0);
1839 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1840 0, ~WLAN_STA_SHORT_PREAMBLE);
1843 if (sta->auth_alg == WLAN_AUTH_FT)
1844 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1846 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1847 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1849 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1852 /* Copy of the association request is not needed anymore */
1853 if (sta->last_assoc_req) {
1854 os_free(sta->last_assoc_req);
1855 sta->last_assoc_req = NULL;
1860 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1863 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1866 case WLAN_FC_STYPE_AUTH:
1867 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1868 handle_auth_cb(hapd, mgmt, len, ok);
1870 case WLAN_FC_STYPE_ASSOC_RESP:
1871 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1872 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1874 case WLAN_FC_STYPE_REASSOC_RESP:
1875 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1876 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1878 case WLAN_FC_STYPE_PROBE_RESP:
1879 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1881 case WLAN_FC_STYPE_DEAUTH:
1885 printf("unknown mgmt cb frame subtype %d\n", stype);
1891 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1894 struct hostapd_data *hapd = eloop_ctx;
1895 hapd->tkip_countermeasures = 0;
1896 hostapd_set_countermeasures(hapd, 0);
1897 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1898 HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1902 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1904 struct sta_info *sta;
1906 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1907 HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1909 wpa_auth_countermeasures_start(hapd->wpa_auth);
1910 hapd->tkip_countermeasures = 1;
1911 hostapd_set_countermeasures(hapd, 1);
1912 wpa_gtk_rekey(hapd->wpa_auth);
1913 eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
1914 eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
1916 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
1917 hostapd_sta_deauth(hapd, sta->addr,
1918 WLAN_REASON_MICHAEL_MIC_FAILURE);
1919 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1920 WLAN_STA_AUTHORIZED);
1921 hostapd_sta_remove(hapd, sta->addr);
1926 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
1931 if (addr && local) {
1932 struct sta_info *sta = ap_get_sta(hapd, addr);
1934 wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
1935 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1937 "Michael MIC failure detected in "
1939 mlme_michaelmicfailure_indication(hapd, addr);
1941 wpa_printf(MSG_DEBUG,
1942 "MLME-MICHAELMICFAILURE.indication "
1943 "for not associated STA (" MACSTR
1944 ") ignored", MAC2STR(addr));
1950 if (now > hapd->michael_mic_failure + 60) {
1951 hapd->michael_mic_failures = 1;
1953 hapd->michael_mic_failures++;
1954 if (hapd->michael_mic_failures > 1)
1955 ieee80211_tkip_countermeasures_start(hapd);
1957 hapd->michael_mic_failure = now;
1961 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1968 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1969 char *buf, size_t buflen)
1975 #endif /* CONFIG_NATIVE_WINDOWS */