ieee802_11.c 51 KB

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