ieee802_11.c 48 KB

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