sme.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. wpas_connection_failed(wpa_s, bss->bssid);
  243. wpa_supplicant_mark_disassoc(wpa_s);
  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. #ifdef CONFIG_HT_OVERRIDES
  325. struct ieee80211_ht_capabilities htcaps;
  326. struct ieee80211_ht_capabilities htcaps_mask;
  327. #endif /* CONFIG_HT_OVERRIDES */
  328. os_memset(&params, 0, sizeof(params));
  329. params.bssid = bssid;
  330. params.ssid = wpa_s->sme.ssid;
  331. params.ssid_len = wpa_s->sme.ssid_len;
  332. params.freq = wpa_s->sme.freq;
  333. params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
  334. wpa_s->sme.assoc_req_ie : NULL;
  335. params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
  336. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  337. params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
  338. #ifdef CONFIG_HT_OVERRIDES
  339. os_memset(&htcaps, 0, sizeof(htcaps));
  340. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  341. params.htcaps = (u8 *) &htcaps;
  342. params.htcaps_mask = (u8 *) &htcaps_mask;
  343. wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
  344. #endif /* CONFIG_HT_OVERRIDES */
  345. #ifdef CONFIG_IEEE80211R
  346. if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
  347. params.wpa_ie = wpa_s->sme.ft_ies;
  348. params.wpa_ie_len = wpa_s->sme.ft_ies_len;
  349. }
  350. #endif /* CONFIG_IEEE80211R */
  351. params.mode = mode;
  352. params.mgmt_frame_protection = wpa_s->sme.mfp;
  353. if (wpa_s->sme.prev_bssid_set)
  354. params.prev_bssid = wpa_s->sme.prev_bssid;
  355. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  356. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  357. params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
  358. params.freq);
  359. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  360. if (params.wpa_ie == NULL ||
  361. ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
  362. < 0) {
  363. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
  364. os_memset(&elems, 0, sizeof(elems));
  365. }
  366. if (elems.rsn_ie) {
  367. params.wpa_proto = WPA_PROTO_RSN;
  368. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
  369. elems.rsn_ie_len + 2);
  370. } else if (elems.wpa_ie) {
  371. params.wpa_proto = WPA_PROTO_WPA;
  372. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
  373. elems.wpa_ie_len + 2);
  374. } else
  375. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  376. if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
  377. params.p2p = 1;
  378. if (wpa_s->parent->set_sta_uapsd)
  379. params.uapsd = wpa_s->parent->sta_uapsd;
  380. else
  381. params.uapsd = -1;
  382. if (wpa_drv_associate(wpa_s, &params) < 0) {
  383. wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
  384. "driver failed");
  385. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  386. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  387. return;
  388. }
  389. eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
  390. NULL);
  391. }
  392. int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  393. const u8 *ies, size_t ies_len)
  394. {
  395. if (md == NULL || ies == NULL) {
  396. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
  397. os_free(wpa_s->sme.ft_ies);
  398. wpa_s->sme.ft_ies = NULL;
  399. wpa_s->sme.ft_ies_len = 0;
  400. wpa_s->sme.ft_used = 0;
  401. return 0;
  402. }
  403. os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  404. wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
  405. os_free(wpa_s->sme.ft_ies);
  406. wpa_s->sme.ft_ies = os_malloc(ies_len);
  407. if (wpa_s->sme.ft_ies == NULL)
  408. return -1;
  409. os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
  410. wpa_s->sme.ft_ies_len = ies_len;
  411. return 0;
  412. }
  413. static void sme_deauth(struct wpa_supplicant *wpa_s)
  414. {
  415. int bssid_changed;
  416. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  417. if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
  418. WLAN_REASON_DEAUTH_LEAVING) < 0) {
  419. wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
  420. "failed");
  421. }
  422. wpa_s->sme.prev_bssid_set = 0;
  423. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  424. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  425. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  426. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  427. if (bssid_changed)
  428. wpas_notify_bssid_changed(wpa_s);
  429. }
  430. void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
  431. union wpa_event_data *data)
  432. {
  433. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
  434. "status code %d", MAC2STR(wpa_s->pending_bssid),
  435. data->assoc_reject.status_code);
  436. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  437. /*
  438. * For now, unconditionally terminate the previous authentication. In
  439. * theory, this should not be needed, but mac80211 gets quite confused
  440. * if the authentication is left pending.. Some roaming cases might
  441. * benefit from using the previous authentication, so this could be
  442. * optimized in the future.
  443. */
  444. sme_deauth(wpa_s);
  445. }
  446. void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
  447. union wpa_event_data *data)
  448. {
  449. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
  450. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  451. wpa_supplicant_mark_disassoc(wpa_s);
  452. }
  453. void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
  454. union wpa_event_data *data)
  455. {
  456. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
  457. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  458. wpa_supplicant_mark_disassoc(wpa_s);
  459. }
  460. void sme_event_disassoc(struct wpa_supplicant *wpa_s,
  461. union wpa_event_data *data)
  462. {
  463. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
  464. if (wpa_s->sme.prev_bssid_set) {
  465. /*
  466. * cfg80211/mac80211 can get into somewhat confused state if
  467. * the AP only disassociates us and leaves us in authenticated
  468. * state. For now, force the state to be cleared to avoid
  469. * confusing errors if we try to associate with the AP again.
  470. */
  471. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
  472. "driver state");
  473. wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
  474. WLAN_REASON_DEAUTH_LEAVING);
  475. }
  476. }
  477. static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
  478. {
  479. struct wpa_supplicant *wpa_s = eloop_ctx;
  480. if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
  481. wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
  482. sme_deauth(wpa_s);
  483. }
  484. }
  485. static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
  486. {
  487. struct wpa_supplicant *wpa_s = eloop_ctx;
  488. if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  489. wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
  490. sme_deauth(wpa_s);
  491. }
  492. }
  493. void sme_state_changed(struct wpa_supplicant *wpa_s)
  494. {
  495. /* Make sure timers are cleaned up appropriately. */
  496. if (wpa_s->wpa_state != WPA_ASSOCIATING)
  497. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  498. if (wpa_s->wpa_state != WPA_AUTHENTICATING)
  499. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  500. }
  501. void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
  502. const u8 *prev_pending_bssid)
  503. {
  504. /*
  505. * mac80211-workaround to force deauth on failed auth cmd,
  506. * requires us to remain in authenticating state to allow the
  507. * second authentication attempt to be continued properly.
  508. */
  509. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
  510. "to proceed after disconnection event");
  511. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  512. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  513. /*
  514. * Re-arm authentication timer in case auth fails for whatever reason.
  515. */
  516. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  517. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  518. NULL);
  519. }
  520. void sme_deinit(struct wpa_supplicant *wpa_s)
  521. {
  522. os_free(wpa_s->sme.ft_ies);
  523. wpa_s->sme.ft_ies = NULL;
  524. wpa_s->sme.ft_ies_len = 0;
  525. #ifdef CONFIG_IEEE80211W
  526. sme_stop_sa_query(wpa_s);
  527. #endif /* CONFIG_IEEE80211W */
  528. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  529. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  530. }
  531. #ifdef CONFIG_IEEE80211W
  532. static const unsigned int sa_query_max_timeout = 1000;
  533. static const unsigned int sa_query_retry_timeout = 201;
  534. static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
  535. {
  536. u32 tu;
  537. struct os_time now, passed;
  538. os_get_time(&now);
  539. os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
  540. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  541. if (sa_query_max_timeout < tu) {
  542. wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
  543. sme_stop_sa_query(wpa_s);
  544. wpa_supplicant_deauthenticate(
  545. wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
  546. return 1;
  547. }
  548. return 0;
  549. }
  550. static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
  551. const u8 *trans_id)
  552. {
  553. u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
  554. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
  555. MACSTR, MAC2STR(wpa_s->bssid));
  556. wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
  557. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  558. req[0] = WLAN_ACTION_SA_QUERY;
  559. req[1] = WLAN_SA_QUERY_REQUEST;
  560. os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  561. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  562. wpa_s->own_addr, wpa_s->bssid,
  563. req, sizeof(req), 0) < 0)
  564. wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
  565. "Request");
  566. }
  567. static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  568. {
  569. struct wpa_supplicant *wpa_s = eloop_ctx;
  570. unsigned int timeout, sec, usec;
  571. u8 *trans_id, *nbuf;
  572. if (wpa_s->sme.sa_query_count > 0 &&
  573. sme_check_sa_query_timeout(wpa_s))
  574. return;
  575. nbuf = os_realloc(wpa_s->sme.sa_query_trans_id,
  576. (wpa_s->sme.sa_query_count + 1) *
  577. WLAN_SA_QUERY_TR_ID_LEN);
  578. if (nbuf == NULL)
  579. return;
  580. if (wpa_s->sme.sa_query_count == 0) {
  581. /* Starting a new SA Query procedure */
  582. os_get_time(&wpa_s->sme.sa_query_start);
  583. }
  584. trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  585. wpa_s->sme.sa_query_trans_id = nbuf;
  586. wpa_s->sme.sa_query_count++;
  587. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  588. timeout = sa_query_retry_timeout;
  589. sec = ((timeout / 1000) * 1024) / 1000;
  590. usec = (timeout % 1000) * 1024;
  591. eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
  592. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
  593. wpa_s->sme.sa_query_count);
  594. sme_send_sa_query_req(wpa_s, trans_id);
  595. }
  596. static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
  597. {
  598. sme_sa_query_timer(wpa_s, NULL);
  599. }
  600. static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
  601. {
  602. eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
  603. os_free(wpa_s->sme.sa_query_trans_id);
  604. wpa_s->sme.sa_query_trans_id = NULL;
  605. wpa_s->sme.sa_query_count = 0;
  606. }
  607. void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
  608. const u8 *da, u16 reason_code)
  609. {
  610. struct wpa_ssid *ssid;
  611. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  612. return;
  613. if (wpa_s->wpa_state != WPA_COMPLETED)
  614. return;
  615. ssid = wpa_s->current_ssid;
  616. if (ssid == NULL || ssid->ieee80211w == 0)
  617. return;
  618. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  619. return;
  620. if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
  621. reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
  622. return;
  623. if (wpa_s->sme.sa_query_count > 0)
  624. return;
  625. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
  626. "possible AP/STA state mismatch - trigger SA Query");
  627. sme_start_sa_query(wpa_s);
  628. }
  629. void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
  630. const u8 *data, size_t len)
  631. {
  632. int i;
  633. if (wpa_s->sme.sa_query_trans_id == NULL ||
  634. len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
  635. data[0] != WLAN_SA_QUERY_RESPONSE)
  636. return;
  637. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
  638. MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
  639. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  640. return;
  641. for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
  642. if (os_memcmp(wpa_s->sme.sa_query_trans_id +
  643. i * WLAN_SA_QUERY_TR_ID_LEN,
  644. data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
  645. break;
  646. }
  647. if (i >= wpa_s->sme.sa_query_count) {
  648. wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
  649. "transaction identifier found");
  650. return;
  651. }
  652. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
  653. "from " MACSTR, MAC2STR(sa));
  654. sme_stop_sa_query(wpa_s);
  655. }
  656. #endif /* CONFIG_IEEE80211W */