ieee802_11.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2007-2008, Intel Corporation
  5. *
  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.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #ifndef CONFIG_NATIVE_WINDOWS
  17. #include <net/if.h>
  18. #include "eloop.h"
  19. #include "hostapd.h"
  20. #include "ieee802_11.h"
  21. #include "beacon.h"
  22. #include "hw_features.h"
  23. #include "radius/radius.h"
  24. #include "radius/radius_client.h"
  25. #include "ieee802_11_auth.h"
  26. #include "sta_info.h"
  27. #include "rc4.h"
  28. #include "ieee802_1x.h"
  29. #include "wpa.h"
  30. #include "wme.h"
  31. #include "ap_list.h"
  32. #include "accounting.h"
  33. #include "driver.h"
  34. #include "mlme.h"
  35. u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
  36. {
  37. u8 *pos = eid;
  38. int i, num, count;
  39. if (hapd->iface->current_rates == NULL)
  40. return eid;
  41. *pos++ = WLAN_EID_SUPP_RATES;
  42. num = hapd->iface->num_rates;
  43. if (num > 8) {
  44. /* rest of the rates are encoded in Extended supported
  45. * rates element */
  46. num = 8;
  47. }
  48. *pos++ = num;
  49. count = 0;
  50. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
  51. i++) {
  52. count++;
  53. *pos = hapd->iface->current_rates[i].rate / 5;
  54. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  55. *pos |= 0x80;
  56. pos++;
  57. }
  58. return pos;
  59. }
  60. u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
  61. {
  62. u8 *pos = eid;
  63. int i, num, count;
  64. if (hapd->iface->current_rates == NULL)
  65. return eid;
  66. num = hapd->iface->num_rates;
  67. if (num <= 8)
  68. return eid;
  69. num -= 8;
  70. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  71. *pos++ = num;
  72. count = 0;
  73. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
  74. i++) {
  75. count++;
  76. if (count <= 8)
  77. continue; /* already in SuppRates IE */
  78. *pos = hapd->iface->current_rates[i].rate / 5;
  79. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  80. *pos |= 0x80;
  81. pos++;
  82. }
  83. return pos;
  84. }
  85. u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
  86. {
  87. #ifdef CONFIG_IEEE80211N
  88. struct ieee80211_ht_capability *cap;
  89. u8 *pos = eid;
  90. if (!hapd->iconf->ieee80211n)
  91. return eid;
  92. *pos++ = WLAN_EID_HT_CAP;
  93. *pos++ = sizeof(*cap);
  94. cap = (struct ieee80211_ht_capability *) pos;
  95. os_memset(cap, 0, sizeof(*cap));
  96. SET_2BIT_U8(&cap->mac_ht_params_info,
  97. MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
  98. MAX_RX_AMPDU_FACTOR_64KB);
  99. cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
  100. cap->supported_mcs_set[0] = 0xff;
  101. cap->supported_mcs_set[1] = 0xff;
  102. pos += sizeof(*cap);
  103. return pos;
  104. #else /* CONFIG_IEEE80211N */
  105. return eid;
  106. #endif /* CONFIG_IEEE80211N */
  107. }
  108. u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
  109. {
  110. #ifdef CONFIG_IEEE80211N
  111. struct ieee80211_ht_operation *oper;
  112. u8 *pos = eid;
  113. if (!hapd->iconf->ieee80211n)
  114. return eid;
  115. *pos++ = WLAN_EID_HT_OPERATION;
  116. *pos++ = sizeof(*oper);
  117. oper = (struct ieee80211_ht_operation *) pos;
  118. os_memset(oper, 0, sizeof(*oper));
  119. oper->control_chan = hapd->iconf->channel;
  120. oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
  121. if (hapd->iconf->secondary_channel == 1)
  122. oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
  123. HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
  124. if (hapd->iconf->secondary_channel == -1)
  125. oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
  126. HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
  127. pos += sizeof(*oper);
  128. return pos;
  129. #else /* CONFIG_IEEE80211N */
  130. return eid;
  131. #endif /* CONFIG_IEEE80211N */
  132. }
  133. #ifdef CONFIG_IEEE80211N
  134. /*
  135. op_mode
  136. Set to 0 (HT pure) under the followign conditions
  137. - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
  138. - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
  139. Set to 1 (HT non-member protection) if there may be non-HT STAs
  140. in both the primary and the secondary channel
  141. Set to 2 if only HT STAs are associated in BSS,
  142. however and at least one 20 MHz HT STA is associated
  143. Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
  144. (currently non-GF HT station is considered as non-HT STA also)
  145. */
  146. int hostapd_ht_operation_update(struct hostapd_iface *iface)
  147. {
  148. u16 cur_op_mode, new_op_mode;
  149. int op_mode_changes = 0;
  150. if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
  151. return 0;
  152. wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
  153. __func__, iface->ht_op_mode);
  154. if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
  155. && iface->num_sta_ht_no_gf) {
  156. iface->ht_op_mode |=
  157. HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
  158. op_mode_changes++;
  159. } else if ((iface->ht_op_mode &
  160. HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
  161. iface->num_sta_ht_no_gf == 0) {
  162. iface->ht_op_mode &=
  163. ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
  164. op_mode_changes++;
  165. }
  166. if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
  167. (iface->num_sta_no_ht || iface->olbc_ht)) {
  168. iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
  169. op_mode_changes++;
  170. } else if ((iface->ht_op_mode &
  171. HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
  172. (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
  173. iface->ht_op_mode &=
  174. ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
  175. op_mode_changes++;
  176. }
  177. /* Note: currently we switch to the MIXED op mode if HT non-greenfield
  178. * station is associated. Probably it's a theoretical case, since
  179. * it looks like all known HT STAs support greenfield.
  180. */
  181. new_op_mode = 0;
  182. if (iface->num_sta_no_ht ||
  183. (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
  184. new_op_mode = OP_MODE_MIXED;
  185. else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
  186. && iface->num_sta_ht_20mhz)
  187. new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
  188. else if (iface->olbc_ht)
  189. new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
  190. else
  191. new_op_mode = OP_MODE_PURE;
  192. cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
  193. if (cur_op_mode != new_op_mode) {
  194. iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
  195. iface->ht_op_mode |= new_op_mode;
  196. op_mode_changes++;
  197. }
  198. wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
  199. __func__, iface->ht_op_mode, op_mode_changes);
  200. return op_mode_changes;
  201. }
  202. #endif /* CONFIG_IEEE80211N */
  203. u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
  204. int probe)
  205. {
  206. int capab = WLAN_CAPABILITY_ESS;
  207. int privacy;
  208. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  209. hapd->iconf->preamble == SHORT_PREAMBLE)
  210. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  211. privacy = hapd->conf->ssid.wep.keys_set;
  212. if (hapd->conf->ieee802_1x &&
  213. (hapd->conf->default_wep_key_len ||
  214. hapd->conf->individual_wep_key_len))
  215. privacy = 1;
  216. if (hapd->conf->wpa)
  217. privacy = 1;
  218. if (sta) {
  219. int policy, def_klen;
  220. if (probe && sta->ssid_probe) {
  221. policy = sta->ssid_probe->security_policy;
  222. def_klen = sta->ssid_probe->wep.default_len;
  223. } else {
  224. policy = sta->ssid->security_policy;
  225. def_klen = sta->ssid->wep.default_len;
  226. }
  227. privacy = policy != SECURITY_PLAINTEXT;
  228. if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
  229. privacy = 0;
  230. }
  231. if (privacy)
  232. capab |= WLAN_CAPABILITY_PRIVACY;
  233. if (hapd->iface->current_mode &&
  234. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  235. hapd->iface->num_sta_no_short_slot_time == 0)
  236. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  237. return capab;
  238. }
  239. #ifdef CONFIG_IEEE80211W
  240. static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
  241. struct sta_info *sta, u8 *eid)
  242. {
  243. u8 *pos = eid;
  244. u32 timeout;
  245. *pos++ = WLAN_EID_ASSOC_COMEBACK_TIME;
  246. *pos++ = 4;
  247. timeout = (hapd->conf->assoc_ping_attempts - sta->ping_count + 1) *
  248. hapd->conf->assoc_ping_timeout;
  249. WPA_PUT_LE32(pos, timeout);
  250. pos += 4;
  251. return pos;
  252. }
  253. #endif /* CONFIG_IEEE80211W */
  254. void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
  255. {
  256. int i;
  257. if (len > HOSTAPD_MAX_SSID_LEN)
  258. len = HOSTAPD_MAX_SSID_LEN;
  259. for (i = 0; i < len; i++) {
  260. if (ssid[i] >= 32 && ssid[i] < 127)
  261. buf[i] = ssid[i];
  262. else
  263. buf[i] = '.';
  264. }
  265. buf[len] = '\0';
  266. }
  267. void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
  268. {
  269. struct ieee80211_mgmt mgmt;
  270. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  271. HOSTAPD_LEVEL_DEBUG,
  272. "deauthenticate - reason %d", reason);
  273. os_memset(&mgmt, 0, sizeof(mgmt));
  274. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  275. WLAN_FC_STYPE_DEAUTH);
  276. os_memcpy(mgmt.da, addr, ETH_ALEN);
  277. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  278. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  279. mgmt.u.deauth.reason_code = host_to_le16(reason);
  280. if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
  281. sizeof(mgmt.u.deauth), 0) < 0)
  282. perror("ieee802_11_send_deauth: send");
  283. }
  284. static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
  285. u16 auth_transaction, u8 *challenge, int iswep)
  286. {
  287. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  288. HOSTAPD_LEVEL_DEBUG,
  289. "authentication (shared key, transaction %d)",
  290. auth_transaction);
  291. if (auth_transaction == 1) {
  292. if (!sta->challenge) {
  293. /* Generate a pseudo-random challenge */
  294. u8 key[8];
  295. time_t now;
  296. int r;
  297. sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
  298. if (sta->challenge == NULL)
  299. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  300. now = time(NULL);
  301. r = random();
  302. os_memcpy(key, &now, 4);
  303. os_memcpy(key + 4, &r, 4);
  304. rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
  305. key, sizeof(key));
  306. }
  307. return 0;
  308. }
  309. if (auth_transaction != 3)
  310. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  311. /* Transaction 3 */
  312. if (!iswep || !sta->challenge || !challenge ||
  313. os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
  314. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  315. HOSTAPD_LEVEL_INFO,
  316. "shared key authentication - invalid "
  317. "challenge-response");
  318. return WLAN_STATUS_CHALLENGE_FAIL;
  319. }
  320. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  321. HOSTAPD_LEVEL_DEBUG,
  322. "authentication OK (shared key)");
  323. #ifdef IEEE80211_REQUIRE_AUTH_ACK
  324. /* Station will be marked authenticated if it ACKs the
  325. * authentication reply. */
  326. #else
  327. sta->flags |= WLAN_STA_AUTH;
  328. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  329. #endif
  330. os_free(sta->challenge);
  331. sta->challenge = NULL;
  332. return 0;
  333. }
  334. static void send_auth_reply(struct hostapd_data *hapd,
  335. const u8 *dst, const u8 *bssid,
  336. u16 auth_alg, u16 auth_transaction, u16 resp,
  337. const u8 *ies, size_t ies_len)
  338. {
  339. struct ieee80211_mgmt *reply;
  340. u8 *buf;
  341. size_t rlen;
  342. rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
  343. buf = os_zalloc(rlen);
  344. if (buf == NULL)
  345. return;
  346. reply = (struct ieee80211_mgmt *) buf;
  347. reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  348. WLAN_FC_STYPE_AUTH);
  349. os_memcpy(reply->da, dst, ETH_ALEN);
  350. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  351. os_memcpy(reply->bssid, bssid, ETH_ALEN);
  352. reply->u.auth.auth_alg = host_to_le16(auth_alg);
  353. reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
  354. reply->u.auth.status_code = host_to_le16(resp);
  355. if (ies && ies_len)
  356. os_memcpy(reply->u.auth.variable, ies, ies_len);
  357. wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
  358. " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
  359. MAC2STR(dst), auth_alg, auth_transaction,
  360. resp, (unsigned long) ies_len);
  361. if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
  362. perror("send_auth_reply: send");
  363. os_free(buf);
  364. }
  365. #ifdef CONFIG_IEEE80211R
  366. static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
  367. u16 auth_transaction, u16 status,
  368. const u8 *ies, size_t ies_len)
  369. {
  370. struct hostapd_data *hapd = ctx;
  371. struct sta_info *sta;
  372. send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
  373. status, ies, ies_len);
  374. if (status != WLAN_STATUS_SUCCESS)
  375. return;
  376. sta = ap_get_sta(hapd, dst);
  377. if (sta == NULL)
  378. return;
  379. hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
  380. HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
  381. sta->flags |= WLAN_STA_AUTH;
  382. mlme_authenticate_indication(hapd, sta);
  383. }
  384. #endif /* CONFIG_IEEE80211R */
  385. static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
  386. size_t len)
  387. {
  388. u16 auth_alg, auth_transaction, status_code;
  389. u16 resp = WLAN_STATUS_SUCCESS;
  390. struct sta_info *sta = NULL;
  391. int res;
  392. u16 fc;
  393. u8 *challenge = NULL;
  394. u32 session_timeout, acct_interim_interval;
  395. int vlan_id = 0;
  396. u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
  397. size_t resp_ies_len = 0;
  398. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  399. printf("handle_auth - too short payload (len=%lu)\n",
  400. (unsigned long) len);
  401. return;
  402. }
  403. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  404. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  405. status_code = le_to_host16(mgmt->u.auth.status_code);
  406. fc = le_to_host16(mgmt->frame_control);
  407. if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
  408. 2 + WLAN_AUTH_CHALLENGE_LEN &&
  409. mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
  410. mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
  411. challenge = &mgmt->u.auth.variable[2];
  412. wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
  413. "auth_transaction=%d status_code=%d wep=%d%s",
  414. MAC2STR(mgmt->sa), auth_alg, auth_transaction,
  415. status_code, !!(fc & WLAN_FC_ISWEP),
  416. challenge ? " challenge" : "");
  417. if (hapd->tkip_countermeasures) {
  418. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  419. goto fail;
  420. }
  421. if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
  422. auth_alg == WLAN_AUTH_OPEN) ||
  423. #ifdef CONFIG_IEEE80211R
  424. (hapd->conf->wpa &&
  425. (hapd->conf->wpa_key_mgmt &
  426. (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
  427. auth_alg == WLAN_AUTH_FT) ||
  428. #endif /* CONFIG_IEEE80211R */
  429. ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
  430. auth_alg == WLAN_AUTH_SHARED_KEY))) {
  431. printf("Unsupported authentication algorithm (%d)\n",
  432. auth_alg);
  433. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  434. goto fail;
  435. }
  436. if (!(auth_transaction == 1 ||
  437. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
  438. printf("Unknown authentication transaction number (%d)\n",
  439. auth_transaction);
  440. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  441. goto fail;
  442. }
  443. if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
  444. printf("Station " MACSTR " not allowed to authenticate.\n",
  445. MAC2STR(mgmt->sa));
  446. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  447. goto fail;
  448. }
  449. res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
  450. &session_timeout,
  451. &acct_interim_interval, &vlan_id);
  452. if (res == HOSTAPD_ACL_REJECT) {
  453. printf("Station " MACSTR " not allowed to authenticate.\n",
  454. MAC2STR(mgmt->sa));
  455. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  456. goto fail;
  457. }
  458. if (res == HOSTAPD_ACL_PENDING) {
  459. wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
  460. " waiting for an external authentication",
  461. MAC2STR(mgmt->sa));
  462. /* Authentication code will re-send the authentication frame
  463. * after it has received (and cached) information from the
  464. * external source. */
  465. return;
  466. }
  467. sta = ap_sta_add(hapd, mgmt->sa);
  468. if (!sta) {
  469. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  470. goto fail;
  471. }
  472. if (vlan_id > 0) {
  473. if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  474. sta->vlan_id) == NULL) {
  475. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  476. HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
  477. "%d received from RADIUS server",
  478. vlan_id);
  479. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  480. goto fail;
  481. }
  482. sta->vlan_id = vlan_id;
  483. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  484. HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
  485. }
  486. sta->flags &= ~WLAN_STA_PREAUTH;
  487. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  488. if (hapd->conf->radius->acct_interim_interval == 0 &&
  489. acct_interim_interval)
  490. sta->acct_interim_interval = acct_interim_interval;
  491. if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  492. ap_sta_session_timeout(hapd, sta, session_timeout);
  493. else
  494. ap_sta_no_session_timeout(hapd, sta);
  495. switch (auth_alg) {
  496. case WLAN_AUTH_OPEN:
  497. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  498. HOSTAPD_LEVEL_DEBUG,
  499. "authentication OK (open system)");
  500. #ifdef IEEE80211_REQUIRE_AUTH_ACK
  501. /* Station will be marked authenticated if it ACKs the
  502. * authentication reply. */
  503. #else
  504. sta->flags |= WLAN_STA_AUTH;
  505. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  506. sta->auth_alg = WLAN_AUTH_OPEN;
  507. mlme_authenticate_indication(hapd, sta);
  508. #endif
  509. break;
  510. case WLAN_AUTH_SHARED_KEY:
  511. resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
  512. fc & WLAN_FC_ISWEP);
  513. sta->auth_alg = WLAN_AUTH_SHARED_KEY;
  514. mlme_authenticate_indication(hapd, sta);
  515. if (sta->challenge && auth_transaction == 1) {
  516. resp_ies[0] = WLAN_EID_CHALLENGE;
  517. resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
  518. os_memcpy(resp_ies + 2, sta->challenge,
  519. WLAN_AUTH_CHALLENGE_LEN);
  520. resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
  521. }
  522. break;
  523. #ifdef CONFIG_IEEE80211R
  524. case WLAN_AUTH_FT:
  525. sta->auth_alg = WLAN_AUTH_FT;
  526. if (sta->wpa_sm == NULL)
  527. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  528. sta->addr);
  529. if (sta->wpa_sm == NULL) {
  530. wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
  531. "state machine");
  532. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  533. goto fail;
  534. }
  535. wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
  536. auth_transaction, mgmt->u.auth.variable,
  537. len - IEEE80211_HDRLEN -
  538. sizeof(mgmt->u.auth),
  539. handle_auth_ft_finish, hapd);
  540. /* handle_auth_ft_finish() callback will complete auth. */
  541. return;
  542. #endif /* CONFIG_IEEE80211R */
  543. }
  544. fail:
  545. send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
  546. auth_transaction + 1, resp, resp_ies, resp_ies_len);
  547. }
  548. static void handle_assoc(struct hostapd_data *hapd,
  549. struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
  550. {
  551. u16 capab_info, listen_interval;
  552. u16 resp = WLAN_STATUS_SUCCESS;
  553. u8 *pos, *wpa_ie;
  554. size_t wpa_ie_len;
  555. int send_deauth = 0, send_len, left, i;
  556. struct sta_info *sta;
  557. struct ieee802_11_elems elems;
  558. u8 buf[sizeof(struct ieee80211_mgmt) + 512];
  559. struct ieee80211_mgmt *reply;
  560. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
  561. sizeof(mgmt->u.assoc_req))) {
  562. printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
  563. "\n", reassoc, (unsigned long) len);
  564. return;
  565. }
  566. if (reassoc) {
  567. capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
  568. listen_interval = le_to_host16(
  569. mgmt->u.reassoc_req.listen_interval);
  570. wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
  571. " capab_info=0x%02x listen_interval=%d current_ap="
  572. MACSTR,
  573. MAC2STR(mgmt->sa), capab_info, listen_interval,
  574. MAC2STR(mgmt->u.reassoc_req.current_ap));
  575. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
  576. pos = mgmt->u.reassoc_req.variable;
  577. } else {
  578. capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  579. listen_interval = le_to_host16(
  580. mgmt->u.assoc_req.listen_interval);
  581. wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
  582. " capab_info=0x%02x listen_interval=%d",
  583. MAC2STR(mgmt->sa), capab_info, listen_interval);
  584. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
  585. pos = mgmt->u.assoc_req.variable;
  586. }
  587. sta = ap_get_sta(hapd, mgmt->sa);
  588. #ifdef CONFIG_IEEE80211R
  589. if (sta && sta->auth_alg == WLAN_AUTH_FT &&
  590. (sta->flags & WLAN_STA_AUTH) == 0) {
  591. wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
  592. "prior to authentication since it is using "
  593. "over-the-DS FT", MAC2STR(mgmt->sa));
  594. } else
  595. #endif /* CONFIG_IEEE80211R */
  596. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  597. printf("STA " MACSTR " trying to associate before "
  598. "authentication\n", MAC2STR(mgmt->sa));
  599. if (sta) {
  600. printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
  601. MAC2STR(sta->addr), sta->aid, sta->flags);
  602. }
  603. send_deauth = 1;
  604. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  605. goto fail;
  606. }
  607. if (hapd->tkip_countermeasures) {
  608. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  609. goto fail;
  610. }
  611. if (listen_interval > hapd->conf->max_listen_interval) {
  612. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  613. HOSTAPD_LEVEL_DEBUG,
  614. "Too large Listen Interval (%d)",
  615. listen_interval);
  616. resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
  617. goto fail;
  618. }
  619. sta->capability = capab_info;
  620. sta->listen_interval = listen_interval;
  621. /* followed by SSID and Supported rates; and HT capabilities if 802.11n
  622. * is used */
  623. if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
  624. !elems.ssid) {
  625. printf("STA " MACSTR " sent invalid association request\n",
  626. MAC2STR(sta->addr));
  627. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  628. goto fail;
  629. }
  630. if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  631. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
  632. {
  633. char ssid_txt[33];
  634. ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
  635. printf("Station " MACSTR " tried to associate with "
  636. "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
  637. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  638. goto fail;
  639. }
  640. sta->flags &= ~WLAN_STA_WME;
  641. if (elems.wme && hapd->conf->wme_enabled) {
  642. if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
  643. hostapd_logger(hapd, sta->addr,
  644. HOSTAPD_MODULE_WPA,
  645. HOSTAPD_LEVEL_DEBUG,
  646. "invalid WME element in association "
  647. "request");
  648. else
  649. sta->flags |= WLAN_STA_WME;
  650. }
  651. if (!elems.supp_rates) {
  652. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  653. HOSTAPD_LEVEL_DEBUG,
  654. "No supported rates element in AssocReq");
  655. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  656. goto fail;
  657. }
  658. if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
  659. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  660. HOSTAPD_LEVEL_DEBUG,
  661. "Invalid supported rates element length %d",
  662. elems.supp_rates_len);
  663. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  664. goto fail;
  665. }
  666. os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  667. os_memcpy(sta->supported_rates, elems.supp_rates,
  668. elems.supp_rates_len);
  669. sta->supported_rates_len = elems.supp_rates_len;
  670. if (elems.ext_supp_rates) {
  671. if (elems.supp_rates_len + elems.ext_supp_rates_len >
  672. sizeof(sta->supported_rates)) {
  673. hostapd_logger(hapd, mgmt->sa,
  674. HOSTAPD_MODULE_IEEE80211,
  675. HOSTAPD_LEVEL_DEBUG,
  676. "Invalid supported rates element length"
  677. " %d+%d", elems.supp_rates_len,
  678. elems.ext_supp_rates_len);
  679. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  680. goto fail;
  681. }
  682. os_memcpy(sta->supported_rates + elems.supp_rates_len,
  683. elems.ext_supp_rates, elems.ext_supp_rates_len);
  684. sta->supported_rates_len += elems.ext_supp_rates_len;
  685. }
  686. #ifdef CONFIG_IEEE80211N
  687. /* save HT capabilities in the sta object */
  688. os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
  689. if (elems.ht_capabilities &&
  690. elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_capability)
  691. && (sta->flags & WLAN_STA_WME)) {
  692. /* note: without WMM capability, treat the sta as non-HT */
  693. sta->flags |= WLAN_STA_HT;
  694. sta->ht_capabilities.id = WLAN_EID_HT_CAP;
  695. sta->ht_capabilities.length =
  696. sizeof(struct ieee80211_ht_capability);
  697. os_memcpy(&sta->ht_capabilities.data,
  698. elems.ht_capabilities,
  699. sizeof(struct ieee80211_ht_capability));
  700. } else
  701. sta->flags &= ~WLAN_STA_HT;
  702. #endif /* CONFIG_IEEE80211N */
  703. if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
  704. wpa_ie = elems.rsn_ie;
  705. wpa_ie_len = elems.rsn_ie_len;
  706. } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
  707. elems.wpa_ie) {
  708. wpa_ie = elems.wpa_ie;
  709. wpa_ie_len = elems.wpa_ie_len;
  710. } else {
  711. wpa_ie = NULL;
  712. wpa_ie_len = 0;
  713. }
  714. #ifdef CONFIG_WPS
  715. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
  716. if (hapd->conf->wps_state && wpa_ie == NULL) {
  717. if (elems.wps_ie) {
  718. wpa_printf(MSG_DEBUG, "STA included WPS IE in "
  719. "(Re)Association Request - assume WPS is "
  720. "used");
  721. sta->flags |= WLAN_STA_WPS;
  722. wpabuf_free(sta->wps_ie);
  723. sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
  724. elems.wps_ie_len - 4);
  725. } else {
  726. wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
  727. "in (Re)Association Request - possible WPS "
  728. "use");
  729. sta->flags |= WLAN_STA_MAYBE_WPS;
  730. }
  731. } else
  732. #endif /* CONFIG_WPS */
  733. if (hapd->conf->wpa && wpa_ie == NULL) {
  734. printf("STA " MACSTR ": No WPA/RSN IE in association "
  735. "request\n", MAC2STR(sta->addr));
  736. resp = WLAN_STATUS_INVALID_IE;
  737. goto fail;
  738. }
  739. if (hapd->conf->wpa && wpa_ie) {
  740. int res;
  741. wpa_ie -= 2;
  742. wpa_ie_len += 2;
  743. if (sta->wpa_sm == NULL)
  744. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  745. sta->addr);
  746. if (sta->wpa_sm == NULL) {
  747. printf("Failed to initialize WPA state machine\n");
  748. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  749. goto fail;
  750. }
  751. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  752. wpa_ie, wpa_ie_len,
  753. elems.mdie, elems.mdie_len);
  754. if (res == WPA_INVALID_GROUP)
  755. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  756. else if (res == WPA_INVALID_PAIRWISE)
  757. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  758. else if (res == WPA_INVALID_AKMP)
  759. resp = WLAN_STATUS_AKMP_NOT_VALID;
  760. else if (res == WPA_ALLOC_FAIL)
  761. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  762. #ifdef CONFIG_IEEE80211W
  763. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  764. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  765. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  766. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  767. #endif /* CONFIG_IEEE80211W */
  768. else if (res == WPA_INVALID_MDIE)
  769. resp = WLAN_STATUS_INVALID_MDIE;
  770. else if (res != WPA_IE_OK)
  771. resp = WLAN_STATUS_INVALID_IE;
  772. if (resp != WLAN_STATUS_SUCCESS)
  773. goto fail;
  774. #ifdef CONFIG_IEEE80211W
  775. if ((sta->flags & WLAN_STA_MFP) && !sta->ping_timed_out) {
  776. /*
  777. * STA has already been associated with MFP and ping
  778. * timeout has not been reached. Reject the
  779. * association attempt temporarily and start ping, if
  780. * one is not pending.
  781. */
  782. if (sta->ping_count == 0)
  783. ap_sta_start_ping(hapd, sta);
  784. resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
  785. goto fail;
  786. }
  787. if (wpa_auth_uses_mfp(sta->wpa_sm))
  788. sta->flags |= WLAN_STA_MFP;
  789. else
  790. sta->flags &= ~WLAN_STA_MFP;
  791. #endif /* CONFIG_IEEE80211W */
  792. #ifdef CONFIG_IEEE80211R
  793. if (sta->auth_alg == WLAN_AUTH_FT) {
  794. if (!reassoc) {
  795. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  796. "to use association (not "
  797. "re-association) with FT auth_alg",
  798. MAC2STR(sta->addr));
  799. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  800. goto fail;
  801. }
  802. resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
  803. if (resp != WLAN_STATUS_SUCCESS)
  804. goto fail;
  805. }
  806. #endif /* CONFIG_IEEE80211R */
  807. } else
  808. wpa_auth_sta_no_wpa(sta->wpa_sm);
  809. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  810. sta->flags |= WLAN_STA_NONERP;
  811. for (i = 0; i < sta->supported_rates_len; i++) {
  812. if ((sta->supported_rates[i] & 0x7f) > 22) {
  813. sta->flags &= ~WLAN_STA_NONERP;
  814. break;
  815. }
  816. }
  817. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  818. sta->nonerp_set = 1;
  819. hapd->iface->num_sta_non_erp++;
  820. if (hapd->iface->num_sta_non_erp == 1)
  821. ieee802_11_set_beacons(hapd->iface);
  822. }
  823. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  824. !sta->no_short_slot_time_set) {
  825. sta->no_short_slot_time_set = 1;
  826. hapd->iface->num_sta_no_short_slot_time++;
  827. if (hapd->iface->current_mode->mode ==
  828. HOSTAPD_MODE_IEEE80211G &&
  829. hapd->iface->num_sta_no_short_slot_time == 1)
  830. ieee802_11_set_beacons(hapd->iface);
  831. }
  832. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  833. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  834. else
  835. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  836. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  837. !sta->no_short_preamble_set) {
  838. sta->no_short_preamble_set = 1;
  839. hapd->iface->num_sta_no_short_preamble++;
  840. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  841. && hapd->iface->num_sta_no_short_preamble == 1)
  842. ieee802_11_set_beacons(hapd->iface);
  843. }
  844. #ifdef CONFIG_IEEE80211N
  845. if (sta->flags & WLAN_STA_HT) {
  846. u16 ht_capab = le_to_host16(
  847. sta->ht_capabilities.data.capabilities_info);
  848. wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
  849. "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
  850. if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
  851. hapd->iface->num_sta_ht_no_gf++;
  852. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
  853. "greenfield, num of non-gf stations %d",
  854. __func__, MAC2STR(sta->addr),
  855. hapd->iface->num_sta_ht_no_gf);
  856. }
  857. if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
  858. hapd->iface->num_sta_ht_20mhz++;
  859. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
  860. "num of 20MHz HT STAs %d",
  861. __func__, MAC2STR(sta->addr),
  862. hapd->iface->num_sta_ht_20mhz);
  863. }
  864. } else {
  865. hapd->iface->num_sta_no_ht++;
  866. if (hapd->iconf->ieee80211n) {
  867. wpa_printf(MSG_DEBUG, "%s STA " MACSTR
  868. " - no HT, num of non-HT stations %d",
  869. __func__, MAC2STR(sta->addr),
  870. hapd->iface->num_sta_no_ht);
  871. }
  872. }
  873. if (hostapd_ht_operation_update(hapd->iface) > 0)
  874. ieee802_11_set_beacons(hapd->iface);
  875. #endif /* CONFIG_IEEE80211N */
  876. /* get a unique AID */
  877. if (sta->aid > 0) {
  878. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  879. } else {
  880. for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
  881. if (hapd->sta_aid[sta->aid - 1] == NULL)
  882. break;
  883. if (sta->aid > MAX_AID_TABLE_SIZE) {
  884. sta->aid = 0;
  885. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  886. wpa_printf(MSG_ERROR, " no room for more AIDs");
  887. goto fail;
  888. } else {
  889. hapd->sta_aid[sta->aid - 1] = sta;
  890. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  891. }
  892. }
  893. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  894. HOSTAPD_LEVEL_DEBUG,
  895. "association OK (aid %d)", sta->aid);
  896. /* Station will be marked associated, after it acknowledges AssocResp
  897. */
  898. if (reassoc) {
  899. os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
  900. ETH_ALEN);
  901. }
  902. if (sta->last_assoc_req)
  903. os_free(sta->last_assoc_req);
  904. sta->last_assoc_req = os_malloc(len);
  905. if (sta->last_assoc_req)
  906. os_memcpy(sta->last_assoc_req, mgmt, len);
  907. /* Make sure that the previously registered inactivity timer will not
  908. * remove the STA immediately. */
  909. sta->timeout_next = STA_NULLFUNC;
  910. fail:
  911. os_memset(buf, 0, sizeof(buf));
  912. reply = (struct ieee80211_mgmt *) buf;
  913. reply->frame_control =
  914. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  915. (send_deauth ? WLAN_FC_STYPE_DEAUTH :
  916. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  917. WLAN_FC_STYPE_ASSOC_RESP)));
  918. os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
  919. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  920. os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
  921. send_len = IEEE80211_HDRLEN;
  922. if (send_deauth) {
  923. send_len += sizeof(reply->u.deauth);
  924. reply->u.deauth.reason_code = host_to_le16(resp);
  925. } else {
  926. u8 *p;
  927. send_len += sizeof(reply->u.assoc_resp);
  928. reply->u.assoc_resp.capab_info =
  929. host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
  930. reply->u.assoc_resp.status_code = host_to_le16(resp);
  931. reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
  932. | BIT(14) | BIT(15));
  933. /* Supported rates */
  934. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  935. /* Extended supported rates */
  936. p = hostapd_eid_ext_supp_rates(hapd, p);
  937. if (sta->flags & WLAN_STA_WME)
  938. p = hostapd_eid_wme(hapd, p);
  939. p = hostapd_eid_ht_capabilities_info(hapd, p);
  940. p = hostapd_eid_ht_operation(hapd, p);
  941. #ifdef CONFIG_IEEE80211R
  942. if (resp == WLAN_STATUS_SUCCESS) {
  943. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  944. * Transition Information, RSN */
  945. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  946. buf + sizeof(buf) - p,
  947. sta->auth_alg);
  948. }
  949. #endif /* CONFIG_IEEE80211R */
  950. #ifdef CONFIG_IEEE80211W
  951. if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
  952. p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
  953. #endif /* CONFIG_IEEE80211W */
  954. send_len += p - reply->u.assoc_resp.variable;
  955. }
  956. if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
  957. perror("handle_assoc: send");
  958. }
  959. static void handle_disassoc(struct hostapd_data *hapd,
  960. struct ieee80211_mgmt *mgmt, size_t len)
  961. {
  962. struct sta_info *sta;
  963. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  964. printf("handle_disassoc - too short payload (len=%lu)\n",
  965. (unsigned long) len);
  966. return;
  967. }
  968. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  969. MAC2STR(mgmt->sa),
  970. le_to_host16(mgmt->u.disassoc.reason_code));
  971. sta = ap_get_sta(hapd, mgmt->sa);
  972. if (sta == NULL) {
  973. printf("Station " MACSTR " trying to disassociate, but it "
  974. "is not associated.\n", MAC2STR(mgmt->sa));
  975. return;
  976. }
  977. sta->flags &= ~WLAN_STA_ASSOC;
  978. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  979. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  980. HOSTAPD_LEVEL_INFO, "disassociated");
  981. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  982. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  983. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  984. * authenticated. */
  985. accounting_sta_stop(hapd, sta);
  986. ieee802_1x_free_station(sta);
  987. hostapd_sta_remove(hapd, sta->addr);
  988. if (sta->timeout_next == STA_NULLFUNC ||
  989. sta->timeout_next == STA_DISASSOC) {
  990. sta->timeout_next = STA_DEAUTH;
  991. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  992. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  993. hapd, sta);
  994. }
  995. mlme_disassociate_indication(
  996. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  997. }
  998. static void handle_deauth(struct hostapd_data *hapd,
  999. struct ieee80211_mgmt *mgmt, size_t len)
  1000. {
  1001. struct sta_info *sta;
  1002. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  1003. printf("handle_deauth - too short payload (len=%lu)\n",
  1004. (unsigned long) len);
  1005. return;
  1006. }
  1007. wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
  1008. " reason_code=%d",
  1009. MAC2STR(mgmt->sa),
  1010. le_to_host16(mgmt->u.deauth.reason_code));
  1011. sta = ap_get_sta(hapd, mgmt->sa);
  1012. if (sta == NULL) {
  1013. printf("Station " MACSTR " trying to deauthenticate, but it "
  1014. "is not authenticated.\n", MAC2STR(mgmt->sa));
  1015. return;
  1016. }
  1017. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1018. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1019. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1020. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  1021. mlme_deauthenticate_indication(
  1022. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  1023. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1024. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1025. ap_free_sta(hapd, sta);
  1026. }
  1027. static void handle_beacon(struct hostapd_data *hapd,
  1028. struct ieee80211_mgmt *mgmt, size_t len,
  1029. struct hostapd_frame_info *fi)
  1030. {
  1031. struct ieee802_11_elems elems;
  1032. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  1033. printf("handle_beacon - too short payload (len=%lu)\n",
  1034. (unsigned long) len);
  1035. return;
  1036. }
  1037. (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1038. len - (IEEE80211_HDRLEN +
  1039. sizeof(mgmt->u.beacon)), &elems,
  1040. 0);
  1041. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  1042. }
  1043. #ifdef CONFIG_IEEE80211W
  1044. static void hostapd_ping_action(struct hostapd_data *hapd,
  1045. struct ieee80211_mgmt *mgmt, size_t len)
  1046. {
  1047. struct sta_info *sta;
  1048. u8 *end;
  1049. int i;
  1050. end = mgmt->u.action.u.ping_resp.trans_id + WLAN_PING_TRANS_ID_LEN;
  1051. if (((u8 *) mgmt) + len < end) {
  1052. wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short Ping Action "
  1053. "frame (len=%lu)", (unsigned long) len);
  1054. return;
  1055. }
  1056. if (mgmt->u.action.u.ping_resp.action != WLAN_PING_RESPONSE) {
  1057. wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected Ping Action %d",
  1058. mgmt->u.action.u.ping_resp.action);
  1059. return;
  1060. }
  1061. /* MLME-PING.confirm */
  1062. sta = ap_get_sta(hapd, mgmt->sa);
  1063. if (sta == NULL || sta->ping_trans_id == NULL) {
  1064. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
  1065. "pending ping request found");
  1066. return;
  1067. }
  1068. for (i = 0; i < sta->ping_count; i++) {
  1069. if (os_memcmp(sta->ping_trans_id + i * WLAN_PING_TRANS_ID_LEN,
  1070. mgmt->u.action.u.ping_resp.trans_id,
  1071. WLAN_PING_TRANS_ID_LEN) == 0)
  1072. break;
  1073. }
  1074. if (i >= sta->ping_count) {
  1075. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching ping "
  1076. "transaction identifier found");
  1077. return;
  1078. }
  1079. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1080. HOSTAPD_LEVEL_DEBUG, "Reply to pending ping received");
  1081. ap_sta_stop_ping(hapd, sta);
  1082. }
  1083. #endif /* CONFIG_IEEE80211W */
  1084. static void handle_action(struct hostapd_data *hapd,
  1085. struct ieee80211_mgmt *mgmt, size_t len)
  1086. {
  1087. if (len < IEEE80211_HDRLEN + 1) {
  1088. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1089. HOSTAPD_LEVEL_DEBUG,
  1090. "handle_action - too short payload (len=%lu)",
  1091. (unsigned long) len);
  1092. return;
  1093. }
  1094. switch (mgmt->u.action.category) {
  1095. #ifdef CONFIG_IEEE80211R
  1096. case WLAN_ACTION_FT:
  1097. {
  1098. struct sta_info *sta;
  1099. sta = ap_get_sta(hapd, mgmt->sa);
  1100. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  1101. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
  1102. "frame from unassociated STA " MACSTR,
  1103. MAC2STR(mgmt->sa));
  1104. return;
  1105. }
  1106. if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  1107. len - IEEE80211_HDRLEN))
  1108. break;
  1109. return;
  1110. }
  1111. #endif /* CONFIG_IEEE80211R */
  1112. case WLAN_ACTION_WMM:
  1113. hostapd_wme_action(hapd, mgmt, len);
  1114. return;
  1115. #ifdef CONFIG_IEEE80211W
  1116. case WLAN_ACTION_PING:
  1117. hostapd_ping_action(hapd, mgmt, len);
  1118. return;
  1119. #endif /* CONFIG_IEEE80211W */
  1120. }
  1121. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1122. HOSTAPD_LEVEL_DEBUG,
  1123. "handle_action - unknown action category %d or invalid "
  1124. "frame",
  1125. mgmt->u.action.category);
  1126. if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
  1127. !(mgmt->sa[0] & 0x01)) {
  1128. /*
  1129. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  1130. * Return the Action frame to the source without change
  1131. * except that MSB of the Category set to 1.
  1132. */
  1133. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  1134. "frame back to sender");
  1135. os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
  1136. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  1137. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  1138. mgmt->u.action.category |= 0x80;
  1139. hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
  1140. }
  1141. }
  1142. /**
  1143. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  1144. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  1145. * sent to)
  1146. * @buf: management frame data (starting from IEEE 802.11 header)
  1147. * @len: length of frame data in octets
  1148. * @stype: management frame subtype from frame control field
  1149. *
  1150. * Process all incoming IEEE 802.11 management frames. This will be called for
  1151. * each frame received from the kernel driver through wlan#ap interface. In
  1152. * addition, it can be called to re-inserted pending frames (e.g., when using
  1153. * external RADIUS server as an MAC ACL).
  1154. */
  1155. void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
  1156. struct hostapd_frame_info *fi)
  1157. {
  1158. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1159. int broadcast;
  1160. if (stype == WLAN_FC_STYPE_BEACON) {
  1161. handle_beacon(hapd, mgmt, len, fi);
  1162. return;
  1163. }
  1164. if (fi && fi->passive_scan)
  1165. return;
  1166. broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
  1167. mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
  1168. mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
  1169. if (!broadcast &&
  1170. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
  1171. printf("MGMT: BSSID=" MACSTR " not our address\n",
  1172. MAC2STR(mgmt->bssid));
  1173. return;
  1174. }
  1175. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  1176. handle_probe_req(hapd, mgmt, len);
  1177. return;
  1178. }
  1179. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  1180. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1181. HOSTAPD_LEVEL_DEBUG,
  1182. "MGMT: DA=" MACSTR " not our address",
  1183. MAC2STR(mgmt->da));
  1184. return;
  1185. }
  1186. switch (stype) {
  1187. case WLAN_FC_STYPE_AUTH:
  1188. wpa_printf(MSG_DEBUG, "mgmt::auth");
  1189. handle_auth(hapd, mgmt, len);
  1190. break;
  1191. case WLAN_FC_STYPE_ASSOC_REQ:
  1192. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  1193. handle_assoc(hapd, mgmt, len, 0);
  1194. break;
  1195. case WLAN_FC_STYPE_REASSOC_REQ:
  1196. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  1197. handle_assoc(hapd, mgmt, len, 1);
  1198. break;
  1199. case WLAN_FC_STYPE_DISASSOC:
  1200. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  1201. handle_disassoc(hapd, mgmt, len);
  1202. break;
  1203. case WLAN_FC_STYPE_DEAUTH:
  1204. wpa_printf(MSG_DEBUG, "mgmt::deauth");
  1205. handle_deauth(hapd, mgmt, len);
  1206. break;
  1207. case WLAN_FC_STYPE_ACTION:
  1208. wpa_printf(MSG_DEBUG, "mgmt::action");
  1209. handle_action(hapd, mgmt, len);
  1210. break;
  1211. default:
  1212. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1213. HOSTAPD_LEVEL_DEBUG,
  1214. "unknown mgmt frame subtype %d", stype);
  1215. break;
  1216. }
  1217. }
  1218. static void handle_auth_cb(struct hostapd_data *hapd,
  1219. struct ieee80211_mgmt *mgmt,
  1220. size_t len, int ok)
  1221. {
  1222. u16 auth_alg, auth_transaction, status_code;
  1223. struct sta_info *sta;
  1224. if (!ok) {
  1225. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1226. HOSTAPD_LEVEL_NOTICE,
  1227. "did not acknowledge authentication response");
  1228. return;
  1229. }
  1230. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1231. printf("handle_auth_cb - too short payload (len=%lu)\n",
  1232. (unsigned long) len);
  1233. return;
  1234. }
  1235. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1236. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1237. status_code = le_to_host16(mgmt->u.auth.status_code);
  1238. sta = ap_get_sta(hapd, mgmt->da);
  1239. if (!sta) {
  1240. printf("handle_auth_cb: STA " MACSTR " not found\n",
  1241. MAC2STR(mgmt->da));
  1242. return;
  1243. }
  1244. if (status_code == WLAN_STATUS_SUCCESS &&
  1245. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  1246. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  1247. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1248. HOSTAPD_LEVEL_INFO, "authenticated");
  1249. sta->flags |= WLAN_STA_AUTH;
  1250. }
  1251. }
  1252. static void handle_assoc_cb(struct hostapd_data *hapd,
  1253. struct ieee80211_mgmt *mgmt,
  1254. size_t len, int reassoc, int ok)
  1255. {
  1256. u16 status;
  1257. struct sta_info *sta;
  1258. int new_assoc = 1;
  1259. struct ht_cap_ie *ht_cap = NULL;
  1260. if (!ok) {
  1261. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1262. HOSTAPD_LEVEL_DEBUG,
  1263. "did not acknowledge association response");
  1264. return;
  1265. }
  1266. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  1267. sizeof(mgmt->u.assoc_resp))) {
  1268. printf("handle_assoc_cb(reassoc=%d) - too short payload "
  1269. "(len=%lu)\n", reassoc, (unsigned long) len);
  1270. return;
  1271. }
  1272. if (reassoc)
  1273. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  1274. else
  1275. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1276. sta = ap_get_sta(hapd, mgmt->da);
  1277. if (!sta) {
  1278. printf("handle_assoc_cb: STA " MACSTR " not found\n",
  1279. MAC2STR(mgmt->da));
  1280. return;
  1281. }
  1282. if (status != WLAN_STATUS_SUCCESS)
  1283. goto fail;
  1284. /* Stop previous accounting session, if one is started, and allocate
  1285. * new session id for the new session. */
  1286. accounting_sta_stop(hapd, sta);
  1287. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1288. HOSTAPD_LEVEL_INFO,
  1289. "associated (aid %d)",
  1290. sta->aid);
  1291. if (sta->flags & WLAN_STA_ASSOC)
  1292. new_assoc = 0;
  1293. sta->flags |= WLAN_STA_ASSOC;
  1294. if (reassoc)
  1295. mlme_reassociate_indication(hapd, sta);
  1296. else
  1297. mlme_associate_indication(hapd, sta);
  1298. #ifdef CONFIG_IEEE80211N
  1299. if (sta->flags & WLAN_STA_HT)
  1300. ht_cap = &sta->ht_capabilities;
  1301. #endif /* CONFIG_IEEE80211N */
  1302. #ifdef CONFIG_IEEE80211W
  1303. sta->ping_timed_out = 0;
  1304. #endif /* CONFIG_IEEE80211W */
  1305. if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
  1306. sta->capability, sta->supported_rates,
  1307. sta->supported_rates_len, 0, sta->listen_interval,
  1308. ht_cap))
  1309. {
  1310. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1311. HOSTAPD_LEVEL_NOTICE,
  1312. "Could not add STA to kernel driver");
  1313. }
  1314. if (sta->eapol_sm == NULL) {
  1315. /*
  1316. * This STA does not use RADIUS server for EAP authentication,
  1317. * so bind it to the selected VLAN interface now, since the
  1318. * interface selection is not going to change anymore.
  1319. */
  1320. ap_sta_bind_vlan(hapd, sta, 0);
  1321. } else if (sta->vlan_id) {
  1322. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  1323. ap_sta_bind_vlan(hapd, sta, 0);
  1324. }
  1325. if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
  1326. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1327. WLAN_STA_SHORT_PREAMBLE, ~0);
  1328. } else {
  1329. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1330. 0, ~WLAN_STA_SHORT_PREAMBLE);
  1331. }
  1332. if (sta->auth_alg == WLAN_AUTH_FT)
  1333. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  1334. else
  1335. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  1336. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  1337. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  1338. fail:
  1339. /* Copy of the association request is not needed anymore */
  1340. if (sta->last_assoc_req) {
  1341. os_free(sta->last_assoc_req);
  1342. sta->last_assoc_req = NULL;
  1343. }
  1344. }
  1345. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  1346. u16 stype, int ok)
  1347. {
  1348. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1349. switch (stype) {
  1350. case WLAN_FC_STYPE_AUTH:
  1351. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  1352. handle_auth_cb(hapd, mgmt, len, ok);
  1353. break;
  1354. case WLAN_FC_STYPE_ASSOC_RESP:
  1355. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  1356. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  1357. break;
  1358. case WLAN_FC_STYPE_REASSOC_RESP:
  1359. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  1360. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  1361. break;
  1362. case WLAN_FC_STYPE_PROBE_RESP:
  1363. wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
  1364. break;
  1365. case WLAN_FC_STYPE_DEAUTH:
  1366. /* ignore */
  1367. break;
  1368. case WLAN_FC_STYPE_ACTION:
  1369. wpa_printf(MSG_DEBUG, "mgmt::action cb");
  1370. break;
  1371. default:
  1372. printf("unknown mgmt cb frame subtype %d\n", stype);
  1373. break;
  1374. }
  1375. }
  1376. static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
  1377. void *timeout_ctx)
  1378. {
  1379. struct hostapd_data *hapd = eloop_ctx;
  1380. hapd->tkip_countermeasures = 0;
  1381. hostapd_set_countermeasures(hapd, 0);
  1382. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1383. HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
  1384. }
  1385. static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
  1386. {
  1387. struct sta_info *sta;
  1388. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1389. HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
  1390. wpa_auth_countermeasures_start(hapd->wpa_auth);
  1391. hapd->tkip_countermeasures = 1;
  1392. hostapd_set_countermeasures(hapd, 1);
  1393. wpa_gtk_rekey(hapd->wpa_auth);
  1394. eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
  1395. eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
  1396. hapd, NULL);
  1397. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  1398. hostapd_sta_deauth(hapd, sta->addr,
  1399. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1400. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1401. WLAN_STA_AUTHORIZED);
  1402. hostapd_sta_remove(hapd, sta->addr);
  1403. }
  1404. }
  1405. void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
  1406. int local)
  1407. {
  1408. time_t now;
  1409. if (addr && local) {
  1410. struct sta_info *sta = ap_get_sta(hapd, addr);
  1411. if (sta != NULL) {
  1412. wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
  1413. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  1414. HOSTAPD_LEVEL_INFO,
  1415. "Michael MIC failure detected in "
  1416. "received frame");
  1417. mlme_michaelmicfailure_indication(hapd, addr);
  1418. } else {
  1419. wpa_printf(MSG_DEBUG,
  1420. "MLME-MICHAELMICFAILURE.indication "
  1421. "for not associated STA (" MACSTR
  1422. ") ignored", MAC2STR(addr));
  1423. return;
  1424. }
  1425. }
  1426. time(&now);
  1427. if (now > hapd->michael_mic_failure + 60) {
  1428. hapd->michael_mic_failures = 1;
  1429. } else {
  1430. hapd->michael_mic_failures++;
  1431. if (hapd->michael_mic_failures > 1)
  1432. ieee80211_tkip_countermeasures_start(hapd);
  1433. }
  1434. hapd->michael_mic_failure = now;
  1435. }
  1436. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1437. {
  1438. /* TODO */
  1439. return 0;
  1440. }
  1441. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1442. char *buf, size_t buflen)
  1443. {
  1444. /* TODO */
  1445. return 0;
  1446. }
  1447. #endif /* CONFIG_NATIVE_WINDOWS */