ieee802_11.c 63 KB

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