sme.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * wpa_supplicant - SME
  3. * Copyright (c) 2009-2010, 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. #include "common.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "common/ieee802_11_common.h"
  18. #include "eapol_supp/eapol_supp_sm.h"
  19. #include "common/wpa_common.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "rsn_supp/pmksa_cache.h"
  22. #include "config.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "driver_i.h"
  25. #include "wpas_glue.h"
  26. #include "wps_supplicant.h"
  27. #include "p2p_supplicant.h"
  28. #include "notify.h"
  29. #include "blacklist.h"
  30. #include "bss.h"
  31. #include "scan.h"
  32. #include "sme.h"
  33. static void add_freq(int *freqs, int *num_freqs, int freq)
  34. {
  35. int i;
  36. for (i = 0; i < *num_freqs; i++) {
  37. if (freqs[i] == freq)
  38. return;
  39. }
  40. freqs[*num_freqs] = freq;
  41. (*num_freqs)++;
  42. }
  43. static int * sme_another_bss_in_ess(struct wpa_supplicant *wpa_s)
  44. {
  45. struct wpa_bss *bss, *cbss;
  46. const int max_freqs = 10;
  47. int *freqs;
  48. int num_freqs = 0;
  49. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  50. if (freqs == NULL)
  51. return NULL;
  52. cbss = wpa_s->current_bss;
  53. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  54. if (bss == cbss)
  55. continue;
  56. if (bss->ssid_len == cbss->ssid_len &&
  57. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  58. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  59. add_freq(freqs, &num_freqs, bss->freq);
  60. if (num_freqs == max_freqs)
  61. break;
  62. }
  63. }
  64. if (num_freqs == 0) {
  65. os_free(freqs);
  66. freqs = NULL;
  67. }
  68. return freqs;
  69. }
  70. static void sme_connection_failed(struct wpa_supplicant *wpa_s,
  71. const u8 *bssid)
  72. {
  73. int timeout;
  74. int count;
  75. int *freqs = NULL;
  76. /*
  77. * Add the failed BSSID into the blacklist and speed up next scan
  78. * attempt if there could be other APs that could accept association.
  79. * The current blacklist count indicates how many times we have tried
  80. * connecting to this AP and multiple attempts mean that other APs are
  81. * either not available or has already been tried, so that we can start
  82. * increasing the delay here to avoid constant scanning.
  83. */
  84. count = wpa_blacklist_add(wpa_s, bssid);
  85. if (count == 1 && wpa_s->current_bss) {
  86. /*
  87. * This BSS was not in the blacklist before. If there is
  88. * another BSS available for the same ESS, we should try that
  89. * next. Otherwise, we may as well try this one once more
  90. * before allowing other, likely worse, ESSes to be considered.
  91. */
  92. freqs = sme_another_bss_in_ess(wpa_s);
  93. if (freqs) {
  94. wpa_printf(MSG_DEBUG, "SME: Another BSS in this ESS "
  95. "has been seen; try it next");
  96. wpa_blacklist_add(wpa_s, bssid);
  97. /*
  98. * On the next scan, go through only the known channels
  99. * used in this ESS based on previous scans to speed up
  100. * common load balancing use case.
  101. */
  102. os_free(wpa_s->next_scan_freqs);
  103. wpa_s->next_scan_freqs = freqs;
  104. }
  105. }
  106. switch (count) {
  107. case 1:
  108. timeout = 100;
  109. break;
  110. case 2:
  111. timeout = 500;
  112. break;
  113. case 3:
  114. timeout = 1000;
  115. break;
  116. default:
  117. timeout = 5000;
  118. }
  119. /*
  120. * TODO: if more than one possible AP is available in scan results,
  121. * could try the other ones before requesting a new scan.
  122. */
  123. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  124. 1000 * (timeout % 1000));
  125. }
  126. void sme_authenticate(struct wpa_supplicant *wpa_s,
  127. struct wpa_bss *bss, struct wpa_ssid *ssid)
  128. {
  129. struct wpa_driver_auth_params params;
  130. struct wpa_ssid *old_ssid;
  131. #ifdef CONFIG_IEEE80211R
  132. const u8 *ie;
  133. #endif /* CONFIG_IEEE80211R */
  134. #ifdef CONFIG_IEEE80211R
  135. const u8 *md = NULL;
  136. #endif /* CONFIG_IEEE80211R */
  137. int i, bssid_changed;
  138. if (bss == NULL) {
  139. wpa_printf(MSG_ERROR, "SME: No scan result available for the "
  140. "network");
  141. return;
  142. }
  143. wpa_s->current_bss = bss;
  144. os_memset(&params, 0, sizeof(params));
  145. wpa_s->reassociate = 0;
  146. params.freq = bss->freq;
  147. params.bssid = bss->bssid;
  148. params.ssid = bss->ssid;
  149. params.ssid_len = bss->ssid_len;
  150. if (wpa_s->sme.ssid_len != params.ssid_len ||
  151. os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
  152. wpa_s->sme.prev_bssid_set = 0;
  153. wpa_s->sme.freq = params.freq;
  154. os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
  155. wpa_s->sme.ssid_len = params.ssid_len;
  156. params.auth_alg = WPA_AUTH_ALG_OPEN;
  157. #ifdef IEEE8021X_EAPOL
  158. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  159. if (ssid->leap) {
  160. if (ssid->non_leap == 0)
  161. params.auth_alg = WPA_AUTH_ALG_LEAP;
  162. else
  163. params.auth_alg |= WPA_AUTH_ALG_LEAP;
  164. }
  165. }
  166. #endif /* IEEE8021X_EAPOL */
  167. wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
  168. params.auth_alg);
  169. if (ssid->auth_alg) {
  170. params.auth_alg = ssid->auth_alg;
  171. wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
  172. params.auth_alg);
  173. }
  174. for (i = 0; i < NUM_WEP_KEYS; i++) {
  175. if (ssid->wep_key_len[i])
  176. params.wep_key[i] = ssid->wep_key[i];
  177. params.wep_key_len[i] = ssid->wep_key_len[i];
  178. }
  179. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  180. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  181. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  182. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  183. if (bssid_changed)
  184. wpas_notify_bssid_changed(wpa_s);
  185. if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  186. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  187. (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  188. WPA_KEY_MGMT_FT_IEEE8021X |
  189. WPA_KEY_MGMT_FT_PSK |
  190. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  191. WPA_KEY_MGMT_PSK_SHA256))) {
  192. int try_opportunistic;
  193. try_opportunistic = ssid->proactive_key_caching &&
  194. (ssid->proto & WPA_PROTO_RSN);
  195. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  196. wpa_s->current_ssid,
  197. try_opportunistic) == 0)
  198. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  199. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  200. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  201. wpa_s->sme.assoc_req_ie,
  202. &wpa_s->sme.assoc_req_ie_len)) {
  203. wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
  204. "management and encryption suites");
  205. return;
  206. }
  207. } else if (ssid->key_mgmt &
  208. (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  209. WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
  210. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
  211. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  212. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  213. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  214. wpa_s->sme.assoc_req_ie,
  215. &wpa_s->sme.assoc_req_ie_len)) {
  216. wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
  217. "management and encryption suites (no scan "
  218. "results)");
  219. return;
  220. }
  221. #ifdef CONFIG_WPS
  222. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  223. struct wpabuf *wps_ie;
  224. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  225. if (wps_ie && wpabuf_len(wps_ie) <=
  226. sizeof(wpa_s->sme.assoc_req_ie)) {
  227. wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
  228. os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
  229. wpa_s->sme.assoc_req_ie_len);
  230. } else
  231. wpa_s->sme.assoc_req_ie_len = 0;
  232. wpabuf_free(wps_ie);
  233. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  234. #endif /* CONFIG_WPS */
  235. } else {
  236. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  237. wpa_s->sme.assoc_req_ie_len = 0;
  238. }
  239. #ifdef CONFIG_IEEE80211R
  240. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  241. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  242. md = ie + 2;
  243. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  244. if (md) {
  245. /* Prepare for the next transition */
  246. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  247. }
  248. if (md && ssid->key_mgmt & (WPA_KEY_MGMT_FT_PSK |
  249. WPA_KEY_MGMT_FT_IEEE8021X)) {
  250. if (wpa_s->sme.assoc_req_ie_len + 5 <
  251. sizeof(wpa_s->sme.assoc_req_ie)) {
  252. struct rsn_mdie *mdie;
  253. u8 *pos = wpa_s->sme.assoc_req_ie +
  254. wpa_s->sme.assoc_req_ie_len;
  255. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  256. *pos++ = sizeof(*mdie);
  257. mdie = (struct rsn_mdie *) pos;
  258. os_memcpy(mdie->mobility_domain, md,
  259. MOBILITY_DOMAIN_ID_LEN);
  260. mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
  261. wpa_s->sme.assoc_req_ie_len += 5;
  262. }
  263. if (wpa_s->sme.ft_used &&
  264. os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
  265. wpa_sm_has_ptk(wpa_s->wpa)) {
  266. wpa_printf(MSG_DEBUG, "SME: Trying to use FT "
  267. "over-the-air");
  268. params.auth_alg = WPA_AUTH_ALG_FT;
  269. params.ie = wpa_s->sme.ft_ies;
  270. params.ie_len = wpa_s->sme.ft_ies_len;
  271. }
  272. }
  273. #endif /* CONFIG_IEEE80211R */
  274. #ifdef CONFIG_IEEE80211W
  275. wpa_s->sme.mfp = ssid->ieee80211w;
  276. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  277. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  278. struct wpa_ie_data _ie;
  279. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
  280. _ie.capabilities &
  281. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  282. wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
  283. "require MFP");
  284. wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
  285. }
  286. }
  287. #endif /* CONFIG_IEEE80211W */
  288. #ifdef CONFIG_P2P
  289. if (wpa_s->global->p2p) {
  290. u8 *pos;
  291. size_t len;
  292. int res;
  293. int p2p_group;
  294. p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
  295. pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
  296. len = sizeof(wpa_s->sme.assoc_req_ie) -
  297. wpa_s->sme.assoc_req_ie_len;
  298. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
  299. if (res >= 0)
  300. wpa_s->sme.assoc_req_ie_len += res;
  301. }
  302. #endif /* CONFIG_P2P */
  303. wpa_supplicant_cancel_scan(wpa_s);
  304. wpa_msg(wpa_s, MSG_INFO, "Trying to authenticate with " MACSTR
  305. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  306. wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
  307. wpa_clear_keys(wpa_s, bss->bssid);
  308. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  309. old_ssid = wpa_s->current_ssid;
  310. wpa_s->current_ssid = ssid;
  311. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  312. wpa_supplicant_initiate_eapol(wpa_s);
  313. if (old_ssid != wpa_s->current_ssid)
  314. wpas_notify_network_changed(wpa_s);
  315. wpa_s->sme.auth_alg = params.auth_alg;
  316. if (wpa_drv_authenticate(wpa_s, &params) < 0) {
  317. wpa_msg(wpa_s, MSG_INFO, "Authentication request to the "
  318. "driver failed");
  319. wpa_supplicant_req_scan(wpa_s, 1, 0);
  320. return;
  321. }
  322. /* TODO: add timeout on authentication */
  323. /*
  324. * Association will be started based on the authentication event from
  325. * the driver.
  326. */
  327. }
  328. void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
  329. {
  330. struct wpa_ssid *ssid = wpa_s->current_ssid;
  331. if (ssid == NULL) {
  332. wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
  333. "network is not selected");
  334. return;
  335. }
  336. if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
  337. wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
  338. "not in authenticating state");
  339. return;
  340. }
  341. if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
  342. wpa_printf(MSG_DEBUG, "SME: Ignore authentication with "
  343. "unexpected peer " MACSTR,
  344. MAC2STR(data->auth.peer));
  345. return;
  346. }
  347. wpa_printf(MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
  348. " auth_type=%d status_code=%d",
  349. MAC2STR(data->auth.peer), data->auth.auth_type,
  350. data->auth.status_code);
  351. wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
  352. data->auth.ies, data->auth.ies_len);
  353. if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
  354. wpa_printf(MSG_DEBUG, "SME: Authentication failed (status "
  355. "code %d)", data->auth.status_code);
  356. if (data->auth.status_code !=
  357. WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
  358. wpa_s->sme.auth_alg == data->auth.auth_type ||
  359. wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
  360. sme_connection_failed(wpa_s, wpa_s->pending_bssid);
  361. return;
  362. }
  363. switch (data->auth.auth_type) {
  364. case WLAN_AUTH_OPEN:
  365. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  366. wpa_printf(MSG_DEBUG, "SME: Trying SHARED auth");
  367. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  368. wpa_s->current_ssid);
  369. return;
  370. case WLAN_AUTH_SHARED_KEY:
  371. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
  372. wpa_printf(MSG_DEBUG, "SME: Trying LEAP auth");
  373. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  374. wpa_s->current_ssid);
  375. return;
  376. default:
  377. return;
  378. }
  379. }
  380. #ifdef CONFIG_IEEE80211R
  381. if (data->auth.auth_type == WLAN_AUTH_FT) {
  382. union wpa_event_data edata;
  383. os_memset(&edata, 0, sizeof(edata));
  384. edata.ft_ies.ies = data->auth.ies;
  385. edata.ft_ies.ies_len = data->auth.ies_len;
  386. os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
  387. wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
  388. }
  389. #endif /* CONFIG_IEEE80211R */
  390. sme_associate(wpa_s, ssid->mode, data->auth.peer,
  391. data->auth.auth_type);
  392. }
  393. void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
  394. const u8 *bssid, u16 auth_type)
  395. {
  396. struct wpa_driver_associate_params params;
  397. struct ieee802_11_elems elems;
  398. os_memset(&params, 0, sizeof(params));
  399. params.bssid = bssid;
  400. params.ssid = wpa_s->sme.ssid;
  401. params.ssid_len = wpa_s->sme.ssid_len;
  402. params.freq = wpa_s->sme.freq;
  403. params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
  404. wpa_s->sme.assoc_req_ie : NULL;
  405. params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
  406. #ifdef CONFIG_IEEE80211R
  407. if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
  408. params.wpa_ie = wpa_s->sme.ft_ies;
  409. params.wpa_ie_len = wpa_s->sme.ft_ies_len;
  410. }
  411. #endif /* CONFIG_IEEE80211R */
  412. params.mode = mode;
  413. params.mgmt_frame_protection = wpa_s->sme.mfp;
  414. if (wpa_s->sme.prev_bssid_set)
  415. params.prev_bssid = wpa_s->sme.prev_bssid;
  416. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  417. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  418. params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
  419. params.freq);
  420. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  421. if (params.wpa_ie == NULL ||
  422. ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
  423. < 0) {
  424. wpa_printf(MSG_DEBUG, "SME: Could not parse own IEs?!");
  425. os_memset(&elems, 0, sizeof(elems));
  426. }
  427. if (elems.rsn_ie)
  428. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
  429. elems.rsn_ie_len + 2);
  430. else if (elems.wpa_ie)
  431. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
  432. elems.wpa_ie_len + 2);
  433. else
  434. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  435. if (elems.p2p &&
  436. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  437. params.p2p = 1;
  438. if (wpa_s->parent->set_sta_uapsd)
  439. params.uapsd = wpa_s->parent->sta_uapsd;
  440. else
  441. params.uapsd = -1;
  442. if (wpa_drv_associate(wpa_s, &params) < 0) {
  443. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  444. "failed");
  445. wpa_supplicant_req_scan(wpa_s, 5, 0);
  446. return;
  447. }
  448. /* TODO: add timeout on association */
  449. }
  450. int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  451. const u8 *ies, size_t ies_len)
  452. {
  453. if (md == NULL || ies == NULL) {
  454. wpa_printf(MSG_DEBUG, "SME: Remove mobility domain");
  455. os_free(wpa_s->sme.ft_ies);
  456. wpa_s->sme.ft_ies = NULL;
  457. wpa_s->sme.ft_ies_len = 0;
  458. wpa_s->sme.ft_used = 0;
  459. return 0;
  460. }
  461. os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  462. wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
  463. os_free(wpa_s->sme.ft_ies);
  464. wpa_s->sme.ft_ies = os_malloc(ies_len);
  465. if (wpa_s->sme.ft_ies == NULL)
  466. return -1;
  467. os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
  468. wpa_s->sme.ft_ies_len = ies_len;
  469. return 0;
  470. }
  471. void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
  472. union wpa_event_data *data)
  473. {
  474. int bssid_changed;
  475. wpa_printf(MSG_DEBUG, "SME: Association with " MACSTR " failed: "
  476. "status code %d", MAC2STR(wpa_s->pending_bssid),
  477. data->assoc_reject.status_code);
  478. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  479. /*
  480. * For now, unconditionally terminate the previous authentication. In
  481. * theory, this should not be needed, but mac80211 gets quite confused
  482. * if the authentication is left pending.. Some roaming cases might
  483. * benefit from using the previous authentication, so this could be
  484. * optimized in the future.
  485. */
  486. if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
  487. WLAN_REASON_DEAUTH_LEAVING) < 0) {
  488. wpa_msg(wpa_s, MSG_INFO,
  489. "Deauth request to the driver failed");
  490. }
  491. wpa_s->sme.prev_bssid_set = 0;
  492. sme_connection_failed(wpa_s, wpa_s->pending_bssid);
  493. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  494. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  495. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  496. if (bssid_changed)
  497. wpas_notify_bssid_changed(wpa_s);
  498. }
  499. void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
  500. union wpa_event_data *data)
  501. {
  502. wpa_printf(MSG_DEBUG, "SME: Authentication timed out");
  503. sme_connection_failed(wpa_s, wpa_s->pending_bssid);
  504. }
  505. void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
  506. union wpa_event_data *data)
  507. {
  508. wpa_printf(MSG_DEBUG, "SME: Association timed out");
  509. sme_connection_failed(wpa_s, wpa_s->pending_bssid);
  510. wpa_supplicant_mark_disassoc(wpa_s);
  511. }
  512. void sme_event_disassoc(struct wpa_supplicant *wpa_s,
  513. union wpa_event_data *data)
  514. {
  515. wpa_printf(MSG_DEBUG, "SME: Disassociation event received");
  516. if (wpa_s->sme.prev_bssid_set &&
  517. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)) {
  518. /*
  519. * cfg80211/mac80211 can get into somewhat confused state if
  520. * the AP only disassociates us and leaves us in authenticated
  521. * state. For now, force the state to be cleared to avoid
  522. * confusing errors if we try to associate with the AP again.
  523. */
  524. wpa_printf(MSG_DEBUG, "SME: Deauthenticate to clear driver "
  525. "state");
  526. wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
  527. WLAN_REASON_DEAUTH_LEAVING);
  528. }
  529. }