ieee802_11.c 52 KB

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