ieee802_11.c 47 KB

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