ieee802_11.c 72 KB

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