ieee802_11.c

Go to the documentation of this file.
00001 
00017 #include "includes.h"
00018 
00019 #ifndef CONFIG_NATIVE_WINDOWS
00020 
00021 #include <net/if.h>
00022 
00023 #include "eloop.h"
00024 #include "hostapd.h"
00025 #include "ieee802_11.h"
00026 #include "beacon.h"
00027 #include "hw_features.h"
00028 #include "radius/radius.h"
00029 #include "radius/radius_client.h"
00030 #include "ieee802_11_auth.h"
00031 #include "sta_info.h"
00032 #include "crypto.h"
00033 #include "ieee802_1x.h"
00034 #include "wpa.h"
00035 #include "wme.h"
00036 #include "ap_list.h"
00037 #include "accounting.h"
00038 #include "driver_i.h"
00039 #include "mlme.h"
00040 #include "wpa_ctrl.h"
00041 
00042 
00043 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
00044 {
00045         u8 *pos = eid;
00046         int i, num, count;
00047 
00048         if (hapd->iface->current_rates == NULL)
00049                 return eid;
00050 
00051         *pos++ = WLAN_EID_SUPP_RATES;
00052         num = hapd->iface->num_rates;
00053         if (num > 8) {
00054                 /* rest of the rates are encoded in Extended supported
00055                  * rates element */
00056                 num = 8;
00057         }
00058 
00059         *pos++ = num;
00060         count = 0;
00061         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
00062              i++) {
00063                 count++;
00064                 *pos = hapd->iface->current_rates[i].rate / 5;
00065                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
00066                         *pos |= 0x80;
00067                 pos++;
00068         }
00069 
00070         return pos;
00071 }
00072 
00073 
00074 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
00075 {
00076         u8 *pos = eid;
00077         int i, num, count;
00078 
00079         if (hapd->iface->current_rates == NULL)
00080                 return eid;
00081 
00082         num = hapd->iface->num_rates;
00083         if (num <= 8)
00084                 return eid;
00085         num -= 8;
00086 
00087         *pos++ = WLAN_EID_EXT_SUPP_RATES;
00088         *pos++ = num;
00089         count = 0;
00090         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
00091              i++) {
00092                 count++;
00093                 if (count <= 8)
00094                         continue; /* already in SuppRates IE */
00095                 *pos = hapd->iface->current_rates[i].rate / 5;
00096                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
00097                         *pos |= 0x80;
00098                 pos++;
00099         }
00100 
00101         return pos;
00102 }
00103 
00104 
00105 u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
00106 {
00107 #ifdef CONFIG_IEEE80211N
00108         struct ieee80211_ht_capability *cap;
00109         u8 *pos = eid;
00110 
00111         if (!hapd->iconf->ieee80211n)
00112                 return eid;
00113 
00114         *pos++ = WLAN_EID_HT_CAP;
00115         *pos++ = sizeof(*cap);
00116 
00117         cap = (struct ieee80211_ht_capability *) pos;
00118         os_memset(cap, 0, sizeof(*cap));
00119         SET_2BIT_U8(&cap->mac_ht_params_info,
00120                     MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
00121                     MAX_RX_AMPDU_FACTOR_64KB);
00122 
00123         cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
00124         os_memcpy(cap->supported_mcs_set, hapd->iface->current_mode->mcs_set,
00125                   16);
00126 
00127         pos += sizeof(*cap);
00128 
00129         return pos;
00130 #else /* CONFIG_IEEE80211N */
00131         return eid;
00132 #endif /* CONFIG_IEEE80211N */
00133 }
00134 
00135 
00136 u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
00137 {
00138 #ifdef CONFIG_IEEE80211N
00139         struct ieee80211_ht_operation *oper;
00140         u8 *pos = eid;
00141 
00142         if (!hapd->iconf->ieee80211n)
00143                 return eid;
00144 
00145         *pos++ = WLAN_EID_HT_OPERATION;
00146         *pos++ = sizeof(*oper);
00147 
00148         oper = (struct ieee80211_ht_operation *) pos;
00149         os_memset(oper, 0, sizeof(*oper));
00150 
00151         oper->control_chan = hapd->iconf->channel;
00152         oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
00153         if (hapd->iconf->secondary_channel == 1)
00154                 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
00155                         HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
00156         if (hapd->iconf->secondary_channel == -1)
00157                 oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
00158                         HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
00159 
00160         pos += sizeof(*oper);
00161 
00162         return pos;
00163 #else /* CONFIG_IEEE80211N */
00164         return eid;
00165 #endif /* CONFIG_IEEE80211N */
00166 }
00167 
00168 
00169 #ifdef CONFIG_IEEE80211N
00170 
00171 /*
00172 op_mode
00173 Set to 0 (HT pure) under the followign conditions
00174         - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
00175         - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
00176 Set to 1 (HT non-member protection) if there may be non-HT STAs
00177         in both the primary and the secondary channel
00178 Set to 2 if only HT STAs are associated in BSS,
00179         however and at least one 20 MHz HT STA is associated
00180 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
00181         (currently non-GF HT station is considered as non-HT STA also)
00182 */
00183 int hostapd_ht_operation_update(struct hostapd_iface *iface)
00184 {
00185         u16 cur_op_mode, new_op_mode;
00186         int op_mode_changes = 0;
00187 
00188         if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
00189                 return 0;
00190 
00191         wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
00192                    __func__, iface->ht_op_mode);
00193 
00194         if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
00195             && iface->num_sta_ht_no_gf) {
00196                 iface->ht_op_mode |=
00197                         HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
00198                 op_mode_changes++;
00199         } else if ((iface->ht_op_mode &
00200                     HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
00201                    iface->num_sta_ht_no_gf == 0) {
00202                 iface->ht_op_mode &=
00203                         ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
00204                 op_mode_changes++;
00205         }
00206 
00207         if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
00208             (iface->num_sta_no_ht || iface->olbc_ht)) {
00209                 iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
00210                 op_mode_changes++;
00211         } else if ((iface->ht_op_mode &
00212                     HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
00213                    (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
00214                 iface->ht_op_mode &=
00215                         ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
00216                 op_mode_changes++;
00217         }
00218 
00219         /* Note: currently we switch to the MIXED op mode if HT non-greenfield
00220          * station is associated. Probably it's a theoretical case, since
00221          * it looks like all known HT STAs support greenfield.
00222          */
00223         new_op_mode = 0;
00224         if (iface->num_sta_no_ht ||
00225             (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
00226                 new_op_mode = OP_MODE_MIXED;
00227         else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
00228                  && iface->num_sta_ht_20mhz)
00229                 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
00230         else if (iface->olbc_ht)
00231                 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
00232         else
00233                 new_op_mode = OP_MODE_PURE;
00234 
00235         cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
00236         if (cur_op_mode != new_op_mode) {
00237                 iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
00238                 iface->ht_op_mode |= new_op_mode;
00239                 op_mode_changes++;
00240         }
00241 
00242         wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
00243                    __func__, iface->ht_op_mode, op_mode_changes);
00244 
00245         return op_mode_changes;
00246 }
00247 
00248 #endif /* CONFIG_IEEE80211N */
00249 
00250 
00251 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
00252                            int probe)
00253 {
00254         int capab = WLAN_CAPABILITY_ESS;
00255         int privacy;
00256 
00257         if (hapd->iface->num_sta_no_short_preamble == 0 &&
00258             hapd->iconf->preamble == SHORT_PREAMBLE)
00259                 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
00260 
00261         privacy = hapd->conf->ssid.wep.keys_set;
00262 
00263         if (hapd->conf->ieee802_1x &&
00264             (hapd->conf->default_wep_key_len ||
00265              hapd->conf->individual_wep_key_len))
00266                 privacy = 1;
00267 
00268         if (hapd->conf->wpa)
00269                 privacy = 1;
00270 
00271         if (sta) {
00272                 int policy, def_klen;
00273                 if (probe && sta->ssid_probe) {
00274                         policy = sta->ssid_probe->security_policy;
00275                         def_klen = sta->ssid_probe->wep.default_len;
00276                 } else {
00277                         policy = sta->ssid->security_policy;
00278                         def_klen = sta->ssid->wep.default_len;
00279                 }
00280                 privacy = policy != SECURITY_PLAINTEXT;
00281                 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
00282                         privacy = 0;
00283         }
00284 
00285         if (privacy)
00286                 capab |= WLAN_CAPABILITY_PRIVACY;
00287 
00288         if (hapd->iface->current_mode &&
00289             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
00290             hapd->iface->num_sta_no_short_slot_time == 0)
00291                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
00292 
00293         return capab;
00294 }
00295 
00296 
00297 #ifdef CONFIG_IEEE80211W
00298 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
00299                                             struct sta_info *sta, u8 *eid)
00300 {
00301         u8 *pos = eid;
00302         u32 timeout, tu;
00303         struct os_time now, passed;
00304 
00305         *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
00306         *pos++ = 5;
00307         *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
00308         os_get_time(&now);
00309         os_time_sub(&now, &sta->sa_query_start, &passed);
00310         tu = (passed.sec * 1000000 + passed.usec) / 1024;
00311         if (hapd->conf->assoc_sa_query_max_timeout > tu)
00312                 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
00313         else
00314                 timeout = 0;
00315         if (timeout < hapd->conf->assoc_sa_query_max_timeout)
00316                 timeout++; /* add some extra time for local timers */
00317         WPA_PUT_LE32(pos, timeout);
00318         pos += 4;
00319 
00320         return pos;
00321 }
00322 #endif /* CONFIG_IEEE80211W */
00323 
00324 
00325 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
00326 {
00327         int i;
00328         if (len > HOSTAPD_MAX_SSID_LEN)
00329                 len = HOSTAPD_MAX_SSID_LEN;
00330         for (i = 0; i < len; i++) {
00331                 if (ssid[i] >= 32 && ssid[i] < 127)
00332                         buf[i] = ssid[i];
00333                 else
00334                         buf[i] = '.';
00335         }
00336         buf[len] = '\0';
00337 }
00338 
00339 
00347 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
00348 {
00349         struct ieee80211_mgmt mgmt;
00350 
00351         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
00352                        HOSTAPD_LEVEL_DEBUG,
00353                        "deauthenticate - reason %d", reason);
00354         os_memset(&mgmt, 0, sizeof(mgmt));
00355         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
00356                                           WLAN_FC_STYPE_DEAUTH);
00357         os_memcpy(mgmt.da, addr, ETH_ALEN);
00358         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
00359         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
00360         mgmt.u.deauth.reason_code = host_to_le16(reason);
00361         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
00362                                     sizeof(mgmt.u.deauth)) < 0)
00363                 perror("ieee802_11_send_deauth: send");
00364 }
00365 
00366 
00367 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
00368                            u16 auth_transaction, u8 *challenge, int iswep)
00369 {
00370         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
00371                        HOSTAPD_LEVEL_DEBUG,
00372                        "authentication (shared key, transaction %d)",
00373                        auth_transaction);
00374 
00375         if (auth_transaction == 1) {
00376                 if (!sta->challenge) {
00377                         /* Generate a pseudo-random challenge */
00378                         u8 key[8];
00379                         time_t now;
00380                         int r;
00381                         sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
00382                         if (sta->challenge == NULL)
00383                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
00384 
00385                         now = time(NULL);
00386                         r = random();
00387                         os_memcpy(key, &now, 4);
00388                         os_memcpy(key + 4, &r, 4);
00389                         rc4_skip(key, sizeof(key), 0,
00390                                  sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
00391                 }
00392                 return 0;
00393         }
00394 
00395         if (auth_transaction != 3)
00396                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
00397 
00398         /* Transaction 3 */
00399         if (!iswep || !sta->challenge || !challenge ||
00400             os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
00401                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
00402                                HOSTAPD_LEVEL_INFO,
00403                                "shared key authentication - invalid "
00404                                "challenge-response");
00405                 return WLAN_STATUS_CHALLENGE_FAIL;
00406         }
00407 
00408         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
00409                        HOSTAPD_LEVEL_DEBUG,
00410                        "authentication OK (shared key)");
00411 #ifdef IEEE80211_REQUIRE_AUTH_ACK
00412         /* Station will be marked authenticated if it ACKs the
00413          * authentication reply. */
00414 #else
00415         sta->flags |= WLAN_STA_AUTH;
00416         wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
00417 #endif
00418         os_free(sta->challenge);
00419         sta->challenge = NULL;
00420 
00421         return 0;
00422 }
00423 
00424 
00425 static void send_auth_reply(struct hostapd_data *hapd,
00426                             const u8 *dst, const u8 *bssid,
00427                             u16 auth_alg, u16 auth_transaction, u16 resp,
00428                             const u8 *ies, size_t ies_len)
00429 {
00430         struct ieee80211_mgmt *reply;
00431         u8 *buf;
00432         size_t rlen;
00433 
00434         rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
00435         buf = os_zalloc(rlen);
00436         if (buf == NULL)
00437                 return;
00438 
00439         reply = (struct ieee80211_mgmt *) buf;
00440         reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
00441                                             WLAN_FC_STYPE_AUTH);
00442         os_memcpy(reply->da, dst, ETH_ALEN);
00443         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
00444         os_memcpy(reply->bssid, bssid, ETH_ALEN);
00445 
00446         reply->u.auth.auth_alg = host_to_le16(auth_alg);
00447         reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
00448         reply->u.auth.status_code = host_to_le16(resp);
00449 
00450         if (ies && ies_len)
00451                 os_memcpy(reply->u.auth.variable, ies, ies_len);
00452 
00453         wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
00454                    " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
00455                    MAC2STR(dst), auth_alg, auth_transaction,
00456                    resp, (unsigned long) ies_len);
00457         if (hostapd_send_mgmt_frame(hapd, reply, rlen) < 0)
00458                 perror("send_auth_reply: send");
00459 
00460         os_free(buf);
00461 }
00462 
00463 
00464 #ifdef CONFIG_IEEE80211R
00465 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
00466                                   u16 auth_transaction, u16 status,
00467                                   const u8 *ies, size_t ies_len)
00468 {
00469         struct hostapd_data *hapd = ctx;
00470         struct sta_info *sta;
00471 
00472         send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
00473                         status, ies, ies_len);
00474 
00475         if (status != WLAN_STATUS_SUCCESS)
00476                 return;
00477 
00478         sta = ap_get_sta(hapd, dst);
00479         if (sta == NULL)
00480                 return;
00481 
00482         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
00483                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
00484         sta->flags |= WLAN_STA_AUTH;
00485         mlme_authenticate_indication(hapd, sta);
00486 }
00487 #endif /* CONFIG_IEEE80211R */
00488 
00489 
00490 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
00491                         size_t len)
00492 {
00493         u16 auth_alg, auth_transaction, status_code;
00494         u16 resp = WLAN_STATUS_SUCCESS;
00495         struct sta_info *sta = NULL;
00496         int res;
00497         u16 fc;
00498         u8 *challenge = NULL;
00499         u32 session_timeout, acct_interim_interval;
00500         int vlan_id = 0;
00501         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
00502         size_t resp_ies_len = 0;
00503 
00504         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
00505                 printf("handle_auth - too short payload (len=%lu)\n",
00506                        (unsigned long) len);
00507                 return;
00508         }
00509 
00510         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
00511         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
00512         status_code = le_to_host16(mgmt->u.auth.status_code);
00513         fc = le_to_host16(mgmt->frame_control);
00514 
00515         if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
00516             2 + WLAN_AUTH_CHALLENGE_LEN &&
00517             mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
00518             mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
00519                 challenge = &mgmt->u.auth.variable[2];
00520 
00521         wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
00522                    "auth_transaction=%d status_code=%d wep=%d%s",
00523                    MAC2STR(mgmt->sa), auth_alg, auth_transaction,
00524                    status_code, !!(fc & WLAN_FC_ISWEP),
00525                    challenge ? " challenge" : "");
00526 
00527         if (hapd->tkip_countermeasures) {
00528                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
00529                 goto fail;
00530         }
00531 
00532         if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
00533                auth_alg == WLAN_AUTH_OPEN) ||
00534 #ifdef CONFIG_IEEE80211R
00535               (hapd->conf->wpa &&
00536                (hapd->conf->wpa_key_mgmt &
00537                 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
00538                auth_alg == WLAN_AUTH_FT) ||
00539 #endif /* CONFIG_IEEE80211R */
00540               ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
00541                auth_alg == WLAN_AUTH_SHARED_KEY))) {
00542                 printf("Unsupported authentication algorithm (%d)\n",
00543                        auth_alg);
00544                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
00545                 goto fail;
00546         }
00547 
00548         if (!(auth_transaction == 1 ||
00549               (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
00550                 printf("Unknown authentication transaction number (%d)\n",
00551                        auth_transaction);
00552                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
00553                 goto fail;
00554         }
00555 
00556         if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
00557                 printf("Station " MACSTR " not allowed to authenticate.\n",
00558                        MAC2STR(mgmt->sa));
00559                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00560                 goto fail;
00561         }
00562 
00563         res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
00564                                       &session_timeout,
00565                                       &acct_interim_interval, &vlan_id);
00566         if (res == HOSTAPD_ACL_REJECT) {
00567                 printf("Station " MACSTR " not allowed to authenticate.\n",
00568                        MAC2STR(mgmt->sa));
00569                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00570                 goto fail;
00571         }
00572         if (res == HOSTAPD_ACL_PENDING) {
00573                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
00574                            " waiting for an external authentication",
00575                            MAC2STR(mgmt->sa));
00576                 /* Authentication code will re-send the authentication frame
00577                  * after it has received (and cached) information from the
00578                  * external source. */
00579                 return;
00580         }
00581 
00582         sta = ap_sta_add(hapd, mgmt->sa);
00583         if (!sta) {
00584                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00585                 goto fail;
00586         }
00587 
00588         if (vlan_id > 0) {
00589                 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
00590                                                vlan_id) == NULL) {
00591                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
00592                                        HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
00593                                        "%d received from RADIUS server",
00594                                        vlan_id);
00595                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00596                         goto fail;
00597                 }
00598                 sta->vlan_id = vlan_id;
00599                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
00600                                HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
00601         }
00602 
00603         sta->flags &= ~WLAN_STA_PREAUTH;
00604         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
00605 
00606         if (hapd->conf->radius->acct_interim_interval == 0 &&
00607             acct_interim_interval)
00608                 sta->acct_interim_interval = acct_interim_interval;
00609         if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
00610                 ap_sta_session_timeout(hapd, sta, session_timeout);
00611         else
00612                 ap_sta_no_session_timeout(hapd, sta);
00613 
00614         switch (auth_alg) {
00615         case WLAN_AUTH_OPEN:
00616                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
00617                                HOSTAPD_LEVEL_DEBUG,
00618                                "authentication OK (open system)");
00619 #ifdef IEEE80211_REQUIRE_AUTH_ACK
00620                 /* Station will be marked authenticated if it ACKs the
00621                  * authentication reply. */
00622 #else
00623                 sta->flags |= WLAN_STA_AUTH;
00624                 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
00625                 sta->auth_alg = WLAN_AUTH_OPEN;
00626                 mlme_authenticate_indication(hapd, sta);
00627 #endif
00628                 break;
00629         case WLAN_AUTH_SHARED_KEY:
00630                 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
00631                                        fc & WLAN_FC_ISWEP);
00632                 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
00633                 mlme_authenticate_indication(hapd, sta);
00634                 if (sta->challenge && auth_transaction == 1) {
00635                         resp_ies[0] = WLAN_EID_CHALLENGE;
00636                         resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
00637                         os_memcpy(resp_ies + 2, sta->challenge,
00638                                   WLAN_AUTH_CHALLENGE_LEN);
00639                         resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
00640                 }
00641                 break;
00642 #ifdef CONFIG_IEEE80211R
00643         case WLAN_AUTH_FT:
00644                 sta->auth_alg = WLAN_AUTH_FT;
00645                 if (sta->wpa_sm == NULL)
00646                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
00647                                                         sta->addr);
00648                 if (sta->wpa_sm == NULL) {
00649                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
00650                                    "state machine");
00651                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00652                         goto fail;
00653                 }
00654                 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
00655                                     auth_transaction, mgmt->u.auth.variable,
00656                                     len - IEEE80211_HDRLEN -
00657                                     sizeof(mgmt->u.auth),
00658                                     handle_auth_ft_finish, hapd);
00659                 /* handle_auth_ft_finish() callback will complete auth. */
00660                 return;
00661 #endif /* CONFIG_IEEE80211R */
00662         }
00663 
00664  fail:
00665         send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
00666                         auth_transaction + 1, resp, resp_ies, resp_ies_len);
00667 }
00668 
00669 
00670 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
00671 {
00672         int i, j = 32, aid;
00673 
00674         /* get a unique AID */
00675         if (sta->aid > 0) {
00676                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
00677                 return 0;
00678         }
00679 
00680         for (i = 0; i < AID_WORDS; i++) {
00681                 if (hapd->sta_aid[i] == (u32) -1)
00682                         continue;
00683                 for (j = 0; j < 32; j++) {
00684                         if (!(hapd->sta_aid[i] & BIT(j)))
00685                                 break;
00686                 }
00687                 if (j < 32)
00688                         break;
00689         }
00690         if (j == 32)
00691                 return -1;
00692         aid = i * 32 + j + 1;
00693         if (aid > 2007)
00694                 return -1;
00695 
00696         sta->aid = aid;
00697         hapd->sta_aid[i] |= BIT(j);
00698         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
00699         return 0;
00700 }
00701 
00702 
00703 static void handle_assoc(struct hostapd_data *hapd,
00704                          struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
00705 {
00706         u16 capab_info, listen_interval;
00707         u16 resp = WLAN_STATUS_SUCCESS;
00708         u8 *pos, *wpa_ie;
00709         size_t wpa_ie_len;
00710         int send_deauth = 0, send_len, left, i;
00711         struct sta_info *sta;
00712         struct ieee802_11_elems elems;
00713         u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
00714         struct ieee80211_mgmt *reply;
00715 
00716         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
00717                                       sizeof(mgmt->u.assoc_req))) {
00718                 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
00719                        "\n", reassoc, (unsigned long) len);
00720                 return;
00721         }
00722 
00723         if (reassoc) {
00724                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
00725                 listen_interval = le_to_host16(
00726                         mgmt->u.reassoc_req.listen_interval);
00727                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
00728                            " capab_info=0x%02x listen_interval=%d current_ap="
00729                            MACSTR,
00730                            MAC2STR(mgmt->sa), capab_info, listen_interval,
00731                            MAC2STR(mgmt->u.reassoc_req.current_ap));
00732                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
00733                 pos = mgmt->u.reassoc_req.variable;
00734         } else {
00735                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
00736                 listen_interval = le_to_host16(
00737                         mgmt->u.assoc_req.listen_interval);
00738                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
00739                            " capab_info=0x%02x listen_interval=%d",
00740                            MAC2STR(mgmt->sa), capab_info, listen_interval);
00741                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
00742                 pos = mgmt->u.assoc_req.variable;
00743         }
00744 
00745         sta = ap_get_sta(hapd, mgmt->sa);
00746 #ifdef CONFIG_IEEE80211R
00747         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
00748             (sta->flags & WLAN_STA_AUTH) == 0) {
00749                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
00750                            "prior to authentication since it is using "
00751                            "over-the-DS FT", MAC2STR(mgmt->sa));
00752         } else
00753 #endif /* CONFIG_IEEE80211R */
00754         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
00755                 printf("STA " MACSTR " trying to associate before "
00756                        "authentication\n", MAC2STR(mgmt->sa));
00757                 if (sta) {
00758                         printf("  sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
00759                                MAC2STR(sta->addr), sta->aid, sta->flags);
00760                 }
00761                 send_deauth = 1;
00762                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00763                 goto fail;
00764         }
00765 
00766         if (hapd->tkip_countermeasures) {
00767                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
00768                 goto fail;
00769         }
00770 
00771         if (listen_interval > hapd->conf->max_listen_interval) {
00772                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
00773                                HOSTAPD_LEVEL_DEBUG,
00774                                "Too large Listen Interval (%d)",
00775                                listen_interval);
00776                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
00777                 goto fail;
00778         }
00779 
00780         sta->capability = capab_info;
00781         sta->listen_interval = listen_interval;
00782 
00783         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
00784          * is used */
00785         if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
00786             !elems.ssid) {
00787                 printf("STA " MACSTR " sent invalid association request\n",
00788                        MAC2STR(sta->addr));
00789                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00790                 goto fail;
00791         }
00792 
00793         if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
00794             os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
00795         {
00796                 char ssid_txt[33];
00797                 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
00798                 printf("Station " MACSTR " tried to associate with "
00799                        "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
00800                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00801                 goto fail;
00802         }
00803 
00804         sta->flags &= ~WLAN_STA_WMM;
00805         if (elems.wmm && hapd->conf->wmm_enabled) {
00806                 if (hostapd_eid_wmm_valid(hapd, elems.wmm, elems.wmm_len))
00807                         hostapd_logger(hapd, sta->addr,
00808                                        HOSTAPD_MODULE_WPA,
00809                                        HOSTAPD_LEVEL_DEBUG,
00810                                        "invalid WMM element in association "
00811                                        "request");
00812                 else
00813                         sta->flags |= WLAN_STA_WMM;
00814         }
00815 
00816         if (!elems.supp_rates) {
00817                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
00818                                HOSTAPD_LEVEL_DEBUG,
00819                                "No supported rates element in AssocReq");
00820                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00821                 goto fail;
00822         }
00823 
00824         if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
00825                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
00826                                HOSTAPD_LEVEL_DEBUG,
00827                                "Invalid supported rates element length %d",
00828                                elems.supp_rates_len);
00829                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00830                 goto fail;
00831         }
00832 
00833         os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
00834         os_memcpy(sta->supported_rates, elems.supp_rates,
00835                   elems.supp_rates_len);
00836         sta->supported_rates_len = elems.supp_rates_len;
00837 
00838         if (elems.ext_supp_rates) {
00839                 if (elems.supp_rates_len + elems.ext_supp_rates_len >
00840                     sizeof(sta->supported_rates)) {
00841                         hostapd_logger(hapd, mgmt->sa,
00842                                        HOSTAPD_MODULE_IEEE80211,
00843                                        HOSTAPD_LEVEL_DEBUG,
00844                                        "Invalid supported rates element length"
00845                                        " %d+%d", elems.supp_rates_len,
00846                                        elems.ext_supp_rates_len);
00847                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00848                         goto fail;
00849                 }
00850 
00851                 os_memcpy(sta->supported_rates + elems.supp_rates_len,
00852                           elems.ext_supp_rates, elems.ext_supp_rates_len);
00853                 sta->supported_rates_len += elems.ext_supp_rates_len;
00854         }
00855 
00856 #ifdef CONFIG_IEEE80211N
00857         /* save HT capabilities in the sta object */
00858         os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
00859         if (elems.ht_capabilities &&
00860             elems.ht_capabilities_len >=
00861             sizeof(struct ieee80211_ht_capability)) {
00862                 sta->flags |= WLAN_STA_HT;
00863                 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
00864                 sta->ht_capabilities.length =
00865                         sizeof(struct ieee80211_ht_capability);
00866                 os_memcpy(&sta->ht_capabilities.data,
00867                           elems.ht_capabilities,
00868                           sizeof(struct ieee80211_ht_capability));
00869         } else
00870                 sta->flags &= ~WLAN_STA_HT;
00871 #endif /* CONFIG_IEEE80211N */
00872 
00873         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
00874                 wpa_ie = elems.rsn_ie;
00875                 wpa_ie_len = elems.rsn_ie_len;
00876         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
00877                    elems.wpa_ie) {
00878                 wpa_ie = elems.wpa_ie;
00879                 wpa_ie_len = elems.wpa_ie_len;
00880         } else {
00881                 wpa_ie = NULL;
00882                 wpa_ie_len = 0;
00883         }
00884 #ifdef CONFIG_WPS
00885         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
00886         if (hapd->conf->wps_state && wpa_ie == NULL) {
00887                 if (elems.wps_ie) {
00888                         wpa_printf(MSG_DEBUG, "STA included WPS IE in "
00889                                    "(Re)Association Request - assume WPS is "
00890                                    "used");
00891                         sta->flags |= WLAN_STA_WPS;
00892                         wpabuf_free(sta->wps_ie);
00893                         sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
00894                                                         elems.wps_ie_len - 4);
00895                 } else {
00896                         wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
00897                                    "in (Re)Association Request - possible WPS "
00898                                    "use");
00899                         sta->flags |= WLAN_STA_MAYBE_WPS;
00900                 }
00901         } else
00902 #endif /* CONFIG_WPS */
00903         if (hapd->conf->wpa && wpa_ie == NULL) {
00904                 printf("STA " MACSTR ": No WPA/RSN IE in association "
00905                        "request\n", MAC2STR(sta->addr));
00906                 resp = WLAN_STATUS_INVALID_IE;
00907                 goto fail;
00908         }
00909 
00910         if (hapd->conf->wpa && wpa_ie) {
00911                 int res;
00912                 wpa_ie -= 2;
00913                 wpa_ie_len += 2;
00914                 if (sta->wpa_sm == NULL)
00915                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
00916                                                         sta->addr);
00917                 if (sta->wpa_sm == NULL) {
00918                         printf("Failed to initialize WPA state machine\n");
00919                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00920                         goto fail;
00921                 }
00922                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
00923                                           wpa_ie, wpa_ie_len,
00924                                           elems.mdie, elems.mdie_len);
00925                 if (res == WPA_INVALID_GROUP)
00926                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
00927                 else if (res == WPA_INVALID_PAIRWISE)
00928                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
00929                 else if (res == WPA_INVALID_AKMP)
00930                         resp = WLAN_STATUS_AKMP_NOT_VALID;
00931                 else if (res == WPA_ALLOC_FAIL)
00932                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00933 #ifdef CONFIG_IEEE80211W
00934                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
00935                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
00936                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
00937                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
00938 #endif /* CONFIG_IEEE80211W */
00939                 else if (res == WPA_INVALID_MDIE)
00940                         resp = WLAN_STATUS_INVALID_MDIE;
00941                 else if (res != WPA_IE_OK)
00942                         resp = WLAN_STATUS_INVALID_IE;
00943                 if (resp != WLAN_STATUS_SUCCESS)
00944                         goto fail;
00945 #ifdef CONFIG_IEEE80211W
00946                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
00947                     sta->sa_query_count > 0)
00948                         ap_check_sa_query_timeout(hapd, sta);
00949                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
00950                     (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
00951                         /*
00952                          * STA has already been associated with MFP and SA
00953                          * Query timeout has not been reached. Reject the
00954                          * association attempt temporarily and start SA Query,
00955                          * if one is not pending.
00956                          */
00957 
00958                         if (sta->sa_query_count == 0)
00959                                 ap_sta_start_sa_query(hapd, sta);
00960 
00961                         resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
00962                         goto fail;
00963                 }
00964 
00965                 if (wpa_auth_uses_mfp(sta->wpa_sm))
00966                         sta->flags |= WLAN_STA_MFP;
00967                 else
00968                         sta->flags &= ~WLAN_STA_MFP;
00969 #endif /* CONFIG_IEEE80211W */
00970 
00971 #ifdef CONFIG_IEEE80211R
00972                 if (sta->auth_alg == WLAN_AUTH_FT) {
00973                         if (!reassoc) {
00974                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
00975                                            "to use association (not "
00976                                            "re-association) with FT auth_alg",
00977                                            MAC2STR(sta->addr));
00978                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
00979                                 goto fail;
00980                         }
00981 
00982                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
00983                         if (resp != WLAN_STATUS_SUCCESS)
00984                                 goto fail;
00985                 }
00986 #endif /* CONFIG_IEEE80211R */
00987 #ifdef CONFIG_IEEE80211N
00988                 if ((sta->flags & WLAN_STA_HT) &&
00989                     wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
00990                         wpa_printf(MSG_DEBUG, "HT: " MACSTR " tried to "
00991                                    "use TKIP with HT association",
00992                                    MAC2STR(sta->addr));
00993                         resp = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
00994                         goto fail;
00995                 }
00996 #endif /* CONFIG_IEEE80211N */
00997         } else
00998                 wpa_auth_sta_no_wpa(sta->wpa_sm);
00999 
01000         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
01001                 sta->flags |= WLAN_STA_NONERP;
01002         for (i = 0; i < sta->supported_rates_len; i++) {
01003                 if ((sta->supported_rates[i] & 0x7f) > 22) {
01004                         sta->flags &= ~WLAN_STA_NONERP;
01005                         break;
01006                 }
01007         }
01008         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
01009                 sta->nonerp_set = 1;
01010                 hapd->iface->num_sta_non_erp++;
01011                 if (hapd->iface->num_sta_non_erp == 1)
01012                         ieee802_11_set_beacons(hapd->iface);
01013         }
01014 
01015         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
01016             !sta->no_short_slot_time_set) {
01017                 sta->no_short_slot_time_set = 1;
01018                 hapd->iface->num_sta_no_short_slot_time++;
01019                 if (hapd->iface->current_mode->mode ==
01020                     HOSTAPD_MODE_IEEE80211G &&
01021                     hapd->iface->num_sta_no_short_slot_time == 1)
01022                         ieee802_11_set_beacons(hapd->iface);
01023         }
01024 
01025         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
01026                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
01027         else
01028                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
01029 
01030         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
01031             !sta->no_short_preamble_set) {
01032                 sta->no_short_preamble_set = 1;
01033                 hapd->iface->num_sta_no_short_preamble++;
01034                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
01035                     && hapd->iface->num_sta_no_short_preamble == 1)
01036                         ieee802_11_set_beacons(hapd->iface);
01037         }
01038 
01039 #ifdef CONFIG_IEEE80211N
01040         if (sta->flags & WLAN_STA_HT) {
01041                 u16 ht_capab = le_to_host16(
01042                         sta->ht_capabilities.data.capabilities_info);
01043                 wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
01044                            "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
01045                 if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
01046                         if (!sta->no_ht_gf_set) {
01047                                 sta->no_ht_gf_set = 1;
01048                                 hapd->iface->num_sta_ht_no_gf++;
01049                         }
01050                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
01051                                    "greenfield, num of non-gf stations %d",
01052                                    __func__, MAC2STR(sta->addr),
01053                                    hapd->iface->num_sta_ht_no_gf);
01054                 }
01055                 if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
01056                         if (!sta->ht_20mhz_set) {
01057                                 sta->ht_20mhz_set = 1;
01058                                 hapd->iface->num_sta_ht_20mhz++;
01059                         }
01060                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
01061                                    "num of 20MHz HT STAs %d",
01062                                    __func__, MAC2STR(sta->addr),
01063                                    hapd->iface->num_sta_ht_20mhz);
01064                 }
01065         } else {
01066                 if (!sta->no_ht_set) {
01067                         sta->no_ht_set = 1;
01068                         hapd->iface->num_sta_no_ht++;
01069                 }
01070                 if (hapd->iconf->ieee80211n) {
01071                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR
01072                                    " - no HT, num of non-HT stations %d",
01073                                    __func__, MAC2STR(sta->addr),
01074                                    hapd->iface->num_sta_no_ht);
01075                 }
01076         }
01077 
01078         if (hostapd_ht_operation_update(hapd->iface) > 0)
01079                 ieee802_11_set_beacons(hapd->iface);
01080 #endif /* CONFIG_IEEE80211N */
01081 
01082         if (hostapd_get_aid(hapd, sta) < 0) {
01083                 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
01084                 wpa_printf(MSG_ERROR, "  no room for more AIDs");
01085                 goto fail;
01086         }
01087 
01088         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
01089                        HOSTAPD_LEVEL_DEBUG,
01090                        "association OK (aid %d)", sta->aid);
01091         /* Station will be marked associated, after it acknowledges AssocResp
01092          */
01093 
01094 #ifdef CONFIG_IEEE80211W
01095         if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
01096                 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
01097                            "SA Query procedure", reassoc ? "re" : "");
01098                 /* TODO: Send a protected Disassociate frame to the STA using
01099                  * the old key and Reason Code "Previous Authentication no
01100                  * longer valid". Make sure this is only sent protected since
01101                  * unprotected frame would be received by the STA that is now
01102                  * trying to associate.
01103                  */
01104         }
01105 #endif /* CONFIG_IEEE80211W */
01106 
01107         if (reassoc) {
01108                 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
01109                           ETH_ALEN);
01110         }
01111 
01112         if (sta->last_assoc_req)
01113                 os_free(sta->last_assoc_req);
01114         sta->last_assoc_req = os_malloc(len);
01115         if (sta->last_assoc_req)
01116                 os_memcpy(sta->last_assoc_req, mgmt, len);
01117 
01118         /* Make sure that the previously registered inactivity timer will not
01119          * remove the STA immediately. */
01120         sta->timeout_next = STA_NULLFUNC;
01121 
01122  fail:
01123         os_memset(buf, 0, sizeof(buf));
01124         reply = (struct ieee80211_mgmt *) buf;
01125         reply->frame_control =
01126                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
01127                              (send_deauth ? WLAN_FC_STYPE_DEAUTH :
01128                               (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
01129                                WLAN_FC_STYPE_ASSOC_RESP)));
01130         os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
01131         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
01132         os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
01133 
01134         send_len = IEEE80211_HDRLEN;
01135         if (send_deauth) {
01136                 send_len += sizeof(reply->u.deauth);
01137                 reply->u.deauth.reason_code = host_to_le16(resp);
01138         } else {
01139                 u8 *p;
01140                 send_len += sizeof(reply->u.assoc_resp);
01141                 reply->u.assoc_resp.capab_info =
01142                         host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
01143                 reply->u.assoc_resp.status_code = host_to_le16(resp);
01144                 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
01145                                                        | BIT(14) | BIT(15));
01146                 /* Supported rates */
01147                 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
01148                 /* Extended supported rates */
01149                 p = hostapd_eid_ext_supp_rates(hapd, p);
01150                 if (sta->flags & WLAN_STA_WMM)
01151                         p = hostapd_eid_wmm(hapd, p);
01152 
01153                 p = hostapd_eid_ht_capabilities_info(hapd, p);
01154                 p = hostapd_eid_ht_operation(hapd, p);
01155 
01156 #ifdef CONFIG_IEEE80211R
01157                 if (resp == WLAN_STATUS_SUCCESS) {
01158                         /* IEEE 802.11r: Mobility Domain Information, Fast BSS
01159                          * Transition Information, RSN, [RIC Response] */
01160                         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
01161                                                         buf + sizeof(buf) - p,
01162                                                         sta->auth_alg,
01163                                                         pos, left);
01164                 }
01165 #endif /* CONFIG_IEEE80211R */
01166 
01167 #ifdef CONFIG_IEEE80211W
01168                 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
01169                         p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
01170 #endif /* CONFIG_IEEE80211W */
01171 
01172                 send_len += p - reply->u.assoc_resp.variable;
01173         }
01174 
01175         if (hostapd_send_mgmt_frame(hapd, reply, send_len) < 0)
01176                 perror("handle_assoc: send");
01177 }
01178 
01179 
01180 static void handle_disassoc(struct hostapd_data *hapd,
01181                             struct ieee80211_mgmt *mgmt, size_t len)
01182 {
01183         struct sta_info *sta;
01184 
01185         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
01186                 printf("handle_disassoc - too short payload (len=%lu)\n",
01187                        (unsigned long) len);
01188                 return;
01189         }
01190 
01191         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
01192                    MAC2STR(mgmt->sa),
01193                    le_to_host16(mgmt->u.disassoc.reason_code));
01194 
01195         sta = ap_get_sta(hapd, mgmt->sa);
01196         if (sta == NULL) {
01197                 printf("Station " MACSTR " trying to disassociate, but it "
01198                        "is not associated.\n", MAC2STR(mgmt->sa));
01199                 return;
01200         }
01201 
01202         sta->flags &= ~WLAN_STA_ASSOC;
01203         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
01204                 MAC2STR(sta->addr));
01205         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
01206         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
01207                        HOSTAPD_LEVEL_INFO, "disassociated");
01208         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
01209         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
01210         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
01211          * authenticated. */
01212         accounting_sta_stop(hapd, sta);
01213         ieee802_1x_free_station(sta);
01214         hostapd_sta_remove(hapd, sta->addr);
01215 
01216         if (sta->timeout_next == STA_NULLFUNC ||
01217             sta->timeout_next == STA_DISASSOC) {
01218                 sta->timeout_next = STA_DEAUTH;
01219                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
01220                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
01221                                        hapd, sta);
01222         }
01223 
01224         mlme_disassociate_indication(
01225                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
01226 }
01227 
01228 
01229 static void handle_deauth(struct hostapd_data *hapd,
01230                           struct ieee80211_mgmt *mgmt, size_t len)
01231 {
01232         struct sta_info *sta;
01233 
01234         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
01235                 printf("handle_deauth - too short payload (len=%lu)\n",
01236                        (unsigned long) len);
01237                 return;
01238         }
01239 
01240         wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
01241                    " reason_code=%d",
01242                    MAC2STR(mgmt->sa),
01243                    le_to_host16(mgmt->u.deauth.reason_code));
01244 
01245         sta = ap_get_sta(hapd, mgmt->sa);
01246         if (sta == NULL) {
01247                 printf("Station " MACSTR " trying to deauthenticate, but it "
01248                        "is not authenticated.\n", MAC2STR(mgmt->sa));
01249                 return;
01250         }
01251 
01252         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
01253         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
01254                 MAC2STR(sta->addr));
01255         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
01256         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
01257                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
01258         mlme_deauthenticate_indication(
01259                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
01260         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
01261         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
01262         ap_free_sta(hapd, sta);
01263 }
01264 
01265 
01266 static void handle_beacon(struct hostapd_data *hapd,
01267                           struct ieee80211_mgmt *mgmt, size_t len,
01268                           struct hostapd_frame_info *fi)
01269 {
01270         struct ieee802_11_elems elems;
01271 
01272         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
01273                 printf("handle_beacon - too short payload (len=%lu)\n",
01274                        (unsigned long) len);
01275                 return;
01276         }
01277 
01278         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
01279                                       len - (IEEE80211_HDRLEN +
01280                                              sizeof(mgmt->u.beacon)), &elems,
01281                                       0);
01282 
01283         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
01284 }
01285 
01286 
01287 #ifdef CONFIG_IEEE80211W
01288 
01289 /* MLME-SAQuery.request */
01290 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
01291                                   const u8 *addr, const u8 *trans_id)
01292 {
01293         struct ieee80211_mgmt mgmt;
01294         u8 *end;
01295 
01296         wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
01297                    MACSTR, MAC2STR(addr));
01298         wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
01299                     trans_id, WLAN_SA_QUERY_TR_ID_LEN);
01300 
01301         os_memset(&mgmt, 0, sizeof(mgmt));
01302         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
01303                                           WLAN_FC_STYPE_ACTION);
01304         os_memcpy(mgmt.da, addr, ETH_ALEN);
01305         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
01306         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
01307         mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
01308         mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
01309         os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
01310                   WLAN_SA_QUERY_TR_ID_LEN);
01311         end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
01312         if (hostapd_send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
01313                 perror("ieee802_11_send_sa_query_req: send");
01314 }
01315 
01316 
01317 static void hostapd_sa_query_action(struct hostapd_data *hapd,
01318                                     struct ieee80211_mgmt *mgmt, size_t len)
01319 {
01320         struct sta_info *sta;
01321         u8 *end;
01322         int i;
01323 
01324         end = mgmt->u.action.u.sa_query_resp.trans_id +
01325                 WLAN_SA_QUERY_TR_ID_LEN;
01326         if (((u8 *) mgmt) + len < end) {
01327                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
01328                            "frame (len=%lu)", (unsigned long) len);
01329                 return;
01330         }
01331 
01332         if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
01333                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
01334                            "Action %d", mgmt->u.action.u.sa_query_resp.action);
01335                 return;
01336         }
01337 
01338         wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
01339                    MACSTR, MAC2STR(mgmt->sa));
01340         wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
01341                     mgmt->u.action.u.sa_query_resp.trans_id,
01342                     WLAN_SA_QUERY_TR_ID_LEN);
01343 
01344         /* MLME-SAQuery.confirm */
01345 
01346         sta = ap_get_sta(hapd, mgmt->sa);
01347         if (sta == NULL || sta->sa_query_trans_id == NULL) {
01348                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
01349                            "pending SA Query request found");
01350                 return;
01351         }
01352 
01353         for (i = 0; i < sta->sa_query_count; i++) {
01354                 if (os_memcmp(sta->sa_query_trans_id +
01355                               i * WLAN_SA_QUERY_TR_ID_LEN,
01356                               mgmt->u.action.u.sa_query_resp.trans_id,
01357                               WLAN_SA_QUERY_TR_ID_LEN) == 0)
01358                         break;
01359         }
01360 
01361         if (i >= sta->sa_query_count) {
01362                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
01363                            "transaction identifier found");
01364                 return;
01365         }
01366 
01367         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
01368                        HOSTAPD_LEVEL_DEBUG,
01369                        "Reply to pending SA Query received");
01370         ap_sta_stop_sa_query(hapd, sta);
01371 }
01372 
01373 
01374 static int robust_action_frame(u8 category)
01375 {
01376         return category != WLAN_ACTION_PUBLIC &&
01377                 category != WLAN_ACTION_HT;
01378 }
01379 #endif /* CONFIG_IEEE80211W */
01380 
01381 
01382 static void handle_action(struct hostapd_data *hapd,
01383                           struct ieee80211_mgmt *mgmt, size_t len)
01384 {
01385         struct sta_info *sta;
01386 
01387         if (len < IEEE80211_HDRLEN + 1) {
01388                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
01389                                HOSTAPD_LEVEL_DEBUG,
01390                                "handle_action - too short payload (len=%lu)",
01391                                (unsigned long) len);
01392                 return;
01393         }
01394 
01395         sta = ap_get_sta(hapd, mgmt->sa);
01396 #ifdef CONFIG_IEEE80211W
01397         if (sta && (sta->flags & WLAN_STA_MFP) &&
01398             !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
01399               robust_action_frame(mgmt->u.action.category))) {
01400                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
01401                                HOSTAPD_LEVEL_DEBUG,
01402                                "Dropped unprotected Robust Action frame from "
01403                                "an MFP STA");
01404                 return;
01405         }
01406 #endif /* CONFIG_IEEE80211W */
01407 
01408         switch (mgmt->u.action.category) {
01409 #ifdef CONFIG_IEEE80211R
01410         case WLAN_ACTION_FT:
01411         {
01412                 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
01413                         wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
01414                                    "frame from unassociated STA " MACSTR,
01415                                    MAC2STR(mgmt->sa));
01416                         return;
01417                 }
01418 
01419                 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
01420                                      len - IEEE80211_HDRLEN))
01421                         break;
01422 
01423                 return;
01424         }
01425 #endif /* CONFIG_IEEE80211R */
01426         case WLAN_ACTION_WMM:
01427                 hostapd_wmm_action(hapd, mgmt, len);
01428                 return;
01429 #ifdef CONFIG_IEEE80211W
01430         case WLAN_ACTION_SA_QUERY:
01431                 hostapd_sa_query_action(hapd, mgmt, len);
01432                 return;
01433 #endif /* CONFIG_IEEE80211W */
01434         }
01435 
01436         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
01437                        HOSTAPD_LEVEL_DEBUG,
01438                        "handle_action - unknown action category %d or invalid "
01439                        "frame",
01440                        mgmt->u.action.category);
01441         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
01442             !(mgmt->sa[0] & 0x01)) {
01443                 /*
01444                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
01445                  * Return the Action frame to the source without change
01446                  * except that MSB of the Category set to 1.
01447                  */
01448                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
01449                            "frame back to sender");
01450                 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
01451                 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
01452                 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
01453                 mgmt->u.action.category |= 0x80;
01454 
01455                 hostapd_send_mgmt_frame(hapd, mgmt, len);
01456         }
01457 }
01458 
01459 
01475 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
01476                      struct hostapd_frame_info *fi)
01477 {
01478         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
01479         int broadcast;
01480 
01481         if (stype == WLAN_FC_STYPE_BEACON) {
01482                 handle_beacon(hapd, mgmt, len, fi);
01483                 return;
01484         }
01485 
01486         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
01487                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
01488                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
01489 
01490         if (!broadcast &&
01491             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
01492                 printf("MGMT: BSSID=" MACSTR " not our address\n",
01493                        MAC2STR(mgmt->bssid));
01494                 return;
01495         }
01496 
01497 
01498         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
01499                 handle_probe_req(hapd, mgmt, len);
01500                 return;
01501         }
01502 
01503         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
01504                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
01505                                HOSTAPD_LEVEL_DEBUG,
01506                                "MGMT: DA=" MACSTR " not our address",
01507                                MAC2STR(mgmt->da));
01508                 return;
01509         }
01510 
01511         switch (stype) {
01512         case WLAN_FC_STYPE_AUTH:
01513                 wpa_printf(MSG_DEBUG, "mgmt::auth");
01514                 handle_auth(hapd, mgmt, len);
01515                 break;
01516         case WLAN_FC_STYPE_ASSOC_REQ:
01517                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
01518                 handle_assoc(hapd, mgmt, len, 0);
01519                 break;
01520         case WLAN_FC_STYPE_REASSOC_REQ:
01521                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
01522                 handle_assoc(hapd, mgmt, len, 1);
01523                 break;
01524         case WLAN_FC_STYPE_DISASSOC:
01525                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
01526                 handle_disassoc(hapd, mgmt, len);
01527                 break;
01528         case WLAN_FC_STYPE_DEAUTH:
01529                 wpa_printf(MSG_DEBUG, "mgmt::deauth");
01530                 handle_deauth(hapd, mgmt, len);
01531                 break;
01532         case WLAN_FC_STYPE_ACTION:
01533                 wpa_printf(MSG_DEBUG, "mgmt::action");
01534                 handle_action(hapd, mgmt, len);
01535                 break;
01536         default:
01537                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
01538                                HOSTAPD_LEVEL_DEBUG,
01539                                "unknown mgmt frame subtype %d", stype);
01540                 break;
01541         }
01542 }
01543 
01544 
01545 static void handle_auth_cb(struct hostapd_data *hapd,
01546                            struct ieee80211_mgmt *mgmt,
01547                            size_t len, int ok)
01548 {
01549         u16 auth_alg, auth_transaction, status_code;
01550         struct sta_info *sta;
01551 
01552         if (!ok) {
01553                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
01554                                HOSTAPD_LEVEL_NOTICE,
01555                                "did not acknowledge authentication response");
01556                 return;
01557         }
01558 
01559         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
01560                 printf("handle_auth_cb - too short payload (len=%lu)\n",
01561                        (unsigned long) len);
01562                 return;
01563         }
01564 
01565         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
01566         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
01567         status_code = le_to_host16(mgmt->u.auth.status_code);
01568 
01569         sta = ap_get_sta(hapd, mgmt->da);
01570         if (!sta) {
01571                 printf("handle_auth_cb: STA " MACSTR " not found\n",
01572                        MAC2STR(mgmt->da));
01573                 return;
01574         }
01575 
01576         if (status_code == WLAN_STATUS_SUCCESS &&
01577             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
01578              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
01579                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
01580                                HOSTAPD_LEVEL_INFO, "authenticated");
01581                 sta->flags |= WLAN_STA_AUTH;
01582         }
01583 }
01584 
01585 
01586 #ifdef CONFIG_IEEE80211N
01587 static void
01588 hostapd_get_ht_capab(struct hostapd_data *hapd,
01589                      struct ht_cap_ie *ht_cap_ie,
01590                      struct ht_cap_ie *neg_ht_cap_ie)
01591 {
01592         u16 cap;
01593 
01594         os_memcpy(neg_ht_cap_ie, ht_cap_ie, sizeof(struct ht_cap_ie));
01595         cap = le_to_host16(neg_ht_cap_ie->data.capabilities_info);
01596         cap &= hapd->iconf->ht_capab;
01597         cap |= (hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_DISABLED);
01598 
01599         /* FIXME: Rx STBC needs to be handled specially */
01600         cap |= (hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK);
01601         neg_ht_cap_ie->data.capabilities_info = host_to_le16(cap);
01602 }
01603 #endif /* CONFIG_IEEE80211N */
01604 
01605 
01606 static void handle_assoc_cb(struct hostapd_data *hapd,
01607                             struct ieee80211_mgmt *mgmt,
01608                             size_t len, int reassoc, int ok)
01609 {
01610         u16 status;
01611         struct sta_info *sta;
01612         int new_assoc = 1;
01613 #ifdef CONFIG_IEEE80211N
01614         struct ht_cap_ie ht_cap;
01615 #endif /* CONFIG_IEEE80211N */
01616         struct ht_cap_ie *ht_cap_ptr = NULL;
01617         int set_flags, flags_and, flags_or;
01618 
01619         if (!ok) {
01620                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
01621                                HOSTAPD_LEVEL_DEBUG,
01622                                "did not acknowledge association response");
01623                 return;
01624         }
01625 
01626         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
01627                                       sizeof(mgmt->u.assoc_resp))) {
01628                 printf("handle_assoc_cb(reassoc=%d) - too short payload "
01629                        "(len=%lu)\n", reassoc, (unsigned long) len);
01630                 return;
01631         }
01632 
01633         if (reassoc)
01634                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
01635         else
01636                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
01637 
01638         sta = ap_get_sta(hapd, mgmt->da);
01639         if (!sta) {
01640                 printf("handle_assoc_cb: STA " MACSTR " not found\n",
01641                        MAC2STR(mgmt->da));
01642                 return;
01643         }
01644 
01645         if (status != WLAN_STATUS_SUCCESS)
01646                 goto fail;
01647 
01648         /* Stop previous accounting session, if one is started, and allocate
01649          * new session id for the new session. */
01650         accounting_sta_stop(hapd, sta);
01651 
01652         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
01653                        HOSTAPD_LEVEL_INFO,
01654                        "associated (aid %d)",
01655                        sta->aid);
01656 
01657         if (sta->flags & WLAN_STA_ASSOC)
01658                 new_assoc = 0;
01659         sta->flags |= WLAN_STA_ASSOC;
01660         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
01661                 /* Open or static WEP; no separate authorization */
01662                 sta->flags |= WLAN_STA_AUTHORIZED;
01663                 wpa_msg(hapd->msg_ctx, MSG_INFO,
01664                         AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
01665         }
01666 
01667         if (reassoc)
01668                 mlme_reassociate_indication(hapd, sta);
01669         else
01670                 mlme_associate_indication(hapd, sta);
01671 
01672 #ifdef CONFIG_IEEE80211N
01673         if (sta->flags & WLAN_STA_HT) {
01674                 ht_cap_ptr = &ht_cap;
01675                 hostapd_get_ht_capab(hapd, &sta->ht_capabilities, ht_cap_ptr);
01676         }
01677 #endif /* CONFIG_IEEE80211N */
01678 
01679 #ifdef CONFIG_IEEE80211W
01680         sta->sa_query_timed_out = 0;
01681 #endif /* CONFIG_IEEE80211W */
01682 
01683         /*
01684          * Remove the STA entry in order to make sure the STA PS state gets
01685          * cleared and configuration gets updated in case of reassociation back
01686          * to the same AP.
01687          */
01688         hostapd_sta_remove(hapd, sta->addr);
01689 
01690         if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
01691                             sta->capability, sta->supported_rates,
01692                             sta->supported_rates_len, 0, sta->listen_interval,
01693                             ht_cap_ptr))
01694         {
01695                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
01696                                HOSTAPD_LEVEL_NOTICE,
01697                                "Could not add STA to kernel driver");
01698         }
01699 
01700         if (sta->eapol_sm == NULL) {
01701                 /*
01702                  * This STA does not use RADIUS server for EAP authentication,
01703                  * so bind it to the selected VLAN interface now, since the
01704                  * interface selection is not going to change anymore.
01705                  */
01706                 ap_sta_bind_vlan(hapd, sta, 0);
01707         } else if (sta->vlan_id) {
01708                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
01709                 ap_sta_bind_vlan(hapd, sta, 0);
01710         }
01711 
01712         set_flags = WLAN_STA_SHORT_PREAMBLE | WLAN_STA_WMM | WLAN_STA_MFP;
01713         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
01714             sta->flags & WLAN_STA_AUTHORIZED)
01715                 set_flags |= WLAN_STA_AUTHORIZED;
01716         flags_or = sta->flags & set_flags;
01717         flags_and = sta->flags | ~set_flags;
01718         hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
01719                               flags_or, flags_and);
01720 
01721         if (sta->auth_alg == WLAN_AUTH_FT)
01722                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
01723         else
01724                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
01725         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
01726 
01727         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
01728 
01729  fail:
01730         /* Copy of the association request is not needed anymore */
01731         if (sta->last_assoc_req) {
01732                 os_free(sta->last_assoc_req);
01733                 sta->last_assoc_req = NULL;
01734         }
01735 }
01736 
01737 
01748 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
01749                         u16 stype, int ok)
01750 {
01751         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
01752 
01753         switch (stype) {
01754         case WLAN_FC_STYPE_AUTH:
01755                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
01756                 handle_auth_cb(hapd, mgmt, len, ok);
01757                 break;
01758         case WLAN_FC_STYPE_ASSOC_RESP:
01759                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
01760                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
01761                 break;
01762         case WLAN_FC_STYPE_REASSOC_RESP:
01763                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
01764                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
01765                 break;
01766         case WLAN_FC_STYPE_PROBE_RESP:
01767                 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
01768                 break;
01769         case WLAN_FC_STYPE_DEAUTH:
01770                 /* ignore */
01771                 break;
01772         case WLAN_FC_STYPE_ACTION:
01773                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
01774                 break;
01775         default:
01776                 printf("unknown mgmt cb frame subtype %d\n", stype);
01777                 break;
01778         }
01779 }
01780 
01781 
01782 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
01783 {
01784         /* TODO */
01785         return 0;
01786 }
01787 
01788 
01789 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
01790                            char *buf, size_t buflen)
01791 {
01792         /* TODO */
01793         return 0;
01794 }
01795 
01796 #endif /* CONFIG_NATIVE_WINDOWS */
01797 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:48 2009 for hostapd by  doxygen 1.6.1