ieee802_11.c 56 KB

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