sme.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/ieee802_11_common.h"
  19. #include "eapol_supp/eapol_supp_sm.h"
  20. #include "common/wpa_common.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "rsn_supp/pmksa_cache.h"
  23. #include "config.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "wpas_glue.h"
  27. #include "wps_supplicant.h"
  28. #include "p2p_supplicant.h"
  29. #include "notify.h"
  30. #include "bss.h"
  31. #include "scan.h"
  32. #include "sme.h"
  33. #define SME_AUTH_TIMEOUT 5
  34. #define SME_ASSOC_TIMEOUT 5
  35. static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
  36. static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
  37. #ifdef CONFIG_IEEE80211W
  38. static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
  39. #endif /* CONFIG_IEEE80211W */
  40. void sme_authenticate(struct wpa_supplicant *wpa_s,
  41. struct wpa_bss *bss, struct wpa_ssid *ssid)
  42. {
  43. struct wpa_driver_auth_params params;
  44. struct wpa_ssid *old_ssid;
  45. #ifdef CONFIG_IEEE80211R
  46. const u8 *ie;
  47. #endif /* CONFIG_IEEE80211R */
  48. #ifdef CONFIG_IEEE80211R
  49. const u8 *md = NULL;
  50. #endif /* CONFIG_IEEE80211R */
  51. int i, bssid_changed;
  52. if (bss == NULL) {
  53. wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
  54. "the network");
  55. return;
  56. }
  57. wpa_s->current_bss = bss;
  58. os_memset(&params, 0, sizeof(params));
  59. wpa_s->reassociate = 0;
  60. params.freq = bss->freq;
  61. params.bssid = bss->bssid;
  62. params.ssid = bss->ssid;
  63. params.ssid_len = bss->ssid_len;
  64. params.p2p = ssid->p2p_group;
  65. if (wpa_s->sme.ssid_len != params.ssid_len ||
  66. os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
  67. wpa_s->sme.prev_bssid_set = 0;
  68. wpa_s->sme.freq = params.freq;
  69. os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
  70. wpa_s->sme.ssid_len = params.ssid_len;
  71. params.auth_alg = WPA_AUTH_ALG_OPEN;
  72. #ifdef IEEE8021X_EAPOL
  73. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  74. if (ssid->leap) {
  75. if (ssid->non_leap == 0)
  76. params.auth_alg = WPA_AUTH_ALG_LEAP;
  77. else
  78. params.auth_alg |= WPA_AUTH_ALG_LEAP;
  79. }
  80. }
  81. #endif /* IEEE8021X_EAPOL */
  82. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
  83. params.auth_alg);
  84. if (ssid->auth_alg) {
  85. params.auth_alg = ssid->auth_alg;
  86. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  87. "0x%x", params.auth_alg);
  88. }
  89. for (i = 0; i < NUM_WEP_KEYS; i++) {
  90. if (ssid->wep_key_len[i])
  91. params.wep_key[i] = ssid->wep_key[i];
  92. params.wep_key_len[i] = ssid->wep_key_len[i];
  93. }
  94. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  95. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  96. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  97. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  98. if (bssid_changed)
  99. wpas_notify_bssid_changed(wpa_s);
  100. if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  101. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  102. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  103. int try_opportunistic;
  104. try_opportunistic = ssid->proactive_key_caching &&
  105. (ssid->proto & WPA_PROTO_RSN);
  106. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  107. wpa_s->current_ssid,
  108. try_opportunistic) == 0)
  109. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  110. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  111. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  112. wpa_s->sme.assoc_req_ie,
  113. &wpa_s->sme.assoc_req_ie_len)) {
  114. wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
  115. "key management and encryption suites");
  116. return;
  117. }
  118. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  119. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  120. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  121. wpa_s->sme.assoc_req_ie,
  122. &wpa_s->sme.assoc_req_ie_len)) {
  123. wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
  124. "key management and encryption suites (no "
  125. "scan results)");
  126. return;
  127. }
  128. #ifdef CONFIG_WPS
  129. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  130. struct wpabuf *wps_ie;
  131. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  132. if (wps_ie && wpabuf_len(wps_ie) <=
  133. sizeof(wpa_s->sme.assoc_req_ie)) {
  134. wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
  135. os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
  136. wpa_s->sme.assoc_req_ie_len);
  137. } else
  138. wpa_s->sme.assoc_req_ie_len = 0;
  139. wpabuf_free(wps_ie);
  140. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  141. #endif /* CONFIG_WPS */
  142. } else {
  143. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  144. wpa_s->sme.assoc_req_ie_len = 0;
  145. }
  146. #ifdef CONFIG_IEEE80211R
  147. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  148. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  149. md = ie + 2;
  150. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  151. if (md) {
  152. /* Prepare for the next transition */
  153. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  154. }
  155. if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
  156. if (wpa_s->sme.assoc_req_ie_len + 5 <
  157. sizeof(wpa_s->sme.assoc_req_ie)) {
  158. struct rsn_mdie *mdie;
  159. u8 *pos = wpa_s->sme.assoc_req_ie +
  160. wpa_s->sme.assoc_req_ie_len;
  161. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  162. *pos++ = sizeof(*mdie);
  163. mdie = (struct rsn_mdie *) pos;
  164. os_memcpy(mdie->mobility_domain, md,
  165. MOBILITY_DOMAIN_ID_LEN);
  166. mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
  167. wpa_s->sme.assoc_req_ie_len += 5;
  168. }
  169. if (wpa_s->sme.ft_used &&
  170. os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
  171. wpa_sm_has_ptk(wpa_s->wpa)) {
  172. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
  173. "over-the-air");
  174. params.auth_alg = WPA_AUTH_ALG_FT;
  175. params.ie = wpa_s->sme.ft_ies;
  176. params.ie_len = wpa_s->sme.ft_ies_len;
  177. }
  178. }
  179. #endif /* CONFIG_IEEE80211R */
  180. #ifdef CONFIG_IEEE80211W
  181. wpa_s->sme.mfp = ssid->ieee80211w;
  182. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  183. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  184. struct wpa_ie_data _ie;
  185. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
  186. _ie.capabilities &
  187. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  188. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
  189. "MFP: require MFP");
  190. wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
  191. }
  192. }
  193. #endif /* CONFIG_IEEE80211W */
  194. #ifdef CONFIG_P2P
  195. if (wpa_s->global->p2p) {
  196. u8 *pos;
  197. size_t len;
  198. int res;
  199. pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
  200. len = sizeof(wpa_s->sme.assoc_req_ie) -
  201. wpa_s->sme.assoc_req_ie_len;
  202. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  203. ssid->p2p_group);
  204. if (res >= 0)
  205. wpa_s->sme.assoc_req_ie_len += res;
  206. }
  207. #endif /* CONFIG_P2P */
  208. #ifdef CONFIG_INTERWORKING
  209. if (wpa_s->conf->interworking) {
  210. u8 *pos = wpa_s->sme.assoc_req_ie;
  211. if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  212. pos += 2 + pos[1];
  213. os_memmove(pos + 6, pos,
  214. wpa_s->sme.assoc_req_ie_len -
  215. (pos - wpa_s->sme.assoc_req_ie));
  216. wpa_s->sme.assoc_req_ie_len += 6;
  217. *pos++ = WLAN_EID_EXT_CAPAB;
  218. *pos++ = 4;
  219. *pos++ = 0x00;
  220. *pos++ = 0x00;
  221. *pos++ = 0x00;
  222. *pos++ = 0x80; /* Bit 31 - Interworking */
  223. }
  224. #endif /* CONFIG_INTERWORKING */
  225. wpa_supplicant_cancel_sched_scan(wpa_s);
  226. wpa_supplicant_cancel_scan(wpa_s);
  227. wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
  228. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  229. wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
  230. wpa_clear_keys(wpa_s, bss->bssid);
  231. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  232. old_ssid = wpa_s->current_ssid;
  233. wpa_s->current_ssid = ssid;
  234. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  235. wpa_supplicant_initiate_eapol(wpa_s);
  236. if (old_ssid != wpa_s->current_ssid)
  237. wpas_notify_network_changed(wpa_s);
  238. wpa_s->sme.auth_alg = params.auth_alg;
  239. if (wpa_drv_authenticate(wpa_s, &params) < 0) {
  240. wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
  241. "driver failed");
  242. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  243. wpas_connection_failed(wpa_s, bss->bssid);
  244. return;
  245. }
  246. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  247. NULL);
  248. /*
  249. * Association will be started based on the authentication event from
  250. * the driver.
  251. */
  252. }
  253. void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
  254. {
  255. struct wpa_ssid *ssid = wpa_s->current_ssid;
  256. if (ssid == NULL) {
  257. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
  258. "when network is not selected");
  259. return;
  260. }
  261. if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
  262. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
  263. "when not in authenticating state");
  264. return;
  265. }
  266. if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
  267. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
  268. "unexpected peer " MACSTR,
  269. MAC2STR(data->auth.peer));
  270. return;
  271. }
  272. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
  273. " auth_type=%d status_code=%d",
  274. MAC2STR(data->auth.peer), data->auth.auth_type,
  275. data->auth.status_code);
  276. wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
  277. data->auth.ies, data->auth.ies_len);
  278. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  279. if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
  280. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
  281. "code %d)", data->auth.status_code);
  282. if (data->auth.status_code !=
  283. WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
  284. wpa_s->sme.auth_alg == data->auth.auth_type ||
  285. wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
  286. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  287. return;
  288. }
  289. switch (data->auth.auth_type) {
  290. case WLAN_AUTH_OPEN:
  291. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  292. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
  293. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  294. wpa_s->current_ssid);
  295. return;
  296. case WLAN_AUTH_SHARED_KEY:
  297. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
  298. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
  299. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  300. wpa_s->current_ssid);
  301. return;
  302. default:
  303. return;
  304. }
  305. }
  306. #ifdef CONFIG_IEEE80211R
  307. if (data->auth.auth_type == WLAN_AUTH_FT) {
  308. union wpa_event_data edata;
  309. os_memset(&edata, 0, sizeof(edata));
  310. edata.ft_ies.ies = data->auth.ies;
  311. edata.ft_ies.ies_len = data->auth.ies_len;
  312. os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
  313. wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
  314. }
  315. #endif /* CONFIG_IEEE80211R */
  316. sme_associate(wpa_s, ssid->mode, data->auth.peer,
  317. data->auth.auth_type);
  318. }
  319. void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
  320. const u8 *bssid, u16 auth_type)
  321. {
  322. struct wpa_driver_associate_params params;
  323. struct ieee802_11_elems elems;
  324. os_memset(&params, 0, sizeof(params));
  325. params.bssid = bssid;
  326. params.ssid = wpa_s->sme.ssid;
  327. params.ssid_len = wpa_s->sme.ssid_len;
  328. params.freq = wpa_s->sme.freq;
  329. params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
  330. wpa_s->sme.assoc_req_ie : NULL;
  331. params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
  332. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  333. params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
  334. #ifdef CONFIG_IEEE80211R
  335. if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
  336. params.wpa_ie = wpa_s->sme.ft_ies;
  337. params.wpa_ie_len = wpa_s->sme.ft_ies_len;
  338. }
  339. #endif /* CONFIG_IEEE80211R */
  340. params.mode = mode;
  341. params.mgmt_frame_protection = wpa_s->sme.mfp;
  342. if (wpa_s->sme.prev_bssid_set)
  343. params.prev_bssid = wpa_s->sme.prev_bssid;
  344. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  345. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  346. params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
  347. params.freq);
  348. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  349. if (params.wpa_ie == NULL ||
  350. ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
  351. < 0) {
  352. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
  353. os_memset(&elems, 0, sizeof(elems));
  354. }
  355. if (elems.rsn_ie) {
  356. params.wpa_proto = WPA_PROTO_RSN;
  357. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
  358. elems.rsn_ie_len + 2);
  359. } else if (elems.wpa_ie) {
  360. params.wpa_proto = WPA_PROTO_WPA;
  361. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
  362. elems.wpa_ie_len + 2);
  363. } else
  364. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  365. if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
  366. params.p2p = 1;
  367. if (wpa_s->parent->set_sta_uapsd)
  368. params.uapsd = wpa_s->parent->sta_uapsd;
  369. else
  370. params.uapsd = -1;
  371. if (wpa_drv_associate(wpa_s, &params) < 0) {
  372. wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
  373. "driver failed");
  374. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  375. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  376. return;
  377. }
  378. eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
  379. NULL);
  380. }
  381. int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  382. const u8 *ies, size_t ies_len)
  383. {
  384. if (md == NULL || ies == NULL) {
  385. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
  386. os_free(wpa_s->sme.ft_ies);
  387. wpa_s->sme.ft_ies = NULL;
  388. wpa_s->sme.ft_ies_len = 0;
  389. wpa_s->sme.ft_used = 0;
  390. return 0;
  391. }
  392. os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  393. wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
  394. os_free(wpa_s->sme.ft_ies);
  395. wpa_s->sme.ft_ies = os_malloc(ies_len);
  396. if (wpa_s->sme.ft_ies == NULL)
  397. return -1;
  398. os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
  399. wpa_s->sme.ft_ies_len = ies_len;
  400. return 0;
  401. }
  402. static void sme_deauth(struct wpa_supplicant *wpa_s)
  403. {
  404. int bssid_changed;
  405. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  406. if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
  407. WLAN_REASON_DEAUTH_LEAVING) < 0) {
  408. wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
  409. "failed");
  410. }
  411. wpa_s->sme.prev_bssid_set = 0;
  412. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  413. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  414. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  415. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  416. if (bssid_changed)
  417. wpas_notify_bssid_changed(wpa_s);
  418. }
  419. void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
  420. union wpa_event_data *data)
  421. {
  422. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
  423. "status code %d", MAC2STR(wpa_s->pending_bssid),
  424. data->assoc_reject.status_code);
  425. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  426. /*
  427. * For now, unconditionally terminate the previous authentication. In
  428. * theory, this should not be needed, but mac80211 gets quite confused
  429. * if the authentication is left pending.. Some roaming cases might
  430. * benefit from using the previous authentication, so this could be
  431. * optimized in the future.
  432. */
  433. sme_deauth(wpa_s);
  434. }
  435. void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
  436. union wpa_event_data *data)
  437. {
  438. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
  439. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  440. }
  441. void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
  442. union wpa_event_data *data)
  443. {
  444. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
  445. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  446. wpa_supplicant_mark_disassoc(wpa_s);
  447. }
  448. void sme_event_disassoc(struct wpa_supplicant *wpa_s,
  449. union wpa_event_data *data)
  450. {
  451. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
  452. if (wpa_s->sme.prev_bssid_set) {
  453. /*
  454. * cfg80211/mac80211 can get into somewhat confused state if
  455. * the AP only disassociates us and leaves us in authenticated
  456. * state. For now, force the state to be cleared to avoid
  457. * confusing errors if we try to associate with the AP again.
  458. */
  459. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
  460. "driver state");
  461. wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
  462. WLAN_REASON_DEAUTH_LEAVING);
  463. }
  464. }
  465. static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
  466. {
  467. struct wpa_supplicant *wpa_s = eloop_ctx;
  468. if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
  469. wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
  470. sme_deauth(wpa_s);
  471. }
  472. }
  473. static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
  474. {
  475. struct wpa_supplicant *wpa_s = eloop_ctx;
  476. if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  477. wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
  478. sme_deauth(wpa_s);
  479. }
  480. }
  481. void sme_state_changed(struct wpa_supplicant *wpa_s)
  482. {
  483. /* Make sure timers are cleaned up appropriately. */
  484. if (wpa_s->wpa_state != WPA_ASSOCIATING)
  485. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  486. if (wpa_s->wpa_state != WPA_AUTHENTICATING)
  487. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  488. }
  489. void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
  490. const u8 *prev_pending_bssid)
  491. {
  492. /*
  493. * mac80211-workaround to force deauth on failed auth cmd,
  494. * requires us to remain in authenticating state to allow the
  495. * second authentication attempt to be continued properly.
  496. */
  497. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
  498. "to proceed after disconnection event");
  499. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  500. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  501. /*
  502. * Re-arm authentication timer in case auth fails for whatever reason.
  503. */
  504. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  505. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  506. NULL);
  507. }
  508. void sme_deinit(struct wpa_supplicant *wpa_s)
  509. {
  510. os_free(wpa_s->sme.ft_ies);
  511. wpa_s->sme.ft_ies = NULL;
  512. wpa_s->sme.ft_ies_len = 0;
  513. #ifdef CONFIG_IEEE80211W
  514. sme_stop_sa_query(wpa_s);
  515. #endif /* CONFIG_IEEE80211W */
  516. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  517. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  518. }
  519. #ifdef CONFIG_IEEE80211W
  520. static const unsigned int sa_query_max_timeout = 1000;
  521. static const unsigned int sa_query_retry_timeout = 201;
  522. static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
  523. {
  524. u32 tu;
  525. struct os_time now, passed;
  526. os_get_time(&now);
  527. os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
  528. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  529. if (sa_query_max_timeout < tu) {
  530. wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
  531. sme_stop_sa_query(wpa_s);
  532. wpa_supplicant_deauthenticate(
  533. wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
  534. return 1;
  535. }
  536. return 0;
  537. }
  538. static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
  539. const u8 *trans_id)
  540. {
  541. u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
  542. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
  543. MACSTR, MAC2STR(wpa_s->bssid));
  544. wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
  545. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  546. req[0] = WLAN_ACTION_SA_QUERY;
  547. req[1] = WLAN_SA_QUERY_REQUEST;
  548. os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  549. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  550. wpa_s->own_addr, wpa_s->bssid,
  551. req, sizeof(req), 0) < 0)
  552. wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
  553. "Request");
  554. }
  555. static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  556. {
  557. struct wpa_supplicant *wpa_s = eloop_ctx;
  558. unsigned int timeout, sec, usec;
  559. u8 *trans_id, *nbuf;
  560. if (wpa_s->sme.sa_query_count > 0 &&
  561. sme_check_sa_query_timeout(wpa_s))
  562. return;
  563. nbuf = os_realloc(wpa_s->sme.sa_query_trans_id,
  564. (wpa_s->sme.sa_query_count + 1) *
  565. WLAN_SA_QUERY_TR_ID_LEN);
  566. if (nbuf == NULL)
  567. return;
  568. if (wpa_s->sme.sa_query_count == 0) {
  569. /* Starting a new SA Query procedure */
  570. os_get_time(&wpa_s->sme.sa_query_start);
  571. }
  572. trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  573. wpa_s->sme.sa_query_trans_id = nbuf;
  574. wpa_s->sme.sa_query_count++;
  575. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  576. timeout = sa_query_retry_timeout;
  577. sec = ((timeout / 1000) * 1024) / 1000;
  578. usec = (timeout % 1000) * 1024;
  579. eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
  580. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
  581. wpa_s->sme.sa_query_count);
  582. sme_send_sa_query_req(wpa_s, trans_id);
  583. }
  584. static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
  585. {
  586. sme_sa_query_timer(wpa_s, NULL);
  587. }
  588. static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
  589. {
  590. eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
  591. os_free(wpa_s->sme.sa_query_trans_id);
  592. wpa_s->sme.sa_query_trans_id = NULL;
  593. wpa_s->sme.sa_query_count = 0;
  594. }
  595. void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
  596. const u8 *da, u16 reason_code)
  597. {
  598. struct wpa_ssid *ssid;
  599. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  600. return;
  601. if (wpa_s->wpa_state != WPA_COMPLETED)
  602. return;
  603. ssid = wpa_s->current_ssid;
  604. if (ssid == NULL || ssid->ieee80211w == 0)
  605. return;
  606. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  607. return;
  608. if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
  609. reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
  610. return;
  611. if (wpa_s->sme.sa_query_count > 0)
  612. return;
  613. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
  614. "possible AP/STA state mismatch - trigger SA Query");
  615. sme_start_sa_query(wpa_s);
  616. }
  617. void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
  618. const u8 *data, size_t len)
  619. {
  620. int i;
  621. if (wpa_s->sme.sa_query_trans_id == NULL ||
  622. len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
  623. data[0] != WLAN_SA_QUERY_RESPONSE)
  624. return;
  625. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
  626. MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
  627. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  628. return;
  629. for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
  630. if (os_memcmp(wpa_s->sme.sa_query_trans_id +
  631. i * WLAN_SA_QUERY_TR_ID_LEN,
  632. data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
  633. break;
  634. }
  635. if (i >= wpa_s->sme.sa_query_count) {
  636. wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
  637. "transaction identifier found");
  638. return;
  639. }
  640. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
  641. "from " MACSTR, MAC2STR(sa));
  642. sme_stop_sa_query(wpa_s);
  643. }
  644. #endif /* CONFIG_IEEE80211W */