ieee802_11.c 50 KB

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