ieee802_11.c 49 KB

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