ieee802_11.c 63 KB

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