ieee802_11.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2007-2008, Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #ifndef CONFIG_NATIVE_WINDOWS
  17. #include <net/if.h>
  18. #include "eloop.h"
  19. #include "hostapd.h"
  20. #include "ieee802_11.h"
  21. #include "beacon.h"
  22. #include "hw_features.h"
  23. #include "radius/radius.h"
  24. #include "radius/radius_client.h"
  25. #include "ieee802_11_auth.h"
  26. #include "sta_info.h"
  27. #include "rc4.h"
  28. #include "ieee802_1x.h"
  29. #include "wpa.h"
  30. #include "wme.h"
  31. #include "ap_list.h"
  32. #include "accounting.h"
  33. #include "driver.h"
  34. #include "ieee802_11h.h"
  35. #include "mlme.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. cap->supported_mcs_set[0] = 0xff;
  102. cap->supported_mcs_set[1] = 0xff;
  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->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
  121. pos += sizeof(*oper);
  122. return pos;
  123. #else /* CONFIG_IEEE80211N */
  124. return eid;
  125. #endif /* CONFIG_IEEE80211N */
  126. }
  127. #ifdef CONFIG_IEEE80211N
  128. /*
  129. op_mode
  130. Set to 0 (HT pure) under the followign conditions
  131. - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
  132. - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
  133. Set to 1 (HT non-member protection) if there may be non-HT STAs
  134. in both the primary and the secondary channel
  135. Set to 2 if only HT STAs are associated in BSS,
  136. however and at least one 20 MHz HT STA is associated
  137. Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
  138. (currently non-GF HT station is considered as non-HT STA also)
  139. */
  140. int hostapd_ht_operation_update(struct hostapd_iface *iface)
  141. {
  142. u16 cur_op_mode, new_op_mode;
  143. int op_mode_changes = 0;
  144. if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
  145. return 0;
  146. wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
  147. __func__, iface->ht_op_mode);
  148. if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
  149. && iface->num_sta_ht_no_gf) {
  150. iface->ht_op_mode |=
  151. HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
  152. op_mode_changes++;
  153. } else if ((iface->ht_op_mode &
  154. HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
  155. iface->num_sta_ht_no_gf == 0) {
  156. iface->ht_op_mode &=
  157. ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
  158. op_mode_changes++;
  159. }
  160. if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
  161. (iface->num_sta_no_ht || iface->olbc_ht)) {
  162. iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
  163. op_mode_changes++;
  164. } else if ((iface->ht_op_mode &
  165. HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
  166. (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
  167. iface->ht_op_mode &=
  168. ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
  169. op_mode_changes++;
  170. }
  171. /* Note: currently we switch to the MIXED op mode if HT non-greenfield
  172. * station is associated. Probably it's a theoretical case, since
  173. * it looks like all known HT STAs support greenfield.
  174. */
  175. new_op_mode = 0;
  176. if (iface->num_sta_no_ht ||
  177. (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
  178. new_op_mode = OP_MODE_MIXED;
  179. else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
  180. && iface->num_sta_ht_20mhz)
  181. new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
  182. else if (iface->olbc_ht)
  183. new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
  184. else
  185. new_op_mode = OP_MODE_PURE;
  186. cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
  187. if (cur_op_mode != new_op_mode) {
  188. iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
  189. iface->ht_op_mode |= new_op_mode;
  190. op_mode_changes++;
  191. }
  192. wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
  193. __func__, iface->ht_op_mode, op_mode_changes);
  194. return op_mode_changes;
  195. }
  196. #endif /* CONFIG_IEEE80211N */
  197. u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
  198. int probe)
  199. {
  200. int capab = WLAN_CAPABILITY_ESS;
  201. int privacy;
  202. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  203. hapd->iconf->preamble == SHORT_PREAMBLE)
  204. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  205. privacy = hapd->conf->ssid.wep.keys_set;
  206. if (hapd->conf->ieee802_1x &&
  207. (hapd->conf->default_wep_key_len ||
  208. hapd->conf->individual_wep_key_len))
  209. privacy = 1;
  210. if (hapd->conf->wpa)
  211. privacy = 1;
  212. if (sta) {
  213. int policy, def_klen;
  214. if (probe && sta->ssid_probe) {
  215. policy = sta->ssid_probe->security_policy;
  216. def_klen = sta->ssid_probe->wep.default_len;
  217. } else {
  218. policy = sta->ssid->security_policy;
  219. def_klen = sta->ssid->wep.default_len;
  220. }
  221. privacy = policy != SECURITY_PLAINTEXT;
  222. if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
  223. privacy = 0;
  224. }
  225. if (privacy)
  226. capab |= WLAN_CAPABILITY_PRIVACY;
  227. if (hapd->iface->current_mode &&
  228. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  229. hapd->iface->num_sta_no_short_slot_time == 0)
  230. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  231. if (hapd->iface->dfs_enable)
  232. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  233. return capab;
  234. }
  235. #define OUI_MICROSOFT 0x0050f2 /* Microsoft (also used in Wi-Fi specs)
  236. * 00:50:F2 */
  237. static int ieee802_11_parse_vendor_specific(struct hostapd_data *hapd,
  238. u8 *pos, size_t elen,
  239. struct ieee802_11_elems *elems,
  240. int show_errors)
  241. {
  242. unsigned int oui;
  243. /* first 3 bytes in vendor specific information element are the IEEE
  244. * OUI of the vendor. The following byte is used a vendor specific
  245. * sub-type. */
  246. if (elen < 4) {
  247. if (show_errors) {
  248. wpa_printf(MSG_MSGDUMP, "short vendor specific "
  249. "information element ignored (len=%lu)",
  250. (unsigned long) elen);
  251. }
  252. return -1;
  253. }
  254. oui = WPA_GET_BE24(pos);
  255. switch (oui) {
  256. case OUI_MICROSOFT:
  257. /* Microsoft/Wi-Fi information elements are further typed and
  258. * subtyped */
  259. switch (pos[3]) {
  260. case 1:
  261. /* Microsoft OUI (00:50:F2) with OUI Type 1:
  262. * real WPA information element */
  263. elems->wpa_ie = pos;
  264. elems->wpa_ie_len = elen;
  265. break;
  266. case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
  267. if (elen < 5) {
  268. wpa_printf(MSG_MSGDUMP, "short WME "
  269. "information element ignored "
  270. "(len=%lu)",
  271. (unsigned long) elen);
  272. return -1;
  273. }
  274. switch (pos[4]) {
  275. case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
  276. case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
  277. elems->wme = pos;
  278. elems->wme_len = elen;
  279. break;
  280. case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
  281. elems->wme_tspec = pos;
  282. elems->wme_tspec_len = elen;
  283. break;
  284. default:
  285. wpa_printf(MSG_MSGDUMP, "unknown WME "
  286. "information element ignored "
  287. "(subtype=%d len=%lu)",
  288. pos[4], (unsigned long) elen);
  289. return -1;
  290. }
  291. break;
  292. default:
  293. wpa_printf(MSG_MSGDUMP, "Unknown Microsoft "
  294. "information element ignored "
  295. "(type=%d len=%lu)\n",
  296. pos[3], (unsigned long) elen);
  297. return -1;
  298. }
  299. break;
  300. default:
  301. wpa_printf(MSG_MSGDUMP, "unknown vendor specific information "
  302. "element ignored (vendor OUI %02x:%02x:%02x "
  303. "len=%lu)",
  304. pos[0], pos[1], pos[2], (unsigned long) elen);
  305. return -1;
  306. }
  307. return 0;
  308. }
  309. ParseRes ieee802_11_parse_elems(struct hostapd_data *hapd, u8 *start,
  310. size_t len,
  311. struct ieee802_11_elems *elems,
  312. int show_errors)
  313. {
  314. size_t left = len;
  315. u8 *pos = start;
  316. int unknown = 0;
  317. os_memset(elems, 0, sizeof(*elems));
  318. while (left >= 2) {
  319. u8 id, elen;
  320. id = *pos++;
  321. elen = *pos++;
  322. left -= 2;
  323. if (elen > left) {
  324. if (show_errors) {
  325. wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
  326. "parse failed (id=%d elen=%d "
  327. "left=%lu)",
  328. id, elen, (unsigned long) left);
  329. wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
  330. }
  331. return ParseFailed;
  332. }
  333. switch (id) {
  334. case WLAN_EID_SSID:
  335. elems->ssid = pos;
  336. elems->ssid_len = elen;
  337. break;
  338. case WLAN_EID_SUPP_RATES:
  339. elems->supp_rates = pos;
  340. elems->supp_rates_len = elen;
  341. break;
  342. case WLAN_EID_FH_PARAMS:
  343. elems->fh_params = pos;
  344. elems->fh_params_len = elen;
  345. break;
  346. case WLAN_EID_DS_PARAMS:
  347. elems->ds_params = pos;
  348. elems->ds_params_len = elen;
  349. break;
  350. case WLAN_EID_CF_PARAMS:
  351. elems->cf_params = pos;
  352. elems->cf_params_len = elen;
  353. break;
  354. case WLAN_EID_TIM:
  355. elems->tim = pos;
  356. elems->tim_len = elen;
  357. break;
  358. case WLAN_EID_IBSS_PARAMS:
  359. elems->ibss_params = pos;
  360. elems->ibss_params_len = elen;
  361. break;
  362. case WLAN_EID_CHALLENGE:
  363. elems->challenge = pos;
  364. elems->challenge_len = elen;
  365. break;
  366. case WLAN_EID_ERP_INFO:
  367. elems->erp_info = pos;
  368. elems->erp_info_len = elen;
  369. break;
  370. case WLAN_EID_EXT_SUPP_RATES:
  371. elems->ext_supp_rates = pos;
  372. elems->ext_supp_rates_len = elen;
  373. break;
  374. case WLAN_EID_VENDOR_SPECIFIC:
  375. if (ieee802_11_parse_vendor_specific(hapd, pos, elen,
  376. elems,
  377. show_errors))
  378. unknown++;
  379. break;
  380. case WLAN_EID_RSN:
  381. elems->rsn_ie = pos;
  382. elems->rsn_ie_len = elen;
  383. break;
  384. case WLAN_EID_PWR_CAPABILITY:
  385. elems->power_cap = pos;
  386. elems->power_cap_len = elen;
  387. break;
  388. case WLAN_EID_SUPPORTED_CHANNELS:
  389. elems->supp_channels = pos;
  390. elems->supp_channels_len = elen;
  391. break;
  392. case WLAN_EID_MOBILITY_DOMAIN:
  393. elems->mdie = pos;
  394. elems->mdie_len = elen;
  395. break;
  396. case WLAN_EID_FAST_BSS_TRANSITION:
  397. elems->ftie = pos;
  398. elems->ftie_len = elen;
  399. break;
  400. case WLAN_EID_HT_CAP:
  401. elems->ht_capabilities = pos;
  402. elems->ht_capabilities_len = elen;
  403. break;
  404. case WLAN_EID_HT_OPERATION:
  405. elems->ht_operation = pos;
  406. elems->ht_operation_len = elen;
  407. break;
  408. default:
  409. unknown++;
  410. if (!show_errors)
  411. break;
  412. wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
  413. "ignored unknown element (id=%d elen=%d)",
  414. id, elen);
  415. break;
  416. }
  417. left -= elen;
  418. pos += elen;
  419. }
  420. if (left)
  421. return ParseFailed;
  422. return unknown ? ParseUnknown : ParseOK;
  423. }
  424. void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
  425. {
  426. int i;
  427. if (len > HOSTAPD_MAX_SSID_LEN)
  428. len = HOSTAPD_MAX_SSID_LEN;
  429. for (i = 0; i < len; i++) {
  430. if (ssid[i] >= 32 && ssid[i] < 127)
  431. buf[i] = ssid[i];
  432. else
  433. buf[i] = '.';
  434. }
  435. buf[len] = '\0';
  436. }
  437. void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
  438. {
  439. struct ieee80211_mgmt mgmt;
  440. char buf[30];
  441. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  442. HOSTAPD_LEVEL_DEBUG,
  443. "deauthenticate - reason %d", reason);
  444. os_snprintf(buf, sizeof(buf), "SEND-DEAUTHENTICATE %d", reason);
  445. os_memset(&mgmt, 0, sizeof(mgmt));
  446. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  447. WLAN_FC_STYPE_DEAUTH);
  448. os_memcpy(mgmt.da, addr, ETH_ALEN);
  449. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  450. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  451. mgmt.u.deauth.reason_code = host_to_le16(reason);
  452. if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
  453. sizeof(mgmt.u.deauth), 0) < 0)
  454. perror("ieee802_11_send_deauth: send");
  455. }
  456. static void ieee802_11_sta_authenticate(void *eloop_ctx, void *timeout_ctx)
  457. {
  458. struct hostapd_data *hapd = eloop_ctx;
  459. struct ieee80211_mgmt mgmt;
  460. char ssid_txt[33];
  461. if (hapd->assoc_ap_state == WAIT_BEACON)
  462. hapd->assoc_ap_state = AUTHENTICATE;
  463. if (hapd->assoc_ap_state != AUTHENTICATE)
  464. return;
  465. ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
  466. hapd->assoc_ap_ssid_len);
  467. printf("Authenticate with AP " MACSTR " SSID=%s (as station)\n",
  468. MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
  469. os_memset(&mgmt, 0, sizeof(mgmt));
  470. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  471. WLAN_FC_STYPE_AUTH);
  472. /* Request TX callback */
  473. mgmt.frame_control |= host_to_le16(BIT(1));
  474. os_memcpy(mgmt.da, hapd->conf->assoc_ap_addr, ETH_ALEN);
  475. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  476. os_memcpy(mgmt.bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
  477. mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
  478. mgmt.u.auth.auth_transaction = host_to_le16(1);
  479. mgmt.u.auth.status_code = host_to_le16(0);
  480. if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
  481. sizeof(mgmt.u.auth), 0) < 0)
  482. perror("ieee802_11_sta_authenticate: send");
  483. /* Try to authenticate again, if this attempt fails or times out. */
  484. eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
  485. }
  486. static void ieee802_11_sta_associate(void *eloop_ctx, void *timeout_ctx)
  487. {
  488. struct hostapd_data *hapd = eloop_ctx;
  489. u8 buf[256];
  490. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  491. u8 *p;
  492. char ssid_txt[33];
  493. if (hapd->assoc_ap_state == AUTHENTICATE)
  494. hapd->assoc_ap_state = ASSOCIATE;
  495. if (hapd->assoc_ap_state != ASSOCIATE)
  496. return;
  497. ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
  498. hapd->assoc_ap_ssid_len);
  499. printf("Associate with AP " MACSTR " SSID=%s (as station)\n",
  500. MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
  501. os_memset(mgmt, 0, sizeof(*mgmt));
  502. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  503. WLAN_FC_STYPE_ASSOC_REQ);
  504. /* Request TX callback */
  505. mgmt->frame_control |= host_to_le16(BIT(1));
  506. os_memcpy(mgmt->da, hapd->conf->assoc_ap_addr, ETH_ALEN);
  507. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  508. os_memcpy(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
  509. mgmt->u.assoc_req.capab_info = host_to_le16(0);
  510. mgmt->u.assoc_req.listen_interval = host_to_le16(1);
  511. p = &mgmt->u.assoc_req.variable[0];
  512. *p++ = WLAN_EID_SSID;
  513. *p++ = hapd->assoc_ap_ssid_len;
  514. os_memcpy(p, hapd->assoc_ap_ssid, hapd->assoc_ap_ssid_len);
  515. p += hapd->assoc_ap_ssid_len;
  516. p = hostapd_eid_supp_rates(hapd, p);
  517. p = hostapd_eid_ext_supp_rates(hapd, p);
  518. if (hostapd_send_mgmt_frame(hapd, mgmt, p - (u8 *) mgmt, 0) < 0)
  519. perror("ieee802_11_sta_associate: send");
  520. /* Try to authenticate again, if this attempt fails or times out. */
  521. eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
  522. }
  523. static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
  524. u16 auth_transaction, u8 *challenge, int iswep)
  525. {
  526. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  527. HOSTAPD_LEVEL_DEBUG,
  528. "authentication (shared key, transaction %d)",
  529. auth_transaction);
  530. if (auth_transaction == 1) {
  531. if (!sta->challenge) {
  532. /* Generate a pseudo-random challenge */
  533. u8 key[8];
  534. time_t now;
  535. int r;
  536. sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
  537. if (sta->challenge == NULL)
  538. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  539. now = time(NULL);
  540. r = random();
  541. os_memcpy(key, &now, 4);
  542. os_memcpy(key + 4, &r, 4);
  543. rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
  544. key, sizeof(key));
  545. }
  546. return 0;
  547. }
  548. if (auth_transaction != 3)
  549. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  550. /* Transaction 3 */
  551. if (!iswep || !sta->challenge || !challenge ||
  552. os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
  553. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  554. HOSTAPD_LEVEL_INFO,
  555. "shared key authentication - invalid "
  556. "challenge-response");
  557. return WLAN_STATUS_CHALLENGE_FAIL;
  558. }
  559. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  560. HOSTAPD_LEVEL_DEBUG,
  561. "authentication OK (shared key)");
  562. #ifdef IEEE80211_REQUIRE_AUTH_ACK
  563. /* Station will be marked authenticated if it ACKs the
  564. * authentication reply. */
  565. #else
  566. sta->flags |= WLAN_STA_AUTH;
  567. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  568. #endif
  569. os_free(sta->challenge);
  570. sta->challenge = NULL;
  571. return 0;
  572. }
  573. static void send_auth_reply(struct hostapd_data *hapd,
  574. const u8 *dst, const u8 *bssid,
  575. u16 auth_alg, u16 auth_transaction, u16 resp,
  576. const u8 *ies, size_t ies_len)
  577. {
  578. struct ieee80211_mgmt *reply;
  579. u8 *buf;
  580. size_t rlen;
  581. rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
  582. buf = os_zalloc(rlen);
  583. if (buf == NULL)
  584. return;
  585. reply = (struct ieee80211_mgmt *) buf;
  586. reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  587. WLAN_FC_STYPE_AUTH);
  588. /* Request TX callback */
  589. reply->frame_control |= host_to_le16(BIT(1));
  590. os_memcpy(reply->da, dst, ETH_ALEN);
  591. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  592. os_memcpy(reply->bssid, bssid, ETH_ALEN);
  593. reply->u.auth.auth_alg = host_to_le16(auth_alg);
  594. reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
  595. reply->u.auth.status_code = host_to_le16(resp);
  596. if (ies && ies_len)
  597. os_memcpy(reply->u.auth.variable, ies, ies_len);
  598. wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
  599. " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
  600. MAC2STR(dst), auth_alg, auth_transaction,
  601. resp, (unsigned long) ies_len);
  602. if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
  603. perror("send_auth_reply: send");
  604. os_free(buf);
  605. }
  606. #ifdef CONFIG_IEEE80211R
  607. static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
  608. u16 auth_transaction, u16 status,
  609. const u8 *ies, size_t ies_len)
  610. {
  611. struct hostapd_data *hapd = ctx;
  612. struct sta_info *sta;
  613. send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
  614. status, ies, ies_len);
  615. if (status != WLAN_STATUS_SUCCESS)
  616. return;
  617. sta = ap_get_sta(hapd, dst);
  618. if (sta == NULL)
  619. return;
  620. hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
  621. HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
  622. sta->flags |= WLAN_STA_AUTH;
  623. mlme_authenticate_indication(hapd, sta);
  624. }
  625. #endif /* CONFIG_IEEE80211R */
  626. static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
  627. size_t len)
  628. {
  629. u16 auth_alg, auth_transaction, status_code;
  630. u16 resp = WLAN_STATUS_SUCCESS;
  631. struct sta_info *sta = NULL;
  632. int res;
  633. u16 fc;
  634. u8 *challenge = NULL;
  635. u32 session_timeout, acct_interim_interval;
  636. int vlan_id = 0;
  637. u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
  638. size_t resp_ies_len = 0;
  639. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  640. printf("handle_auth - too short payload (len=%lu)\n",
  641. (unsigned long) len);
  642. return;
  643. }
  644. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  645. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  646. status_code = le_to_host16(mgmt->u.auth.status_code);
  647. fc = le_to_host16(mgmt->frame_control);
  648. if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
  649. 2 + WLAN_AUTH_CHALLENGE_LEN &&
  650. mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
  651. mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
  652. challenge = &mgmt->u.auth.variable[2];
  653. wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
  654. "auth_transaction=%d status_code=%d wep=%d%s",
  655. MAC2STR(mgmt->sa), auth_alg, auth_transaction,
  656. status_code, !!(fc & WLAN_FC_ISWEP),
  657. challenge ? " challenge" : "");
  658. if (hapd->assoc_ap_state == AUTHENTICATE && auth_transaction == 2 &&
  659. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0 &&
  660. os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  661. if (status_code != 0) {
  662. printf("Authentication (as station) with AP "
  663. MACSTR " failed (status_code=%d)\n",
  664. MAC2STR(hapd->conf->assoc_ap_addr),
  665. status_code);
  666. return;
  667. }
  668. printf("Authenticated (as station) with AP " MACSTR "\n",
  669. MAC2STR(hapd->conf->assoc_ap_addr));
  670. ieee802_11_sta_associate(hapd, NULL);
  671. return;
  672. }
  673. if (hapd->tkip_countermeasures) {
  674. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  675. goto fail;
  676. }
  677. if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
  678. auth_alg == WLAN_AUTH_OPEN) ||
  679. #ifdef CONFIG_IEEE80211R
  680. (hapd->conf->wpa &&
  681. (hapd->conf->wpa_key_mgmt &
  682. (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
  683. auth_alg == WLAN_AUTH_FT) ||
  684. #endif /* CONFIG_IEEE80211R */
  685. ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
  686. auth_alg == WLAN_AUTH_SHARED_KEY))) {
  687. printf("Unsupported authentication algorithm (%d)\n",
  688. auth_alg);
  689. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  690. goto fail;
  691. }
  692. if (!(auth_transaction == 1 ||
  693. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
  694. printf("Unknown authentication transaction number (%d)\n",
  695. auth_transaction);
  696. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  697. goto fail;
  698. }
  699. if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
  700. printf("Station " MACSTR " not allowed to authenticate.\n",
  701. MAC2STR(mgmt->sa));
  702. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  703. goto fail;
  704. }
  705. res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
  706. &session_timeout,
  707. &acct_interim_interval, &vlan_id);
  708. if (res == HOSTAPD_ACL_REJECT) {
  709. printf("Station " MACSTR " not allowed to authenticate.\n",
  710. MAC2STR(mgmt->sa));
  711. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  712. goto fail;
  713. }
  714. if (res == HOSTAPD_ACL_PENDING) {
  715. wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
  716. " waiting for an external authentication",
  717. MAC2STR(mgmt->sa));
  718. /* Authentication code will re-send the authentication frame
  719. * after it has received (and cached) information from the
  720. * external source. */
  721. return;
  722. }
  723. sta = ap_sta_add(hapd, mgmt->sa);
  724. if (!sta) {
  725. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  726. goto fail;
  727. }
  728. if (vlan_id > 0) {
  729. if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  730. sta->vlan_id) == NULL) {
  731. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  732. HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
  733. "%d received from RADIUS server",
  734. vlan_id);
  735. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  736. goto fail;
  737. }
  738. sta->vlan_id = vlan_id;
  739. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  740. HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
  741. }
  742. sta->flags &= ~WLAN_STA_PREAUTH;
  743. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  744. if (hapd->conf->radius->acct_interim_interval == 0 &&
  745. acct_interim_interval)
  746. sta->acct_interim_interval = acct_interim_interval;
  747. if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  748. ap_sta_session_timeout(hapd, sta, session_timeout);
  749. else
  750. ap_sta_no_session_timeout(hapd, sta);
  751. switch (auth_alg) {
  752. case WLAN_AUTH_OPEN:
  753. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  754. HOSTAPD_LEVEL_DEBUG,
  755. "authentication OK (open system)");
  756. #ifdef IEEE80211_REQUIRE_AUTH_ACK
  757. /* Station will be marked authenticated if it ACKs the
  758. * authentication reply. */
  759. #else
  760. sta->flags |= WLAN_STA_AUTH;
  761. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  762. sta->auth_alg = WLAN_AUTH_OPEN;
  763. mlme_authenticate_indication(hapd, sta);
  764. #endif
  765. break;
  766. case WLAN_AUTH_SHARED_KEY:
  767. resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
  768. fc & WLAN_FC_ISWEP);
  769. sta->auth_alg = WLAN_AUTH_SHARED_KEY;
  770. mlme_authenticate_indication(hapd, sta);
  771. if (sta->challenge && auth_transaction == 1) {
  772. resp_ies[0] = WLAN_EID_CHALLENGE;
  773. resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
  774. os_memcpy(resp_ies + 2, sta->challenge,
  775. WLAN_AUTH_CHALLENGE_LEN);
  776. resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
  777. }
  778. break;
  779. #ifdef CONFIG_IEEE80211R
  780. case WLAN_AUTH_FT:
  781. sta->auth_alg = WLAN_AUTH_FT;
  782. if (sta->wpa_sm == NULL)
  783. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  784. sta->addr);
  785. if (sta->wpa_sm == NULL) {
  786. wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
  787. "state machine");
  788. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  789. goto fail;
  790. }
  791. wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
  792. auth_transaction, mgmt->u.auth.variable,
  793. len - IEEE80211_HDRLEN -
  794. sizeof(mgmt->u.auth),
  795. handle_auth_ft_finish, hapd);
  796. /* handle_auth_ft_finish() callback will complete auth. */
  797. return;
  798. #endif /* CONFIG_IEEE80211R */
  799. }
  800. fail:
  801. send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
  802. auth_transaction + 1, resp, resp_ies, resp_ies_len);
  803. }
  804. static void handle_assoc(struct hostapd_data *hapd,
  805. struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
  806. {
  807. u16 capab_info, listen_interval;
  808. u16 resp = WLAN_STATUS_SUCCESS;
  809. u8 *pos, *wpa_ie;
  810. size_t wpa_ie_len;
  811. int send_deauth = 0, send_len, left, i;
  812. struct sta_info *sta;
  813. struct ieee802_11_elems elems;
  814. u8 buf[sizeof(struct ieee80211_mgmt) + 512];
  815. struct ieee80211_mgmt *reply;
  816. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
  817. sizeof(mgmt->u.assoc_req))) {
  818. printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
  819. "\n", reassoc, (unsigned long) len);
  820. return;
  821. }
  822. if (reassoc) {
  823. capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
  824. listen_interval = le_to_host16(
  825. mgmt->u.reassoc_req.listen_interval);
  826. wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
  827. " capab_info=0x%02x listen_interval=%d current_ap="
  828. MACSTR,
  829. MAC2STR(mgmt->sa), capab_info, listen_interval,
  830. MAC2STR(mgmt->u.reassoc_req.current_ap));
  831. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
  832. pos = mgmt->u.reassoc_req.variable;
  833. } else {
  834. capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  835. listen_interval = le_to_host16(
  836. mgmt->u.assoc_req.listen_interval);
  837. wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
  838. " capab_info=0x%02x listen_interval=%d",
  839. MAC2STR(mgmt->sa), capab_info, listen_interval);
  840. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
  841. pos = mgmt->u.assoc_req.variable;
  842. }
  843. sta = ap_get_sta(hapd, mgmt->sa);
  844. #ifdef CONFIG_IEEE80211R
  845. if (sta && sta->auth_alg == WLAN_AUTH_FT &&
  846. (sta->flags & WLAN_STA_AUTH) == 0) {
  847. wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
  848. "prior to authentication since it is using "
  849. "over-the-DS FT", MAC2STR(mgmt->sa));
  850. } else
  851. #endif /* CONFIG_IEEE80211R */
  852. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  853. printf("STA " MACSTR " trying to associate before "
  854. "authentication\n", MAC2STR(mgmt->sa));
  855. if (sta) {
  856. printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
  857. MAC2STR(sta->addr), sta->aid, sta->flags);
  858. }
  859. send_deauth = 1;
  860. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  861. goto fail;
  862. }
  863. if (hapd->tkip_countermeasures) {
  864. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  865. goto fail;
  866. }
  867. if (listen_interval > hapd->conf->max_listen_interval) {
  868. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  869. HOSTAPD_LEVEL_DEBUG,
  870. "Too large Listen Interval (%d)",
  871. listen_interval);
  872. resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
  873. goto fail;
  874. }
  875. if (reassoc) {
  876. os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
  877. ETH_ALEN);
  878. }
  879. sta->capability = capab_info;
  880. /* followed by SSID and Supported rates; and HT capabilities if 802.11n
  881. * is used */
  882. if (ieee802_11_parse_elems(hapd, pos, left, &elems, 1) == ParseFailed
  883. || !elems.ssid) {
  884. printf("STA " MACSTR " sent invalid association request\n",
  885. MAC2STR(sta->addr));
  886. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  887. goto fail;
  888. }
  889. if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  890. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
  891. {
  892. char ssid_txt[33];
  893. ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
  894. printf("Station " MACSTR " tried to associate with "
  895. "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
  896. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  897. goto fail;
  898. }
  899. sta->flags &= ~WLAN_STA_WME;
  900. if (elems.wme && hapd->conf->wme_enabled) {
  901. if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
  902. hostapd_logger(hapd, sta->addr,
  903. HOSTAPD_MODULE_WPA,
  904. HOSTAPD_LEVEL_DEBUG,
  905. "invalid WME element in association "
  906. "request");
  907. else
  908. sta->flags |= WLAN_STA_WME;
  909. }
  910. if (!elems.supp_rates) {
  911. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  912. HOSTAPD_LEVEL_DEBUG,
  913. "No supported rates element in AssocReq");
  914. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  915. goto fail;
  916. }
  917. if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
  918. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  919. HOSTAPD_LEVEL_DEBUG,
  920. "Invalid supported rates element length %d",
  921. elems.supp_rates_len);
  922. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  923. goto fail;
  924. }
  925. os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  926. os_memcpy(sta->supported_rates, elems.supp_rates,
  927. elems.supp_rates_len);
  928. sta->supported_rates_len = elems.supp_rates_len;
  929. if (elems.ext_supp_rates) {
  930. if (elems.supp_rates_len + elems.ext_supp_rates_len >
  931. sizeof(sta->supported_rates)) {
  932. hostapd_logger(hapd, mgmt->sa,
  933. HOSTAPD_MODULE_IEEE80211,
  934. HOSTAPD_LEVEL_DEBUG,
  935. "Invalid supported rates element length"
  936. " %d+%d", elems.supp_rates_len,
  937. elems.ext_supp_rates_len);
  938. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  939. goto fail;
  940. }
  941. os_memcpy(sta->supported_rates + elems.supp_rates_len,
  942. elems.ext_supp_rates, elems.ext_supp_rates_len);
  943. sta->supported_rates_len += elems.ext_supp_rates_len;
  944. }
  945. #ifdef CONFIG_IEEE80211N
  946. /* save HT capabilities in the sta object */
  947. os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
  948. if (elems.ht_capabilities &&
  949. elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_capability)
  950. && (sta->flags & WLAN_STA_WME)) {
  951. /* note: without WMM capability, treat the sta as non-HT */
  952. sta->flags |= WLAN_STA_HT;
  953. sta->ht_capabilities.id = WLAN_EID_HT_CAP;
  954. sta->ht_capabilities.length =
  955. sizeof(struct ieee80211_ht_capability);
  956. os_memcpy(&sta->ht_capabilities.data,
  957. elems.ht_capabilities,
  958. sizeof(struct ieee80211_ht_capability));
  959. } else
  960. sta->flags &= ~WLAN_STA_HT;
  961. #endif /* CONFIG_IEEE80211N */
  962. if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
  963. wpa_ie = elems.rsn_ie;
  964. wpa_ie_len = elems.rsn_ie_len;
  965. } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
  966. elems.wpa_ie) {
  967. wpa_ie = elems.wpa_ie;
  968. wpa_ie_len = elems.wpa_ie_len;
  969. } else {
  970. wpa_ie = NULL;
  971. wpa_ie_len = 0;
  972. }
  973. if (hapd->conf->wpa && wpa_ie == NULL) {
  974. printf("STA " MACSTR ": No WPA/RSN IE in association "
  975. "request\n", MAC2STR(sta->addr));
  976. resp = WLAN_STATUS_INVALID_IE;
  977. goto fail;
  978. }
  979. if (hapd->conf->wpa && wpa_ie) {
  980. int res;
  981. wpa_ie -= 2;
  982. wpa_ie_len += 2;
  983. if (sta->wpa_sm == NULL)
  984. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  985. sta->addr);
  986. if (sta->wpa_sm == NULL) {
  987. printf("Failed to initialize WPA state machine\n");
  988. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  989. goto fail;
  990. }
  991. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  992. wpa_ie, wpa_ie_len,
  993. elems.mdie, elems.mdie_len);
  994. if (res == WPA_INVALID_GROUP)
  995. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  996. else if (res == WPA_INVALID_PAIRWISE)
  997. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  998. else if (res == WPA_INVALID_AKMP)
  999. resp = WLAN_STATUS_AKMP_NOT_VALID;
  1000. else if (res == WPA_ALLOC_FAIL)
  1001. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1002. #ifdef CONFIG_IEEE80211W
  1003. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  1004. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  1005. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  1006. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  1007. #endif /* CONFIG_IEEE80211W */
  1008. else if (res == WPA_INVALID_MDIE)
  1009. resp = WLAN_STATUS_INVALID_MDIE;
  1010. else if (res != WPA_IE_OK)
  1011. resp = WLAN_STATUS_INVALID_IE;
  1012. if (resp != WLAN_STATUS_SUCCESS)
  1013. goto fail;
  1014. #ifdef CONFIG_IEEE80211W
  1015. if (wpa_auth_uses_mfp(sta->wpa_sm))
  1016. sta->flags |= WLAN_STA_MFP;
  1017. else
  1018. sta->flags &= ~WLAN_STA_MFP;
  1019. #endif /* CONFIG_IEEE80211W */
  1020. #ifdef CONFIG_IEEE80211R
  1021. if (sta->auth_alg == WLAN_AUTH_FT) {
  1022. if (!reassoc) {
  1023. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  1024. "to use association (not "
  1025. "re-association) with FT auth_alg",
  1026. MAC2STR(sta->addr));
  1027. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1028. goto fail;
  1029. }
  1030. resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
  1031. if (resp != WLAN_STATUS_SUCCESS)
  1032. goto fail;
  1033. }
  1034. #endif /* CONFIG_IEEE80211R */
  1035. }
  1036. if (hapd->iface->dfs_enable &&
  1037. hapd->iconf->ieee80211h == SPECT_STRICT_BINDING) {
  1038. if (hostapd_check_power_cap(hapd, elems.power_cap,
  1039. elems.power_cap_len)) {
  1040. resp = WLAN_STATUS_PWR_CAPABILITY_NOT_VALID;
  1041. hostapd_logger(hapd, sta->addr,
  1042. HOSTAPD_MODULE_IEEE80211,
  1043. HOSTAPD_LEVEL_DEBUG,
  1044. "Power capabilities of the station not "
  1045. "acceptable");
  1046. goto fail;
  1047. }
  1048. }
  1049. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  1050. sta->flags |= WLAN_STA_NONERP;
  1051. for (i = 0; i < sta->supported_rates_len; i++) {
  1052. if ((sta->supported_rates[i] & 0x7f) > 22) {
  1053. sta->flags &= ~WLAN_STA_NONERP;
  1054. break;
  1055. }
  1056. }
  1057. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  1058. sta->nonerp_set = 1;
  1059. hapd->iface->num_sta_non_erp++;
  1060. if (hapd->iface->num_sta_non_erp == 1)
  1061. ieee802_11_set_beacons(hapd->iface);
  1062. }
  1063. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  1064. !sta->no_short_slot_time_set) {
  1065. sta->no_short_slot_time_set = 1;
  1066. hapd->iface->num_sta_no_short_slot_time++;
  1067. if (hapd->iface->current_mode->mode ==
  1068. HOSTAPD_MODE_IEEE80211G &&
  1069. hapd->iface->num_sta_no_short_slot_time == 1)
  1070. ieee802_11_set_beacons(hapd->iface);
  1071. }
  1072. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  1073. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  1074. else
  1075. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  1076. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  1077. !sta->no_short_preamble_set) {
  1078. sta->no_short_preamble_set = 1;
  1079. hapd->iface->num_sta_no_short_preamble++;
  1080. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  1081. && hapd->iface->num_sta_no_short_preamble == 1)
  1082. ieee802_11_set_beacons(hapd->iface);
  1083. }
  1084. #ifdef CONFIG_IEEE80211N
  1085. if (sta->flags & WLAN_STA_HT) {
  1086. if ((sta->ht_capabilities.data.capabilities_info &
  1087. HT_CAP_INFO_GREEN_FIELD) == 0) {
  1088. hapd->iface->num_sta_ht_no_gf++;
  1089. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
  1090. "greenfield, num of non-gf stations %d",
  1091. __func__, MAC2STR(sta->addr),
  1092. hapd->iface->num_sta_ht_no_gf);
  1093. }
  1094. if ((sta->ht_capabilities.data.capabilities_info &
  1095. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
  1096. hapd->iface->num_sta_ht_20mhz++;
  1097. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
  1098. "num of 20MHz HT STAs %d",
  1099. __func__, MAC2STR(sta->addr),
  1100. hapd->iface->num_sta_ht_20mhz);
  1101. }
  1102. } else {
  1103. hapd->iface->num_sta_no_ht++;
  1104. if (hapd->iconf->ieee80211n) {
  1105. wpa_printf(MSG_DEBUG, "%s STA " MACSTR
  1106. " - no HT, num of non-HT stations %d",
  1107. __func__, MAC2STR(sta->addr),
  1108. hapd->iface->num_sta_no_ht);
  1109. }
  1110. }
  1111. if (hostapd_ht_operation_update(hapd->iface) > 0)
  1112. ieee802_11_set_beacons(hapd->iface);
  1113. #endif /* CONFIG_IEEE80211N */
  1114. /* get a unique AID */
  1115. if (sta->aid > 0) {
  1116. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  1117. } else {
  1118. for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
  1119. if (hapd->sta_aid[sta->aid - 1] == NULL)
  1120. break;
  1121. if (sta->aid > MAX_AID_TABLE_SIZE) {
  1122. sta->aid = 0;
  1123. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  1124. wpa_printf(MSG_ERROR, " no room for more AIDs");
  1125. goto fail;
  1126. } else {
  1127. hapd->sta_aid[sta->aid - 1] = sta;
  1128. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  1129. }
  1130. }
  1131. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1132. HOSTAPD_LEVEL_DEBUG,
  1133. "association OK (aid %d)", sta->aid);
  1134. /* Station will be marked associated, after it acknowledges AssocResp
  1135. */
  1136. if (sta->last_assoc_req)
  1137. os_free(sta->last_assoc_req);
  1138. sta->last_assoc_req = os_malloc(len);
  1139. if (sta->last_assoc_req)
  1140. os_memcpy(sta->last_assoc_req, mgmt, len);
  1141. /* Make sure that the previously registered inactivity timer will not
  1142. * remove the STA immediately. */
  1143. sta->timeout_next = STA_NULLFUNC;
  1144. fail:
  1145. os_memset(buf, 0, sizeof(buf));
  1146. reply = (struct ieee80211_mgmt *) buf;
  1147. reply->frame_control =
  1148. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1149. (send_deauth ? WLAN_FC_STYPE_DEAUTH :
  1150. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  1151. WLAN_FC_STYPE_ASSOC_RESP)));
  1152. os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
  1153. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  1154. os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
  1155. send_len = IEEE80211_HDRLEN;
  1156. if (send_deauth) {
  1157. send_len += sizeof(reply->u.deauth);
  1158. reply->u.deauth.reason_code = host_to_le16(resp);
  1159. } else {
  1160. u8 *p;
  1161. send_len += sizeof(reply->u.assoc_resp);
  1162. reply->u.assoc_resp.capab_info =
  1163. host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
  1164. reply->u.assoc_resp.status_code = host_to_le16(resp);
  1165. reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
  1166. | BIT(14) | BIT(15));
  1167. /* Supported rates */
  1168. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  1169. /* Extended supported rates */
  1170. p = hostapd_eid_ext_supp_rates(hapd, p);
  1171. if (sta->flags & WLAN_STA_WME)
  1172. p = hostapd_eid_wme(hapd, p);
  1173. p = hostapd_eid_ht_capabilities_info(hapd, p);
  1174. p = hostapd_eid_ht_operation(hapd, p);
  1175. #ifdef CONFIG_IEEE80211R
  1176. if (resp == WLAN_STATUS_SUCCESS) {
  1177. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  1178. * Transition Information, RSN */
  1179. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  1180. buf + sizeof(buf) - p,
  1181. sta->auth_alg);
  1182. }
  1183. #endif /* CONFIG_IEEE80211R */
  1184. send_len += p - reply->u.assoc_resp.variable;
  1185. /* Request TX callback */
  1186. reply->frame_control |= host_to_le16(BIT(1));
  1187. }
  1188. if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
  1189. perror("handle_assoc: send");
  1190. }
  1191. static void handle_assoc_resp(struct hostapd_data *hapd,
  1192. struct ieee80211_mgmt *mgmt, size_t len)
  1193. {
  1194. u16 status_code, aid;
  1195. if (hapd->assoc_ap_state != ASSOCIATE) {
  1196. printf("Unexpected association response received from " MACSTR
  1197. "\n", MAC2STR(mgmt->sa));
  1198. return;
  1199. }
  1200. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
  1201. printf("handle_assoc_resp - too short payload (len=%lu)\n",
  1202. (unsigned long) len);
  1203. return;
  1204. }
  1205. if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
  1206. os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
  1207. printf("Received association response from unexpected address "
  1208. "(SA=" MACSTR " BSSID=" MACSTR "\n",
  1209. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  1210. return;
  1211. }
  1212. status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
  1213. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  1214. aid &= ~(BIT(14) | BIT(15));
  1215. if (status_code != 0) {
  1216. printf("Association (as station) with AP " MACSTR " failed "
  1217. "(status_code=%d)\n",
  1218. MAC2STR(hapd->conf->assoc_ap_addr), status_code);
  1219. /* Try to authenticate again */
  1220. hapd->assoc_ap_state = AUTHENTICATE;
  1221. eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
  1222. hapd, NULL);
  1223. }
  1224. printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
  1225. MAC2STR(hapd->conf->assoc_ap_addr), aid);
  1226. hapd->assoc_ap_aid = aid;
  1227. hapd->assoc_ap_state = ASSOCIATED;
  1228. if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
  1229. printf("Could not set associated AP address to kernel "
  1230. "driver.\n");
  1231. }
  1232. }
  1233. static void handle_disassoc(struct hostapd_data *hapd,
  1234. struct ieee80211_mgmt *mgmt, size_t len)
  1235. {
  1236. struct sta_info *sta;
  1237. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  1238. printf("handle_disassoc - too short payload (len=%lu)\n",
  1239. (unsigned long) len);
  1240. return;
  1241. }
  1242. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  1243. MAC2STR(mgmt->sa),
  1244. le_to_host16(mgmt->u.disassoc.reason_code));
  1245. if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
  1246. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1247. printf("Assoc AP " MACSTR " sent disassociation "
  1248. "(reason_code=%d) - try to authenticate\n",
  1249. MAC2STR(hapd->conf->assoc_ap_addr),
  1250. le_to_host16(mgmt->u.disassoc.reason_code));
  1251. hapd->assoc_ap_state = AUTHENTICATE;
  1252. ieee802_11_sta_authenticate(hapd, NULL);
  1253. eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
  1254. hapd, NULL);
  1255. return;
  1256. }
  1257. sta = ap_get_sta(hapd, mgmt->sa);
  1258. if (sta == NULL) {
  1259. printf("Station " MACSTR " trying to disassociate, but it "
  1260. "is not associated.\n", MAC2STR(mgmt->sa));
  1261. return;
  1262. }
  1263. sta->flags &= ~WLAN_STA_ASSOC;
  1264. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  1265. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1266. HOSTAPD_LEVEL_INFO, "disassociated");
  1267. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1268. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1269. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  1270. * authenticated. */
  1271. accounting_sta_stop(hapd, sta);
  1272. ieee802_1x_free_station(sta);
  1273. hostapd_sta_remove(hapd, sta->addr);
  1274. if (sta->timeout_next == STA_NULLFUNC ||
  1275. sta->timeout_next == STA_DISASSOC) {
  1276. sta->timeout_next = STA_DEAUTH;
  1277. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1278. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  1279. hapd, sta);
  1280. }
  1281. mlme_disassociate_indication(
  1282. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  1283. }
  1284. static void handle_deauth(struct hostapd_data *hapd,
  1285. struct ieee80211_mgmt *mgmt, size_t len)
  1286. {
  1287. struct sta_info *sta;
  1288. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  1289. printf("handle_deauth - too short payload (len=%lu)\n",
  1290. (unsigned long) len);
  1291. return;
  1292. }
  1293. wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
  1294. " reason_code=%d",
  1295. MAC2STR(mgmt->sa),
  1296. le_to_host16(mgmt->u.deauth.reason_code));
  1297. if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
  1298. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1299. printf("Assoc AP " MACSTR " sent deauthentication "
  1300. "(reason_code=%d) - try to authenticate\n",
  1301. MAC2STR(hapd->conf->assoc_ap_addr),
  1302. le_to_host16(mgmt->u.deauth.reason_code));
  1303. hapd->assoc_ap_state = AUTHENTICATE;
  1304. eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
  1305. hapd, NULL);
  1306. return;
  1307. }
  1308. sta = ap_get_sta(hapd, mgmt->sa);
  1309. if (sta == NULL) {
  1310. printf("Station " MACSTR " trying to deauthenticate, but it "
  1311. "is not authenticated.\n", MAC2STR(mgmt->sa));
  1312. return;
  1313. }
  1314. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1315. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1316. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1317. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  1318. mlme_deauthenticate_indication(
  1319. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  1320. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1321. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1322. ap_free_sta(hapd, sta);
  1323. }
  1324. static void handle_beacon(struct hostapd_data *hapd,
  1325. struct ieee80211_mgmt *mgmt, size_t len,
  1326. struct hostapd_frame_info *fi)
  1327. {
  1328. struct ieee802_11_elems elems;
  1329. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  1330. printf("handle_beacon - too short payload (len=%lu)\n",
  1331. (unsigned long) len);
  1332. return;
  1333. }
  1334. (void) ieee802_11_parse_elems(hapd, mgmt->u.beacon.variable,
  1335. len - (IEEE80211_HDRLEN +
  1336. sizeof(mgmt->u.beacon)), &elems,
  1337. 0);
  1338. if (hapd->assoc_ap_state == WAIT_BEACON &&
  1339. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1340. if (elems.ssid && elems.ssid_len <= 32) {
  1341. os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
  1342. elems.ssid_len);
  1343. hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
  1344. hapd->assoc_ap_ssid_len = elems.ssid_len;
  1345. }
  1346. ieee802_11_sta_authenticate(hapd, NULL);
  1347. }
  1348. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  1349. }
  1350. static void handle_action(struct hostapd_data *hapd,
  1351. struct ieee80211_mgmt *mgmt, size_t len)
  1352. {
  1353. if (len < IEEE80211_HDRLEN + 1) {
  1354. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1355. HOSTAPD_LEVEL_DEBUG,
  1356. "handle_action - too short payload (len=%lu)",
  1357. (unsigned long) len);
  1358. return;
  1359. }
  1360. switch (mgmt->u.action.category) {
  1361. #ifdef CONFIG_IEEE80211R
  1362. case WLAN_ACTION_FT:
  1363. {
  1364. struct sta_info *sta;
  1365. sta = ap_get_sta(hapd, mgmt->sa);
  1366. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  1367. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
  1368. "frame from unassociated STA " MACSTR,
  1369. MAC2STR(mgmt->sa));
  1370. return;
  1371. }
  1372. if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  1373. len - IEEE80211_HDRLEN))
  1374. break;
  1375. return;
  1376. }
  1377. #endif /* CONFIG_IEEE80211R */
  1378. case WME_ACTION_CATEGORY:
  1379. hostapd_wme_action(hapd, mgmt, len);
  1380. return;
  1381. }
  1382. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1383. HOSTAPD_LEVEL_DEBUG,
  1384. "handle_action - unknown action category %d or invalid "
  1385. "frame",
  1386. mgmt->u.action.category);
  1387. if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
  1388. !(mgmt->sa[0] & 0x01)) {
  1389. /*
  1390. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  1391. * Return the Action frame to the source without change
  1392. * except that MSB of the Category set to 1.
  1393. */
  1394. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  1395. "frame back to sender");
  1396. os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
  1397. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  1398. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  1399. mgmt->u.action.category |= 0x80;
  1400. hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
  1401. }
  1402. }
  1403. /**
  1404. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  1405. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  1406. * sent to)
  1407. * @buf: management frame data (starting from IEEE 802.11 header)
  1408. * @len: length of frame data in octets
  1409. * @stype: management frame subtype from frame control field
  1410. *
  1411. * Process all incoming IEEE 802.11 management frames. This will be called for
  1412. * each frame received from the kernel driver through wlan#ap interface. In
  1413. * addition, it can be called to re-inserted pending frames (e.g., when using
  1414. * external RADIUS server as an MAC ACL).
  1415. */
  1416. void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
  1417. struct hostapd_frame_info *fi)
  1418. {
  1419. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1420. int broadcast;
  1421. if (stype == WLAN_FC_STYPE_BEACON) {
  1422. handle_beacon(hapd, mgmt, len, fi);
  1423. return;
  1424. }
  1425. if (fi && fi->passive_scan)
  1426. return;
  1427. broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
  1428. mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
  1429. mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
  1430. if (!broadcast &&
  1431. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
  1432. (hapd->assoc_ap_state == DO_NOT_ASSOC ||
  1433. os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
  1434. {
  1435. printf("MGMT: BSSID=" MACSTR " not our address\n",
  1436. MAC2STR(mgmt->bssid));
  1437. return;
  1438. }
  1439. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  1440. handle_probe_req(hapd, mgmt, len);
  1441. return;
  1442. }
  1443. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  1444. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1445. HOSTAPD_LEVEL_DEBUG,
  1446. "MGMT: DA=" MACSTR " not our address",
  1447. MAC2STR(mgmt->da));
  1448. return;
  1449. }
  1450. switch (stype) {
  1451. case WLAN_FC_STYPE_AUTH:
  1452. wpa_printf(MSG_DEBUG, "mgmt::auth");
  1453. handle_auth(hapd, mgmt, len);
  1454. break;
  1455. case WLAN_FC_STYPE_ASSOC_REQ:
  1456. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  1457. handle_assoc(hapd, mgmt, len, 0);
  1458. break;
  1459. case WLAN_FC_STYPE_ASSOC_RESP:
  1460. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
  1461. handle_assoc_resp(hapd, mgmt, len);
  1462. break;
  1463. case WLAN_FC_STYPE_REASSOC_REQ:
  1464. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  1465. handle_assoc(hapd, mgmt, len, 1);
  1466. break;
  1467. case WLAN_FC_STYPE_DISASSOC:
  1468. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  1469. handle_disassoc(hapd, mgmt, len);
  1470. break;
  1471. case WLAN_FC_STYPE_DEAUTH:
  1472. wpa_printf(MSG_DEBUG, "mgmt::deauth");
  1473. handle_deauth(hapd, mgmt, len);
  1474. break;
  1475. case WLAN_FC_STYPE_ACTION:
  1476. wpa_printf(MSG_DEBUG, "mgmt::action");
  1477. handle_action(hapd, mgmt, len);
  1478. break;
  1479. default:
  1480. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1481. HOSTAPD_LEVEL_DEBUG,
  1482. "unknown mgmt frame subtype %d", stype);
  1483. break;
  1484. }
  1485. }
  1486. static void handle_auth_cb(struct hostapd_data *hapd,
  1487. struct ieee80211_mgmt *mgmt,
  1488. size_t len, int ok)
  1489. {
  1490. u16 auth_alg, auth_transaction, status_code;
  1491. struct sta_info *sta;
  1492. if (!ok) {
  1493. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1494. HOSTAPD_LEVEL_NOTICE,
  1495. "did not acknowledge authentication response");
  1496. return;
  1497. }
  1498. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1499. printf("handle_auth_cb - too short payload (len=%lu)\n",
  1500. (unsigned long) len);
  1501. return;
  1502. }
  1503. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1504. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1505. status_code = le_to_host16(mgmt->u.auth.status_code);
  1506. sta = ap_get_sta(hapd, mgmt->da);
  1507. if (!sta) {
  1508. printf("handle_auth_cb: STA " MACSTR " not found\n",
  1509. MAC2STR(mgmt->da));
  1510. return;
  1511. }
  1512. if (status_code == WLAN_STATUS_SUCCESS &&
  1513. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  1514. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  1515. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1516. HOSTAPD_LEVEL_INFO, "authenticated");
  1517. sta->flags |= WLAN_STA_AUTH;
  1518. }
  1519. }
  1520. static void handle_assoc_cb(struct hostapd_data *hapd,
  1521. struct ieee80211_mgmt *mgmt,
  1522. size_t len, int reassoc, int ok)
  1523. {
  1524. u16 status;
  1525. struct sta_info *sta;
  1526. int new_assoc = 1;
  1527. struct ht_cap_ie *ht_cap = NULL;
  1528. if (!ok) {
  1529. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1530. HOSTAPD_LEVEL_DEBUG,
  1531. "did not acknowledge association response");
  1532. return;
  1533. }
  1534. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  1535. sizeof(mgmt->u.assoc_resp))) {
  1536. printf("handle_assoc_cb(reassoc=%d) - too short payload "
  1537. "(len=%lu)\n", reassoc, (unsigned long) len);
  1538. return;
  1539. }
  1540. if (reassoc)
  1541. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  1542. else
  1543. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1544. sta = ap_get_sta(hapd, mgmt->da);
  1545. if (!sta) {
  1546. printf("handle_assoc_cb: STA " MACSTR " not found\n",
  1547. MAC2STR(mgmt->da));
  1548. return;
  1549. }
  1550. if (status != WLAN_STATUS_SUCCESS)
  1551. goto fail;
  1552. /* Stop previous accounting session, if one is started, and allocate
  1553. * new session id for the new session. */
  1554. accounting_sta_stop(hapd, sta);
  1555. accounting_sta_get_id(hapd, sta);
  1556. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1557. HOSTAPD_LEVEL_INFO,
  1558. "associated (aid %d, accounting session %08X-%08X)",
  1559. sta->aid, sta->acct_session_id_hi,
  1560. sta->acct_session_id_lo);
  1561. if (sta->flags & WLAN_STA_ASSOC)
  1562. new_assoc = 0;
  1563. sta->flags |= WLAN_STA_ASSOC;
  1564. if (reassoc)
  1565. mlme_reassociate_indication(hapd, sta);
  1566. else
  1567. mlme_associate_indication(hapd, sta);
  1568. #ifdef CONFIG_IEEE80211N
  1569. if (sta->flags & WLAN_STA_HT)
  1570. ht_cap = &sta->ht_capabilities;
  1571. #endif /* CONFIG_IEEE80211N */
  1572. if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
  1573. sta->capability, sta->supported_rates,
  1574. sta->supported_rates_len, 0, sta->listen_interval,
  1575. ht_cap))
  1576. {
  1577. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1578. HOSTAPD_LEVEL_NOTICE,
  1579. "Could not add STA to kernel driver");
  1580. }
  1581. if (sta->eapol_sm == NULL) {
  1582. /*
  1583. * This STA does not use RADIUS server for EAP authentication,
  1584. * so bind it to the selected VLAN interface now, since the
  1585. * interface selection is not going to change anymore.
  1586. */
  1587. ap_sta_bind_vlan(hapd, sta, 0);
  1588. } else if (sta->vlan_id) {
  1589. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  1590. ap_sta_bind_vlan(hapd, sta, 0);
  1591. }
  1592. if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
  1593. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1594. WLAN_STA_SHORT_PREAMBLE, ~0);
  1595. } else {
  1596. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1597. 0, ~WLAN_STA_SHORT_PREAMBLE);
  1598. }
  1599. if (sta->auth_alg == WLAN_AUTH_FT)
  1600. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  1601. else
  1602. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  1603. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  1604. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  1605. fail:
  1606. /* Copy of the association request is not needed anymore */
  1607. if (sta->last_assoc_req) {
  1608. os_free(sta->last_assoc_req);
  1609. sta->last_assoc_req = NULL;
  1610. }
  1611. }
  1612. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  1613. u16 stype, int ok)
  1614. {
  1615. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1616. switch (stype) {
  1617. case WLAN_FC_STYPE_AUTH:
  1618. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  1619. handle_auth_cb(hapd, mgmt, len, ok);
  1620. break;
  1621. case WLAN_FC_STYPE_ASSOC_RESP:
  1622. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  1623. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  1624. break;
  1625. case WLAN_FC_STYPE_REASSOC_RESP:
  1626. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  1627. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  1628. break;
  1629. case WLAN_FC_STYPE_PROBE_RESP:
  1630. wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
  1631. break;
  1632. case WLAN_FC_STYPE_DEAUTH:
  1633. /* ignore */
  1634. break;
  1635. default:
  1636. printf("unknown mgmt cb frame subtype %d\n", stype);
  1637. break;
  1638. }
  1639. }
  1640. static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
  1641. void *timeout_ctx)
  1642. {
  1643. struct hostapd_data *hapd = eloop_ctx;
  1644. hapd->tkip_countermeasures = 0;
  1645. hostapd_set_countermeasures(hapd, 0);
  1646. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1647. HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
  1648. }
  1649. static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
  1650. {
  1651. struct sta_info *sta;
  1652. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1653. HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
  1654. wpa_auth_countermeasures_start(hapd->wpa_auth);
  1655. hapd->tkip_countermeasures = 1;
  1656. hostapd_set_countermeasures(hapd, 1);
  1657. wpa_gtk_rekey(hapd->wpa_auth);
  1658. eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
  1659. eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
  1660. hapd, NULL);
  1661. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  1662. hostapd_sta_deauth(hapd, sta->addr,
  1663. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1664. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1665. WLAN_STA_AUTHORIZED);
  1666. hostapd_sta_remove(hapd, sta->addr);
  1667. }
  1668. }
  1669. void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
  1670. int local)
  1671. {
  1672. time_t now;
  1673. if (addr && local) {
  1674. struct sta_info *sta = ap_get_sta(hapd, addr);
  1675. if (sta != NULL) {
  1676. wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
  1677. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  1678. HOSTAPD_LEVEL_INFO,
  1679. "Michael MIC failure detected in "
  1680. "received frame");
  1681. mlme_michaelmicfailure_indication(hapd, addr);
  1682. } else {
  1683. wpa_printf(MSG_DEBUG,
  1684. "MLME-MICHAELMICFAILURE.indication "
  1685. "for not associated STA (" MACSTR
  1686. ") ignored", MAC2STR(addr));
  1687. return;
  1688. }
  1689. }
  1690. time(&now);
  1691. if (now > hapd->michael_mic_failure + 60) {
  1692. hapd->michael_mic_failures = 1;
  1693. } else {
  1694. hapd->michael_mic_failures++;
  1695. if (hapd->michael_mic_failures > 1)
  1696. ieee80211_tkip_countermeasures_start(hapd);
  1697. }
  1698. hapd->michael_mic_failure = now;
  1699. }
  1700. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1701. {
  1702. /* TODO */
  1703. return 0;
  1704. }
  1705. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1706. char *buf, size_t buflen)
  1707. {
  1708. /* TODO */
  1709. return 0;
  1710. }
  1711. #endif /* CONFIG_NATIVE_WINDOWS */