ieee802_11.c 48 KB

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