ieee802_11.c 51 KB

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