ieee802_11.c 51 KB

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