ieee802_11.c 50 KB

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