sme.c 22 KB

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