ieee802_11.c 63 KB

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