2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2007-2008, Intel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
18 #ifndef CONFIG_NATIVE_WINDOWS
24 #include "ieee802_11.h"
26 #include "hw_features.h"
27 #include "radius/radius.h"
28 #include "radius/radius_client.h"
29 #include "ieee802_11_auth.h"
32 #include "ieee802_1x.h"
36 #include "accounting.h"
41 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
46 if (hapd->iface->current_rates == NULL)
49 *pos++ = WLAN_EID_SUPP_RATES;
50 num = hapd->iface->num_rates;
52 /* rest of the rates are encoded in Extended supported
59 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
62 *pos = hapd->iface->current_rates[i].rate / 5;
63 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
72 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
77 if (hapd->iface->current_rates == NULL)
80 num = hapd->iface->num_rates;
85 *pos++ = WLAN_EID_EXT_SUPP_RATES;
88 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
92 continue; /* already in SuppRates IE */
93 *pos = hapd->iface->current_rates[i].rate / 5;
94 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
103 u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
105 #ifdef CONFIG_IEEE80211N
106 struct ieee80211_ht_capability *cap;
109 if (!hapd->iconf->ieee80211n)
112 *pos++ = WLAN_EID_HT_CAP;
113 *pos++ = sizeof(*cap);
115 cap = (struct ieee80211_ht_capability *) pos;
116 os_memset(cap, 0, sizeof(*cap));
117 SET_2BIT_U8(&cap->mac_ht_params_info,
118 MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
119 MAX_RX_AMPDU_FACTOR_64KB);
121 cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
123 cap->supported_mcs_set[0] = 0xff;
124 cap->supported_mcs_set[1] = 0xff;
129 #else /* CONFIG_IEEE80211N */
131 #endif /* CONFIG_IEEE80211N */
135 u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
137 #ifdef CONFIG_IEEE80211N
138 struct ieee80211_ht_operation *oper;
141 if (!hapd->iconf->ieee80211n)
144 *pos++ = WLAN_EID_HT_OPERATION;
145 *pos++ = sizeof(*oper);
147 oper = (struct ieee80211_ht_operation *) pos;
148 os_memset(oper, 0, sizeof(*oper));
150 oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
152 pos += sizeof(*oper);
155 #else /* CONFIG_IEEE80211N */
157 #endif /* CONFIG_IEEE80211N */
161 #ifdef CONFIG_IEEE80211N
165 Set to 0 (HT pure) under the followign conditions
166 - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
167 - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
168 Set to 1 (HT non-member protection) if there may be non-HT STAs
169 in both the primary and the secondary channel
170 Set to 2 if only HT STAs are associated in BSS,
171 however and at least one 20 MHz HT STA is associated
172 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
173 (currently non-GF HT station is considered as non-HT STA also)
175 int hostapd_ht_operation_update(struct hostapd_iface *iface)
177 u16 cur_op_mode, new_op_mode;
178 int op_mode_changes = 0;
180 if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
183 wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
184 __func__, iface->ht_op_mode);
186 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
187 && iface->num_sta_ht_no_gf) {
189 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
191 } else if ((iface->ht_op_mode &
192 HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
193 iface->num_sta_ht_no_gf == 0) {
195 ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
199 if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
200 (iface->num_sta_no_ht || iface->olbc_ht)) {
201 iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
203 } else if ((iface->ht_op_mode &
204 HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
205 (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
207 ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
211 /* Note: currently we switch to the MIXED op mode if HT non-greenfield
212 * station is associated. Probably it's a theoretical case, since
213 * it looks like all known HT STAs support greenfield.
216 if (iface->num_sta_no_ht ||
217 (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
218 new_op_mode = OP_MODE_MIXED;
219 else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
220 && iface->num_sta_ht_20mhz)
221 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
222 else if (iface->olbc_ht)
223 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
225 new_op_mode = OP_MODE_PURE;
227 cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
228 if (cur_op_mode != new_op_mode) {
229 iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
230 iface->ht_op_mode |= new_op_mode;
234 wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
235 __func__, iface->ht_op_mode, op_mode_changes);
237 return op_mode_changes;
240 #endif /* CONFIG_IEEE80211N */
243 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
246 int capab = WLAN_CAPABILITY_ESS;
249 if (hapd->iface->num_sta_no_short_preamble == 0 &&
250 hapd->iconf->preamble == SHORT_PREAMBLE)
251 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
253 privacy = hapd->conf->ssid.wep.keys_set;
255 if (hapd->conf->ieee802_1x &&
256 (hapd->conf->default_wep_key_len ||
257 hapd->conf->individual_wep_key_len))
264 int policy, def_klen;
265 if (probe && sta->ssid_probe) {
266 policy = sta->ssid_probe->security_policy;
267 def_klen = sta->ssid_probe->wep.default_len;
269 policy = sta->ssid->security_policy;
270 def_klen = sta->ssid->wep.default_len;
272 privacy = policy != SECURITY_PLAINTEXT;
273 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
278 capab |= WLAN_CAPABILITY_PRIVACY;
280 if (hapd->iface->current_mode &&
281 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
282 hapd->iface->num_sta_no_short_slot_time == 0)
283 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
289 #ifdef CONFIG_IEEE80211W
290 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
291 struct sta_info *sta, u8 *eid)
296 *pos++ = WLAN_EID_ASSOC_COMEBACK_TIME;
298 timeout = (hapd->conf->assoc_ping_attempts - sta->ping_count + 1) *
299 hapd->conf->assoc_ping_timeout;
300 WPA_PUT_LE32(pos, timeout);
305 #endif /* CONFIG_IEEE80211W */
308 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
311 if (len > HOSTAPD_MAX_SSID_LEN)
312 len = HOSTAPD_MAX_SSID_LEN;
313 for (i = 0; i < len; i++) {
314 if (ssid[i] >= 32 && ssid[i] < 127)
323 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
325 struct ieee80211_mgmt mgmt;
327 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
329 "deauthenticate - reason %d", reason);
330 os_memset(&mgmt, 0, sizeof(mgmt));
331 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
332 WLAN_FC_STYPE_DEAUTH);
333 os_memcpy(mgmt.da, addr, ETH_ALEN);
334 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
335 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
336 mgmt.u.deauth.reason_code = host_to_le16(reason);
337 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
338 sizeof(mgmt.u.deauth), 0) < 0)
339 perror("ieee802_11_send_deauth: send");
343 static void ieee802_11_sta_authenticate(void *eloop_ctx, void *timeout_ctx)
345 struct hostapd_data *hapd = eloop_ctx;
346 struct ieee80211_mgmt mgmt;
349 if (hapd->assoc_ap_state == WAIT_BEACON)
350 hapd->assoc_ap_state = AUTHENTICATE;
351 if (hapd->assoc_ap_state != AUTHENTICATE)
354 ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
355 hapd->assoc_ap_ssid_len);
356 printf("Authenticate with AP " MACSTR " SSID=%s (as station)\n",
357 MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
359 os_memset(&mgmt, 0, sizeof(mgmt));
360 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
362 os_memcpy(mgmt.da, hapd->conf->assoc_ap_addr, ETH_ALEN);
363 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
364 os_memcpy(mgmt.bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
365 mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
366 mgmt.u.auth.auth_transaction = host_to_le16(1);
367 mgmt.u.auth.status_code = host_to_le16(0);
368 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
369 sizeof(mgmt.u.auth), 0) < 0)
370 perror("ieee802_11_sta_authenticate: send");
372 /* Try to authenticate again, if this attempt fails or times out. */
373 eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
377 static void ieee802_11_sta_associate(void *eloop_ctx, void *timeout_ctx)
379 struct hostapd_data *hapd = eloop_ctx;
381 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
385 if (hapd->assoc_ap_state == AUTHENTICATE)
386 hapd->assoc_ap_state = ASSOCIATE;
387 if (hapd->assoc_ap_state != ASSOCIATE)
390 ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
391 hapd->assoc_ap_ssid_len);
392 printf("Associate with AP " MACSTR " SSID=%s (as station)\n",
393 MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
395 os_memset(mgmt, 0, sizeof(*mgmt));
396 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
397 WLAN_FC_STYPE_ASSOC_REQ);
398 os_memcpy(mgmt->da, hapd->conf->assoc_ap_addr, ETH_ALEN);
399 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
400 os_memcpy(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
401 mgmt->u.assoc_req.capab_info = host_to_le16(0);
402 mgmt->u.assoc_req.listen_interval = host_to_le16(1);
403 p = &mgmt->u.assoc_req.variable[0];
405 *p++ = WLAN_EID_SSID;
406 *p++ = hapd->assoc_ap_ssid_len;
407 os_memcpy(p, hapd->assoc_ap_ssid, hapd->assoc_ap_ssid_len);
408 p += hapd->assoc_ap_ssid_len;
410 p = hostapd_eid_supp_rates(hapd, p);
411 p = hostapd_eid_ext_supp_rates(hapd, p);
413 if (hostapd_send_mgmt_frame(hapd, mgmt, p - (u8 *) mgmt, 0) < 0)
414 perror("ieee802_11_sta_associate: send");
416 /* Try to authenticate again, if this attempt fails or times out. */
417 eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
421 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
422 u16 auth_transaction, u8 *challenge, int iswep)
424 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
426 "authentication (shared key, transaction %d)",
429 if (auth_transaction == 1) {
430 if (!sta->challenge) {
431 /* Generate a pseudo-random challenge */
435 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
436 if (sta->challenge == NULL)
437 return WLAN_STATUS_UNSPECIFIED_FAILURE;
441 os_memcpy(key, &now, 4);
442 os_memcpy(key + 4, &r, 4);
443 rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
449 if (auth_transaction != 3)
450 return WLAN_STATUS_UNSPECIFIED_FAILURE;
453 if (!iswep || !sta->challenge || !challenge ||
454 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
455 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
457 "shared key authentication - invalid "
458 "challenge-response");
459 return WLAN_STATUS_CHALLENGE_FAIL;
462 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
464 "authentication OK (shared key)");
465 #ifdef IEEE80211_REQUIRE_AUTH_ACK
466 /* Station will be marked authenticated if it ACKs the
467 * authentication reply. */
469 sta->flags |= WLAN_STA_AUTH;
470 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
472 os_free(sta->challenge);
473 sta->challenge = NULL;
479 static void send_auth_reply(struct hostapd_data *hapd,
480 const u8 *dst, const u8 *bssid,
481 u16 auth_alg, u16 auth_transaction, u16 resp,
482 const u8 *ies, size_t ies_len)
484 struct ieee80211_mgmt *reply;
488 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
489 buf = os_zalloc(rlen);
493 reply = (struct ieee80211_mgmt *) buf;
494 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
496 os_memcpy(reply->da, dst, ETH_ALEN);
497 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
498 os_memcpy(reply->bssid, bssid, ETH_ALEN);
500 reply->u.auth.auth_alg = host_to_le16(auth_alg);
501 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
502 reply->u.auth.status_code = host_to_le16(resp);
505 os_memcpy(reply->u.auth.variable, ies, ies_len);
507 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
508 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
509 MAC2STR(dst), auth_alg, auth_transaction,
510 resp, (unsigned long) ies_len);
511 if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
512 perror("send_auth_reply: send");
518 #ifdef CONFIG_IEEE80211R
519 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
520 u16 auth_transaction, u16 status,
521 const u8 *ies, size_t ies_len)
523 struct hostapd_data *hapd = ctx;
524 struct sta_info *sta;
526 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
527 status, ies, ies_len);
529 if (status != WLAN_STATUS_SUCCESS)
532 sta = ap_get_sta(hapd, dst);
536 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
537 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
538 sta->flags |= WLAN_STA_AUTH;
539 mlme_authenticate_indication(hapd, sta);
541 #endif /* CONFIG_IEEE80211R */
544 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
547 u16 auth_alg, auth_transaction, status_code;
548 u16 resp = WLAN_STATUS_SUCCESS;
549 struct sta_info *sta = NULL;
552 u8 *challenge = NULL;
553 u32 session_timeout, acct_interim_interval;
555 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
556 size_t resp_ies_len = 0;
558 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
559 printf("handle_auth - too short payload (len=%lu)\n",
560 (unsigned long) len);
564 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
565 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
566 status_code = le_to_host16(mgmt->u.auth.status_code);
567 fc = le_to_host16(mgmt->frame_control);
569 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
570 2 + WLAN_AUTH_CHALLENGE_LEN &&
571 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
572 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
573 challenge = &mgmt->u.auth.variable[2];
575 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
576 "auth_transaction=%d status_code=%d wep=%d%s",
577 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
578 status_code, !!(fc & WLAN_FC_ISWEP),
579 challenge ? " challenge" : "");
581 if (hapd->assoc_ap_state == AUTHENTICATE && auth_transaction == 2 &&
582 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0 &&
583 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
584 if (status_code != 0) {
585 printf("Authentication (as station) with AP "
586 MACSTR " failed (status_code=%d)\n",
587 MAC2STR(hapd->conf->assoc_ap_addr),
591 printf("Authenticated (as station) with AP " MACSTR "\n",
592 MAC2STR(hapd->conf->assoc_ap_addr));
593 ieee802_11_sta_associate(hapd, NULL);
597 if (hapd->tkip_countermeasures) {
598 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
602 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
603 auth_alg == WLAN_AUTH_OPEN) ||
604 #ifdef CONFIG_IEEE80211R
606 (hapd->conf->wpa_key_mgmt &
607 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
608 auth_alg == WLAN_AUTH_FT) ||
609 #endif /* CONFIG_IEEE80211R */
610 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
611 auth_alg == WLAN_AUTH_SHARED_KEY))) {
612 printf("Unsupported authentication algorithm (%d)\n",
614 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
618 if (!(auth_transaction == 1 ||
619 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
620 printf("Unknown authentication transaction number (%d)\n",
622 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
626 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
627 printf("Station " MACSTR " not allowed to authenticate.\n",
629 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
633 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
635 &acct_interim_interval, &vlan_id);
636 if (res == HOSTAPD_ACL_REJECT) {
637 printf("Station " MACSTR " not allowed to authenticate.\n",
639 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
642 if (res == HOSTAPD_ACL_PENDING) {
643 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
644 " waiting for an external authentication",
646 /* Authentication code will re-send the authentication frame
647 * after it has received (and cached) information from the
648 * external source. */
652 sta = ap_sta_add(hapd, mgmt->sa);
654 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
659 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
660 sta->vlan_id) == NULL) {
661 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
662 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
663 "%d received from RADIUS server",
665 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
668 sta->vlan_id = vlan_id;
669 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
670 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
673 sta->flags &= ~WLAN_STA_PREAUTH;
674 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
676 if (hapd->conf->radius->acct_interim_interval == 0 &&
677 acct_interim_interval)
678 sta->acct_interim_interval = acct_interim_interval;
679 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
680 ap_sta_session_timeout(hapd, sta, session_timeout);
682 ap_sta_no_session_timeout(hapd, sta);
686 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
688 "authentication OK (open system)");
689 #ifdef IEEE80211_REQUIRE_AUTH_ACK
690 /* Station will be marked authenticated if it ACKs the
691 * authentication reply. */
693 sta->flags |= WLAN_STA_AUTH;
694 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
695 sta->auth_alg = WLAN_AUTH_OPEN;
696 mlme_authenticate_indication(hapd, sta);
699 case WLAN_AUTH_SHARED_KEY:
700 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
702 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
703 mlme_authenticate_indication(hapd, sta);
704 if (sta->challenge && auth_transaction == 1) {
705 resp_ies[0] = WLAN_EID_CHALLENGE;
706 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
707 os_memcpy(resp_ies + 2, sta->challenge,
708 WLAN_AUTH_CHALLENGE_LEN);
709 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
712 #ifdef CONFIG_IEEE80211R
714 sta->auth_alg = WLAN_AUTH_FT;
715 if (sta->wpa_sm == NULL)
716 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
718 if (sta->wpa_sm == NULL) {
719 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
721 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
724 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
725 auth_transaction, mgmt->u.auth.variable,
726 len - IEEE80211_HDRLEN -
727 sizeof(mgmt->u.auth),
728 handle_auth_ft_finish, hapd);
729 /* handle_auth_ft_finish() callback will complete auth. */
731 #endif /* CONFIG_IEEE80211R */
735 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
736 auth_transaction + 1, resp, resp_ies, resp_ies_len);
740 static void handle_assoc(struct hostapd_data *hapd,
741 struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
743 u16 capab_info, listen_interval;
744 u16 resp = WLAN_STATUS_SUCCESS;
747 int send_deauth = 0, send_len, left, i;
748 struct sta_info *sta;
749 struct ieee802_11_elems elems;
750 u8 buf[sizeof(struct ieee80211_mgmt) + 512];
751 struct ieee80211_mgmt *reply;
753 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
754 sizeof(mgmt->u.assoc_req))) {
755 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
756 "\n", reassoc, (unsigned long) len);
761 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
762 listen_interval = le_to_host16(
763 mgmt->u.reassoc_req.listen_interval);
764 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
765 " capab_info=0x%02x listen_interval=%d current_ap="
767 MAC2STR(mgmt->sa), capab_info, listen_interval,
768 MAC2STR(mgmt->u.reassoc_req.current_ap));
769 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
770 pos = mgmt->u.reassoc_req.variable;
772 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
773 listen_interval = le_to_host16(
774 mgmt->u.assoc_req.listen_interval);
775 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
776 " capab_info=0x%02x listen_interval=%d",
777 MAC2STR(mgmt->sa), capab_info, listen_interval);
778 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
779 pos = mgmt->u.assoc_req.variable;
782 sta = ap_get_sta(hapd, mgmt->sa);
783 #ifdef CONFIG_IEEE80211R
784 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
785 (sta->flags & WLAN_STA_AUTH) == 0) {
786 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
787 "prior to authentication since it is using "
788 "over-the-DS FT", MAC2STR(mgmt->sa));
790 #endif /* CONFIG_IEEE80211R */
791 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
792 printf("STA " MACSTR " trying to associate before "
793 "authentication\n", MAC2STR(mgmt->sa));
795 printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
796 MAC2STR(sta->addr), sta->aid, sta->flags);
799 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
803 if (hapd->tkip_countermeasures) {
804 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
808 if (listen_interval > hapd->conf->max_listen_interval) {
809 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
811 "Too large Listen Interval (%d)",
813 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
817 sta->capability = capab_info;
819 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
821 if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
823 printf("STA " MACSTR " sent invalid association request\n",
825 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
829 if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
830 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
833 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
834 printf("Station " MACSTR " tried to associate with "
835 "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
836 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
840 sta->flags &= ~WLAN_STA_WME;
841 if (elems.wme && hapd->conf->wme_enabled) {
842 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
843 hostapd_logger(hapd, sta->addr,
846 "invalid WME element in association "
849 sta->flags |= WLAN_STA_WME;
852 if (!elems.supp_rates) {
853 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
855 "No supported rates element in AssocReq");
856 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
860 if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
861 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
863 "Invalid supported rates element length %d",
864 elems.supp_rates_len);
865 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
869 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
870 os_memcpy(sta->supported_rates, elems.supp_rates,
871 elems.supp_rates_len);
872 sta->supported_rates_len = elems.supp_rates_len;
874 if (elems.ext_supp_rates) {
875 if (elems.supp_rates_len + elems.ext_supp_rates_len >
876 sizeof(sta->supported_rates)) {
877 hostapd_logger(hapd, mgmt->sa,
878 HOSTAPD_MODULE_IEEE80211,
880 "Invalid supported rates element length"
881 " %d+%d", elems.supp_rates_len,
882 elems.ext_supp_rates_len);
883 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
887 os_memcpy(sta->supported_rates + elems.supp_rates_len,
888 elems.ext_supp_rates, elems.ext_supp_rates_len);
889 sta->supported_rates_len += elems.ext_supp_rates_len;
892 #ifdef CONFIG_IEEE80211N
893 /* save HT capabilities in the sta object */
894 os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
895 if (elems.ht_capabilities &&
896 elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_capability)
897 && (sta->flags & WLAN_STA_WME)) {
898 /* note: without WMM capability, treat the sta as non-HT */
899 sta->flags |= WLAN_STA_HT;
900 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
901 sta->ht_capabilities.length =
902 sizeof(struct ieee80211_ht_capability);
903 os_memcpy(&sta->ht_capabilities.data,
904 elems.ht_capabilities,
905 sizeof(struct ieee80211_ht_capability));
907 sta->flags &= ~WLAN_STA_HT;
908 #endif /* CONFIG_IEEE80211N */
910 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
911 wpa_ie = elems.rsn_ie;
912 wpa_ie_len = elems.rsn_ie_len;
913 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
915 wpa_ie = elems.wpa_ie;
916 wpa_ie_len = elems.wpa_ie_len;
921 if (hapd->conf->wpa && wpa_ie == NULL) {
922 printf("STA " MACSTR ": No WPA/RSN IE in association "
923 "request\n", MAC2STR(sta->addr));
924 resp = WLAN_STATUS_INVALID_IE;
928 if (hapd->conf->wpa && wpa_ie) {
932 if (sta->wpa_sm == NULL)
933 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
935 if (sta->wpa_sm == NULL) {
936 printf("Failed to initialize WPA state machine\n");
937 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
940 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
942 elems.mdie, elems.mdie_len);
943 if (res == WPA_INVALID_GROUP)
944 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
945 else if (res == WPA_INVALID_PAIRWISE)
946 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
947 else if (res == WPA_INVALID_AKMP)
948 resp = WLAN_STATUS_AKMP_NOT_VALID;
949 else if (res == WPA_ALLOC_FAIL)
950 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
951 #ifdef CONFIG_IEEE80211W
952 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
953 resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
954 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
955 resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
956 #endif /* CONFIG_IEEE80211W */
957 else if (res == WPA_INVALID_MDIE)
958 resp = WLAN_STATUS_INVALID_MDIE;
959 else if (res != WPA_IE_OK)
960 resp = WLAN_STATUS_INVALID_IE;
961 if (resp != WLAN_STATUS_SUCCESS)
963 #ifdef CONFIG_IEEE80211W
964 if ((sta->flags & WLAN_STA_MFP) && !sta->ping_timed_out) {
966 * STA has already been associated with MFP and ping
967 * timeout has not been reached. Reject the
968 * association attempt temporarily and start ping, if
969 * one is not pending.
972 if (sta->ping_count == 0)
973 ap_sta_start_ping(hapd, sta);
975 resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
979 if (wpa_auth_uses_mfp(sta->wpa_sm))
980 sta->flags |= WLAN_STA_MFP;
982 sta->flags &= ~WLAN_STA_MFP;
983 #endif /* CONFIG_IEEE80211W */
985 #ifdef CONFIG_IEEE80211R
986 if (sta->auth_alg == WLAN_AUTH_FT) {
988 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
989 "to use association (not "
990 "re-association) with FT auth_alg",
992 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
996 resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
997 if (resp != WLAN_STATUS_SUCCESS)
1000 #endif /* CONFIG_IEEE80211R */
1003 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1004 sta->flags |= WLAN_STA_NONERP;
1005 for (i = 0; i < sta->supported_rates_len; i++) {
1006 if ((sta->supported_rates[i] & 0x7f) > 22) {
1007 sta->flags &= ~WLAN_STA_NONERP;
1011 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1012 sta->nonerp_set = 1;
1013 hapd->iface->num_sta_non_erp++;
1014 if (hapd->iface->num_sta_non_erp == 1)
1015 ieee802_11_set_beacons(hapd->iface);
1018 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1019 !sta->no_short_slot_time_set) {
1020 sta->no_short_slot_time_set = 1;
1021 hapd->iface->num_sta_no_short_slot_time++;
1022 if (hapd->iface->current_mode->mode ==
1023 HOSTAPD_MODE_IEEE80211G &&
1024 hapd->iface->num_sta_no_short_slot_time == 1)
1025 ieee802_11_set_beacons(hapd->iface);
1028 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1029 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1031 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1033 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1034 !sta->no_short_preamble_set) {
1035 sta->no_short_preamble_set = 1;
1036 hapd->iface->num_sta_no_short_preamble++;
1037 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1038 && hapd->iface->num_sta_no_short_preamble == 1)
1039 ieee802_11_set_beacons(hapd->iface);
1042 #ifdef CONFIG_IEEE80211N
1043 if (sta->flags & WLAN_STA_HT) {
1044 if ((sta->ht_capabilities.data.capabilities_info &
1045 HT_CAP_INFO_GREEN_FIELD) == 0) {
1046 hapd->iface->num_sta_ht_no_gf++;
1047 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1048 "greenfield, num of non-gf stations %d",
1049 __func__, MAC2STR(sta->addr),
1050 hapd->iface->num_sta_ht_no_gf);
1052 if ((sta->ht_capabilities.data.capabilities_info &
1053 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1054 hapd->iface->num_sta_ht_20mhz++;
1055 wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1056 "num of 20MHz HT STAs %d",
1057 __func__, MAC2STR(sta->addr),
1058 hapd->iface->num_sta_ht_20mhz);
1061 hapd->iface->num_sta_no_ht++;
1062 if (hapd->iconf->ieee80211n) {
1063 wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1064 " - no HT, num of non-HT stations %d",
1065 __func__, MAC2STR(sta->addr),
1066 hapd->iface->num_sta_no_ht);
1070 if (hostapd_ht_operation_update(hapd->iface) > 0)
1071 ieee802_11_set_beacons(hapd->iface);
1072 #endif /* CONFIG_IEEE80211N */
1074 /* get a unique AID */
1076 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
1078 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1079 if (hapd->sta_aid[sta->aid - 1] == NULL)
1081 if (sta->aid > MAX_AID_TABLE_SIZE) {
1083 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1084 wpa_printf(MSG_ERROR, " no room for more AIDs");
1087 hapd->sta_aid[sta->aid - 1] = sta;
1088 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
1092 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1093 HOSTAPD_LEVEL_DEBUG,
1094 "association OK (aid %d)", sta->aid);
1095 /* Station will be marked associated, after it acknowledges AssocResp
1099 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1103 if (sta->last_assoc_req)
1104 os_free(sta->last_assoc_req);
1105 sta->last_assoc_req = os_malloc(len);
1106 if (sta->last_assoc_req)
1107 os_memcpy(sta->last_assoc_req, mgmt, len);
1109 /* Make sure that the previously registered inactivity timer will not
1110 * remove the STA immediately. */
1111 sta->timeout_next = STA_NULLFUNC;
1114 os_memset(buf, 0, sizeof(buf));
1115 reply = (struct ieee80211_mgmt *) buf;
1116 reply->frame_control =
1117 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1118 (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1119 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1120 WLAN_FC_STYPE_ASSOC_RESP)));
1121 os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1122 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1123 os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1125 send_len = IEEE80211_HDRLEN;
1127 send_len += sizeof(reply->u.deauth);
1128 reply->u.deauth.reason_code = host_to_le16(resp);
1131 send_len += sizeof(reply->u.assoc_resp);
1132 reply->u.assoc_resp.capab_info =
1133 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1134 reply->u.assoc_resp.status_code = host_to_le16(resp);
1135 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1136 | BIT(14) | BIT(15));
1137 /* Supported rates */
1138 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1139 /* Extended supported rates */
1140 p = hostapd_eid_ext_supp_rates(hapd, p);
1141 if (sta->flags & WLAN_STA_WME)
1142 p = hostapd_eid_wme(hapd, p);
1144 p = hostapd_eid_ht_capabilities_info(hapd, p);
1145 p = hostapd_eid_ht_operation(hapd, p);
1147 #ifdef CONFIG_IEEE80211R
1148 if (resp == WLAN_STATUS_SUCCESS) {
1149 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1150 * Transition Information, RSN */
1151 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1152 buf + sizeof(buf) - p,
1155 #endif /* CONFIG_IEEE80211R */
1157 #ifdef CONFIG_IEEE80211W
1158 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1159 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1160 #endif /* CONFIG_IEEE80211W */
1162 send_len += p - reply->u.assoc_resp.variable;
1165 if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1166 perror("handle_assoc: send");
1170 static void handle_assoc_resp(struct hostapd_data *hapd,
1171 struct ieee80211_mgmt *mgmt, size_t len)
1173 u16 status_code, aid;
1175 if (hapd->assoc_ap_state != ASSOCIATE) {
1176 printf("Unexpected association response received from " MACSTR
1177 "\n", MAC2STR(mgmt->sa));
1181 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
1182 printf("handle_assoc_resp - too short payload (len=%lu)\n",
1183 (unsigned long) len);
1187 if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
1188 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
1189 printf("Received association response from unexpected address "
1190 "(SA=" MACSTR " BSSID=" MACSTR "\n",
1191 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
1195 status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
1196 aid = le_to_host16(mgmt->u.assoc_resp.aid);
1197 aid &= ~(BIT(14) | BIT(15));
1199 if (status_code != 0) {
1200 printf("Association (as station) with AP " MACSTR " failed "
1201 "(status_code=%d)\n",
1202 MAC2STR(hapd->conf->assoc_ap_addr), status_code);
1203 /* Try to authenticate again */
1204 hapd->assoc_ap_state = AUTHENTICATE;
1205 eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
1209 printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
1210 MAC2STR(hapd->conf->assoc_ap_addr), aid);
1211 hapd->assoc_ap_aid = aid;
1212 hapd->assoc_ap_state = ASSOCIATED;
1214 if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
1215 printf("Could not set associated AP address to kernel "
1221 static void handle_disassoc(struct hostapd_data *hapd,
1222 struct ieee80211_mgmt *mgmt, size_t len)
1224 struct sta_info *sta;
1226 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1227 printf("handle_disassoc - too short payload (len=%lu)\n",
1228 (unsigned long) len);
1232 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1234 le_to_host16(mgmt->u.disassoc.reason_code));
1236 if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1237 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1238 printf("Assoc AP " MACSTR " sent disassociation "
1239 "(reason_code=%d) - try to authenticate\n",
1240 MAC2STR(hapd->conf->assoc_ap_addr),
1241 le_to_host16(mgmt->u.disassoc.reason_code));
1242 hapd->assoc_ap_state = AUTHENTICATE;
1243 ieee802_11_sta_authenticate(hapd, NULL);
1244 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1249 sta = ap_get_sta(hapd, mgmt->sa);
1251 printf("Station " MACSTR " trying to disassociate, but it "
1252 "is not associated.\n", MAC2STR(mgmt->sa));
1256 sta->flags &= ~WLAN_STA_ASSOC;
1257 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1258 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1259 HOSTAPD_LEVEL_INFO, "disassociated");
1260 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1261 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1262 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1264 accounting_sta_stop(hapd, sta);
1265 ieee802_1x_free_station(sta);
1266 hostapd_sta_remove(hapd, sta->addr);
1268 if (sta->timeout_next == STA_NULLFUNC ||
1269 sta->timeout_next == STA_DISASSOC) {
1270 sta->timeout_next = STA_DEAUTH;
1271 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1272 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1276 mlme_disassociate_indication(
1277 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1281 static void handle_deauth(struct hostapd_data *hapd,
1282 struct ieee80211_mgmt *mgmt, size_t len)
1284 struct sta_info *sta;
1286 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1287 printf("handle_deauth - too short payload (len=%lu)\n",
1288 (unsigned long) len);
1292 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1295 le_to_host16(mgmt->u.deauth.reason_code));
1297 if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1298 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1299 printf("Assoc AP " MACSTR " sent deauthentication "
1300 "(reason_code=%d) - try to authenticate\n",
1301 MAC2STR(hapd->conf->assoc_ap_addr),
1302 le_to_host16(mgmt->u.deauth.reason_code));
1303 hapd->assoc_ap_state = AUTHENTICATE;
1304 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1309 sta = ap_get_sta(hapd, mgmt->sa);
1311 printf("Station " MACSTR " trying to deauthenticate, but it "
1312 "is not authenticated.\n", MAC2STR(mgmt->sa));
1316 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1317 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1318 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1319 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1320 mlme_deauthenticate_indication(
1321 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1322 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1323 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1324 ap_free_sta(hapd, sta);
1328 static void handle_beacon(struct hostapd_data *hapd,
1329 struct ieee80211_mgmt *mgmt, size_t len,
1330 struct hostapd_frame_info *fi)
1332 struct ieee802_11_elems elems;
1334 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1335 printf("handle_beacon - too short payload (len=%lu)\n",
1336 (unsigned long) len);
1340 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1341 len - (IEEE80211_HDRLEN +
1342 sizeof(mgmt->u.beacon)), &elems,
1345 if (hapd->assoc_ap_state == WAIT_BEACON &&
1346 os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1347 if (elems.ssid && elems.ssid_len <= 32) {
1348 os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
1350 hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
1351 hapd->assoc_ap_ssid_len = elems.ssid_len;
1353 ieee802_11_sta_authenticate(hapd, NULL);
1356 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1360 #ifdef CONFIG_IEEE80211W
1361 static void hostapd_ping_action(struct hostapd_data *hapd,
1362 struct ieee80211_mgmt *mgmt, size_t len)
1364 struct sta_info *sta;
1368 end = mgmt->u.action.u.ping_resp.trans_id + WLAN_PING_TRANS_ID_LEN;
1369 if (((u8 *) mgmt) + len < end) {
1370 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short Ping Action "
1371 "frame (len=%lu)", (unsigned long) len);
1375 if (mgmt->u.action.u.ping_resp.action != WLAN_PING_RESPONSE) {
1376 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected Ping Action %d",
1377 mgmt->u.action.u.ping_resp.action);
1381 /* MLME-PING.confirm */
1383 sta = ap_get_sta(hapd, mgmt->sa);
1384 if (sta == NULL || sta->ping_trans_id == NULL) {
1385 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1386 "pending ping request found");
1390 for (i = 0; i < sta->ping_count; i++) {
1391 if (os_memcmp(sta->ping_trans_id + i * WLAN_PING_TRANS_ID_LEN,
1392 mgmt->u.action.u.ping_resp.trans_id,
1393 WLAN_PING_TRANS_ID_LEN) == 0)
1397 if (i >= sta->ping_count) {
1398 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching ping "
1399 "transaction identifier found");
1403 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1404 HOSTAPD_LEVEL_DEBUG, "Reply to pending ping received");
1405 ap_sta_stop_ping(hapd, sta);
1407 #endif /* CONFIG_IEEE80211W */
1410 static void handle_action(struct hostapd_data *hapd,
1411 struct ieee80211_mgmt *mgmt, size_t len)
1413 if (len < IEEE80211_HDRLEN + 1) {
1414 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1415 HOSTAPD_LEVEL_DEBUG,
1416 "handle_action - too short payload (len=%lu)",
1417 (unsigned long) len);
1421 switch (mgmt->u.action.category) {
1422 #ifdef CONFIG_IEEE80211R
1423 case WLAN_ACTION_FT:
1425 struct sta_info *sta;
1427 sta = ap_get_sta(hapd, mgmt->sa);
1428 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1429 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1430 "frame from unassociated STA " MACSTR,
1435 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1436 len - IEEE80211_HDRLEN))
1441 #endif /* CONFIG_IEEE80211R */
1442 case WLAN_ACTION_WMM:
1443 hostapd_wme_action(hapd, mgmt, len);
1445 #ifdef CONFIG_IEEE80211W
1446 case WLAN_ACTION_PING:
1447 hostapd_ping_action(hapd, mgmt, len);
1449 #endif /* CONFIG_IEEE80211W */
1452 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1453 HOSTAPD_LEVEL_DEBUG,
1454 "handle_action - unknown action category %d or invalid "
1456 mgmt->u.action.category);
1457 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1458 !(mgmt->sa[0] & 0x01)) {
1460 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1461 * Return the Action frame to the source without change
1462 * except that MSB of the Category set to 1.
1464 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1465 "frame back to sender");
1466 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1467 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1468 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1469 mgmt->u.action.category |= 0x80;
1471 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1477 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1478 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1480 * @buf: management frame data (starting from IEEE 802.11 header)
1481 * @len: length of frame data in octets
1482 * @stype: management frame subtype from frame control field
1484 * Process all incoming IEEE 802.11 management frames. This will be called for
1485 * each frame received from the kernel driver through wlan#ap interface. In
1486 * addition, it can be called to re-inserted pending frames (e.g., when using
1487 * external RADIUS server as an MAC ACL).
1489 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1490 struct hostapd_frame_info *fi)
1492 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1495 if (stype == WLAN_FC_STYPE_BEACON) {
1496 handle_beacon(hapd, mgmt, len, fi);
1500 if (fi && fi->passive_scan)
1503 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1504 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1505 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1508 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
1509 (hapd->assoc_ap_state == DO_NOT_ASSOC ||
1510 os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
1512 printf("MGMT: BSSID=" MACSTR " not our address\n",
1513 MAC2STR(mgmt->bssid));
1518 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1519 handle_probe_req(hapd, mgmt, len);
1523 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1524 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1525 HOSTAPD_LEVEL_DEBUG,
1526 "MGMT: DA=" MACSTR " not our address",
1532 case WLAN_FC_STYPE_AUTH:
1533 wpa_printf(MSG_DEBUG, "mgmt::auth");
1534 handle_auth(hapd, mgmt, len);
1536 case WLAN_FC_STYPE_ASSOC_REQ:
1537 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1538 handle_assoc(hapd, mgmt, len, 0);
1540 case WLAN_FC_STYPE_ASSOC_RESP:
1541 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
1542 handle_assoc_resp(hapd, mgmt, len);
1544 case WLAN_FC_STYPE_REASSOC_REQ:
1545 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1546 handle_assoc(hapd, mgmt, len, 1);
1548 case WLAN_FC_STYPE_DISASSOC:
1549 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1550 handle_disassoc(hapd, mgmt, len);
1552 case WLAN_FC_STYPE_DEAUTH:
1553 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1554 handle_deauth(hapd, mgmt, len);
1556 case WLAN_FC_STYPE_ACTION:
1557 wpa_printf(MSG_DEBUG, "mgmt::action");
1558 handle_action(hapd, mgmt, len);
1561 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1562 HOSTAPD_LEVEL_DEBUG,
1563 "unknown mgmt frame subtype %d", stype);
1569 static void handle_auth_cb(struct hostapd_data *hapd,
1570 struct ieee80211_mgmt *mgmt,
1573 u16 auth_alg, auth_transaction, status_code;
1574 struct sta_info *sta;
1577 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1578 HOSTAPD_LEVEL_NOTICE,
1579 "did not acknowledge authentication response");
1583 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1584 printf("handle_auth_cb - too short payload (len=%lu)\n",
1585 (unsigned long) len);
1589 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1590 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1591 status_code = le_to_host16(mgmt->u.auth.status_code);
1593 sta = ap_get_sta(hapd, mgmt->da);
1595 printf("handle_auth_cb: STA " MACSTR " not found\n",
1600 if (status_code == WLAN_STATUS_SUCCESS &&
1601 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1602 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1603 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1604 HOSTAPD_LEVEL_INFO, "authenticated");
1605 sta->flags |= WLAN_STA_AUTH;
1610 static void handle_assoc_cb(struct hostapd_data *hapd,
1611 struct ieee80211_mgmt *mgmt,
1612 size_t len, int reassoc, int ok)
1615 struct sta_info *sta;
1617 struct ht_cap_ie *ht_cap = NULL;
1620 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1621 HOSTAPD_LEVEL_DEBUG,
1622 "did not acknowledge association response");
1626 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1627 sizeof(mgmt->u.assoc_resp))) {
1628 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1629 "(len=%lu)\n", reassoc, (unsigned long) len);
1634 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1636 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1638 sta = ap_get_sta(hapd, mgmt->da);
1640 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1645 if (status != WLAN_STATUS_SUCCESS)
1648 /* Stop previous accounting session, if one is started, and allocate
1649 * new session id for the new session. */
1650 accounting_sta_stop(hapd, sta);
1651 accounting_sta_get_id(hapd, sta);
1653 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1655 "associated (aid %d, accounting session %08X-%08X)",
1656 sta->aid, sta->acct_session_id_hi,
1657 sta->acct_session_id_lo);
1659 if (sta->flags & WLAN_STA_ASSOC)
1661 sta->flags |= WLAN_STA_ASSOC;
1664 mlme_reassociate_indication(hapd, sta);
1666 mlme_associate_indication(hapd, sta);
1668 #ifdef CONFIG_IEEE80211N
1669 if (sta->flags & WLAN_STA_HT)
1670 ht_cap = &sta->ht_capabilities;
1671 #endif /* CONFIG_IEEE80211N */
1673 #ifdef CONFIG_IEEE80211W
1674 sta->ping_timed_out = 0;
1675 #endif /* CONFIG_IEEE80211W */
1677 if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1678 sta->capability, sta->supported_rates,
1679 sta->supported_rates_len, 0, sta->listen_interval,
1682 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1683 HOSTAPD_LEVEL_NOTICE,
1684 "Could not add STA to kernel driver");
1687 if (sta->eapol_sm == NULL) {
1689 * This STA does not use RADIUS server for EAP authentication,
1690 * so bind it to the selected VLAN interface now, since the
1691 * interface selection is not going to change anymore.
1693 ap_sta_bind_vlan(hapd, sta, 0);
1694 } else if (sta->vlan_id) {
1695 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1696 ap_sta_bind_vlan(hapd, sta, 0);
1698 if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1699 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1700 WLAN_STA_SHORT_PREAMBLE, ~0);
1702 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1703 0, ~WLAN_STA_SHORT_PREAMBLE);
1706 if (sta->auth_alg == WLAN_AUTH_FT)
1707 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1709 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1710 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1712 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1715 /* Copy of the association request is not needed anymore */
1716 if (sta->last_assoc_req) {
1717 os_free(sta->last_assoc_req);
1718 sta->last_assoc_req = NULL;
1723 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1726 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1729 case WLAN_FC_STYPE_AUTH:
1730 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1731 handle_auth_cb(hapd, mgmt, len, ok);
1733 case WLAN_FC_STYPE_ASSOC_RESP:
1734 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1735 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1737 case WLAN_FC_STYPE_REASSOC_RESP:
1738 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1739 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1741 case WLAN_FC_STYPE_PROBE_RESP:
1742 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1744 case WLAN_FC_STYPE_DEAUTH:
1747 case WLAN_FC_STYPE_ACTION:
1748 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1751 printf("unknown mgmt cb frame subtype %d\n", stype);
1757 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1760 struct hostapd_data *hapd = eloop_ctx;
1761 hapd->tkip_countermeasures = 0;
1762 hostapd_set_countermeasures(hapd, 0);
1763 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1764 HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1768 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1770 struct sta_info *sta;
1772 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1773 HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1775 wpa_auth_countermeasures_start(hapd->wpa_auth);
1776 hapd->tkip_countermeasures = 1;
1777 hostapd_set_countermeasures(hapd, 1);
1778 wpa_gtk_rekey(hapd->wpa_auth);
1779 eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
1780 eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
1782 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
1783 hostapd_sta_deauth(hapd, sta->addr,
1784 WLAN_REASON_MICHAEL_MIC_FAILURE);
1785 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1786 WLAN_STA_AUTHORIZED);
1787 hostapd_sta_remove(hapd, sta->addr);
1792 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
1797 if (addr && local) {
1798 struct sta_info *sta = ap_get_sta(hapd, addr);
1800 wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
1801 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1803 "Michael MIC failure detected in "
1805 mlme_michaelmicfailure_indication(hapd, addr);
1807 wpa_printf(MSG_DEBUG,
1808 "MLME-MICHAELMICFAILURE.indication "
1809 "for not associated STA (" MACSTR
1810 ") ignored", MAC2STR(addr));
1816 if (now > hapd->michael_mic_failure + 60) {
1817 hapd->michael_mic_failures = 1;
1819 hapd->michael_mic_failures++;
1820 if (hapd->michael_mic_failures > 1)
1821 ieee80211_tkip_countermeasures_start(hapd);
1823 hapd->michael_mic_failure = now;
1827 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1834 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1835 char *buf, size_t buflen)
1841 #endif /* CONFIG_NATIVE_WINDOWS */