ieee802_11.c 51 KB

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