ieee802_11.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #ifndef CONFIG_NATIVE_WINDOWS
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "crypto/crypto.h"
  13. #include "crypto/sha256.h"
  14. #include "crypto/random.h"
  15. #include "common/ieee802_11_defs.h"
  16. #include "common/ieee802_11_common.h"
  17. #include "common/wpa_ctrl.h"
  18. #include "common/sae.h"
  19. #include "radius/radius.h"
  20. #include "radius/radius_client.h"
  21. #include "p2p/p2p.h"
  22. #include "wps/wps.h"
  23. #include "hostapd.h"
  24. #include "beacon.h"
  25. #include "ieee802_11_auth.h"
  26. #include "sta_info.h"
  27. #include "ieee802_1x.h"
  28. #include "wpa_auth.h"
  29. #include "wmm.h"
  30. #include "ap_list.h"
  31. #include "accounting.h"
  32. #include "ap_config.h"
  33. #include "ap_mlme.h"
  34. #include "p2p_hostapd.h"
  35. #include "ap_drv_ops.h"
  36. #include "wnm_ap.h"
  37. #include "ieee802_11.h"
  38. #include "dfs.h"
  39. u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
  40. {
  41. u8 *pos = eid;
  42. int i, num, count;
  43. if (hapd->iface->current_rates == NULL)
  44. return eid;
  45. *pos++ = WLAN_EID_SUPP_RATES;
  46. num = hapd->iface->num_rates;
  47. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
  48. num++;
  49. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
  50. num++;
  51. if (num > 8) {
  52. /* rest of the rates are encoded in Extended supported
  53. * rates element */
  54. num = 8;
  55. }
  56. *pos++ = num;
  57. count = 0;
  58. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
  59. i++) {
  60. count++;
  61. *pos = hapd->iface->current_rates[i].rate / 5;
  62. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  63. *pos |= 0x80;
  64. pos++;
  65. }
  66. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
  67. count++;
  68. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
  69. }
  70. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
  71. count++;
  72. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
  73. }
  74. return pos;
  75. }
  76. u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
  77. {
  78. u8 *pos = eid;
  79. int i, num, count;
  80. if (hapd->iface->current_rates == NULL)
  81. return eid;
  82. num = hapd->iface->num_rates;
  83. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
  84. num++;
  85. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
  86. num++;
  87. if (num <= 8)
  88. return eid;
  89. num -= 8;
  90. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  91. *pos++ = num;
  92. count = 0;
  93. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
  94. i++) {
  95. count++;
  96. if (count <= 8)
  97. continue; /* already in SuppRates IE */
  98. *pos = hapd->iface->current_rates[i].rate / 5;
  99. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  100. *pos |= 0x80;
  101. pos++;
  102. }
  103. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
  104. count++;
  105. if (count > 8)
  106. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
  107. }
  108. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
  109. count++;
  110. if (count > 8)
  111. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
  112. }
  113. return pos;
  114. }
  115. u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
  116. int probe)
  117. {
  118. int capab = WLAN_CAPABILITY_ESS;
  119. int privacy;
  120. int dfs;
  121. /* Check if any of configured channels require DFS */
  122. dfs = hostapd_is_dfs_required(hapd->iface);
  123. if (dfs < 0) {
  124. wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
  125. dfs);
  126. dfs = 0;
  127. }
  128. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  129. hapd->iconf->preamble == SHORT_PREAMBLE)
  130. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  131. privacy = hapd->conf->ssid.wep.keys_set;
  132. if (hapd->conf->ieee802_1x &&
  133. (hapd->conf->default_wep_key_len ||
  134. hapd->conf->individual_wep_key_len))
  135. privacy = 1;
  136. if (hapd->conf->wpa)
  137. privacy = 1;
  138. #ifdef CONFIG_HS20
  139. if (hapd->conf->osen)
  140. privacy = 1;
  141. #endif /* CONFIG_HS20 */
  142. if (sta) {
  143. int policy, def_klen;
  144. if (probe && sta->ssid_probe) {
  145. policy = sta->ssid_probe->security_policy;
  146. def_klen = sta->ssid_probe->wep.default_len;
  147. } else {
  148. policy = sta->ssid->security_policy;
  149. def_klen = sta->ssid->wep.default_len;
  150. }
  151. privacy = policy != SECURITY_PLAINTEXT;
  152. if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
  153. privacy = 0;
  154. }
  155. if (privacy)
  156. capab |= WLAN_CAPABILITY_PRIVACY;
  157. if (hapd->iface->current_mode &&
  158. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  159. hapd->iface->num_sta_no_short_slot_time == 0)
  160. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  161. /*
  162. * Currently, Spectrum Management capability bit is set when directly
  163. * requested in configuration by spectrum_mgmt_required or when AP is
  164. * running on DFS channel.
  165. * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
  166. */
  167. if (hapd->iface->current_mode &&
  168. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
  169. (hapd->iconf->spectrum_mgmt_required || dfs))
  170. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  171. return capab;
  172. }
  173. static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
  174. u16 auth_transaction, const u8 *challenge,
  175. int iswep)
  176. {
  177. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  178. HOSTAPD_LEVEL_DEBUG,
  179. "authentication (shared key, transaction %d)",
  180. auth_transaction);
  181. if (auth_transaction == 1) {
  182. if (!sta->challenge) {
  183. /* Generate a pseudo-random challenge */
  184. u8 key[8];
  185. struct os_time now;
  186. int r;
  187. sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
  188. if (sta->challenge == NULL)
  189. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  190. os_get_time(&now);
  191. r = os_random();
  192. os_memcpy(key, &now.sec, 4);
  193. os_memcpy(key + 4, &r, 4);
  194. rc4_skip(key, sizeof(key), 0,
  195. sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
  196. }
  197. return 0;
  198. }
  199. if (auth_transaction != 3)
  200. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  201. /* Transaction 3 */
  202. if (!iswep || !sta->challenge || !challenge ||
  203. os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
  204. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  205. HOSTAPD_LEVEL_INFO,
  206. "shared key authentication - invalid "
  207. "challenge-response");
  208. return WLAN_STATUS_CHALLENGE_FAIL;
  209. }
  210. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  211. HOSTAPD_LEVEL_DEBUG,
  212. "authentication OK (shared key)");
  213. sta->flags |= WLAN_STA_AUTH;
  214. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  215. os_free(sta->challenge);
  216. sta->challenge = NULL;
  217. return 0;
  218. }
  219. static void send_auth_reply(struct hostapd_data *hapd,
  220. const u8 *dst, const u8 *bssid,
  221. u16 auth_alg, u16 auth_transaction, u16 resp,
  222. const u8 *ies, size_t ies_len)
  223. {
  224. struct ieee80211_mgmt *reply;
  225. u8 *buf;
  226. size_t rlen;
  227. rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
  228. buf = os_zalloc(rlen);
  229. if (buf == NULL)
  230. return;
  231. reply = (struct ieee80211_mgmt *) buf;
  232. reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  233. WLAN_FC_STYPE_AUTH);
  234. os_memcpy(reply->da, dst, ETH_ALEN);
  235. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  236. os_memcpy(reply->bssid, bssid, ETH_ALEN);
  237. reply->u.auth.auth_alg = host_to_le16(auth_alg);
  238. reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
  239. reply->u.auth.status_code = host_to_le16(resp);
  240. if (ies && ies_len)
  241. os_memcpy(reply->u.auth.variable, ies, ies_len);
  242. wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
  243. " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
  244. MAC2STR(dst), auth_alg, auth_transaction,
  245. resp, (unsigned long) ies_len);
  246. if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
  247. wpa_printf(MSG_INFO, "send_auth_reply: send");
  248. os_free(buf);
  249. }
  250. #ifdef CONFIG_IEEE80211R
  251. static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
  252. u16 auth_transaction, u16 status,
  253. const u8 *ies, size_t ies_len)
  254. {
  255. struct hostapd_data *hapd = ctx;
  256. struct sta_info *sta;
  257. send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
  258. status, ies, ies_len);
  259. if (status != WLAN_STATUS_SUCCESS)
  260. return;
  261. sta = ap_get_sta(hapd, dst);
  262. if (sta == NULL)
  263. return;
  264. hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
  265. HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
  266. sta->flags |= WLAN_STA_AUTH;
  267. mlme_authenticate_indication(hapd, sta);
  268. }
  269. #endif /* CONFIG_IEEE80211R */
  270. #ifdef CONFIG_SAE
  271. static struct wpabuf * auth_process_sae_commit(struct hostapd_data *hapd,
  272. struct sta_info *sta)
  273. {
  274. struct wpabuf *buf;
  275. if (hapd->conf->ssid.wpa_passphrase == NULL) {
  276. wpa_printf(MSG_DEBUG, "SAE: No password available");
  277. return NULL;
  278. }
  279. if (sae_prepare_commit(hapd->own_addr, sta->addr,
  280. (u8 *) hapd->conf->ssid.wpa_passphrase,
  281. os_strlen(hapd->conf->ssid.wpa_passphrase),
  282. sta->sae) < 0) {
  283. wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
  284. return NULL;
  285. }
  286. if (sae_process_commit(sta->sae) < 0) {
  287. wpa_printf(MSG_DEBUG, "SAE: Failed to process peer commit");
  288. return NULL;
  289. }
  290. buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
  291. if (buf == NULL)
  292. return NULL;
  293. sae_write_commit(sta->sae, buf, NULL);
  294. return buf;
  295. }
  296. static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
  297. struct sta_info *sta)
  298. {
  299. struct wpabuf *buf;
  300. buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
  301. if (buf == NULL)
  302. return NULL;
  303. sae_write_confirm(sta->sae, buf);
  304. return buf;
  305. }
  306. static int use_sae_anti_clogging(struct hostapd_data *hapd)
  307. {
  308. struct sta_info *sta;
  309. unsigned int open = 0;
  310. if (hapd->conf->sae_anti_clogging_threshold == 0)
  311. return 1;
  312. for (sta = hapd->sta_list; sta; sta = sta->next) {
  313. if (!sta->sae)
  314. continue;
  315. if (sta->sae->state != SAE_COMMITTED &&
  316. sta->sae->state != SAE_CONFIRMED)
  317. continue;
  318. open++;
  319. if (open >= hapd->conf->sae_anti_clogging_threshold)
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
  325. const u8 *token, size_t token_len)
  326. {
  327. u8 mac[SHA256_MAC_LEN];
  328. if (token_len != SHA256_MAC_LEN)
  329. return -1;
  330. if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
  331. addr, ETH_ALEN, mac) < 0 ||
  332. os_memcmp(token, mac, SHA256_MAC_LEN) != 0)
  333. return -1;
  334. return 0;
  335. }
  336. static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
  337. const u8 *addr)
  338. {
  339. struct wpabuf *buf;
  340. u8 *token;
  341. struct os_reltime now;
  342. os_get_reltime(&now);
  343. if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
  344. os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60)) {
  345. if (random_get_bytes(hapd->sae_token_key,
  346. sizeof(hapd->sae_token_key)) < 0)
  347. return NULL;
  348. wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
  349. hapd->sae_token_key, sizeof(hapd->sae_token_key));
  350. hapd->last_sae_token_key_update = now;
  351. }
  352. buf = wpabuf_alloc(SHA256_MAC_LEN);
  353. if (buf == NULL)
  354. return NULL;
  355. token = wpabuf_put(buf, SHA256_MAC_LEN);
  356. hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
  357. addr, ETH_ALEN, token);
  358. return buf;
  359. }
  360. static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
  361. const struct ieee80211_mgmt *mgmt, size_t len,
  362. u8 auth_transaction)
  363. {
  364. u16 resp = WLAN_STATUS_SUCCESS;
  365. struct wpabuf *data = NULL;
  366. if (!sta->sae) {
  367. if (auth_transaction != 1)
  368. return;
  369. sta->sae = os_zalloc(sizeof(*sta->sae));
  370. if (sta->sae == NULL)
  371. return;
  372. sta->sae->state = SAE_NOTHING;
  373. }
  374. if (auth_transaction == 1) {
  375. const u8 *token = NULL;
  376. size_t token_len = 0;
  377. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  378. HOSTAPD_LEVEL_DEBUG,
  379. "start SAE authentication (RX commit)");
  380. resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
  381. ((const u8 *) mgmt) + len -
  382. mgmt->u.auth.variable, &token,
  383. &token_len, hapd->conf->sae_groups);
  384. if (token && check_sae_token(hapd, sta->addr, token, token_len)
  385. < 0) {
  386. wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
  387. "incorrect token from " MACSTR,
  388. MAC2STR(sta->addr));
  389. return;
  390. }
  391. if (resp == WLAN_STATUS_SUCCESS) {
  392. if (!token && use_sae_anti_clogging(hapd)) {
  393. wpa_printf(MSG_DEBUG, "SAE: Request anti-"
  394. "clogging token from " MACSTR,
  395. MAC2STR(sta->addr));
  396. data = auth_build_token_req(hapd, sta->addr);
  397. resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
  398. } else {
  399. data = auth_process_sae_commit(hapd, sta);
  400. if (data == NULL)
  401. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  402. else
  403. sta->sae->state = SAE_COMMITTED;
  404. }
  405. }
  406. } else if (auth_transaction == 2) {
  407. if (sta->sae->state != SAE_COMMITTED) {
  408. hostapd_logger(hapd, sta->addr,
  409. HOSTAPD_MODULE_IEEE80211,
  410. HOSTAPD_LEVEL_DEBUG,
  411. "SAE confirm before commit");
  412. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  413. goto failed;
  414. }
  415. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  416. HOSTAPD_LEVEL_DEBUG,
  417. "SAE authentication (RX confirm)");
  418. if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
  419. ((u8 *) mgmt) + len -
  420. mgmt->u.auth.variable) < 0) {
  421. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  422. } else {
  423. resp = WLAN_STATUS_SUCCESS;
  424. sta->flags |= WLAN_STA_AUTH;
  425. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  426. sta->auth_alg = WLAN_AUTH_SAE;
  427. mlme_authenticate_indication(hapd, sta);
  428. data = auth_build_sae_confirm(hapd, sta);
  429. if (data == NULL)
  430. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  431. else {
  432. sta->sae->state = SAE_ACCEPTED;
  433. sae_clear_temp_data(sta->sae);
  434. }
  435. }
  436. } else {
  437. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  438. HOSTAPD_LEVEL_DEBUG,
  439. "unexpected SAE authentication transaction %u",
  440. auth_transaction);
  441. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  442. }
  443. failed:
  444. sta->auth_alg = WLAN_AUTH_SAE;
  445. send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
  446. auth_transaction, resp,
  447. data ? wpabuf_head(data) : (u8 *) "",
  448. data ? wpabuf_len(data) : 0);
  449. wpabuf_free(data);
  450. }
  451. #endif /* CONFIG_SAE */
  452. static void handle_auth(struct hostapd_data *hapd,
  453. const struct ieee80211_mgmt *mgmt, size_t len)
  454. {
  455. u16 auth_alg, auth_transaction, status_code;
  456. u16 resp = WLAN_STATUS_SUCCESS;
  457. struct sta_info *sta = NULL;
  458. int res;
  459. u16 fc;
  460. const u8 *challenge = NULL;
  461. u32 session_timeout, acct_interim_interval;
  462. int vlan_id = 0;
  463. struct hostapd_sta_wpa_psk_short *psk = NULL;
  464. u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
  465. size_t resp_ies_len = 0;
  466. char *identity = NULL;
  467. char *radius_cui = NULL;
  468. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  469. wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
  470. (unsigned long) len);
  471. return;
  472. }
  473. #ifdef CONFIG_TESTING_OPTIONS
  474. if (hapd->iconf->ignore_auth_probability > 0.0d &&
  475. drand48() < hapd->iconf->ignore_auth_probability) {
  476. wpa_printf(MSG_INFO,
  477. "TESTING: ignoring auth frame from " MACSTR,
  478. MAC2STR(mgmt->sa));
  479. return;
  480. }
  481. #endif /* CONFIG_TESTING_OPTIONS */
  482. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  483. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  484. status_code = le_to_host16(mgmt->u.auth.status_code);
  485. fc = le_to_host16(mgmt->frame_control);
  486. if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
  487. 2 + WLAN_AUTH_CHALLENGE_LEN &&
  488. mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
  489. mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
  490. challenge = &mgmt->u.auth.variable[2];
  491. wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
  492. "auth_transaction=%d status_code=%d wep=%d%s",
  493. MAC2STR(mgmt->sa), auth_alg, auth_transaction,
  494. status_code, !!(fc & WLAN_FC_ISWEP),
  495. challenge ? " challenge" : "");
  496. if (hapd->tkip_countermeasures) {
  497. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  498. goto fail;
  499. }
  500. if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
  501. auth_alg == WLAN_AUTH_OPEN) ||
  502. #ifdef CONFIG_IEEE80211R
  503. (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
  504. auth_alg == WLAN_AUTH_FT) ||
  505. #endif /* CONFIG_IEEE80211R */
  506. #ifdef CONFIG_SAE
  507. (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
  508. auth_alg == WLAN_AUTH_SAE) ||
  509. #endif /* CONFIG_SAE */
  510. ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
  511. auth_alg == WLAN_AUTH_SHARED_KEY))) {
  512. wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
  513. auth_alg);
  514. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  515. goto fail;
  516. }
  517. if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
  518. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
  519. wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
  520. auth_transaction);
  521. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  522. goto fail;
  523. }
  524. if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
  525. wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
  526. MAC2STR(mgmt->sa));
  527. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  528. goto fail;
  529. }
  530. res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
  531. &session_timeout,
  532. &acct_interim_interval, &vlan_id,
  533. &psk, &identity, &radius_cui);
  534. if (res == HOSTAPD_ACL_REJECT) {
  535. wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
  536. MAC2STR(mgmt->sa));
  537. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  538. goto fail;
  539. }
  540. if (res == HOSTAPD_ACL_PENDING) {
  541. wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
  542. " waiting for an external authentication",
  543. MAC2STR(mgmt->sa));
  544. /* Authentication code will re-send the authentication frame
  545. * after it has received (and cached) information from the
  546. * external source. */
  547. return;
  548. }
  549. sta = ap_sta_add(hapd, mgmt->sa);
  550. if (!sta) {
  551. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  552. goto fail;
  553. }
  554. if (vlan_id > 0) {
  555. if (!hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
  556. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  557. HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
  558. "%d received from RADIUS server",
  559. vlan_id);
  560. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  561. goto fail;
  562. }
  563. sta->vlan_id = vlan_id;
  564. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  565. HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
  566. }
  567. hostapd_free_psk_list(sta->psk);
  568. if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
  569. sta->psk = psk;
  570. psk = NULL;
  571. } else {
  572. sta->psk = NULL;
  573. }
  574. sta->identity = identity;
  575. identity = NULL;
  576. sta->radius_cui = radius_cui;
  577. radius_cui = NULL;
  578. sta->flags &= ~WLAN_STA_PREAUTH;
  579. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  580. if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
  581. sta->acct_interim_interval = acct_interim_interval;
  582. if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  583. ap_sta_session_timeout(hapd, sta, session_timeout);
  584. else
  585. ap_sta_no_session_timeout(hapd, sta);
  586. switch (auth_alg) {
  587. case WLAN_AUTH_OPEN:
  588. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  589. HOSTAPD_LEVEL_DEBUG,
  590. "authentication OK (open system)");
  591. sta->flags |= WLAN_STA_AUTH;
  592. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  593. sta->auth_alg = WLAN_AUTH_OPEN;
  594. mlme_authenticate_indication(hapd, sta);
  595. break;
  596. case WLAN_AUTH_SHARED_KEY:
  597. resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
  598. fc & WLAN_FC_ISWEP);
  599. sta->auth_alg = WLAN_AUTH_SHARED_KEY;
  600. mlme_authenticate_indication(hapd, sta);
  601. if (sta->challenge && auth_transaction == 1) {
  602. resp_ies[0] = WLAN_EID_CHALLENGE;
  603. resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
  604. os_memcpy(resp_ies + 2, sta->challenge,
  605. WLAN_AUTH_CHALLENGE_LEN);
  606. resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
  607. }
  608. break;
  609. #ifdef CONFIG_IEEE80211R
  610. case WLAN_AUTH_FT:
  611. sta->auth_alg = WLAN_AUTH_FT;
  612. if (sta->wpa_sm == NULL)
  613. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  614. sta->addr, NULL);
  615. if (sta->wpa_sm == NULL) {
  616. wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
  617. "state machine");
  618. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  619. goto fail;
  620. }
  621. wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
  622. auth_transaction, mgmt->u.auth.variable,
  623. len - IEEE80211_HDRLEN -
  624. sizeof(mgmt->u.auth),
  625. handle_auth_ft_finish, hapd);
  626. /* handle_auth_ft_finish() callback will complete auth. */
  627. return;
  628. #endif /* CONFIG_IEEE80211R */
  629. #ifdef CONFIG_SAE
  630. case WLAN_AUTH_SAE:
  631. handle_auth_sae(hapd, sta, mgmt, len, auth_transaction);
  632. return;
  633. #endif /* CONFIG_SAE */
  634. }
  635. fail:
  636. os_free(identity);
  637. os_free(radius_cui);
  638. hostapd_free_psk_list(psk);
  639. send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
  640. auth_transaction + 1, resp, resp_ies, resp_ies_len);
  641. }
  642. static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
  643. {
  644. int i, j = 32, aid;
  645. /* get a unique AID */
  646. if (sta->aid > 0) {
  647. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  648. return 0;
  649. }
  650. for (i = 0; i < AID_WORDS; i++) {
  651. if (hapd->sta_aid[i] == (u32) -1)
  652. continue;
  653. for (j = 0; j < 32; j++) {
  654. if (!(hapd->sta_aid[i] & BIT(j)))
  655. break;
  656. }
  657. if (j < 32)
  658. break;
  659. }
  660. if (j == 32)
  661. return -1;
  662. aid = i * 32 + j + 1;
  663. if (aid > 2007)
  664. return -1;
  665. sta->aid = aid;
  666. hapd->sta_aid[i] |= BIT(j);
  667. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  668. return 0;
  669. }
  670. static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
  671. const u8 *ssid_ie, size_t ssid_ie_len)
  672. {
  673. if (ssid_ie == NULL)
  674. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  675. if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
  676. os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
  677. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  678. HOSTAPD_LEVEL_INFO,
  679. "Station tried to associate with unknown SSID "
  680. "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
  681. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  682. }
  683. return WLAN_STATUS_SUCCESS;
  684. }
  685. static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
  686. const u8 *wmm_ie, size_t wmm_ie_len)
  687. {
  688. sta->flags &= ~WLAN_STA_WMM;
  689. sta->qosinfo = 0;
  690. if (wmm_ie && hapd->conf->wmm_enabled) {
  691. struct wmm_information_element *wmm;
  692. if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
  693. hostapd_logger(hapd, sta->addr,
  694. HOSTAPD_MODULE_WPA,
  695. HOSTAPD_LEVEL_DEBUG,
  696. "invalid WMM element in association "
  697. "request");
  698. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  699. }
  700. sta->flags |= WLAN_STA_WMM;
  701. wmm = (struct wmm_information_element *) wmm_ie;
  702. sta->qosinfo = wmm->qos_info;
  703. }
  704. return WLAN_STATUS_SUCCESS;
  705. }
  706. static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
  707. struct ieee802_11_elems *elems)
  708. {
  709. if (!elems->supp_rates) {
  710. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  711. HOSTAPD_LEVEL_DEBUG,
  712. "No supported rates element in AssocReq");
  713. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  714. }
  715. if (elems->supp_rates_len + elems->ext_supp_rates_len >
  716. sizeof(sta->supported_rates)) {
  717. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  718. HOSTAPD_LEVEL_DEBUG,
  719. "Invalid supported rates element length %d+%d",
  720. elems->supp_rates_len,
  721. elems->ext_supp_rates_len);
  722. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  723. }
  724. sta->supported_rates_len = merge_byte_arrays(
  725. sta->supported_rates, sizeof(sta->supported_rates),
  726. elems->supp_rates, elems->supp_rates_len,
  727. elems->ext_supp_rates, elems->ext_supp_rates_len);
  728. return WLAN_STATUS_SUCCESS;
  729. }
  730. static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
  731. const u8 *ext_capab_ie, size_t ext_capab_ie_len)
  732. {
  733. #ifdef CONFIG_INTERWORKING
  734. /* check for QoS Map support */
  735. if (ext_capab_ie_len >= 5) {
  736. if (ext_capab_ie[4] & 0x01)
  737. sta->qos_map_enabled = 1;
  738. }
  739. #endif /* CONFIG_INTERWORKING */
  740. return WLAN_STATUS_SUCCESS;
  741. }
  742. static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
  743. const u8 *ies, size_t ies_len, int reassoc)
  744. {
  745. struct ieee802_11_elems elems;
  746. u16 resp;
  747. const u8 *wpa_ie;
  748. size_t wpa_ie_len;
  749. const u8 *p2p_dev_addr = NULL;
  750. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
  751. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  752. HOSTAPD_LEVEL_INFO, "Station sent an invalid "
  753. "association request");
  754. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  755. }
  756. resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
  757. if (resp != WLAN_STATUS_SUCCESS)
  758. return resp;
  759. resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
  760. if (resp != WLAN_STATUS_SUCCESS)
  761. return resp;
  762. resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
  763. if (resp != WLAN_STATUS_SUCCESS)
  764. return resp;
  765. resp = copy_supp_rates(hapd, sta, &elems);
  766. if (resp != WLAN_STATUS_SUCCESS)
  767. return resp;
  768. #ifdef CONFIG_IEEE80211N
  769. resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
  770. elems.ht_capabilities_len);
  771. if (resp != WLAN_STATUS_SUCCESS)
  772. return resp;
  773. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
  774. !(sta->flags & WLAN_STA_HT)) {
  775. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  776. HOSTAPD_LEVEL_INFO, "Station does not support "
  777. "mandatory HT PHY - reject association");
  778. return WLAN_STATUS_ASSOC_DENIED_NO_HT;
  779. }
  780. #endif /* CONFIG_IEEE80211N */
  781. #ifdef CONFIG_IEEE80211AC
  782. resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
  783. elems.vht_capabilities_len);
  784. if (resp != WLAN_STATUS_SUCCESS)
  785. return resp;
  786. resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
  787. if (resp != WLAN_STATUS_SUCCESS)
  788. return resp;
  789. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
  790. !(sta->flags & WLAN_STA_VHT)) {
  791. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  792. HOSTAPD_LEVEL_INFO, "Station does not support "
  793. "mandatory VHT PHY - reject association");
  794. return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
  795. }
  796. #endif /* CONFIG_IEEE80211AC */
  797. #ifdef CONFIG_P2P
  798. if (elems.p2p) {
  799. wpabuf_free(sta->p2p_ie);
  800. sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  801. P2P_IE_VENDOR_TYPE);
  802. if (sta->p2p_ie)
  803. p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
  804. } else {
  805. wpabuf_free(sta->p2p_ie);
  806. sta->p2p_ie = NULL;
  807. }
  808. #endif /* CONFIG_P2P */
  809. if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
  810. wpa_ie = elems.rsn_ie;
  811. wpa_ie_len = elems.rsn_ie_len;
  812. } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
  813. elems.wpa_ie) {
  814. wpa_ie = elems.wpa_ie;
  815. wpa_ie_len = elems.wpa_ie_len;
  816. } else {
  817. wpa_ie = NULL;
  818. wpa_ie_len = 0;
  819. }
  820. #ifdef CONFIG_WPS
  821. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
  822. if (hapd->conf->wps_state && elems.wps_ie) {
  823. wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
  824. "Request - assume WPS is used");
  825. sta->flags |= WLAN_STA_WPS;
  826. wpabuf_free(sta->wps_ie);
  827. sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  828. WPS_IE_VENDOR_TYPE);
  829. if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
  830. wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
  831. sta->flags |= WLAN_STA_WPS2;
  832. }
  833. wpa_ie = NULL;
  834. wpa_ie_len = 0;
  835. if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
  836. wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
  837. "(Re)Association Request - reject");
  838. return WLAN_STATUS_INVALID_IE;
  839. }
  840. } else if (hapd->conf->wps_state && wpa_ie == NULL) {
  841. wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
  842. "(Re)Association Request - possible WPS use");
  843. sta->flags |= WLAN_STA_MAYBE_WPS;
  844. } else
  845. #endif /* CONFIG_WPS */
  846. if (hapd->conf->wpa && wpa_ie == NULL) {
  847. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  848. HOSTAPD_LEVEL_INFO,
  849. "No WPA/RSN IE in association request");
  850. return WLAN_STATUS_INVALID_IE;
  851. }
  852. if (hapd->conf->wpa && wpa_ie) {
  853. int res;
  854. wpa_ie -= 2;
  855. wpa_ie_len += 2;
  856. if (sta->wpa_sm == NULL)
  857. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  858. sta->addr,
  859. p2p_dev_addr);
  860. if (sta->wpa_sm == NULL) {
  861. wpa_printf(MSG_WARNING, "Failed to initialize WPA "
  862. "state machine");
  863. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  864. }
  865. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  866. wpa_ie, wpa_ie_len,
  867. elems.mdie, elems.mdie_len);
  868. if (res == WPA_INVALID_GROUP)
  869. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  870. else if (res == WPA_INVALID_PAIRWISE)
  871. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  872. else if (res == WPA_INVALID_AKMP)
  873. resp = WLAN_STATUS_AKMP_NOT_VALID;
  874. else if (res == WPA_ALLOC_FAIL)
  875. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  876. #ifdef CONFIG_IEEE80211W
  877. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  878. resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
  879. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  880. resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
  881. #endif /* CONFIG_IEEE80211W */
  882. else if (res == WPA_INVALID_MDIE)
  883. resp = WLAN_STATUS_INVALID_MDIE;
  884. else if (res != WPA_IE_OK)
  885. resp = WLAN_STATUS_INVALID_IE;
  886. if (resp != WLAN_STATUS_SUCCESS)
  887. return resp;
  888. #ifdef CONFIG_IEEE80211W
  889. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  890. sta->sa_query_count > 0)
  891. ap_check_sa_query_timeout(hapd, sta);
  892. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  893. (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
  894. /*
  895. * STA has already been associated with MFP and SA
  896. * Query timeout has not been reached. Reject the
  897. * association attempt temporarily and start SA Query,
  898. * if one is not pending.
  899. */
  900. if (sta->sa_query_count == 0)
  901. ap_sta_start_sa_query(hapd, sta);
  902. return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
  903. }
  904. if (wpa_auth_uses_mfp(sta->wpa_sm))
  905. sta->flags |= WLAN_STA_MFP;
  906. else
  907. sta->flags &= ~WLAN_STA_MFP;
  908. #endif /* CONFIG_IEEE80211W */
  909. #ifdef CONFIG_IEEE80211R
  910. if (sta->auth_alg == WLAN_AUTH_FT) {
  911. if (!reassoc) {
  912. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  913. "to use association (not "
  914. "re-association) with FT auth_alg",
  915. MAC2STR(sta->addr));
  916. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  917. }
  918. resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
  919. ies_len);
  920. if (resp != WLAN_STATUS_SUCCESS)
  921. return resp;
  922. }
  923. #endif /* CONFIG_IEEE80211R */
  924. #ifdef CONFIG_SAE
  925. if (wpa_auth_uses_sae(sta->wpa_sm) &&
  926. sta->auth_alg != WLAN_AUTH_SAE &&
  927. !(sta->auth_alg == WLAN_AUTH_FT &&
  928. wpa_auth_uses_ft_sae(sta->wpa_sm))) {
  929. wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
  930. "SAE AKM after non-SAE auth_alg %u",
  931. MAC2STR(sta->addr), sta->auth_alg);
  932. return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  933. }
  934. #endif /* CONFIG_SAE */
  935. #ifdef CONFIG_IEEE80211N
  936. if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
  937. wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
  938. hostapd_logger(hapd, sta->addr,
  939. HOSTAPD_MODULE_IEEE80211,
  940. HOSTAPD_LEVEL_INFO,
  941. "Station tried to use TKIP with HT "
  942. "association");
  943. return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
  944. }
  945. #endif /* CONFIG_IEEE80211N */
  946. #ifdef CONFIG_HS20
  947. } else if (hapd->conf->osen) {
  948. if (elems.osen == NULL) {
  949. hostapd_logger(
  950. hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  951. HOSTAPD_LEVEL_INFO,
  952. "No HS 2.0 OSEN element in association request");
  953. return WLAN_STATUS_INVALID_IE;
  954. }
  955. wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
  956. if (sta->wpa_sm == NULL)
  957. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  958. sta->addr, NULL);
  959. if (sta->wpa_sm == NULL) {
  960. wpa_printf(MSG_WARNING, "Failed to initialize WPA "
  961. "state machine");
  962. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  963. }
  964. if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
  965. elems.osen - 2, elems.osen_len + 2) < 0)
  966. return WLAN_STATUS_INVALID_IE;
  967. #endif /* CONFIG_HS20 */
  968. } else
  969. wpa_auth_sta_no_wpa(sta->wpa_sm);
  970. #ifdef CONFIG_P2P
  971. p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
  972. #endif /* CONFIG_P2P */
  973. #ifdef CONFIG_HS20
  974. wpabuf_free(sta->hs20_ie);
  975. if (elems.hs20 && elems.hs20_len > 4) {
  976. sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
  977. elems.hs20_len - 4);
  978. } else
  979. sta->hs20_ie = NULL;
  980. #endif /* CONFIG_HS20 */
  981. return WLAN_STATUS_SUCCESS;
  982. }
  983. static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
  984. u16 reason_code)
  985. {
  986. int send_len;
  987. struct ieee80211_mgmt reply;
  988. os_memset(&reply, 0, sizeof(reply));
  989. reply.frame_control =
  990. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
  991. os_memcpy(reply.da, addr, ETH_ALEN);
  992. os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
  993. os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
  994. send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
  995. reply.u.deauth.reason_code = host_to_le16(reason_code);
  996. if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
  997. wpa_printf(MSG_INFO, "Failed to send deauth: %s",
  998. strerror(errno));
  999. }
  1000. static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
  1001. u16 status_code, int reassoc, const u8 *ies,
  1002. size_t ies_len)
  1003. {
  1004. int send_len;
  1005. u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
  1006. struct ieee80211_mgmt *reply;
  1007. u8 *p;
  1008. os_memset(buf, 0, sizeof(buf));
  1009. reply = (struct ieee80211_mgmt *) buf;
  1010. reply->frame_control =
  1011. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1012. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  1013. WLAN_FC_STYPE_ASSOC_RESP));
  1014. os_memcpy(reply->da, sta->addr, ETH_ALEN);
  1015. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  1016. os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
  1017. send_len = IEEE80211_HDRLEN;
  1018. send_len += sizeof(reply->u.assoc_resp);
  1019. reply->u.assoc_resp.capab_info =
  1020. host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
  1021. reply->u.assoc_resp.status_code = host_to_le16(status_code);
  1022. reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
  1023. /* Supported rates */
  1024. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  1025. /* Extended supported rates */
  1026. p = hostapd_eid_ext_supp_rates(hapd, p);
  1027. #ifdef CONFIG_IEEE80211R
  1028. if (status_code == WLAN_STATUS_SUCCESS) {
  1029. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  1030. * Transition Information, RSN, [RIC Response] */
  1031. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  1032. buf + sizeof(buf) - p,
  1033. sta->auth_alg, ies, ies_len);
  1034. }
  1035. #endif /* CONFIG_IEEE80211R */
  1036. #ifdef CONFIG_IEEE80211W
  1037. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
  1038. p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
  1039. #endif /* CONFIG_IEEE80211W */
  1040. #ifdef CONFIG_IEEE80211N
  1041. p = hostapd_eid_ht_capabilities(hapd, p);
  1042. p = hostapd_eid_ht_operation(hapd, p);
  1043. #endif /* CONFIG_IEEE80211N */
  1044. #ifdef CONFIG_IEEE80211AC
  1045. p = hostapd_eid_vht_capabilities(hapd, p);
  1046. p = hostapd_eid_vht_operation(hapd, p);
  1047. #endif /* CONFIG_IEEE80211AC */
  1048. p = hostapd_eid_ext_capab(hapd, p);
  1049. p = hostapd_eid_bss_max_idle_period(hapd, p);
  1050. if (sta->qos_map_enabled)
  1051. p = hostapd_eid_qos_map_set(hapd, p);
  1052. if (sta->flags & WLAN_STA_WMM)
  1053. p = hostapd_eid_wmm(hapd, p);
  1054. #ifdef CONFIG_WPS
  1055. if ((sta->flags & WLAN_STA_WPS) ||
  1056. ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
  1057. struct wpabuf *wps = wps_build_assoc_resp_ie();
  1058. if (wps) {
  1059. os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
  1060. p += wpabuf_len(wps);
  1061. wpabuf_free(wps);
  1062. }
  1063. }
  1064. #endif /* CONFIG_WPS */
  1065. #ifdef CONFIG_P2P
  1066. if (sta->p2p_ie) {
  1067. struct wpabuf *p2p_resp_ie;
  1068. enum p2p_status_code status;
  1069. switch (status_code) {
  1070. case WLAN_STATUS_SUCCESS:
  1071. status = P2P_SC_SUCCESS;
  1072. break;
  1073. case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
  1074. status = P2P_SC_FAIL_LIMIT_REACHED;
  1075. break;
  1076. default:
  1077. status = P2P_SC_FAIL_INVALID_PARAMS;
  1078. break;
  1079. }
  1080. p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
  1081. if (p2p_resp_ie) {
  1082. os_memcpy(p, wpabuf_head(p2p_resp_ie),
  1083. wpabuf_len(p2p_resp_ie));
  1084. p += wpabuf_len(p2p_resp_ie);
  1085. wpabuf_free(p2p_resp_ie);
  1086. }
  1087. }
  1088. #endif /* CONFIG_P2P */
  1089. #ifdef CONFIG_P2P_MANAGER
  1090. if (hapd->conf->p2p & P2P_MANAGE)
  1091. p = hostapd_eid_p2p_manage(hapd, p);
  1092. #endif /* CONFIG_P2P_MANAGER */
  1093. send_len += p - reply->u.assoc_resp.variable;
  1094. if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
  1095. wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
  1096. strerror(errno));
  1097. }
  1098. static void handle_assoc(struct hostapd_data *hapd,
  1099. const struct ieee80211_mgmt *mgmt, size_t len,
  1100. int reassoc)
  1101. {
  1102. u16 capab_info, listen_interval;
  1103. u16 resp = WLAN_STATUS_SUCCESS;
  1104. const u8 *pos;
  1105. int left, i;
  1106. struct sta_info *sta;
  1107. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
  1108. sizeof(mgmt->u.assoc_req))) {
  1109. wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
  1110. reassoc, (unsigned long) len);
  1111. return;
  1112. }
  1113. #ifdef CONFIG_TESTING_OPTIONS
  1114. if (reassoc) {
  1115. if (hapd->iconf->ignore_reassoc_probability > 0.0d &&
  1116. drand48() < hapd->iconf->ignore_reassoc_probability) {
  1117. wpa_printf(MSG_INFO,
  1118. "TESTING: ignoring reassoc request from "
  1119. MACSTR, MAC2STR(mgmt->sa));
  1120. return;
  1121. }
  1122. } else {
  1123. if (hapd->iconf->ignore_assoc_probability > 0.0d &&
  1124. drand48() < hapd->iconf->ignore_assoc_probability) {
  1125. wpa_printf(MSG_INFO,
  1126. "TESTING: ignoring assoc request from "
  1127. MACSTR, MAC2STR(mgmt->sa));
  1128. return;
  1129. }
  1130. }
  1131. #endif /* CONFIG_TESTING_OPTIONS */
  1132. if (reassoc) {
  1133. capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
  1134. listen_interval = le_to_host16(
  1135. mgmt->u.reassoc_req.listen_interval);
  1136. wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
  1137. " capab_info=0x%02x listen_interval=%d current_ap="
  1138. MACSTR,
  1139. MAC2STR(mgmt->sa), capab_info, listen_interval,
  1140. MAC2STR(mgmt->u.reassoc_req.current_ap));
  1141. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
  1142. pos = mgmt->u.reassoc_req.variable;
  1143. } else {
  1144. capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  1145. listen_interval = le_to_host16(
  1146. mgmt->u.assoc_req.listen_interval);
  1147. wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
  1148. " capab_info=0x%02x listen_interval=%d",
  1149. MAC2STR(mgmt->sa), capab_info, listen_interval);
  1150. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
  1151. pos = mgmt->u.assoc_req.variable;
  1152. }
  1153. sta = ap_get_sta(hapd, mgmt->sa);
  1154. #ifdef CONFIG_IEEE80211R
  1155. if (sta && sta->auth_alg == WLAN_AUTH_FT &&
  1156. (sta->flags & WLAN_STA_AUTH) == 0) {
  1157. wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
  1158. "prior to authentication since it is using "
  1159. "over-the-DS FT", MAC2STR(mgmt->sa));
  1160. } else
  1161. #endif /* CONFIG_IEEE80211R */
  1162. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  1163. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1164. HOSTAPD_LEVEL_INFO, "Station tried to "
  1165. "associate before authentication "
  1166. "(aid=%d flags=0x%x)",
  1167. sta ? sta->aid : -1,
  1168. sta ? sta->flags : 0);
  1169. send_deauth(hapd, mgmt->sa,
  1170. WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
  1171. return;
  1172. }
  1173. if (hapd->tkip_countermeasures) {
  1174. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  1175. goto fail;
  1176. }
  1177. if (listen_interval > hapd->conf->max_listen_interval) {
  1178. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1179. HOSTAPD_LEVEL_DEBUG,
  1180. "Too large Listen Interval (%d)",
  1181. listen_interval);
  1182. resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
  1183. goto fail;
  1184. }
  1185. /* followed by SSID and Supported rates; and HT capabilities if 802.11n
  1186. * is used */
  1187. resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
  1188. if (resp != WLAN_STATUS_SUCCESS)
  1189. goto fail;
  1190. if (hostapd_get_aid(hapd, sta) < 0) {
  1191. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1192. HOSTAPD_LEVEL_INFO, "No room for more AIDs");
  1193. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  1194. goto fail;
  1195. }
  1196. sta->capability = capab_info;
  1197. sta->listen_interval = listen_interval;
  1198. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  1199. sta->flags |= WLAN_STA_NONERP;
  1200. for (i = 0; i < sta->supported_rates_len; i++) {
  1201. if ((sta->supported_rates[i] & 0x7f) > 22) {
  1202. sta->flags &= ~WLAN_STA_NONERP;
  1203. break;
  1204. }
  1205. }
  1206. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  1207. sta->nonerp_set = 1;
  1208. hapd->iface->num_sta_non_erp++;
  1209. if (hapd->iface->num_sta_non_erp == 1)
  1210. ieee802_11_set_beacons(hapd->iface);
  1211. }
  1212. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  1213. !sta->no_short_slot_time_set) {
  1214. sta->no_short_slot_time_set = 1;
  1215. hapd->iface->num_sta_no_short_slot_time++;
  1216. if (hapd->iface->current_mode->mode ==
  1217. HOSTAPD_MODE_IEEE80211G &&
  1218. hapd->iface->num_sta_no_short_slot_time == 1)
  1219. ieee802_11_set_beacons(hapd->iface);
  1220. }
  1221. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  1222. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  1223. else
  1224. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  1225. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  1226. !sta->no_short_preamble_set) {
  1227. sta->no_short_preamble_set = 1;
  1228. hapd->iface->num_sta_no_short_preamble++;
  1229. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  1230. && hapd->iface->num_sta_no_short_preamble == 1)
  1231. ieee802_11_set_beacons(hapd->iface);
  1232. }
  1233. #ifdef CONFIG_IEEE80211N
  1234. update_ht_state(hapd, sta);
  1235. #endif /* CONFIG_IEEE80211N */
  1236. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1237. HOSTAPD_LEVEL_DEBUG,
  1238. "association OK (aid %d)", sta->aid);
  1239. /* Station will be marked associated, after it acknowledges AssocResp
  1240. */
  1241. sta->flags |= WLAN_STA_ASSOC_REQ_OK;
  1242. #ifdef CONFIG_IEEE80211W
  1243. if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
  1244. wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
  1245. "SA Query procedure", reassoc ? "re" : "");
  1246. /* TODO: Send a protected Disassociate frame to the STA using
  1247. * the old key and Reason Code "Previous Authentication no
  1248. * longer valid". Make sure this is only sent protected since
  1249. * unprotected frame would be received by the STA that is now
  1250. * trying to associate.
  1251. */
  1252. }
  1253. #endif /* CONFIG_IEEE80211W */
  1254. /* Make sure that the previously registered inactivity timer will not
  1255. * remove the STA immediately. */
  1256. sta->timeout_next = STA_NULLFUNC;
  1257. fail:
  1258. send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
  1259. }
  1260. static void handle_disassoc(struct hostapd_data *hapd,
  1261. const struct ieee80211_mgmt *mgmt, size_t len)
  1262. {
  1263. struct sta_info *sta;
  1264. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  1265. wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
  1266. (unsigned long) len);
  1267. return;
  1268. }
  1269. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  1270. MAC2STR(mgmt->sa),
  1271. le_to_host16(mgmt->u.disassoc.reason_code));
  1272. sta = ap_get_sta(hapd, mgmt->sa);
  1273. if (sta == NULL) {
  1274. wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
  1275. MAC2STR(mgmt->sa));
  1276. return;
  1277. }
  1278. ap_sta_set_authorized(hapd, sta, 0);
  1279. sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
  1280. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  1281. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1282. HOSTAPD_LEVEL_INFO, "disassociated");
  1283. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1284. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1285. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  1286. * authenticated. */
  1287. accounting_sta_stop(hapd, sta);
  1288. ieee802_1x_free_station(sta);
  1289. hostapd_drv_sta_remove(hapd, sta->addr);
  1290. if (sta->timeout_next == STA_NULLFUNC ||
  1291. sta->timeout_next == STA_DISASSOC) {
  1292. sta->timeout_next = STA_DEAUTH;
  1293. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1294. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  1295. hapd, sta);
  1296. }
  1297. mlme_disassociate_indication(
  1298. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  1299. }
  1300. static void handle_deauth(struct hostapd_data *hapd,
  1301. const struct ieee80211_mgmt *mgmt, size_t len)
  1302. {
  1303. struct sta_info *sta;
  1304. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  1305. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
  1306. "payload (len=%lu)", (unsigned long) len);
  1307. return;
  1308. }
  1309. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
  1310. " reason_code=%d",
  1311. MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
  1312. sta = ap_get_sta(hapd, mgmt->sa);
  1313. if (sta == NULL) {
  1314. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
  1315. "to deauthenticate, but it is not authenticated",
  1316. MAC2STR(mgmt->sa));
  1317. return;
  1318. }
  1319. ap_sta_set_authorized(hapd, sta, 0);
  1320. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1321. WLAN_STA_ASSOC_REQ_OK);
  1322. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1323. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1324. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  1325. mlme_deauthenticate_indication(
  1326. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  1327. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1328. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1329. ap_free_sta(hapd, sta);
  1330. }
  1331. static void handle_beacon(struct hostapd_data *hapd,
  1332. const struct ieee80211_mgmt *mgmt, size_t len,
  1333. struct hostapd_frame_info *fi)
  1334. {
  1335. struct ieee802_11_elems elems;
  1336. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  1337. wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
  1338. (unsigned long) len);
  1339. return;
  1340. }
  1341. (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1342. len - (IEEE80211_HDRLEN +
  1343. sizeof(mgmt->u.beacon)), &elems,
  1344. 0);
  1345. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  1346. }
  1347. #ifdef CONFIG_IEEE80211W
  1348. static int hostapd_sa_query_action(struct hostapd_data *hapd,
  1349. const struct ieee80211_mgmt *mgmt,
  1350. size_t len)
  1351. {
  1352. const u8 *end;
  1353. end = mgmt->u.action.u.sa_query_resp.trans_id +
  1354. WLAN_SA_QUERY_TR_ID_LEN;
  1355. if (((u8 *) mgmt) + len < end) {
  1356. wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
  1357. "frame (len=%lu)", (unsigned long) len);
  1358. return 0;
  1359. }
  1360. ieee802_11_sa_query_action(hapd, mgmt->sa,
  1361. mgmt->u.action.u.sa_query_resp.action,
  1362. mgmt->u.action.u.sa_query_resp.trans_id);
  1363. return 1;
  1364. }
  1365. static int robust_action_frame(u8 category)
  1366. {
  1367. return category != WLAN_ACTION_PUBLIC &&
  1368. category != WLAN_ACTION_HT;
  1369. }
  1370. #endif /* CONFIG_IEEE80211W */
  1371. static int handle_action(struct hostapd_data *hapd,
  1372. const struct ieee80211_mgmt *mgmt, size_t len)
  1373. {
  1374. struct sta_info *sta;
  1375. sta = ap_get_sta(hapd, mgmt->sa);
  1376. if (len < IEEE80211_HDRLEN + 1) {
  1377. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1378. HOSTAPD_LEVEL_DEBUG,
  1379. "handle_action - too short payload (len=%lu)",
  1380. (unsigned long) len);
  1381. return 0;
  1382. }
  1383. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  1384. (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
  1385. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
  1386. "frame (category=%u) from unassociated STA " MACSTR,
  1387. MAC2STR(mgmt->sa), mgmt->u.action.category);
  1388. return 0;
  1389. }
  1390. #ifdef CONFIG_IEEE80211W
  1391. if (sta && (sta->flags & WLAN_STA_MFP) &&
  1392. !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
  1393. robust_action_frame(mgmt->u.action.category)) {
  1394. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1395. HOSTAPD_LEVEL_DEBUG,
  1396. "Dropped unprotected Robust Action frame from "
  1397. "an MFP STA");
  1398. return 0;
  1399. }
  1400. #endif /* CONFIG_IEEE80211W */
  1401. switch (mgmt->u.action.category) {
  1402. #ifdef CONFIG_IEEE80211R
  1403. case WLAN_ACTION_FT:
  1404. if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  1405. len - IEEE80211_HDRLEN))
  1406. break;
  1407. return 1;
  1408. #endif /* CONFIG_IEEE80211R */
  1409. case WLAN_ACTION_WMM:
  1410. hostapd_wmm_action(hapd, mgmt, len);
  1411. return 1;
  1412. #ifdef CONFIG_IEEE80211W
  1413. case WLAN_ACTION_SA_QUERY:
  1414. return hostapd_sa_query_action(hapd, mgmt, len);
  1415. #endif /* CONFIG_IEEE80211W */
  1416. #ifdef CONFIG_WNM
  1417. case WLAN_ACTION_WNM:
  1418. ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
  1419. return 1;
  1420. #endif /* CONFIG_WNM */
  1421. case WLAN_ACTION_PUBLIC:
  1422. case WLAN_ACTION_PROTECTED_DUAL:
  1423. if (hapd->public_action_cb) {
  1424. hapd->public_action_cb(hapd->public_action_cb_ctx,
  1425. (u8 *) mgmt, len,
  1426. hapd->iface->freq);
  1427. }
  1428. if (hapd->public_action_cb2) {
  1429. hapd->public_action_cb2(hapd->public_action_cb2_ctx,
  1430. (u8 *) mgmt, len,
  1431. hapd->iface->freq);
  1432. }
  1433. if (hapd->public_action_cb || hapd->public_action_cb2)
  1434. return 1;
  1435. break;
  1436. case WLAN_ACTION_VENDOR_SPECIFIC:
  1437. if (hapd->vendor_action_cb) {
  1438. if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
  1439. (u8 *) mgmt, len,
  1440. hapd->iface->freq) == 0)
  1441. return 1;
  1442. }
  1443. break;
  1444. }
  1445. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1446. HOSTAPD_LEVEL_DEBUG,
  1447. "handle_action - unknown action category %d or invalid "
  1448. "frame",
  1449. mgmt->u.action.category);
  1450. if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
  1451. !(mgmt->sa[0] & 0x01)) {
  1452. struct ieee80211_mgmt *resp;
  1453. /*
  1454. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  1455. * Return the Action frame to the source without change
  1456. * except that MSB of the Category set to 1.
  1457. */
  1458. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  1459. "frame back to sender");
  1460. resp = os_malloc(len);
  1461. if (resp == NULL)
  1462. return 0;
  1463. os_memcpy(resp, mgmt, len);
  1464. os_memcpy(resp->da, resp->sa, ETH_ALEN);
  1465. os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
  1466. os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
  1467. resp->u.action.category |= 0x80;
  1468. if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
  1469. wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
  1470. "Action frame");
  1471. }
  1472. os_free(resp);
  1473. }
  1474. return 1;
  1475. }
  1476. /**
  1477. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  1478. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  1479. * sent to)
  1480. * @buf: management frame data (starting from IEEE 802.11 header)
  1481. * @len: length of frame data in octets
  1482. * @fi: meta data about received frame (signal level, etc.)
  1483. *
  1484. * Process all incoming IEEE 802.11 management frames. This will be called for
  1485. * each frame received from the kernel driver through wlan#ap interface. In
  1486. * addition, it can be called to re-inserted pending frames (e.g., when using
  1487. * external RADIUS server as an MAC ACL).
  1488. */
  1489. int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
  1490. struct hostapd_frame_info *fi)
  1491. {
  1492. struct ieee80211_mgmt *mgmt;
  1493. int broadcast;
  1494. u16 fc, stype;
  1495. int ret = 0;
  1496. if (len < 24)
  1497. return 0;
  1498. mgmt = (struct ieee80211_mgmt *) buf;
  1499. fc = le_to_host16(mgmt->frame_control);
  1500. stype = WLAN_FC_GET_STYPE(fc);
  1501. if (stype == WLAN_FC_STYPE_BEACON) {
  1502. handle_beacon(hapd, mgmt, len, fi);
  1503. return 1;
  1504. }
  1505. broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
  1506. mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
  1507. mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
  1508. if (!broadcast &&
  1509. #ifdef CONFIG_P2P
  1510. /* Invitation responses can be sent with the peer MAC as BSSID */
  1511. !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
  1512. stype == WLAN_FC_STYPE_ACTION) &&
  1513. #endif /* CONFIG_P2P */
  1514. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
  1515. wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
  1516. MAC2STR(mgmt->bssid));
  1517. return 0;
  1518. }
  1519. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  1520. handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
  1521. return 1;
  1522. }
  1523. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  1524. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1525. HOSTAPD_LEVEL_DEBUG,
  1526. "MGMT: DA=" MACSTR " not our address",
  1527. MAC2STR(mgmt->da));
  1528. return 0;
  1529. }
  1530. switch (stype) {
  1531. case WLAN_FC_STYPE_AUTH:
  1532. wpa_printf(MSG_DEBUG, "mgmt::auth");
  1533. handle_auth(hapd, mgmt, len);
  1534. ret = 1;
  1535. break;
  1536. case WLAN_FC_STYPE_ASSOC_REQ:
  1537. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  1538. handle_assoc(hapd, mgmt, len, 0);
  1539. ret = 1;
  1540. break;
  1541. case WLAN_FC_STYPE_REASSOC_REQ:
  1542. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  1543. handle_assoc(hapd, mgmt, len, 1);
  1544. ret = 1;
  1545. break;
  1546. case WLAN_FC_STYPE_DISASSOC:
  1547. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  1548. handle_disassoc(hapd, mgmt, len);
  1549. ret = 1;
  1550. break;
  1551. case WLAN_FC_STYPE_DEAUTH:
  1552. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
  1553. handle_deauth(hapd, mgmt, len);
  1554. ret = 1;
  1555. break;
  1556. case WLAN_FC_STYPE_ACTION:
  1557. wpa_printf(MSG_DEBUG, "mgmt::action");
  1558. ret = handle_action(hapd, mgmt, len);
  1559. break;
  1560. default:
  1561. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1562. HOSTAPD_LEVEL_DEBUG,
  1563. "unknown mgmt frame subtype %d", stype);
  1564. break;
  1565. }
  1566. return ret;
  1567. }
  1568. static void handle_auth_cb(struct hostapd_data *hapd,
  1569. const struct ieee80211_mgmt *mgmt,
  1570. size_t len, int ok)
  1571. {
  1572. u16 auth_alg, auth_transaction, status_code;
  1573. struct sta_info *sta;
  1574. if (!ok) {
  1575. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1576. HOSTAPD_LEVEL_NOTICE,
  1577. "did not acknowledge authentication response");
  1578. return;
  1579. }
  1580. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1581. wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
  1582. (unsigned long) len);
  1583. return;
  1584. }
  1585. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1586. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1587. status_code = le_to_host16(mgmt->u.auth.status_code);
  1588. sta = ap_get_sta(hapd, mgmt->da);
  1589. if (!sta) {
  1590. wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
  1591. MAC2STR(mgmt->da));
  1592. return;
  1593. }
  1594. if (status_code == WLAN_STATUS_SUCCESS &&
  1595. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  1596. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  1597. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1598. HOSTAPD_LEVEL_INFO, "authenticated");
  1599. sta->flags |= WLAN_STA_AUTH;
  1600. }
  1601. }
  1602. static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
  1603. struct sta_info *sta,
  1604. char *ifname_wds)
  1605. {
  1606. int i;
  1607. struct hostapd_ssid *ssid = sta->ssid;
  1608. if (hapd->conf->ieee802_1x || hapd->conf->wpa)
  1609. return;
  1610. for (i = 0; i < 4; i++) {
  1611. if (ssid->wep.key[i] &&
  1612. hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
  1613. i == ssid->wep.idx, NULL, 0,
  1614. ssid->wep.key[i], ssid->wep.len[i])) {
  1615. wpa_printf(MSG_WARNING,
  1616. "Could not set WEP keys for WDS interface; %s",
  1617. ifname_wds);
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. static void handle_assoc_cb(struct hostapd_data *hapd,
  1623. const struct ieee80211_mgmt *mgmt,
  1624. size_t len, int reassoc, int ok)
  1625. {
  1626. u16 status;
  1627. struct sta_info *sta;
  1628. int new_assoc = 1;
  1629. struct ieee80211_ht_capabilities ht_cap;
  1630. struct ieee80211_vht_capabilities vht_cap;
  1631. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  1632. sizeof(mgmt->u.assoc_resp))) {
  1633. wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
  1634. reassoc, (unsigned long) len);
  1635. return;
  1636. }
  1637. sta = ap_get_sta(hapd, mgmt->da);
  1638. if (!sta) {
  1639. wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
  1640. MAC2STR(mgmt->da));
  1641. return;
  1642. }
  1643. if (!ok) {
  1644. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1645. HOSTAPD_LEVEL_DEBUG,
  1646. "did not acknowledge association response");
  1647. sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
  1648. return;
  1649. }
  1650. if (reassoc)
  1651. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  1652. else
  1653. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1654. if (status != WLAN_STATUS_SUCCESS)
  1655. return;
  1656. /* Stop previous accounting session, if one is started, and allocate
  1657. * new session id for the new session. */
  1658. accounting_sta_stop(hapd, sta);
  1659. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1660. HOSTAPD_LEVEL_INFO,
  1661. "associated (aid %d)",
  1662. sta->aid);
  1663. if (sta->flags & WLAN_STA_ASSOC)
  1664. new_assoc = 0;
  1665. sta->flags |= WLAN_STA_ASSOC;
  1666. sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
  1667. if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
  1668. sta->auth_alg == WLAN_AUTH_FT) {
  1669. /*
  1670. * Open, static WEP, or FT protocol; no separate authorization
  1671. * step.
  1672. */
  1673. ap_sta_set_authorized(hapd, sta, 1);
  1674. }
  1675. if (reassoc)
  1676. mlme_reassociate_indication(hapd, sta);
  1677. else
  1678. mlme_associate_indication(hapd, sta);
  1679. #ifdef CONFIG_IEEE80211W
  1680. sta->sa_query_timed_out = 0;
  1681. #endif /* CONFIG_IEEE80211W */
  1682. /*
  1683. * Remove the STA entry in order to make sure the STA PS state gets
  1684. * cleared and configuration gets updated in case of reassociation back
  1685. * to the same AP.
  1686. */
  1687. hostapd_drv_sta_remove(hapd, sta->addr);
  1688. #ifdef CONFIG_IEEE80211N
  1689. if (sta->flags & WLAN_STA_HT)
  1690. hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
  1691. #endif /* CONFIG_IEEE80211N */
  1692. #ifdef CONFIG_IEEE80211AC
  1693. if (sta->flags & WLAN_STA_VHT)
  1694. hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
  1695. #endif /* CONFIG_IEEE80211AC */
  1696. if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
  1697. sta->supported_rates, sta->supported_rates_len,
  1698. sta->listen_interval,
  1699. sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
  1700. sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
  1701. sta->flags, sta->qosinfo, sta->vht_opmode)) {
  1702. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1703. HOSTAPD_LEVEL_NOTICE,
  1704. "Could not add STA to kernel driver");
  1705. ap_sta_disconnect(hapd, sta, sta->addr,
  1706. WLAN_REASON_DISASSOC_AP_BUSY);
  1707. return;
  1708. }
  1709. if (sta->flags & WLAN_STA_WDS) {
  1710. int ret;
  1711. char ifname_wds[IFNAMSIZ + 1];
  1712. ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
  1713. sta->aid, 1);
  1714. if (!ret)
  1715. hostapd_set_wds_encryption(hapd, sta, ifname_wds);
  1716. }
  1717. if (sta->eapol_sm == NULL) {
  1718. /*
  1719. * This STA does not use RADIUS server for EAP authentication,
  1720. * so bind it to the selected VLAN interface now, since the
  1721. * interface selection is not going to change anymore.
  1722. */
  1723. if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
  1724. return;
  1725. } else if (sta->vlan_id) {
  1726. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  1727. if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
  1728. return;
  1729. }
  1730. hostapd_set_sta_flags(hapd, sta);
  1731. if (sta->auth_alg == WLAN_AUTH_FT)
  1732. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  1733. else
  1734. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  1735. hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
  1736. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  1737. }
  1738. static void handle_deauth_cb(struct hostapd_data *hapd,
  1739. const struct ieee80211_mgmt *mgmt,
  1740. size_t len, int ok)
  1741. {
  1742. struct sta_info *sta;
  1743. if (mgmt->da[0] & 0x01)
  1744. return;
  1745. sta = ap_get_sta(hapd, mgmt->da);
  1746. if (!sta) {
  1747. wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
  1748. " not found", MAC2STR(mgmt->da));
  1749. return;
  1750. }
  1751. if (ok)
  1752. wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
  1753. MAC2STR(sta->addr));
  1754. else
  1755. wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
  1756. "deauth", MAC2STR(sta->addr));
  1757. ap_sta_deauth_cb(hapd, sta);
  1758. }
  1759. static void handle_disassoc_cb(struct hostapd_data *hapd,
  1760. const struct ieee80211_mgmt *mgmt,
  1761. size_t len, int ok)
  1762. {
  1763. struct sta_info *sta;
  1764. if (mgmt->da[0] & 0x01)
  1765. return;
  1766. sta = ap_get_sta(hapd, mgmt->da);
  1767. if (!sta) {
  1768. wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
  1769. " not found", MAC2STR(mgmt->da));
  1770. return;
  1771. }
  1772. if (ok)
  1773. wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
  1774. MAC2STR(sta->addr));
  1775. else
  1776. wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
  1777. "disassoc", MAC2STR(sta->addr));
  1778. ap_sta_disassoc_cb(hapd, sta);
  1779. }
  1780. /**
  1781. * ieee802_11_mgmt_cb - Process management frame TX status callback
  1782. * @hapd: hostapd BSS data structure (the BSS from which the management frame
  1783. * was sent from)
  1784. * @buf: management frame data (starting from IEEE 802.11 header)
  1785. * @len: length of frame data in octets
  1786. * @stype: management frame subtype from frame control field
  1787. * @ok: Whether the frame was ACK'ed
  1788. */
  1789. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
  1790. u16 stype, int ok)
  1791. {
  1792. const struct ieee80211_mgmt *mgmt;
  1793. mgmt = (const struct ieee80211_mgmt *) buf;
  1794. #ifdef CONFIG_TESTING_OPTIONS
  1795. if (hapd->ext_mgmt_frame_handling) {
  1796. wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
  1797. stype, ok);
  1798. return;
  1799. }
  1800. #endif /* CONFIG_TESTING_OPTIONS */
  1801. switch (stype) {
  1802. case WLAN_FC_STYPE_AUTH:
  1803. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  1804. handle_auth_cb(hapd, mgmt, len, ok);
  1805. break;
  1806. case WLAN_FC_STYPE_ASSOC_RESP:
  1807. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  1808. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  1809. break;
  1810. case WLAN_FC_STYPE_REASSOC_RESP:
  1811. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  1812. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  1813. break;
  1814. case WLAN_FC_STYPE_PROBE_RESP:
  1815. wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
  1816. break;
  1817. case WLAN_FC_STYPE_DEAUTH:
  1818. wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
  1819. handle_deauth_cb(hapd, mgmt, len, ok);
  1820. break;
  1821. case WLAN_FC_STYPE_DISASSOC:
  1822. wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
  1823. handle_disassoc_cb(hapd, mgmt, len, ok);
  1824. break;
  1825. case WLAN_FC_STYPE_ACTION:
  1826. wpa_printf(MSG_DEBUG, "mgmt::action cb");
  1827. break;
  1828. default:
  1829. wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
  1830. break;
  1831. }
  1832. }
  1833. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1834. {
  1835. /* TODO */
  1836. return 0;
  1837. }
  1838. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1839. char *buf, size_t buflen)
  1840. {
  1841. /* TODO */
  1842. return 0;
  1843. }
  1844. void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
  1845. const u8 *buf, size_t len, int ack)
  1846. {
  1847. struct sta_info *sta;
  1848. struct hostapd_iface *iface = hapd->iface;
  1849. sta = ap_get_sta(hapd, addr);
  1850. if (sta == NULL && iface->num_bss > 1) {
  1851. size_t j;
  1852. for (j = 0; j < iface->num_bss; j++) {
  1853. hapd = iface->bss[j];
  1854. sta = ap_get_sta(hapd, addr);
  1855. if (sta)
  1856. break;
  1857. }
  1858. }
  1859. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
  1860. return;
  1861. if (sta->flags & WLAN_STA_PENDING_POLL) {
  1862. wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
  1863. "activity poll", MAC2STR(sta->addr),
  1864. ack ? "ACKed" : "did not ACK");
  1865. if (ack)
  1866. sta->flags &= ~WLAN_STA_PENDING_POLL;
  1867. }
  1868. ieee802_1x_tx_status(hapd, sta, buf, len, ack);
  1869. }
  1870. void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
  1871. const u8 *data, size_t len, int ack)
  1872. {
  1873. struct sta_info *sta;
  1874. struct hostapd_iface *iface = hapd->iface;
  1875. sta = ap_get_sta(hapd, dst);
  1876. if (sta == NULL && iface->num_bss > 1) {
  1877. size_t j;
  1878. for (j = 0; j < iface->num_bss; j++) {
  1879. hapd = iface->bss[j];
  1880. sta = ap_get_sta(hapd, dst);
  1881. if (sta)
  1882. break;
  1883. }
  1884. }
  1885. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  1886. wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
  1887. MACSTR " that is not currently associated",
  1888. MAC2STR(dst));
  1889. return;
  1890. }
  1891. ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
  1892. }
  1893. void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
  1894. {
  1895. struct sta_info *sta;
  1896. struct hostapd_iface *iface = hapd->iface;
  1897. sta = ap_get_sta(hapd, addr);
  1898. if (sta == NULL && iface->num_bss > 1) {
  1899. size_t j;
  1900. for (j = 0; j < iface->num_bss; j++) {
  1901. hapd = iface->bss[j];
  1902. sta = ap_get_sta(hapd, addr);
  1903. if (sta)
  1904. break;
  1905. }
  1906. }
  1907. if (sta == NULL)
  1908. return;
  1909. if (!(sta->flags & WLAN_STA_PENDING_POLL))
  1910. return;
  1911. wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
  1912. "activity poll", MAC2STR(sta->addr));
  1913. sta->flags &= ~WLAN_STA_PENDING_POLL;
  1914. }
  1915. void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
  1916. int wds)
  1917. {
  1918. struct sta_info *sta;
  1919. sta = ap_get_sta(hapd, src);
  1920. if (sta && (sta->flags & WLAN_STA_ASSOC)) {
  1921. if (!hapd->conf->wds_sta)
  1922. return;
  1923. if (wds && !(sta->flags & WLAN_STA_WDS)) {
  1924. int ret;
  1925. char ifname_wds[IFNAMSIZ + 1];
  1926. wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
  1927. "STA " MACSTR " (aid %u)",
  1928. MAC2STR(sta->addr), sta->aid);
  1929. sta->flags |= WLAN_STA_WDS;
  1930. ret = hostapd_set_wds_sta(hapd, ifname_wds,
  1931. sta->addr, sta->aid, 1);
  1932. if (!ret)
  1933. hostapd_set_wds_encryption(hapd, sta,
  1934. ifname_wds);
  1935. }
  1936. return;
  1937. }
  1938. wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
  1939. MACSTR, MAC2STR(src));
  1940. if (src[0] & 0x01) {
  1941. /* Broadcast bit set in SA?! Ignore the frame silently. */
  1942. return;
  1943. }
  1944. if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
  1945. wpa_printf(MSG_DEBUG, "Association Response to the STA has "
  1946. "already been sent, but no TX status yet known - "
  1947. "ignore Class 3 frame issue with " MACSTR,
  1948. MAC2STR(src));
  1949. return;
  1950. }
  1951. if (sta && (sta->flags & WLAN_STA_AUTH))
  1952. hostapd_drv_sta_disassoc(
  1953. hapd, src,
  1954. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  1955. else
  1956. hostapd_drv_sta_deauth(
  1957. hapd, src,
  1958. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  1959. }
  1960. #endif /* CONFIG_NATIVE_WINDOWS */