ieee802_11.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  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. if (hapd->conf->wps_state && wpa_ie == NULL) {
  716. if (elems.wps_ie) {
  717. wpa_printf(MSG_DEBUG, "STA included WPS IE in "
  718. "(Re)Association Request - assume WPS is "
  719. "used");
  720. sta->flags |= WLAN_STA_WPS;
  721. } else {
  722. wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
  723. "in (Re)Association Request - possible WPS "
  724. "use");
  725. sta->flags |= WLAN_STA_MAYBE_WPS;
  726. }
  727. } else
  728. #endif /* CONFIG_WPS */
  729. if (hapd->conf->wpa && wpa_ie == NULL) {
  730. printf("STA " MACSTR ": No WPA/RSN IE in association "
  731. "request\n", MAC2STR(sta->addr));
  732. resp = WLAN_STATUS_INVALID_IE;
  733. goto fail;
  734. }
  735. if (hapd->conf->wpa && wpa_ie) {
  736. int res;
  737. wpa_ie -= 2;
  738. wpa_ie_len += 2;
  739. if (sta->wpa_sm == NULL)
  740. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  741. sta->addr);
  742. if (sta->wpa_sm == NULL) {
  743. printf("Failed to initialize WPA state machine\n");
  744. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  745. goto fail;
  746. }
  747. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  748. wpa_ie, wpa_ie_len,
  749. elems.mdie, elems.mdie_len);
  750. if (res == WPA_INVALID_GROUP)
  751. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  752. else if (res == WPA_INVALID_PAIRWISE)
  753. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  754. else if (res == WPA_INVALID_AKMP)
  755. resp = WLAN_STATUS_AKMP_NOT_VALID;
  756. else if (res == WPA_ALLOC_FAIL)
  757. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  758. #ifdef CONFIG_IEEE80211W
  759. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  760. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  761. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  762. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  763. #endif /* CONFIG_IEEE80211W */
  764. else if (res == WPA_INVALID_MDIE)
  765. resp = WLAN_STATUS_INVALID_MDIE;
  766. else if (res != WPA_IE_OK)
  767. resp = WLAN_STATUS_INVALID_IE;
  768. if (resp != WLAN_STATUS_SUCCESS)
  769. goto fail;
  770. #ifdef CONFIG_IEEE80211W
  771. if ((sta->flags & WLAN_STA_MFP) && !sta->ping_timed_out) {
  772. /*
  773. * STA has already been associated with MFP and ping
  774. * timeout has not been reached. Reject the
  775. * association attempt temporarily and start ping, if
  776. * one is not pending.
  777. */
  778. if (sta->ping_count == 0)
  779. ap_sta_start_ping(hapd, sta);
  780. resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
  781. goto fail;
  782. }
  783. if (wpa_auth_uses_mfp(sta->wpa_sm))
  784. sta->flags |= WLAN_STA_MFP;
  785. else
  786. sta->flags &= ~WLAN_STA_MFP;
  787. #endif /* CONFIG_IEEE80211W */
  788. #ifdef CONFIG_IEEE80211R
  789. if (sta->auth_alg == WLAN_AUTH_FT) {
  790. if (!reassoc) {
  791. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  792. "to use association (not "
  793. "re-association) with FT auth_alg",
  794. MAC2STR(sta->addr));
  795. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  796. goto fail;
  797. }
  798. resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
  799. if (resp != WLAN_STATUS_SUCCESS)
  800. goto fail;
  801. }
  802. #endif /* CONFIG_IEEE80211R */
  803. }
  804. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  805. sta->flags |= WLAN_STA_NONERP;
  806. for (i = 0; i < sta->supported_rates_len; i++) {
  807. if ((sta->supported_rates[i] & 0x7f) > 22) {
  808. sta->flags &= ~WLAN_STA_NONERP;
  809. break;
  810. }
  811. }
  812. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  813. sta->nonerp_set = 1;
  814. hapd->iface->num_sta_non_erp++;
  815. if (hapd->iface->num_sta_non_erp == 1)
  816. ieee802_11_set_beacons(hapd->iface);
  817. }
  818. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  819. !sta->no_short_slot_time_set) {
  820. sta->no_short_slot_time_set = 1;
  821. hapd->iface->num_sta_no_short_slot_time++;
  822. if (hapd->iface->current_mode->mode ==
  823. HOSTAPD_MODE_IEEE80211G &&
  824. hapd->iface->num_sta_no_short_slot_time == 1)
  825. ieee802_11_set_beacons(hapd->iface);
  826. }
  827. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  828. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  829. else
  830. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  831. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  832. !sta->no_short_preamble_set) {
  833. sta->no_short_preamble_set = 1;
  834. hapd->iface->num_sta_no_short_preamble++;
  835. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  836. && hapd->iface->num_sta_no_short_preamble == 1)
  837. ieee802_11_set_beacons(hapd->iface);
  838. }
  839. #ifdef CONFIG_IEEE80211N
  840. if (sta->flags & WLAN_STA_HT) {
  841. u16 ht_capab = le_to_host16(
  842. sta->ht_capabilities.data.capabilities_info);
  843. wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
  844. "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
  845. if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
  846. hapd->iface->num_sta_ht_no_gf++;
  847. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
  848. "greenfield, num of non-gf stations %d",
  849. __func__, MAC2STR(sta->addr),
  850. hapd->iface->num_sta_ht_no_gf);
  851. }
  852. if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
  853. hapd->iface->num_sta_ht_20mhz++;
  854. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
  855. "num of 20MHz HT STAs %d",
  856. __func__, MAC2STR(sta->addr),
  857. hapd->iface->num_sta_ht_20mhz);
  858. }
  859. } else {
  860. hapd->iface->num_sta_no_ht++;
  861. if (hapd->iconf->ieee80211n) {
  862. wpa_printf(MSG_DEBUG, "%s STA " MACSTR
  863. " - no HT, num of non-HT stations %d",
  864. __func__, MAC2STR(sta->addr),
  865. hapd->iface->num_sta_no_ht);
  866. }
  867. }
  868. if (hostapd_ht_operation_update(hapd->iface) > 0)
  869. ieee802_11_set_beacons(hapd->iface);
  870. #endif /* CONFIG_IEEE80211N */
  871. /* get a unique AID */
  872. if (sta->aid > 0) {
  873. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  874. } else {
  875. for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
  876. if (hapd->sta_aid[sta->aid - 1] == NULL)
  877. break;
  878. if (sta->aid > MAX_AID_TABLE_SIZE) {
  879. sta->aid = 0;
  880. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  881. wpa_printf(MSG_ERROR, " no room for more AIDs");
  882. goto fail;
  883. } else {
  884. hapd->sta_aid[sta->aid - 1] = sta;
  885. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  886. }
  887. }
  888. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  889. HOSTAPD_LEVEL_DEBUG,
  890. "association OK (aid %d)", sta->aid);
  891. /* Station will be marked associated, after it acknowledges AssocResp
  892. */
  893. if (reassoc) {
  894. os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
  895. ETH_ALEN);
  896. }
  897. if (sta->last_assoc_req)
  898. os_free(sta->last_assoc_req);
  899. sta->last_assoc_req = os_malloc(len);
  900. if (sta->last_assoc_req)
  901. os_memcpy(sta->last_assoc_req, mgmt, len);
  902. /* Make sure that the previously registered inactivity timer will not
  903. * remove the STA immediately. */
  904. sta->timeout_next = STA_NULLFUNC;
  905. fail:
  906. os_memset(buf, 0, sizeof(buf));
  907. reply = (struct ieee80211_mgmt *) buf;
  908. reply->frame_control =
  909. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  910. (send_deauth ? WLAN_FC_STYPE_DEAUTH :
  911. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  912. WLAN_FC_STYPE_ASSOC_RESP)));
  913. os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
  914. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  915. os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
  916. send_len = IEEE80211_HDRLEN;
  917. if (send_deauth) {
  918. send_len += sizeof(reply->u.deauth);
  919. reply->u.deauth.reason_code = host_to_le16(resp);
  920. } else {
  921. u8 *p;
  922. send_len += sizeof(reply->u.assoc_resp);
  923. reply->u.assoc_resp.capab_info =
  924. host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
  925. reply->u.assoc_resp.status_code = host_to_le16(resp);
  926. reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
  927. | BIT(14) | BIT(15));
  928. /* Supported rates */
  929. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  930. /* Extended supported rates */
  931. p = hostapd_eid_ext_supp_rates(hapd, p);
  932. if (sta->flags & WLAN_STA_WME)
  933. p = hostapd_eid_wme(hapd, p);
  934. p = hostapd_eid_ht_capabilities_info(hapd, p);
  935. p = hostapd_eid_ht_operation(hapd, p);
  936. #ifdef CONFIG_IEEE80211R
  937. if (resp == WLAN_STATUS_SUCCESS) {
  938. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  939. * Transition Information, RSN */
  940. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  941. buf + sizeof(buf) - p,
  942. sta->auth_alg);
  943. }
  944. #endif /* CONFIG_IEEE80211R */
  945. #ifdef CONFIG_IEEE80211W
  946. if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
  947. p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
  948. #endif /* CONFIG_IEEE80211W */
  949. send_len += p - reply->u.assoc_resp.variable;
  950. }
  951. if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
  952. perror("handle_assoc: send");
  953. }
  954. static void handle_disassoc(struct hostapd_data *hapd,
  955. struct ieee80211_mgmt *mgmt, size_t len)
  956. {
  957. struct sta_info *sta;
  958. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  959. printf("handle_disassoc - too short payload (len=%lu)\n",
  960. (unsigned long) len);
  961. return;
  962. }
  963. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  964. MAC2STR(mgmt->sa),
  965. le_to_host16(mgmt->u.disassoc.reason_code));
  966. sta = ap_get_sta(hapd, mgmt->sa);
  967. if (sta == NULL) {
  968. printf("Station " MACSTR " trying to disassociate, but it "
  969. "is not associated.\n", MAC2STR(mgmt->sa));
  970. return;
  971. }
  972. sta->flags &= ~WLAN_STA_ASSOC;
  973. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  974. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  975. HOSTAPD_LEVEL_INFO, "disassociated");
  976. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  977. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  978. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  979. * authenticated. */
  980. accounting_sta_stop(hapd, sta);
  981. ieee802_1x_free_station(sta);
  982. hostapd_sta_remove(hapd, sta->addr);
  983. if (sta->timeout_next == STA_NULLFUNC ||
  984. sta->timeout_next == STA_DISASSOC) {
  985. sta->timeout_next = STA_DEAUTH;
  986. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  987. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  988. hapd, sta);
  989. }
  990. mlme_disassociate_indication(
  991. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  992. }
  993. static void handle_deauth(struct hostapd_data *hapd,
  994. struct ieee80211_mgmt *mgmt, size_t len)
  995. {
  996. struct sta_info *sta;
  997. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  998. printf("handle_deauth - too short payload (len=%lu)\n",
  999. (unsigned long) len);
  1000. return;
  1001. }
  1002. wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
  1003. " reason_code=%d",
  1004. MAC2STR(mgmt->sa),
  1005. le_to_host16(mgmt->u.deauth.reason_code));
  1006. sta = ap_get_sta(hapd, mgmt->sa);
  1007. if (sta == NULL) {
  1008. printf("Station " MACSTR " trying to deauthenticate, but it "
  1009. "is not authenticated.\n", MAC2STR(mgmt->sa));
  1010. return;
  1011. }
  1012. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1013. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1014. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1015. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  1016. mlme_deauthenticate_indication(
  1017. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  1018. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1019. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1020. ap_free_sta(hapd, sta);
  1021. }
  1022. static void handle_beacon(struct hostapd_data *hapd,
  1023. struct ieee80211_mgmt *mgmt, size_t len,
  1024. struct hostapd_frame_info *fi)
  1025. {
  1026. struct ieee802_11_elems elems;
  1027. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  1028. printf("handle_beacon - too short payload (len=%lu)\n",
  1029. (unsigned long) len);
  1030. return;
  1031. }
  1032. (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1033. len - (IEEE80211_HDRLEN +
  1034. sizeof(mgmt->u.beacon)), &elems,
  1035. 0);
  1036. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  1037. }
  1038. #ifdef CONFIG_IEEE80211W
  1039. static void hostapd_ping_action(struct hostapd_data *hapd,
  1040. struct ieee80211_mgmt *mgmt, size_t len)
  1041. {
  1042. struct sta_info *sta;
  1043. u8 *end;
  1044. int i;
  1045. end = mgmt->u.action.u.ping_resp.trans_id + WLAN_PING_TRANS_ID_LEN;
  1046. if (((u8 *) mgmt) + len < end) {
  1047. wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short Ping Action "
  1048. "frame (len=%lu)", (unsigned long) len);
  1049. return;
  1050. }
  1051. if (mgmt->u.action.u.ping_resp.action != WLAN_PING_RESPONSE) {
  1052. wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected Ping Action %d",
  1053. mgmt->u.action.u.ping_resp.action);
  1054. return;
  1055. }
  1056. /* MLME-PING.confirm */
  1057. sta = ap_get_sta(hapd, mgmt->sa);
  1058. if (sta == NULL || sta->ping_trans_id == NULL) {
  1059. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
  1060. "pending ping request found");
  1061. return;
  1062. }
  1063. for (i = 0; i < sta->ping_count; i++) {
  1064. if (os_memcmp(sta->ping_trans_id + i * WLAN_PING_TRANS_ID_LEN,
  1065. mgmt->u.action.u.ping_resp.trans_id,
  1066. WLAN_PING_TRANS_ID_LEN) == 0)
  1067. break;
  1068. }
  1069. if (i >= sta->ping_count) {
  1070. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching ping "
  1071. "transaction identifier found");
  1072. return;
  1073. }
  1074. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1075. HOSTAPD_LEVEL_DEBUG, "Reply to pending ping received");
  1076. ap_sta_stop_ping(hapd, sta);
  1077. }
  1078. #endif /* CONFIG_IEEE80211W */
  1079. static void handle_action(struct hostapd_data *hapd,
  1080. struct ieee80211_mgmt *mgmt, size_t len)
  1081. {
  1082. if (len < IEEE80211_HDRLEN + 1) {
  1083. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1084. HOSTAPD_LEVEL_DEBUG,
  1085. "handle_action - too short payload (len=%lu)",
  1086. (unsigned long) len);
  1087. return;
  1088. }
  1089. switch (mgmt->u.action.category) {
  1090. #ifdef CONFIG_IEEE80211R
  1091. case WLAN_ACTION_FT:
  1092. {
  1093. struct sta_info *sta;
  1094. sta = ap_get_sta(hapd, mgmt->sa);
  1095. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  1096. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
  1097. "frame from unassociated STA " MACSTR,
  1098. MAC2STR(mgmt->sa));
  1099. return;
  1100. }
  1101. if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  1102. len - IEEE80211_HDRLEN))
  1103. break;
  1104. return;
  1105. }
  1106. #endif /* CONFIG_IEEE80211R */
  1107. case WLAN_ACTION_WMM:
  1108. hostapd_wme_action(hapd, mgmt, len);
  1109. return;
  1110. #ifdef CONFIG_IEEE80211W
  1111. case WLAN_ACTION_PING:
  1112. hostapd_ping_action(hapd, mgmt, len);
  1113. return;
  1114. #endif /* CONFIG_IEEE80211W */
  1115. }
  1116. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1117. HOSTAPD_LEVEL_DEBUG,
  1118. "handle_action - unknown action category %d or invalid "
  1119. "frame",
  1120. mgmt->u.action.category);
  1121. if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
  1122. !(mgmt->sa[0] & 0x01)) {
  1123. /*
  1124. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  1125. * Return the Action frame to the source without change
  1126. * except that MSB of the Category set to 1.
  1127. */
  1128. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  1129. "frame back to sender");
  1130. os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
  1131. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  1132. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  1133. mgmt->u.action.category |= 0x80;
  1134. hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
  1135. }
  1136. }
  1137. /**
  1138. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  1139. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  1140. * sent to)
  1141. * @buf: management frame data (starting from IEEE 802.11 header)
  1142. * @len: length of frame data in octets
  1143. * @stype: management frame subtype from frame control field
  1144. *
  1145. * Process all incoming IEEE 802.11 management frames. This will be called for
  1146. * each frame received from the kernel driver through wlan#ap interface. In
  1147. * addition, it can be called to re-inserted pending frames (e.g., when using
  1148. * external RADIUS server as an MAC ACL).
  1149. */
  1150. void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
  1151. struct hostapd_frame_info *fi)
  1152. {
  1153. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1154. int broadcast;
  1155. if (stype == WLAN_FC_STYPE_BEACON) {
  1156. handle_beacon(hapd, mgmt, len, fi);
  1157. return;
  1158. }
  1159. if (fi && fi->passive_scan)
  1160. return;
  1161. broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
  1162. mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
  1163. mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
  1164. if (!broadcast &&
  1165. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
  1166. printf("MGMT: BSSID=" MACSTR " not our address\n",
  1167. MAC2STR(mgmt->bssid));
  1168. return;
  1169. }
  1170. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  1171. handle_probe_req(hapd, mgmt, len);
  1172. return;
  1173. }
  1174. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  1175. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1176. HOSTAPD_LEVEL_DEBUG,
  1177. "MGMT: DA=" MACSTR " not our address",
  1178. MAC2STR(mgmt->da));
  1179. return;
  1180. }
  1181. switch (stype) {
  1182. case WLAN_FC_STYPE_AUTH:
  1183. wpa_printf(MSG_DEBUG, "mgmt::auth");
  1184. handle_auth(hapd, mgmt, len);
  1185. break;
  1186. case WLAN_FC_STYPE_ASSOC_REQ:
  1187. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  1188. handle_assoc(hapd, mgmt, len, 0);
  1189. break;
  1190. case WLAN_FC_STYPE_REASSOC_REQ:
  1191. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  1192. handle_assoc(hapd, mgmt, len, 1);
  1193. break;
  1194. case WLAN_FC_STYPE_DISASSOC:
  1195. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  1196. handle_disassoc(hapd, mgmt, len);
  1197. break;
  1198. case WLAN_FC_STYPE_DEAUTH:
  1199. wpa_printf(MSG_DEBUG, "mgmt::deauth");
  1200. handle_deauth(hapd, mgmt, len);
  1201. break;
  1202. case WLAN_FC_STYPE_ACTION:
  1203. wpa_printf(MSG_DEBUG, "mgmt::action");
  1204. handle_action(hapd, mgmt, len);
  1205. break;
  1206. default:
  1207. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1208. HOSTAPD_LEVEL_DEBUG,
  1209. "unknown mgmt frame subtype %d", stype);
  1210. break;
  1211. }
  1212. }
  1213. static void handle_auth_cb(struct hostapd_data *hapd,
  1214. struct ieee80211_mgmt *mgmt,
  1215. size_t len, int ok)
  1216. {
  1217. u16 auth_alg, auth_transaction, status_code;
  1218. struct sta_info *sta;
  1219. if (!ok) {
  1220. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1221. HOSTAPD_LEVEL_NOTICE,
  1222. "did not acknowledge authentication response");
  1223. return;
  1224. }
  1225. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1226. printf("handle_auth_cb - too short payload (len=%lu)\n",
  1227. (unsigned long) len);
  1228. return;
  1229. }
  1230. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1231. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1232. status_code = le_to_host16(mgmt->u.auth.status_code);
  1233. sta = ap_get_sta(hapd, mgmt->da);
  1234. if (!sta) {
  1235. printf("handle_auth_cb: STA " MACSTR " not found\n",
  1236. MAC2STR(mgmt->da));
  1237. return;
  1238. }
  1239. if (status_code == WLAN_STATUS_SUCCESS &&
  1240. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  1241. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  1242. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1243. HOSTAPD_LEVEL_INFO, "authenticated");
  1244. sta->flags |= WLAN_STA_AUTH;
  1245. }
  1246. }
  1247. static void handle_assoc_cb(struct hostapd_data *hapd,
  1248. struct ieee80211_mgmt *mgmt,
  1249. size_t len, int reassoc, int ok)
  1250. {
  1251. u16 status;
  1252. struct sta_info *sta;
  1253. int new_assoc = 1;
  1254. struct ht_cap_ie *ht_cap = NULL;
  1255. if (!ok) {
  1256. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1257. HOSTAPD_LEVEL_DEBUG,
  1258. "did not acknowledge association response");
  1259. return;
  1260. }
  1261. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  1262. sizeof(mgmt->u.assoc_resp))) {
  1263. printf("handle_assoc_cb(reassoc=%d) - too short payload "
  1264. "(len=%lu)\n", reassoc, (unsigned long) len);
  1265. return;
  1266. }
  1267. if (reassoc)
  1268. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  1269. else
  1270. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1271. sta = ap_get_sta(hapd, mgmt->da);
  1272. if (!sta) {
  1273. printf("handle_assoc_cb: STA " MACSTR " not found\n",
  1274. MAC2STR(mgmt->da));
  1275. return;
  1276. }
  1277. if (status != WLAN_STATUS_SUCCESS)
  1278. goto fail;
  1279. /* Stop previous accounting session, if one is started, and allocate
  1280. * new session id for the new session. */
  1281. accounting_sta_stop(hapd, sta);
  1282. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1283. HOSTAPD_LEVEL_INFO,
  1284. "associated (aid %d)",
  1285. sta->aid);
  1286. if (sta->flags & WLAN_STA_ASSOC)
  1287. new_assoc = 0;
  1288. sta->flags |= WLAN_STA_ASSOC;
  1289. if (reassoc)
  1290. mlme_reassociate_indication(hapd, sta);
  1291. else
  1292. mlme_associate_indication(hapd, sta);
  1293. #ifdef CONFIG_IEEE80211N
  1294. if (sta->flags & WLAN_STA_HT)
  1295. ht_cap = &sta->ht_capabilities;
  1296. #endif /* CONFIG_IEEE80211N */
  1297. #ifdef CONFIG_IEEE80211W
  1298. sta->ping_timed_out = 0;
  1299. #endif /* CONFIG_IEEE80211W */
  1300. if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
  1301. sta->capability, sta->supported_rates,
  1302. sta->supported_rates_len, 0, sta->listen_interval,
  1303. ht_cap))
  1304. {
  1305. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1306. HOSTAPD_LEVEL_NOTICE,
  1307. "Could not add STA to kernel driver");
  1308. }
  1309. if (sta->eapol_sm == NULL) {
  1310. /*
  1311. * This STA does not use RADIUS server for EAP authentication,
  1312. * so bind it to the selected VLAN interface now, since the
  1313. * interface selection is not going to change anymore.
  1314. */
  1315. ap_sta_bind_vlan(hapd, sta, 0);
  1316. } else if (sta->vlan_id) {
  1317. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  1318. ap_sta_bind_vlan(hapd, sta, 0);
  1319. }
  1320. if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
  1321. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1322. WLAN_STA_SHORT_PREAMBLE, ~0);
  1323. } else {
  1324. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1325. 0, ~WLAN_STA_SHORT_PREAMBLE);
  1326. }
  1327. if (sta->auth_alg == WLAN_AUTH_FT)
  1328. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  1329. else
  1330. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  1331. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  1332. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  1333. fail:
  1334. /* Copy of the association request is not needed anymore */
  1335. if (sta->last_assoc_req) {
  1336. os_free(sta->last_assoc_req);
  1337. sta->last_assoc_req = NULL;
  1338. }
  1339. }
  1340. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  1341. u16 stype, int ok)
  1342. {
  1343. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1344. switch (stype) {
  1345. case WLAN_FC_STYPE_AUTH:
  1346. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  1347. handle_auth_cb(hapd, mgmt, len, ok);
  1348. break;
  1349. case WLAN_FC_STYPE_ASSOC_RESP:
  1350. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  1351. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  1352. break;
  1353. case WLAN_FC_STYPE_REASSOC_RESP:
  1354. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  1355. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  1356. break;
  1357. case WLAN_FC_STYPE_PROBE_RESP:
  1358. wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
  1359. break;
  1360. case WLAN_FC_STYPE_DEAUTH:
  1361. /* ignore */
  1362. break;
  1363. case WLAN_FC_STYPE_ACTION:
  1364. wpa_printf(MSG_DEBUG, "mgmt::action cb");
  1365. break;
  1366. default:
  1367. printf("unknown mgmt cb frame subtype %d\n", stype);
  1368. break;
  1369. }
  1370. }
  1371. static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
  1372. void *timeout_ctx)
  1373. {
  1374. struct hostapd_data *hapd = eloop_ctx;
  1375. hapd->tkip_countermeasures = 0;
  1376. hostapd_set_countermeasures(hapd, 0);
  1377. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1378. HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
  1379. }
  1380. static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
  1381. {
  1382. struct sta_info *sta;
  1383. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1384. HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
  1385. wpa_auth_countermeasures_start(hapd->wpa_auth);
  1386. hapd->tkip_countermeasures = 1;
  1387. hostapd_set_countermeasures(hapd, 1);
  1388. wpa_gtk_rekey(hapd->wpa_auth);
  1389. eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
  1390. eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
  1391. hapd, NULL);
  1392. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  1393. hostapd_sta_deauth(hapd, sta->addr,
  1394. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1395. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1396. WLAN_STA_AUTHORIZED);
  1397. hostapd_sta_remove(hapd, sta->addr);
  1398. }
  1399. }
  1400. void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
  1401. int local)
  1402. {
  1403. time_t now;
  1404. if (addr && local) {
  1405. struct sta_info *sta = ap_get_sta(hapd, addr);
  1406. if (sta != NULL) {
  1407. wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
  1408. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  1409. HOSTAPD_LEVEL_INFO,
  1410. "Michael MIC failure detected in "
  1411. "received frame");
  1412. mlme_michaelmicfailure_indication(hapd, addr);
  1413. } else {
  1414. wpa_printf(MSG_DEBUG,
  1415. "MLME-MICHAELMICFAILURE.indication "
  1416. "for not associated STA (" MACSTR
  1417. ") ignored", MAC2STR(addr));
  1418. return;
  1419. }
  1420. }
  1421. time(&now);
  1422. if (now > hapd->michael_mic_failure + 60) {
  1423. hapd->michael_mic_failures = 1;
  1424. } else {
  1425. hapd->michael_mic_failures++;
  1426. if (hapd->michael_mic_failures > 1)
  1427. ieee80211_tkip_countermeasures_start(hapd);
  1428. }
  1429. hapd->michael_mic_failure = now;
  1430. }
  1431. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1432. {
  1433. /* TODO */
  1434. return 0;
  1435. }
  1436. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1437. char *buf, size_t buflen)
  1438. {
  1439. /* TODO */
  1440. return 0;
  1441. }
  1442. #endif /* CONFIG_NATIVE_WINDOWS */