ieee802_11.c 50 KB

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