ieee802_11.c 58 KB

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