ieee802_11.c 55 KB

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