ieee802_11.c 52 KB

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