ieee802_11.c 50 KB

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