ieee802_11.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2007-2008, Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #ifndef CONFIG_NATIVE_WINDOWS
  17. #include <net/if.h>
  18. #include "eloop.h"
  19. #include "hostapd.h"
  20. #include "ieee802_11.h"
  21. #include "beacon.h"
  22. #include "hw_features.h"
  23. #include "radius/radius.h"
  24. #include "radius/radius_client.h"
  25. #include "ieee802_11_auth.h"
  26. #include "sta_info.h"
  27. #include "rc4.h"
  28. #include "ieee802_1x.h"
  29. #include "wpa.h"
  30. #include "wme.h"
  31. #include "ap_list.h"
  32. #include "accounting.h"
  33. #include "driver_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) < 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(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
  320. key, sizeof(key));
  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) < 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. sta->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 void handle_assoc(struct hostapd_data *hapd,
  564. struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
  565. {
  566. u16 capab_info, listen_interval;
  567. u16 resp = WLAN_STATUS_SUCCESS;
  568. u8 *pos, *wpa_ie;
  569. size_t wpa_ie_len;
  570. int send_deauth = 0, send_len, left, i;
  571. struct sta_info *sta;
  572. struct ieee802_11_elems elems;
  573. u8 buf[sizeof(struct ieee80211_mgmt) + 512];
  574. struct ieee80211_mgmt *reply;
  575. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
  576. sizeof(mgmt->u.assoc_req))) {
  577. printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
  578. "\n", reassoc, (unsigned long) len);
  579. return;
  580. }
  581. if (reassoc) {
  582. capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
  583. listen_interval = le_to_host16(
  584. mgmt->u.reassoc_req.listen_interval);
  585. wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
  586. " capab_info=0x%02x listen_interval=%d current_ap="
  587. MACSTR,
  588. MAC2STR(mgmt->sa), capab_info, listen_interval,
  589. MAC2STR(mgmt->u.reassoc_req.current_ap));
  590. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
  591. pos = mgmt->u.reassoc_req.variable;
  592. } else {
  593. capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  594. listen_interval = le_to_host16(
  595. mgmt->u.assoc_req.listen_interval);
  596. wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
  597. " capab_info=0x%02x listen_interval=%d",
  598. MAC2STR(mgmt->sa), capab_info, listen_interval);
  599. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
  600. pos = mgmt->u.assoc_req.variable;
  601. }
  602. sta = ap_get_sta(hapd, mgmt->sa);
  603. #ifdef CONFIG_IEEE80211R
  604. if (sta && sta->auth_alg == WLAN_AUTH_FT &&
  605. (sta->flags & WLAN_STA_AUTH) == 0) {
  606. wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
  607. "prior to authentication since it is using "
  608. "over-the-DS FT", MAC2STR(mgmt->sa));
  609. } else
  610. #endif /* CONFIG_IEEE80211R */
  611. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  612. printf("STA " MACSTR " trying to associate before "
  613. "authentication\n", MAC2STR(mgmt->sa));
  614. if (sta) {
  615. printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
  616. MAC2STR(sta->addr), sta->aid, sta->flags);
  617. }
  618. send_deauth = 1;
  619. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  620. goto fail;
  621. }
  622. if (hapd->tkip_countermeasures) {
  623. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  624. goto fail;
  625. }
  626. if (listen_interval > hapd->conf->max_listen_interval) {
  627. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  628. HOSTAPD_LEVEL_DEBUG,
  629. "Too large Listen Interval (%d)",
  630. listen_interval);
  631. resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
  632. goto fail;
  633. }
  634. sta->capability = capab_info;
  635. sta->listen_interval = listen_interval;
  636. /* followed by SSID and Supported rates; and HT capabilities if 802.11n
  637. * is used */
  638. if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
  639. !elems.ssid) {
  640. printf("STA " MACSTR " sent invalid association request\n",
  641. MAC2STR(sta->addr));
  642. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  643. goto fail;
  644. }
  645. if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  646. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
  647. {
  648. char ssid_txt[33];
  649. ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
  650. printf("Station " MACSTR " tried to associate with "
  651. "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
  652. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  653. goto fail;
  654. }
  655. sta->flags &= ~WLAN_STA_WME;
  656. if (elems.wme && hapd->conf->wme_enabled) {
  657. if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
  658. hostapd_logger(hapd, sta->addr,
  659. HOSTAPD_MODULE_WPA,
  660. HOSTAPD_LEVEL_DEBUG,
  661. "invalid WME element in association "
  662. "request");
  663. else
  664. sta->flags |= WLAN_STA_WME;
  665. }
  666. if (!elems.supp_rates) {
  667. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  668. HOSTAPD_LEVEL_DEBUG,
  669. "No supported rates element in AssocReq");
  670. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  671. goto fail;
  672. }
  673. if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
  674. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  675. HOSTAPD_LEVEL_DEBUG,
  676. "Invalid supported rates element length %d",
  677. elems.supp_rates_len);
  678. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  679. goto fail;
  680. }
  681. os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  682. os_memcpy(sta->supported_rates, elems.supp_rates,
  683. elems.supp_rates_len);
  684. sta->supported_rates_len = elems.supp_rates_len;
  685. if (elems.ext_supp_rates) {
  686. if (elems.supp_rates_len + elems.ext_supp_rates_len >
  687. sizeof(sta->supported_rates)) {
  688. hostapd_logger(hapd, mgmt->sa,
  689. HOSTAPD_MODULE_IEEE80211,
  690. HOSTAPD_LEVEL_DEBUG,
  691. "Invalid supported rates element length"
  692. " %d+%d", elems.supp_rates_len,
  693. elems.ext_supp_rates_len);
  694. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  695. goto fail;
  696. }
  697. os_memcpy(sta->supported_rates + elems.supp_rates_len,
  698. elems.ext_supp_rates, elems.ext_supp_rates_len);
  699. sta->supported_rates_len += elems.ext_supp_rates_len;
  700. }
  701. #ifdef CONFIG_IEEE80211N
  702. /* save HT capabilities in the sta object */
  703. os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
  704. if (elems.ht_capabilities &&
  705. elems.ht_capabilities_len >=
  706. sizeof(struct ieee80211_ht_capability)) {
  707. sta->flags |= WLAN_STA_HT;
  708. sta->ht_capabilities.id = WLAN_EID_HT_CAP;
  709. sta->ht_capabilities.length =
  710. sizeof(struct ieee80211_ht_capability);
  711. os_memcpy(&sta->ht_capabilities.data,
  712. elems.ht_capabilities,
  713. sizeof(struct ieee80211_ht_capability));
  714. } else
  715. sta->flags &= ~WLAN_STA_HT;
  716. #endif /* CONFIG_IEEE80211N */
  717. if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
  718. wpa_ie = elems.rsn_ie;
  719. wpa_ie_len = elems.rsn_ie_len;
  720. } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
  721. elems.wpa_ie) {
  722. wpa_ie = elems.wpa_ie;
  723. wpa_ie_len = elems.wpa_ie_len;
  724. } else {
  725. wpa_ie = NULL;
  726. wpa_ie_len = 0;
  727. }
  728. #ifdef CONFIG_WPS
  729. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
  730. if (hapd->conf->wps_state && wpa_ie == NULL) {
  731. if (elems.wps_ie) {
  732. wpa_printf(MSG_DEBUG, "STA included WPS IE in "
  733. "(Re)Association Request - assume WPS is "
  734. "used");
  735. sta->flags |= WLAN_STA_WPS;
  736. wpabuf_free(sta->wps_ie);
  737. sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
  738. elems.wps_ie_len - 4);
  739. } else {
  740. wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
  741. "in (Re)Association Request - possible WPS "
  742. "use");
  743. sta->flags |= WLAN_STA_MAYBE_WPS;
  744. }
  745. } else
  746. #endif /* CONFIG_WPS */
  747. if (hapd->conf->wpa && wpa_ie == NULL) {
  748. printf("STA " MACSTR ": No WPA/RSN IE in association "
  749. "request\n", MAC2STR(sta->addr));
  750. resp = WLAN_STATUS_INVALID_IE;
  751. goto fail;
  752. }
  753. if (hapd->conf->wpa && wpa_ie) {
  754. int res;
  755. wpa_ie -= 2;
  756. wpa_ie_len += 2;
  757. if (sta->wpa_sm == NULL)
  758. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  759. sta->addr);
  760. if (sta->wpa_sm == NULL) {
  761. printf("Failed to initialize WPA state machine\n");
  762. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  763. goto fail;
  764. }
  765. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  766. wpa_ie, wpa_ie_len,
  767. elems.mdie, elems.mdie_len);
  768. if (res == WPA_INVALID_GROUP)
  769. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  770. else if (res == WPA_INVALID_PAIRWISE)
  771. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  772. else if (res == WPA_INVALID_AKMP)
  773. resp = WLAN_STATUS_AKMP_NOT_VALID;
  774. else if (res == WPA_ALLOC_FAIL)
  775. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  776. #ifdef CONFIG_IEEE80211W
  777. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  778. resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
  779. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  780. resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
  781. #endif /* CONFIG_IEEE80211W */
  782. else if (res == WPA_INVALID_MDIE)
  783. resp = WLAN_STATUS_INVALID_MDIE;
  784. else if (res != WPA_IE_OK)
  785. resp = WLAN_STATUS_INVALID_IE;
  786. if (resp != WLAN_STATUS_SUCCESS)
  787. goto fail;
  788. #ifdef CONFIG_IEEE80211W
  789. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  790. sta->sa_query_count > 0)
  791. ap_check_sa_query_timeout(hapd, sta);
  792. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  793. (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
  794. /*
  795. * STA has already been associated with MFP and SA
  796. * Query timeout has not been reached. Reject the
  797. * association attempt temporarily and start SA Query,
  798. * if one is not pending.
  799. */
  800. if (sta->sa_query_count == 0)
  801. ap_sta_start_sa_query(hapd, sta);
  802. resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
  803. goto fail;
  804. }
  805. if (wpa_auth_uses_mfp(sta->wpa_sm))
  806. sta->flags |= WLAN_STA_MFP;
  807. else
  808. sta->flags &= ~WLAN_STA_MFP;
  809. #endif /* CONFIG_IEEE80211W */
  810. #ifdef CONFIG_IEEE80211R
  811. if (sta->auth_alg == WLAN_AUTH_FT) {
  812. if (!reassoc) {
  813. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  814. "to use association (not "
  815. "re-association) with FT auth_alg",
  816. MAC2STR(sta->addr));
  817. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  818. goto fail;
  819. }
  820. resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
  821. if (resp != WLAN_STATUS_SUCCESS)
  822. goto fail;
  823. }
  824. #endif /* CONFIG_IEEE80211R */
  825. #ifdef CONFIG_IEEE80211N
  826. if ((sta->flags & WLAN_STA_HT) &&
  827. wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
  828. wpa_printf(MSG_DEBUG, "HT: " MACSTR " tried to "
  829. "use TKIP with HT association",
  830. MAC2STR(sta->addr));
  831. resp = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
  832. goto fail;
  833. }
  834. #endif /* CONFIG_IEEE80211N */
  835. } else
  836. wpa_auth_sta_no_wpa(sta->wpa_sm);
  837. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  838. sta->flags |= WLAN_STA_NONERP;
  839. for (i = 0; i < sta->supported_rates_len; i++) {
  840. if ((sta->supported_rates[i] & 0x7f) > 22) {
  841. sta->flags &= ~WLAN_STA_NONERP;
  842. break;
  843. }
  844. }
  845. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  846. sta->nonerp_set = 1;
  847. hapd->iface->num_sta_non_erp++;
  848. if (hapd->iface->num_sta_non_erp == 1)
  849. ieee802_11_set_beacons(hapd->iface);
  850. }
  851. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  852. !sta->no_short_slot_time_set) {
  853. sta->no_short_slot_time_set = 1;
  854. hapd->iface->num_sta_no_short_slot_time++;
  855. if (hapd->iface->current_mode->mode ==
  856. HOSTAPD_MODE_IEEE80211G &&
  857. hapd->iface->num_sta_no_short_slot_time == 1)
  858. ieee802_11_set_beacons(hapd->iface);
  859. }
  860. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  861. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  862. else
  863. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  864. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  865. !sta->no_short_preamble_set) {
  866. sta->no_short_preamble_set = 1;
  867. hapd->iface->num_sta_no_short_preamble++;
  868. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  869. && hapd->iface->num_sta_no_short_preamble == 1)
  870. ieee802_11_set_beacons(hapd->iface);
  871. }
  872. #ifdef CONFIG_IEEE80211N
  873. if (sta->flags & WLAN_STA_HT) {
  874. u16 ht_capab = le_to_host16(
  875. sta->ht_capabilities.data.capabilities_info);
  876. wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
  877. "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
  878. if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
  879. if (!sta->no_ht_gf_set) {
  880. sta->no_ht_gf_set = 1;
  881. hapd->iface->num_sta_ht_no_gf++;
  882. }
  883. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
  884. "greenfield, num of non-gf stations %d",
  885. __func__, MAC2STR(sta->addr),
  886. hapd->iface->num_sta_ht_no_gf);
  887. }
  888. if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
  889. if (!sta->ht_20mhz_set) {
  890. sta->ht_20mhz_set = 1;
  891. hapd->iface->num_sta_ht_20mhz++;
  892. }
  893. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
  894. "num of 20MHz HT STAs %d",
  895. __func__, MAC2STR(sta->addr),
  896. hapd->iface->num_sta_ht_20mhz);
  897. }
  898. } else {
  899. if (!sta->no_ht_set) {
  900. sta->no_ht_set = 1;
  901. hapd->iface->num_sta_no_ht++;
  902. }
  903. if (hapd->iconf->ieee80211n) {
  904. wpa_printf(MSG_DEBUG, "%s STA " MACSTR
  905. " - no HT, num of non-HT stations %d",
  906. __func__, MAC2STR(sta->addr),
  907. hapd->iface->num_sta_no_ht);
  908. }
  909. }
  910. if (hostapd_ht_operation_update(hapd->iface) > 0)
  911. ieee802_11_set_beacons(hapd->iface);
  912. #endif /* CONFIG_IEEE80211N */
  913. /* get a unique AID */
  914. if (sta->aid > 0) {
  915. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  916. } else {
  917. for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
  918. if (hapd->sta_aid[sta->aid - 1] == NULL)
  919. break;
  920. if (sta->aid > MAX_AID_TABLE_SIZE) {
  921. sta->aid = 0;
  922. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  923. wpa_printf(MSG_ERROR, " no room for more AIDs");
  924. goto fail;
  925. } else {
  926. hapd->sta_aid[sta->aid - 1] = sta;
  927. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  928. }
  929. }
  930. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  931. HOSTAPD_LEVEL_DEBUG,
  932. "association OK (aid %d)", sta->aid);
  933. /* Station will be marked associated, after it acknowledges AssocResp
  934. */
  935. #ifdef CONFIG_IEEE80211W
  936. if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
  937. wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
  938. "SA Query procedure", reassoc ? "re" : "");
  939. /* TODO: Send a protected Disassociate frame to the STA using
  940. * the old key and Reason Code "Previous Authentication no
  941. * longer valid". Make sure this is only sent protected since
  942. * unprotected frame would be received by the STA that is now
  943. * trying to associate.
  944. */
  945. }
  946. #endif /* CONFIG_IEEE80211W */
  947. if (reassoc) {
  948. os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
  949. ETH_ALEN);
  950. }
  951. if (sta->last_assoc_req)
  952. os_free(sta->last_assoc_req);
  953. sta->last_assoc_req = os_malloc(len);
  954. if (sta->last_assoc_req)
  955. os_memcpy(sta->last_assoc_req, mgmt, len);
  956. /* Make sure that the previously registered inactivity timer will not
  957. * remove the STA immediately. */
  958. sta->timeout_next = STA_NULLFUNC;
  959. fail:
  960. os_memset(buf, 0, sizeof(buf));
  961. reply = (struct ieee80211_mgmt *) buf;
  962. reply->frame_control =
  963. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  964. (send_deauth ? WLAN_FC_STYPE_DEAUTH :
  965. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  966. WLAN_FC_STYPE_ASSOC_RESP)));
  967. os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
  968. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  969. os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
  970. send_len = IEEE80211_HDRLEN;
  971. if (send_deauth) {
  972. send_len += sizeof(reply->u.deauth);
  973. reply->u.deauth.reason_code = host_to_le16(resp);
  974. } else {
  975. u8 *p;
  976. send_len += sizeof(reply->u.assoc_resp);
  977. reply->u.assoc_resp.capab_info =
  978. host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
  979. reply->u.assoc_resp.status_code = host_to_le16(resp);
  980. reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
  981. | BIT(14) | BIT(15));
  982. /* Supported rates */
  983. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  984. /* Extended supported rates */
  985. p = hostapd_eid_ext_supp_rates(hapd, p);
  986. if (sta->flags & WLAN_STA_WME)
  987. p = hostapd_eid_wme(hapd, p);
  988. p = hostapd_eid_ht_capabilities_info(hapd, p);
  989. p = hostapd_eid_ht_operation(hapd, p);
  990. #ifdef CONFIG_IEEE80211R
  991. if (resp == WLAN_STATUS_SUCCESS) {
  992. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  993. * Transition Information, RSN */
  994. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  995. buf + sizeof(buf) - p,
  996. sta->auth_alg);
  997. }
  998. #endif /* CONFIG_IEEE80211R */
  999. #ifdef CONFIG_IEEE80211W
  1000. if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
  1001. p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
  1002. #endif /* CONFIG_IEEE80211W */
  1003. send_len += p - reply->u.assoc_resp.variable;
  1004. }
  1005. if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
  1006. perror("handle_assoc: send");
  1007. }
  1008. static void handle_disassoc(struct hostapd_data *hapd,
  1009. struct ieee80211_mgmt *mgmt, size_t len)
  1010. {
  1011. struct sta_info *sta;
  1012. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  1013. printf("handle_disassoc - too short payload (len=%lu)\n",
  1014. (unsigned long) len);
  1015. return;
  1016. }
  1017. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  1018. MAC2STR(mgmt->sa),
  1019. le_to_host16(mgmt->u.disassoc.reason_code));
  1020. sta = ap_get_sta(hapd, mgmt->sa);
  1021. if (sta == NULL) {
  1022. printf("Station " MACSTR " trying to disassociate, but it "
  1023. "is not associated.\n", MAC2STR(mgmt->sa));
  1024. return;
  1025. }
  1026. sta->flags &= ~WLAN_STA_ASSOC;
  1027. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  1028. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1029. HOSTAPD_LEVEL_INFO, "disassociated");
  1030. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1031. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1032. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  1033. * authenticated. */
  1034. accounting_sta_stop(hapd, sta);
  1035. ieee802_1x_free_station(sta);
  1036. hostapd_sta_remove(hapd, sta->addr);
  1037. if (sta->timeout_next == STA_NULLFUNC ||
  1038. sta->timeout_next == STA_DISASSOC) {
  1039. sta->timeout_next = STA_DEAUTH;
  1040. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1041. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  1042. hapd, sta);
  1043. }
  1044. mlme_disassociate_indication(
  1045. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  1046. }
  1047. static void handle_deauth(struct hostapd_data *hapd,
  1048. struct ieee80211_mgmt *mgmt, size_t len)
  1049. {
  1050. struct sta_info *sta;
  1051. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  1052. printf("handle_deauth - too short payload (len=%lu)\n",
  1053. (unsigned long) len);
  1054. return;
  1055. }
  1056. wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
  1057. " reason_code=%d",
  1058. MAC2STR(mgmt->sa),
  1059. le_to_host16(mgmt->u.deauth.reason_code));
  1060. sta = ap_get_sta(hapd, mgmt->sa);
  1061. if (sta == NULL) {
  1062. printf("Station " MACSTR " trying to deauthenticate, but it "
  1063. "is not authenticated.\n", MAC2STR(mgmt->sa));
  1064. return;
  1065. }
  1066. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1067. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1068. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1069. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  1070. mlme_deauthenticate_indication(
  1071. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  1072. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1073. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1074. ap_free_sta(hapd, sta);
  1075. }
  1076. static void handle_beacon(struct hostapd_data *hapd,
  1077. struct ieee80211_mgmt *mgmt, size_t len,
  1078. struct hostapd_frame_info *fi)
  1079. {
  1080. struct ieee802_11_elems elems;
  1081. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  1082. printf("handle_beacon - too short payload (len=%lu)\n",
  1083. (unsigned long) len);
  1084. return;
  1085. }
  1086. (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1087. len - (IEEE80211_HDRLEN +
  1088. sizeof(mgmt->u.beacon)), &elems,
  1089. 0);
  1090. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  1091. }
  1092. #ifdef CONFIG_IEEE80211W
  1093. /* MLME-SAQuery.request */
  1094. void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
  1095. const u8 *addr, const u8 *trans_id)
  1096. {
  1097. struct ieee80211_mgmt mgmt;
  1098. u8 *end;
  1099. os_memset(&mgmt, 0, sizeof(mgmt));
  1100. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1101. WLAN_FC_STYPE_ACTION);
  1102. os_memcpy(mgmt.da, addr, ETH_ALEN);
  1103. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  1104. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  1105. mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
  1106. mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
  1107. os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
  1108. WLAN_SA_QUERY_TR_ID_LEN);
  1109. end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
  1110. if (hostapd_send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt, 0) < 0)
  1111. perror("ieee802_11_send_sa_query_req: send");
  1112. }
  1113. static void hostapd_sa_query_action(struct hostapd_data *hapd,
  1114. struct ieee80211_mgmt *mgmt, size_t len)
  1115. {
  1116. struct sta_info *sta;
  1117. u8 *end;
  1118. int i;
  1119. end = mgmt->u.action.u.sa_query_resp.trans_id +
  1120. WLAN_SA_QUERY_TR_ID_LEN;
  1121. if (((u8 *) mgmt) + len < end) {
  1122. wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
  1123. "frame (len=%lu)", (unsigned long) len);
  1124. return;
  1125. }
  1126. if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
  1127. wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
  1128. "Action %d", mgmt->u.action.u.sa_query_resp.action);
  1129. return;
  1130. }
  1131. /* MLME-SAQuery.confirm */
  1132. sta = ap_get_sta(hapd, mgmt->sa);
  1133. if (sta == NULL || sta->sa_query_trans_id == NULL) {
  1134. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
  1135. "pending SA Query request found");
  1136. return;
  1137. }
  1138. for (i = 0; i < sta->sa_query_count; i++) {
  1139. if (os_memcmp(sta->sa_query_trans_id +
  1140. i * WLAN_SA_QUERY_TR_ID_LEN,
  1141. mgmt->u.action.u.sa_query_resp.trans_id,
  1142. WLAN_SA_QUERY_TR_ID_LEN) == 0)
  1143. break;
  1144. }
  1145. if (i >= sta->sa_query_count) {
  1146. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
  1147. "transaction identifier found");
  1148. return;
  1149. }
  1150. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1151. HOSTAPD_LEVEL_DEBUG,
  1152. "Reply to pending SA Query received");
  1153. ap_sta_stop_sa_query(hapd, sta);
  1154. }
  1155. #endif /* CONFIG_IEEE80211W */
  1156. static void handle_action(struct hostapd_data *hapd,
  1157. struct ieee80211_mgmt *mgmt, size_t len)
  1158. {
  1159. if (len < IEEE80211_HDRLEN + 1) {
  1160. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1161. HOSTAPD_LEVEL_DEBUG,
  1162. "handle_action - too short payload (len=%lu)",
  1163. (unsigned long) len);
  1164. return;
  1165. }
  1166. switch (mgmt->u.action.category) {
  1167. #ifdef CONFIG_IEEE80211R
  1168. case WLAN_ACTION_FT:
  1169. {
  1170. struct sta_info *sta;
  1171. sta = ap_get_sta(hapd, mgmt->sa);
  1172. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  1173. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
  1174. "frame from unassociated STA " MACSTR,
  1175. MAC2STR(mgmt->sa));
  1176. return;
  1177. }
  1178. if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  1179. len - IEEE80211_HDRLEN))
  1180. break;
  1181. return;
  1182. }
  1183. #endif /* CONFIG_IEEE80211R */
  1184. case WLAN_ACTION_WMM:
  1185. hostapd_wme_action(hapd, mgmt, len);
  1186. return;
  1187. #ifdef CONFIG_IEEE80211W
  1188. case WLAN_ACTION_SA_QUERY:
  1189. hostapd_sa_query_action(hapd, mgmt, len);
  1190. return;
  1191. #endif /* CONFIG_IEEE80211W */
  1192. }
  1193. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1194. HOSTAPD_LEVEL_DEBUG,
  1195. "handle_action - unknown action category %d or invalid "
  1196. "frame",
  1197. mgmt->u.action.category);
  1198. if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
  1199. !(mgmt->sa[0] & 0x01)) {
  1200. /*
  1201. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  1202. * Return the Action frame to the source without change
  1203. * except that MSB of the Category set to 1.
  1204. */
  1205. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  1206. "frame back to sender");
  1207. os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
  1208. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  1209. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  1210. mgmt->u.action.category |= 0x80;
  1211. hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
  1212. }
  1213. }
  1214. /**
  1215. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  1216. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  1217. * sent to)
  1218. * @buf: management frame data (starting from IEEE 802.11 header)
  1219. * @len: length of frame data in octets
  1220. * @stype: management frame subtype from frame control field
  1221. * @fi: meta data about received frame (signal level, etc.)
  1222. *
  1223. * Process all incoming IEEE 802.11 management frames. This will be called for
  1224. * each frame received from the kernel driver through wlan#ap interface. In
  1225. * addition, it can be called to re-inserted pending frames (e.g., when using
  1226. * external RADIUS server as an MAC ACL).
  1227. */
  1228. void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
  1229. struct hostapd_frame_info *fi)
  1230. {
  1231. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1232. int broadcast;
  1233. if (stype == WLAN_FC_STYPE_BEACON) {
  1234. handle_beacon(hapd, mgmt, len, fi);
  1235. return;
  1236. }
  1237. if (fi && fi->passive_scan)
  1238. return;
  1239. broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
  1240. mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
  1241. mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
  1242. if (!broadcast &&
  1243. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
  1244. printf("MGMT: BSSID=" MACSTR " not our address\n",
  1245. MAC2STR(mgmt->bssid));
  1246. return;
  1247. }
  1248. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  1249. handle_probe_req(hapd, mgmt, len);
  1250. return;
  1251. }
  1252. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  1253. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1254. HOSTAPD_LEVEL_DEBUG,
  1255. "MGMT: DA=" MACSTR " not our address",
  1256. MAC2STR(mgmt->da));
  1257. return;
  1258. }
  1259. switch (stype) {
  1260. case WLAN_FC_STYPE_AUTH:
  1261. wpa_printf(MSG_DEBUG, "mgmt::auth");
  1262. handle_auth(hapd, mgmt, len);
  1263. break;
  1264. case WLAN_FC_STYPE_ASSOC_REQ:
  1265. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  1266. handle_assoc(hapd, mgmt, len, 0);
  1267. break;
  1268. case WLAN_FC_STYPE_REASSOC_REQ:
  1269. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  1270. handle_assoc(hapd, mgmt, len, 1);
  1271. break;
  1272. case WLAN_FC_STYPE_DISASSOC:
  1273. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  1274. handle_disassoc(hapd, mgmt, len);
  1275. break;
  1276. case WLAN_FC_STYPE_DEAUTH:
  1277. wpa_printf(MSG_DEBUG, "mgmt::deauth");
  1278. handle_deauth(hapd, mgmt, len);
  1279. break;
  1280. case WLAN_FC_STYPE_ACTION:
  1281. wpa_printf(MSG_DEBUG, "mgmt::action");
  1282. handle_action(hapd, mgmt, len);
  1283. break;
  1284. default:
  1285. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1286. HOSTAPD_LEVEL_DEBUG,
  1287. "unknown mgmt frame subtype %d", stype);
  1288. break;
  1289. }
  1290. }
  1291. static void handle_auth_cb(struct hostapd_data *hapd,
  1292. struct ieee80211_mgmt *mgmt,
  1293. size_t len, int ok)
  1294. {
  1295. u16 auth_alg, auth_transaction, status_code;
  1296. struct sta_info *sta;
  1297. if (!ok) {
  1298. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1299. HOSTAPD_LEVEL_NOTICE,
  1300. "did not acknowledge authentication response");
  1301. return;
  1302. }
  1303. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1304. printf("handle_auth_cb - too short payload (len=%lu)\n",
  1305. (unsigned long) len);
  1306. return;
  1307. }
  1308. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1309. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1310. status_code = le_to_host16(mgmt->u.auth.status_code);
  1311. sta = ap_get_sta(hapd, mgmt->da);
  1312. if (!sta) {
  1313. printf("handle_auth_cb: STA " MACSTR " not found\n",
  1314. MAC2STR(mgmt->da));
  1315. return;
  1316. }
  1317. if (status_code == WLAN_STATUS_SUCCESS &&
  1318. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  1319. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  1320. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1321. HOSTAPD_LEVEL_INFO, "authenticated");
  1322. sta->flags |= WLAN_STA_AUTH;
  1323. }
  1324. }
  1325. #ifdef CONFIG_IEEE80211N
  1326. static void
  1327. hostapd_get_ht_capab(struct hostapd_data *hapd,
  1328. struct ht_cap_ie *ht_cap_ie,
  1329. struct ht_cap_ie *neg_ht_cap_ie)
  1330. {
  1331. os_memcpy(neg_ht_cap_ie, ht_cap_ie, sizeof(struct ht_cap_ie));
  1332. neg_ht_cap_ie->data.capabilities_info =
  1333. ht_cap_ie->data.capabilities_info & hapd->iconf->ht_capab;
  1334. neg_ht_cap_ie->data.capabilities_info &= ~HT_CAP_INFO_SMPS_DISABLED;
  1335. if ((ht_cap_ie->data.capabilities_info & HT_CAP_INFO_SMPS_DISABLED) ==
  1336. (hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_DISABLED))
  1337. neg_ht_cap_ie->data.capabilities_info |=
  1338. hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_DISABLED;
  1339. else
  1340. neg_ht_cap_ie->data.capabilities_info |=
  1341. HT_CAP_INFO_SMPS_DISABLED;
  1342. /* FIXME: Rx STBC needs to be handled specially */
  1343. neg_ht_cap_ie->data.capabilities_info &= ~HT_CAP_INFO_RX_STBC_MASK;
  1344. neg_ht_cap_ie->data.capabilities_info |=
  1345. hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK;
  1346. }
  1347. #endif /* CONFIG_IEEE80211N */
  1348. static void handle_assoc_cb(struct hostapd_data *hapd,
  1349. struct ieee80211_mgmt *mgmt,
  1350. size_t len, int reassoc, int ok)
  1351. {
  1352. u16 status;
  1353. struct sta_info *sta;
  1354. int new_assoc = 1;
  1355. struct ht_cap_ie ht_cap;
  1356. if (!ok) {
  1357. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1358. HOSTAPD_LEVEL_DEBUG,
  1359. "did not acknowledge association response");
  1360. return;
  1361. }
  1362. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  1363. sizeof(mgmt->u.assoc_resp))) {
  1364. printf("handle_assoc_cb(reassoc=%d) - too short payload "
  1365. "(len=%lu)\n", reassoc, (unsigned long) len);
  1366. return;
  1367. }
  1368. if (reassoc)
  1369. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  1370. else
  1371. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1372. sta = ap_get_sta(hapd, mgmt->da);
  1373. if (!sta) {
  1374. printf("handle_assoc_cb: STA " MACSTR " not found\n",
  1375. MAC2STR(mgmt->da));
  1376. return;
  1377. }
  1378. if (status != WLAN_STATUS_SUCCESS)
  1379. goto fail;
  1380. /* Stop previous accounting session, if one is started, and allocate
  1381. * new session id for the new session. */
  1382. accounting_sta_stop(hapd, sta);
  1383. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1384. HOSTAPD_LEVEL_INFO,
  1385. "associated (aid %d)",
  1386. sta->aid);
  1387. if (sta->flags & WLAN_STA_ASSOC)
  1388. new_assoc = 0;
  1389. sta->flags |= WLAN_STA_ASSOC;
  1390. if (reassoc)
  1391. mlme_reassociate_indication(hapd, sta);
  1392. else
  1393. mlme_associate_indication(hapd, sta);
  1394. #ifdef CONFIG_IEEE80211N
  1395. if (sta->flags & WLAN_STA_HT)
  1396. hostapd_get_ht_capab(hapd, &sta->ht_capabilities, &ht_cap);
  1397. #endif /* CONFIG_IEEE80211N */
  1398. #ifdef CONFIG_IEEE80211W
  1399. sta->sa_query_timed_out = 0;
  1400. #endif /* CONFIG_IEEE80211W */
  1401. if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
  1402. sta->capability, sta->supported_rates,
  1403. sta->supported_rates_len, 0, sta->listen_interval,
  1404. &ht_cap))
  1405. {
  1406. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1407. HOSTAPD_LEVEL_NOTICE,
  1408. "Could not add STA to kernel driver");
  1409. }
  1410. if (sta->eapol_sm == NULL) {
  1411. /*
  1412. * This STA does not use RADIUS server for EAP authentication,
  1413. * so bind it to the selected VLAN interface now, since the
  1414. * interface selection is not going to change anymore.
  1415. */
  1416. ap_sta_bind_vlan(hapd, sta, 0);
  1417. } else if (sta->vlan_id) {
  1418. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  1419. ap_sta_bind_vlan(hapd, sta, 0);
  1420. }
  1421. if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
  1422. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1423. WLAN_STA_SHORT_PREAMBLE, ~0);
  1424. } else {
  1425. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1426. 0, ~WLAN_STA_SHORT_PREAMBLE);
  1427. }
  1428. if (sta->auth_alg == WLAN_AUTH_FT)
  1429. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  1430. else
  1431. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  1432. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  1433. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  1434. fail:
  1435. /* Copy of the association request is not needed anymore */
  1436. if (sta->last_assoc_req) {
  1437. os_free(sta->last_assoc_req);
  1438. sta->last_assoc_req = NULL;
  1439. }
  1440. }
  1441. /**
  1442. * ieee802_11_mgmt_cb - Process management frame TX status callback
  1443. * @hapd: hostapd BSS data structure (the BSS from which the management frame
  1444. * was sent from)
  1445. * @buf: management frame data (starting from IEEE 802.11 header)
  1446. * @len: length of frame data in octets
  1447. * @stype: management frame subtype from frame control field
  1448. * @ok: Whether the frame was ACK'ed
  1449. */
  1450. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  1451. u16 stype, int ok)
  1452. {
  1453. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1454. switch (stype) {
  1455. case WLAN_FC_STYPE_AUTH:
  1456. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  1457. handle_auth_cb(hapd, mgmt, len, ok);
  1458. break;
  1459. case WLAN_FC_STYPE_ASSOC_RESP:
  1460. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  1461. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  1462. break;
  1463. case WLAN_FC_STYPE_REASSOC_RESP:
  1464. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  1465. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  1466. break;
  1467. case WLAN_FC_STYPE_PROBE_RESP:
  1468. wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
  1469. break;
  1470. case WLAN_FC_STYPE_DEAUTH:
  1471. /* ignore */
  1472. break;
  1473. case WLAN_FC_STYPE_ACTION:
  1474. wpa_printf(MSG_DEBUG, "mgmt::action cb");
  1475. break;
  1476. default:
  1477. printf("unknown mgmt cb frame subtype %d\n", stype);
  1478. break;
  1479. }
  1480. }
  1481. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1482. {
  1483. /* TODO */
  1484. return 0;
  1485. }
  1486. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1487. char *buf, size_t buflen)
  1488. {
  1489. /* TODO */
  1490. return 0;
  1491. }
  1492. #endif /* CONFIG_NATIVE_WINDOWS */