ieee802_11.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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 (reassoc) {
  748. os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
  749. ETH_ALEN);
  750. }
  751. sta->capability = capab_info;
  752. /* followed by SSID and Supported rates */
  753. if (ieee802_11_parse_elems(hapd, pos, left, &elems, 1) == ParseFailed
  754. || !elems.ssid) {
  755. printf("STA " MACSTR " sent invalid association request\n",
  756. MAC2STR(sta->addr));
  757. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  758. goto fail;
  759. }
  760. if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  761. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
  762. {
  763. char ssid_txt[33];
  764. ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
  765. printf("Station " MACSTR " tried to associate with "
  766. "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
  767. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  768. goto fail;
  769. }
  770. sta->flags &= ~WLAN_STA_WME;
  771. if (elems.wme && hapd->conf->wme_enabled) {
  772. if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
  773. hostapd_logger(hapd, sta->addr,
  774. HOSTAPD_MODULE_WPA,
  775. HOSTAPD_LEVEL_DEBUG,
  776. "invalid WME element in association "
  777. "request");
  778. else
  779. sta->flags |= WLAN_STA_WME;
  780. }
  781. if (!elems.supp_rates) {
  782. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  783. HOSTAPD_LEVEL_DEBUG,
  784. "No supported rates element in AssocReq");
  785. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  786. goto fail;
  787. }
  788. if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
  789. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  790. HOSTAPD_LEVEL_DEBUG,
  791. "Invalid supported rates element length %d",
  792. elems.supp_rates_len);
  793. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  794. goto fail;
  795. }
  796. os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  797. os_memcpy(sta->supported_rates, elems.supp_rates,
  798. elems.supp_rates_len);
  799. sta->supported_rates_len = elems.supp_rates_len;
  800. if (elems.ext_supp_rates) {
  801. if (elems.supp_rates_len + elems.ext_supp_rates_len >
  802. sizeof(sta->supported_rates)) {
  803. hostapd_logger(hapd, mgmt->sa,
  804. HOSTAPD_MODULE_IEEE80211,
  805. HOSTAPD_LEVEL_DEBUG,
  806. "Invalid supported rates element length"
  807. " %d+%d", elems.supp_rates_len,
  808. elems.ext_supp_rates_len);
  809. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  810. goto fail;
  811. }
  812. os_memcpy(sta->supported_rates + elems.supp_rates_len,
  813. elems.ext_supp_rates, elems.ext_supp_rates_len);
  814. sta->supported_rates_len += elems.ext_supp_rates_len;
  815. }
  816. if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
  817. wpa_ie = elems.rsn_ie;
  818. wpa_ie_len = elems.rsn_ie_len;
  819. } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
  820. elems.wpa_ie) {
  821. wpa_ie = elems.wpa_ie;
  822. wpa_ie_len = elems.wpa_ie_len;
  823. } else {
  824. wpa_ie = NULL;
  825. wpa_ie_len = 0;
  826. }
  827. if (hapd->conf->wpa && wpa_ie == NULL) {
  828. printf("STA " MACSTR ": No WPA/RSN IE in association "
  829. "request\n", MAC2STR(sta->addr));
  830. resp = WLAN_STATUS_INVALID_IE;
  831. goto fail;
  832. }
  833. if (hapd->conf->wpa && wpa_ie) {
  834. int res;
  835. wpa_ie -= 2;
  836. wpa_ie_len += 2;
  837. if (sta->wpa_sm == NULL)
  838. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  839. sta->addr);
  840. if (sta->wpa_sm == NULL) {
  841. printf("Failed to initialize WPA state machine\n");
  842. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  843. goto fail;
  844. }
  845. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  846. wpa_ie, wpa_ie_len,
  847. elems.mdie, elems.mdie_len);
  848. if (res == WPA_INVALID_GROUP)
  849. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  850. else if (res == WPA_INVALID_PAIRWISE)
  851. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  852. else if (res == WPA_INVALID_AKMP)
  853. resp = WLAN_STATUS_AKMP_NOT_VALID;
  854. else if (res == WPA_ALLOC_FAIL)
  855. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  856. #ifdef CONFIG_IEEE80211W
  857. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  858. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  859. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  860. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  861. #endif /* CONFIG_IEEE80211W */
  862. else if (res == WPA_INVALID_MDIE)
  863. resp = WLAN_STATUS_INVALID_MDIE;
  864. else if (res != WPA_IE_OK)
  865. resp = WLAN_STATUS_INVALID_IE;
  866. if (resp != WLAN_STATUS_SUCCESS)
  867. goto fail;
  868. #ifdef CONFIG_IEEE80211R
  869. if (sta->auth_alg == WLAN_AUTH_FT) {
  870. if (!reassoc) {
  871. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  872. "to use association (not "
  873. "re-association) with FT auth_alg",
  874. MAC2STR(sta->addr));
  875. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  876. goto fail;
  877. }
  878. resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
  879. if (resp != WLAN_STATUS_SUCCESS)
  880. goto fail;
  881. }
  882. #endif /* CONFIG_IEEE80211R */
  883. }
  884. if (hapd->iface->dfs_enable &&
  885. hapd->iconf->ieee80211h == SPECT_STRICT_BINDING) {
  886. if (hostapd_check_power_cap(hapd, elems.power_cap,
  887. elems.power_cap_len)) {
  888. resp = WLAN_STATUS_PWR_CAPABILITY_NOT_VALID;
  889. hostapd_logger(hapd, sta->addr,
  890. HOSTAPD_MODULE_IEEE80211,
  891. HOSTAPD_LEVEL_DEBUG,
  892. "Power capabilities of the station not "
  893. "acceptable");
  894. goto fail;
  895. }
  896. }
  897. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  898. sta->flags |= WLAN_STA_NONERP;
  899. for (i = 0; i < sta->supported_rates_len; i++) {
  900. if ((sta->supported_rates[i] & 0x7f) > 22) {
  901. sta->flags &= ~WLAN_STA_NONERP;
  902. break;
  903. }
  904. }
  905. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  906. sta->nonerp_set = 1;
  907. hapd->iface->num_sta_non_erp++;
  908. if (hapd->iface->num_sta_non_erp == 1)
  909. ieee802_11_set_beacons(hapd->iface);
  910. }
  911. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  912. !sta->no_short_slot_time_set) {
  913. sta->no_short_slot_time_set = 1;
  914. hapd->iface->num_sta_no_short_slot_time++;
  915. if (hapd->iface->current_mode->mode ==
  916. HOSTAPD_MODE_IEEE80211G &&
  917. hapd->iface->num_sta_no_short_slot_time == 1)
  918. ieee802_11_set_beacons(hapd->iface);
  919. }
  920. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  921. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  922. else
  923. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  924. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  925. !sta->no_short_preamble_set) {
  926. sta->no_short_preamble_set = 1;
  927. hapd->iface->num_sta_no_short_preamble++;
  928. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  929. && hapd->iface->num_sta_no_short_preamble == 1)
  930. ieee802_11_set_beacons(hapd->iface);
  931. }
  932. /* get a unique AID */
  933. if (sta->aid > 0) {
  934. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  935. } else {
  936. for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
  937. if (hapd->sta_aid[sta->aid - 1] == NULL)
  938. break;
  939. if (sta->aid > MAX_AID_TABLE_SIZE) {
  940. sta->aid = 0;
  941. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  942. wpa_printf(MSG_ERROR, " no room for more AIDs");
  943. goto fail;
  944. } else {
  945. hapd->sta_aid[sta->aid - 1] = sta;
  946. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  947. }
  948. }
  949. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  950. HOSTAPD_LEVEL_DEBUG,
  951. "association OK (aid %d)", sta->aid);
  952. /* Station will be marked associated, after it acknowledges AssocResp
  953. */
  954. if (sta->last_assoc_req)
  955. os_free(sta->last_assoc_req);
  956. sta->last_assoc_req = os_malloc(len);
  957. if (sta->last_assoc_req)
  958. os_memcpy(sta->last_assoc_req, mgmt, len);
  959. /* Make sure that the previously registered inactivity timer will not
  960. * remove the STA immediately. */
  961. sta->timeout_next = STA_NULLFUNC;
  962. fail:
  963. os_memset(buf, 0, sizeof(buf));
  964. reply = (struct ieee80211_mgmt *) buf;
  965. reply->frame_control =
  966. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  967. (send_deauth ? WLAN_FC_STYPE_DEAUTH :
  968. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  969. WLAN_FC_STYPE_ASSOC_RESP)));
  970. os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
  971. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  972. os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
  973. send_len = IEEE80211_HDRLEN;
  974. if (send_deauth) {
  975. send_len += sizeof(reply->u.deauth);
  976. reply->u.deauth.reason_code = host_to_le16(resp);
  977. } else {
  978. u8 *p;
  979. send_len += sizeof(reply->u.assoc_resp);
  980. reply->u.assoc_resp.capab_info =
  981. host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
  982. reply->u.assoc_resp.status_code = host_to_le16(resp);
  983. reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
  984. | BIT(14) | BIT(15));
  985. /* Supported rates */
  986. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  987. /* Extended supported rates */
  988. p = hostapd_eid_ext_supp_rates(hapd, p);
  989. if (sta->flags & WLAN_STA_WME)
  990. p = hostapd_eid_wme(hapd, p);
  991. #ifdef CONFIG_IEEE80211R
  992. if (resp == WLAN_STATUS_SUCCESS) {
  993. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  994. * Transition Information, RSN */
  995. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  996. buf + sizeof(buf) - p,
  997. sta->auth_alg);
  998. }
  999. #endif /* CONFIG_IEEE80211R */
  1000. send_len += p - reply->u.assoc_resp.variable;
  1001. /* Request TX callback */
  1002. reply->frame_control |= host_to_le16(BIT(1));
  1003. }
  1004. if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
  1005. perror("handle_assoc: send");
  1006. }
  1007. static void handle_assoc_resp(struct hostapd_data *hapd,
  1008. struct ieee80211_mgmt *mgmt, size_t len)
  1009. {
  1010. u16 status_code, aid;
  1011. if (hapd->assoc_ap_state != ASSOCIATE) {
  1012. printf("Unexpected association response received from " MACSTR
  1013. "\n", MAC2STR(mgmt->sa));
  1014. return;
  1015. }
  1016. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
  1017. printf("handle_assoc_resp - too short payload (len=%lu)\n",
  1018. (unsigned long) len);
  1019. return;
  1020. }
  1021. if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
  1022. os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
  1023. printf("Received association response from unexpected address "
  1024. "(SA=" MACSTR " BSSID=" MACSTR "\n",
  1025. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  1026. return;
  1027. }
  1028. status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
  1029. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  1030. aid &= ~(BIT(14) | BIT(15));
  1031. if (status_code != 0) {
  1032. printf("Association (as station) with AP " MACSTR " failed "
  1033. "(status_code=%d)\n",
  1034. MAC2STR(hapd->conf->assoc_ap_addr), status_code);
  1035. /* Try to authenticate again */
  1036. hapd->assoc_ap_state = AUTHENTICATE;
  1037. eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
  1038. hapd, NULL);
  1039. }
  1040. printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
  1041. MAC2STR(hapd->conf->assoc_ap_addr), aid);
  1042. hapd->assoc_ap_aid = aid;
  1043. hapd->assoc_ap_state = ASSOCIATED;
  1044. if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
  1045. printf("Could not set associated AP address to kernel "
  1046. "driver.\n");
  1047. }
  1048. }
  1049. static void handle_disassoc(struct hostapd_data *hapd,
  1050. struct ieee80211_mgmt *mgmt, size_t len)
  1051. {
  1052. struct sta_info *sta;
  1053. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  1054. printf("handle_disassoc - too short payload (len=%lu)\n",
  1055. (unsigned long) len);
  1056. return;
  1057. }
  1058. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  1059. MAC2STR(mgmt->sa),
  1060. le_to_host16(mgmt->u.disassoc.reason_code));
  1061. if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
  1062. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1063. printf("Assoc AP " MACSTR " sent disassociation "
  1064. "(reason_code=%d) - try to authenticate\n",
  1065. MAC2STR(hapd->conf->assoc_ap_addr),
  1066. le_to_host16(mgmt->u.disassoc.reason_code));
  1067. hapd->assoc_ap_state = AUTHENTICATE;
  1068. ieee802_11_sta_authenticate(hapd, NULL);
  1069. eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
  1070. hapd, NULL);
  1071. return;
  1072. }
  1073. sta = ap_get_sta(hapd, mgmt->sa);
  1074. if (sta == NULL) {
  1075. printf("Station " MACSTR " trying to disassociate, but it "
  1076. "is not associated.\n", MAC2STR(mgmt->sa));
  1077. return;
  1078. }
  1079. sta->flags &= ~WLAN_STA_ASSOC;
  1080. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  1081. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1082. HOSTAPD_LEVEL_INFO, "disassociated");
  1083. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1084. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1085. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  1086. * authenticated. */
  1087. accounting_sta_stop(hapd, sta);
  1088. ieee802_1x_free_station(sta);
  1089. hostapd_sta_remove(hapd, sta->addr);
  1090. if (sta->timeout_next == STA_NULLFUNC ||
  1091. sta->timeout_next == STA_DISASSOC) {
  1092. sta->timeout_next = STA_DEAUTH;
  1093. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1094. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  1095. hapd, sta);
  1096. }
  1097. mlme_disassociate_indication(
  1098. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  1099. }
  1100. static void handle_deauth(struct hostapd_data *hapd,
  1101. struct ieee80211_mgmt *mgmt, size_t len)
  1102. {
  1103. struct sta_info *sta;
  1104. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  1105. printf("handle_deauth - too short payload (len=%lu)\n",
  1106. (unsigned long) len);
  1107. return;
  1108. }
  1109. wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
  1110. " reason_code=%d",
  1111. MAC2STR(mgmt->sa),
  1112. le_to_host16(mgmt->u.deauth.reason_code));
  1113. if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
  1114. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1115. printf("Assoc AP " MACSTR " sent deauthentication "
  1116. "(reason_code=%d) - try to authenticate\n",
  1117. MAC2STR(hapd->conf->assoc_ap_addr),
  1118. le_to_host16(mgmt->u.deauth.reason_code));
  1119. hapd->assoc_ap_state = AUTHENTICATE;
  1120. eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
  1121. hapd, NULL);
  1122. return;
  1123. }
  1124. sta = ap_get_sta(hapd, mgmt->sa);
  1125. if (sta == NULL) {
  1126. printf("Station " MACSTR " trying to deauthenticate, but it "
  1127. "is not authenticated.\n", MAC2STR(mgmt->sa));
  1128. return;
  1129. }
  1130. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1131. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1132. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1133. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  1134. mlme_deauthenticate_indication(
  1135. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  1136. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1137. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1138. ap_free_sta(hapd, sta);
  1139. }
  1140. static void handle_beacon(struct hostapd_data *hapd,
  1141. struct ieee80211_mgmt *mgmt, size_t len,
  1142. struct hostapd_frame_info *fi)
  1143. {
  1144. struct ieee802_11_elems elems;
  1145. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  1146. printf("handle_beacon - too short payload (len=%lu)\n",
  1147. (unsigned long) len);
  1148. return;
  1149. }
  1150. (void) ieee802_11_parse_elems(hapd, mgmt->u.beacon.variable,
  1151. len - (IEEE80211_HDRLEN +
  1152. sizeof(mgmt->u.beacon)), &elems,
  1153. 0);
  1154. if (hapd->assoc_ap_state == WAIT_BEACON &&
  1155. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1156. if (elems.ssid && elems.ssid_len <= 32) {
  1157. os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
  1158. elems.ssid_len);
  1159. hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
  1160. hapd->assoc_ap_ssid_len = elems.ssid_len;
  1161. }
  1162. ieee802_11_sta_authenticate(hapd, NULL);
  1163. }
  1164. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  1165. }
  1166. static void handle_action(struct hostapd_data *hapd,
  1167. struct ieee80211_mgmt *mgmt, size_t len)
  1168. {
  1169. if (len < IEEE80211_HDRLEN + 1) {
  1170. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1171. HOSTAPD_LEVEL_DEBUG,
  1172. "handle_action - too short payload (len=%lu)",
  1173. (unsigned long) len);
  1174. return;
  1175. }
  1176. switch (mgmt->u.action.category) {
  1177. #ifdef CONFIG_IEEE80211R
  1178. case WLAN_ACTION_FT:
  1179. {
  1180. struct sta_info *sta;
  1181. sta = ap_get_sta(hapd, mgmt->sa);
  1182. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  1183. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
  1184. "frame from unassociated STA " MACSTR,
  1185. MAC2STR(mgmt->sa));
  1186. return;
  1187. }
  1188. if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  1189. len - IEEE80211_HDRLEN))
  1190. break;
  1191. return;
  1192. }
  1193. #endif /* CONFIG_IEEE80211R */
  1194. case WME_ACTION_CATEGORY:
  1195. hostapd_wme_action(hapd, mgmt, len);
  1196. return;
  1197. }
  1198. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1199. HOSTAPD_LEVEL_DEBUG,
  1200. "handle_action - unknown action category %d or invalid "
  1201. "frame",
  1202. mgmt->u.action.category);
  1203. if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
  1204. !(mgmt->sa[0] & 0x01)) {
  1205. /*
  1206. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  1207. * Return the Action frame to the source without change
  1208. * except that MSB of the Category set to 1.
  1209. */
  1210. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  1211. "frame back to sender");
  1212. os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
  1213. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  1214. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  1215. mgmt->u.action.category |= 0x80;
  1216. hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
  1217. }
  1218. }
  1219. /**
  1220. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  1221. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  1222. * sent to)
  1223. * @buf: management frame data (starting from IEEE 802.11 header)
  1224. * @len: length of frame data in octets
  1225. * @stype: management frame subtype from frame control field
  1226. *
  1227. * Process all incoming IEEE 802.11 management frames. This will be called for
  1228. * each frame received from the kernel driver through wlan#ap interface. In
  1229. * addition, it can be called to re-inserted pending frames (e.g., when using
  1230. * external RADIUS server as an MAC ACL).
  1231. */
  1232. void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
  1233. struct hostapd_frame_info *fi)
  1234. {
  1235. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1236. int broadcast;
  1237. if (stype == WLAN_FC_STYPE_BEACON) {
  1238. handle_beacon(hapd, mgmt, len, fi);
  1239. return;
  1240. }
  1241. if (fi && fi->passive_scan)
  1242. return;
  1243. broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
  1244. mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
  1245. mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
  1246. if (!broadcast &&
  1247. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
  1248. (hapd->assoc_ap_state == DO_NOT_ASSOC ||
  1249. os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
  1250. {
  1251. printf("MGMT: BSSID=" MACSTR " not our address\n",
  1252. MAC2STR(mgmt->bssid));
  1253. return;
  1254. }
  1255. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  1256. handle_probe_req(hapd, mgmt, len);
  1257. return;
  1258. }
  1259. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  1260. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1261. HOSTAPD_LEVEL_DEBUG,
  1262. "MGMT: DA=" MACSTR " not our address",
  1263. MAC2STR(mgmt->da));
  1264. return;
  1265. }
  1266. switch (stype) {
  1267. case WLAN_FC_STYPE_AUTH:
  1268. wpa_printf(MSG_DEBUG, "mgmt::auth");
  1269. handle_auth(hapd, mgmt, len);
  1270. break;
  1271. case WLAN_FC_STYPE_ASSOC_REQ:
  1272. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  1273. handle_assoc(hapd, mgmt, len, 0);
  1274. break;
  1275. case WLAN_FC_STYPE_ASSOC_RESP:
  1276. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
  1277. handle_assoc_resp(hapd, mgmt, len);
  1278. break;
  1279. case WLAN_FC_STYPE_REASSOC_REQ:
  1280. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  1281. handle_assoc(hapd, mgmt, len, 1);
  1282. break;
  1283. case WLAN_FC_STYPE_DISASSOC:
  1284. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  1285. handle_disassoc(hapd, mgmt, len);
  1286. break;
  1287. case WLAN_FC_STYPE_DEAUTH:
  1288. wpa_printf(MSG_DEBUG, "mgmt::deauth");
  1289. handle_deauth(hapd, mgmt, len);
  1290. break;
  1291. case WLAN_FC_STYPE_ACTION:
  1292. wpa_printf(MSG_DEBUG, "mgmt::action");
  1293. handle_action(hapd, mgmt, len);
  1294. break;
  1295. default:
  1296. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1297. HOSTAPD_LEVEL_DEBUG,
  1298. "unknown mgmt frame subtype %d", stype);
  1299. break;
  1300. }
  1301. }
  1302. static void handle_auth_cb(struct hostapd_data *hapd,
  1303. struct ieee80211_mgmt *mgmt,
  1304. size_t len, int ok)
  1305. {
  1306. u16 auth_alg, auth_transaction, status_code;
  1307. struct sta_info *sta;
  1308. if (!ok) {
  1309. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1310. HOSTAPD_LEVEL_NOTICE,
  1311. "did not acknowledge authentication response");
  1312. return;
  1313. }
  1314. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1315. printf("handle_auth_cb - too short payload (len=%lu)\n",
  1316. (unsigned long) len);
  1317. return;
  1318. }
  1319. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1320. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1321. status_code = le_to_host16(mgmt->u.auth.status_code);
  1322. sta = ap_get_sta(hapd, mgmt->da);
  1323. if (!sta) {
  1324. printf("handle_auth_cb: STA " MACSTR " not found\n",
  1325. MAC2STR(mgmt->da));
  1326. return;
  1327. }
  1328. if (status_code == WLAN_STATUS_SUCCESS &&
  1329. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  1330. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  1331. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1332. HOSTAPD_LEVEL_INFO, "authenticated");
  1333. sta->flags |= WLAN_STA_AUTH;
  1334. }
  1335. }
  1336. static void handle_assoc_cb(struct hostapd_data *hapd,
  1337. struct ieee80211_mgmt *mgmt,
  1338. size_t len, int reassoc, int ok)
  1339. {
  1340. u16 status;
  1341. struct sta_info *sta;
  1342. int new_assoc = 1;
  1343. if (!ok) {
  1344. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1345. HOSTAPD_LEVEL_DEBUG,
  1346. "did not acknowledge association response");
  1347. return;
  1348. }
  1349. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  1350. sizeof(mgmt->u.assoc_resp))) {
  1351. printf("handle_assoc_cb(reassoc=%d) - too short payload "
  1352. "(len=%lu)\n", reassoc, (unsigned long) len);
  1353. return;
  1354. }
  1355. if (reassoc)
  1356. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  1357. else
  1358. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1359. sta = ap_get_sta(hapd, mgmt->da);
  1360. if (!sta) {
  1361. printf("handle_assoc_cb: STA " MACSTR " not found\n",
  1362. MAC2STR(mgmt->da));
  1363. return;
  1364. }
  1365. if (status != WLAN_STATUS_SUCCESS)
  1366. goto fail;
  1367. /* Stop previous accounting session, if one is started, and allocate
  1368. * new session id for the new session. */
  1369. accounting_sta_stop(hapd, sta);
  1370. accounting_sta_get_id(hapd, sta);
  1371. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1372. HOSTAPD_LEVEL_INFO,
  1373. "associated (aid %d, accounting session %08X-%08X)",
  1374. sta->aid, sta->acct_session_id_hi,
  1375. sta->acct_session_id_lo);
  1376. if (sta->flags & WLAN_STA_ASSOC)
  1377. new_assoc = 0;
  1378. sta->flags |= WLAN_STA_ASSOC;
  1379. if (reassoc)
  1380. mlme_reassociate_indication(hapd, sta);
  1381. else
  1382. mlme_associate_indication(hapd, sta);
  1383. if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
  1384. sta->capability, sta->supported_rates,
  1385. sta->supported_rates_len, 0)) {
  1386. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1387. HOSTAPD_LEVEL_NOTICE,
  1388. "Could not add STA to kernel driver");
  1389. }
  1390. if (sta->eapol_sm == NULL) {
  1391. /*
  1392. * This STA does not use RADIUS server for EAP authentication,
  1393. * so bind it to the selected VLAN interface now, since the
  1394. * interface selection is not going to change anymore.
  1395. */
  1396. ap_sta_bind_vlan(hapd, sta, 0);
  1397. } else if (sta->vlan_id) {
  1398. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  1399. ap_sta_bind_vlan(hapd, sta, 0);
  1400. }
  1401. if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
  1402. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1403. WLAN_STA_SHORT_PREAMBLE, ~0);
  1404. } else {
  1405. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1406. 0, ~WLAN_STA_SHORT_PREAMBLE);
  1407. }
  1408. if (sta->auth_alg == WLAN_AUTH_FT)
  1409. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  1410. else
  1411. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  1412. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  1413. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  1414. fail:
  1415. /* Copy of the association request is not needed anymore */
  1416. if (sta->last_assoc_req) {
  1417. os_free(sta->last_assoc_req);
  1418. sta->last_assoc_req = NULL;
  1419. }
  1420. }
  1421. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  1422. u16 stype, int ok)
  1423. {
  1424. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1425. switch (stype) {
  1426. case WLAN_FC_STYPE_AUTH:
  1427. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  1428. handle_auth_cb(hapd, mgmt, len, ok);
  1429. break;
  1430. case WLAN_FC_STYPE_ASSOC_RESP:
  1431. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  1432. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  1433. break;
  1434. case WLAN_FC_STYPE_REASSOC_RESP:
  1435. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  1436. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  1437. break;
  1438. case WLAN_FC_STYPE_PROBE_RESP:
  1439. wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
  1440. break;
  1441. case WLAN_FC_STYPE_DEAUTH:
  1442. /* ignore */
  1443. break;
  1444. default:
  1445. printf("unknown mgmt cb frame subtype %d\n", stype);
  1446. break;
  1447. }
  1448. }
  1449. static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
  1450. void *timeout_ctx)
  1451. {
  1452. struct hostapd_data *hapd = eloop_ctx;
  1453. hapd->tkip_countermeasures = 0;
  1454. hostapd_set_countermeasures(hapd, 0);
  1455. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1456. HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
  1457. }
  1458. static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
  1459. {
  1460. struct sta_info *sta;
  1461. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1462. HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
  1463. wpa_auth_countermeasures_start(hapd->wpa_auth);
  1464. hapd->tkip_countermeasures = 1;
  1465. hostapd_set_countermeasures(hapd, 1);
  1466. wpa_gtk_rekey(hapd->wpa_auth);
  1467. eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
  1468. eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
  1469. hapd, NULL);
  1470. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  1471. hostapd_sta_deauth(hapd, sta->addr,
  1472. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1473. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1474. WLAN_STA_AUTHORIZED);
  1475. hostapd_sta_remove(hapd, sta->addr);
  1476. }
  1477. }
  1478. void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
  1479. int local)
  1480. {
  1481. time_t now;
  1482. if (addr && local) {
  1483. struct sta_info *sta = ap_get_sta(hapd, addr);
  1484. if (sta != NULL) {
  1485. wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
  1486. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  1487. HOSTAPD_LEVEL_INFO,
  1488. "Michael MIC failure detected in "
  1489. "received frame");
  1490. mlme_michaelmicfailure_indication(hapd, addr);
  1491. } else {
  1492. wpa_printf(MSG_DEBUG,
  1493. "MLME-MICHAELMICFAILURE.indication "
  1494. "for not associated STA (" MACSTR
  1495. ") ignored", MAC2STR(addr));
  1496. return;
  1497. }
  1498. }
  1499. time(&now);
  1500. if (now > hapd->michael_mic_failure + 60) {
  1501. hapd->michael_mic_failures = 1;
  1502. } else {
  1503. hapd->michael_mic_failures++;
  1504. if (hapd->michael_mic_failures > 1)
  1505. ieee80211_tkip_countermeasures_start(hapd);
  1506. }
  1507. hapd->michael_mic_failure = now;
  1508. }
  1509. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1510. {
  1511. /* TODO */
  1512. return 0;
  1513. }
  1514. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1515. char *buf, size_t buflen)
  1516. {
  1517. /* TODO */
  1518. return 0;
  1519. }
  1520. #endif /* CONFIG_NATIVE_WINDOWS */