ieee802_11.c 53 KB

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