You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ieee802_11.c 53KB

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