ieee802_11.c 58 KB

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