ieee802_11.c 51 KB

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