ieee802_11.c 48 KB

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