ieee802_11.c 48 KB

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