ieee802_11.c 51 KB

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