sme.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * wpa_supplicant - SME
  3. * Copyright (c) 2009, 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 "eapol_supp/eapol_supp_sm.h"
  18. #include "common/wpa_common.h"
  19. #include "rsn_supp/wpa.h"
  20. #include "rsn_supp/pmksa_cache.h"
  21. #include "config.h"
  22. #include "wpa_supplicant_i.h"
  23. #include "driver_i.h"
  24. #include "wpas_glue.h"
  25. #include "wps_supplicant.h"
  26. #include "notify.h"
  27. #include "blacklist.h"
  28. #include "bss.h"
  29. #include "scan.h"
  30. #include "sme.h"
  31. void sme_authenticate(struct wpa_supplicant *wpa_s,
  32. struct wpa_bss *bss, struct wpa_ssid *ssid)
  33. {
  34. struct wpa_driver_auth_params params;
  35. struct wpa_ssid *old_ssid;
  36. #ifdef CONFIG_IEEE80211R
  37. const u8 *ie;
  38. #endif /* CONFIG_IEEE80211R */
  39. #ifdef CONFIG_IEEE80211R
  40. const u8 *md = NULL;
  41. #endif /* CONFIG_IEEE80211R */
  42. int i, bssid_changed;
  43. if (bss == NULL) {
  44. wpa_printf(MSG_ERROR, "SME: No scan result available for the "
  45. "network");
  46. return;
  47. }
  48. wpa_s->current_bss = bss;
  49. os_memset(&params, 0, sizeof(params));
  50. wpa_s->reassociate = 0;
  51. params.freq = bss->freq;
  52. params.bssid = bss->bssid;
  53. params.ssid = bss->ssid;
  54. params.ssid_len = bss->ssid_len;
  55. if (wpa_s->sme.ssid_len != params.ssid_len ||
  56. os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
  57. wpa_s->sme.prev_bssid_set = 0;
  58. wpa_s->sme.freq = params.freq;
  59. os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
  60. wpa_s->sme.ssid_len = params.ssid_len;
  61. params.auth_alg = WPA_AUTH_ALG_OPEN;
  62. #ifdef IEEE8021X_EAPOL
  63. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  64. if (ssid->leap) {
  65. if (ssid->non_leap == 0)
  66. params.auth_alg = WPA_AUTH_ALG_LEAP;
  67. else
  68. params.auth_alg |= WPA_AUTH_ALG_LEAP;
  69. }
  70. }
  71. #endif /* IEEE8021X_EAPOL */
  72. wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
  73. params.auth_alg);
  74. if (ssid->auth_alg) {
  75. params.auth_alg = ssid->auth_alg;
  76. wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
  77. params.auth_alg);
  78. }
  79. for (i = 0; i < NUM_WEP_KEYS; i++) {
  80. if (ssid->wep_key_len[i])
  81. params.wep_key[i] = ssid->wep_key[i];
  82. params.wep_key_len[i] = ssid->wep_key_len[i];
  83. }
  84. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  85. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  86. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  87. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  88. if (bssid_changed)
  89. wpas_notify_bssid_changed(wpa_s);
  90. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  91. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  92. (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  93. WPA_KEY_MGMT_FT_IEEE8021X |
  94. WPA_KEY_MGMT_FT_PSK |
  95. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  96. WPA_KEY_MGMT_PSK_SHA256))) {
  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_printf(MSG_WARNING, "SME: Failed to set WPA key "
  109. "management and encryption suites");
  110. return;
  111. }
  112. } else if (ssid->key_mgmt &
  113. (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  114. WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
  115. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
  116. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  117. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  118. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  119. wpa_s->sme.assoc_req_ie,
  120. &wpa_s->sme.assoc_req_ie_len)) {
  121. wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
  122. "management and encryption suites (no scan "
  123. "results)");
  124. return;
  125. }
  126. #ifdef CONFIG_WPS
  127. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  128. struct wpabuf *wps_ie;
  129. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  130. if (wps_ie && wpabuf_len(wps_ie) <=
  131. sizeof(wpa_s->sme.assoc_req_ie)) {
  132. wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
  133. os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
  134. wpa_s->sme.assoc_req_ie_len);
  135. } else
  136. wpa_s->sme.assoc_req_ie_len = 0;
  137. wpabuf_free(wps_ie);
  138. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  139. #endif /* CONFIG_WPS */
  140. } else {
  141. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  142. wpa_s->sme.assoc_req_ie_len = 0;
  143. }
  144. #ifdef CONFIG_IEEE80211R
  145. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  146. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  147. md = ie + 2;
  148. wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
  149. if (md) {
  150. /* Prepare for the next transition */
  151. wpa_ft_prepare_auth_request(wpa_s->wpa);
  152. }
  153. if (md && ssid->key_mgmt & (WPA_KEY_MGMT_FT_PSK |
  154. WPA_KEY_MGMT_FT_IEEE8021X)) {
  155. if (wpa_s->sme.assoc_req_ie_len + 5 <
  156. sizeof(wpa_s->sme.assoc_req_ie)) {
  157. struct rsn_mdie *mdie;
  158. u8 *pos = wpa_s->sme.assoc_req_ie +
  159. wpa_s->sme.assoc_req_ie_len;
  160. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  161. *pos++ = sizeof(*mdie);
  162. mdie = (struct rsn_mdie *) pos;
  163. os_memcpy(mdie->mobility_domain, md,
  164. MOBILITY_DOMAIN_ID_LEN);
  165. mdie->ft_capab = 0;
  166. wpa_s->sme.assoc_req_ie_len += 5;
  167. }
  168. if (wpa_s->sme.ft_used &&
  169. os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0) {
  170. wpa_printf(MSG_DEBUG, "SME: Trying to use FT "
  171. "over-the-air");
  172. params.auth_alg = WPA_AUTH_ALG_FT;
  173. params.ie = wpa_s->sme.ft_ies;
  174. params.ie_len = wpa_s->sme.ft_ies_len;
  175. }
  176. }
  177. #endif /* CONFIG_IEEE80211R */
  178. #ifdef CONFIG_IEEE80211W
  179. wpa_s->sme.mfp = ssid->ieee80211w;
  180. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  181. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  182. struct wpa_ie_data _ie;
  183. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
  184. _ie.capabilities &
  185. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  186. wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
  187. "require MFP");
  188. wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
  189. }
  190. }
  191. #endif /* CONFIG_IEEE80211W */
  192. wpa_supplicant_cancel_scan(wpa_s);
  193. wpa_msg(wpa_s, MSG_INFO, "Trying to authenticate with " MACSTR
  194. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  195. wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
  196. wpa_clear_keys(wpa_s, bss->bssid);
  197. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  198. old_ssid = wpa_s->current_ssid;
  199. wpa_s->current_ssid = ssid;
  200. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  201. wpa_supplicant_initiate_eapol(wpa_s);
  202. if (old_ssid != wpa_s->current_ssid)
  203. wpas_notify_network_changed(wpa_s);
  204. if (wpa_drv_authenticate(wpa_s, &params) < 0) {
  205. wpa_msg(wpa_s, MSG_INFO, "Authentication request to the "
  206. "driver failed");
  207. return;
  208. }
  209. /* TODO: add timeout on authentication */
  210. /*
  211. * Association will be started based on the authentication event from
  212. * the driver.
  213. */
  214. }
  215. void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
  216. {
  217. struct wpa_driver_associate_params params;
  218. struct wpa_ssid *ssid = wpa_s->current_ssid;
  219. if (ssid == NULL) {
  220. wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
  221. "network is not selected");
  222. return;
  223. }
  224. if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
  225. wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
  226. "not in authenticating state");
  227. return;
  228. }
  229. if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
  230. wpa_printf(MSG_DEBUG, "SME: Ignore authentication with "
  231. "unexpected peer " MACSTR,
  232. MAC2STR(data->auth.peer));
  233. return;
  234. }
  235. wpa_printf(MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
  236. " auth_type=%d status_code=%d",
  237. MAC2STR(data->auth.peer), data->auth.auth_type,
  238. data->auth.status_code);
  239. wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
  240. data->auth.ies, data->auth.ies_len);
  241. if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
  242. wpa_printf(MSG_DEBUG, "SME: Authentication failed (status "
  243. "code %d)", data->auth.status_code);
  244. return;
  245. }
  246. #ifdef CONFIG_IEEE80211R
  247. if (data->auth.auth_type == WLAN_AUTH_FT) {
  248. union wpa_event_data edata;
  249. os_memset(&edata, 0, sizeof(edata));
  250. edata.ft_ies.ies = data->auth.ies;
  251. edata.ft_ies.ies_len = data->auth.ies_len;
  252. os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
  253. wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
  254. }
  255. #endif /* CONFIG_IEEE80211R */
  256. os_memset(&params, 0, sizeof(params));
  257. params.bssid = data->auth.peer;
  258. params.ssid = wpa_s->sme.ssid;
  259. params.ssid_len = wpa_s->sme.ssid_len;
  260. params.freq = wpa_s->sme.freq;
  261. params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
  262. wpa_s->sme.assoc_req_ie : NULL;
  263. params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
  264. #ifdef CONFIG_IEEE80211R
  265. if (data->auth.auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
  266. params.wpa_ie = wpa_s->sme.ft_ies;
  267. params.wpa_ie_len = wpa_s->sme.ft_ies_len;
  268. }
  269. #endif /* CONFIG_IEEE80211R */
  270. params.mode = ssid->mode;
  271. params.mgmt_frame_protection = wpa_s->sme.mfp;
  272. if (wpa_s->sme.prev_bssid_set)
  273. params.prev_bssid = wpa_s->sme.prev_bssid;
  274. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  275. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  276. params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
  277. params.freq);
  278. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  279. if (wpa_drv_associate(wpa_s, &params) < 0) {
  280. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  281. "failed");
  282. return;
  283. }
  284. /* TODO: add timeout on association */
  285. }
  286. int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  287. const u8 *ies, size_t ies_len)
  288. {
  289. if (md == NULL || ies == NULL) {
  290. wpa_printf(MSG_DEBUG, "SME: Remove mobility domain");
  291. os_free(wpa_s->sme.ft_ies);
  292. wpa_s->sme.ft_ies = NULL;
  293. wpa_s->sme.ft_ies_len = 0;
  294. wpa_s->sme.ft_used = 0;
  295. return 0;
  296. }
  297. os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  298. wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
  299. os_free(wpa_s->sme.ft_ies);
  300. wpa_s->sme.ft_ies = os_malloc(ies_len);
  301. if (wpa_s->sme.ft_ies == NULL)
  302. return -1;
  303. os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
  304. wpa_s->sme.ft_ies_len = ies_len;
  305. return 0;
  306. }
  307. void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
  308. union wpa_event_data *data)
  309. {
  310. int bssid_changed;
  311. int timeout = 5000;
  312. wpa_printf(MSG_DEBUG, "SME: Association with " MACSTR " failed: "
  313. "status code %d", MAC2STR(wpa_s->pending_bssid),
  314. data->assoc_reject.status_code);
  315. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  316. /*
  317. * For now, unconditionally terminate the previous authentication. In
  318. * theory, this should not be needed, but mac80211 gets quite confused
  319. * if the authentication is left pending.. Some roaming cases might
  320. * benefit from using the previous authentication, so this could be
  321. * optimized in the future.
  322. */
  323. if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
  324. WLAN_REASON_DEAUTH_LEAVING) < 0) {
  325. wpa_msg(wpa_s, MSG_INFO,
  326. "Deauth request to the driver failed");
  327. }
  328. wpa_s->sme.prev_bssid_set = 0;
  329. if (wpa_blacklist_add(wpa_s, wpa_s->pending_bssid) == 0) {
  330. struct wpa_blacklist *b;
  331. b = wpa_blacklist_get(wpa_s, wpa_s->pending_bssid);
  332. if (b && b->count < 3) {
  333. /*
  334. * Speed up next attempt if there could be other APs
  335. * that could accept association.
  336. */
  337. timeout = 100;
  338. }
  339. }
  340. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  341. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  342. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  343. if (bssid_changed)
  344. wpas_notify_bssid_changed(wpa_s);
  345. /*
  346. * TODO: if more than one possible AP is available in scan results,
  347. * could try the other ones before requesting a new scan.
  348. */
  349. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  350. 1000 * (timeout % 1000));
  351. }
  352. void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
  353. union wpa_event_data *data)
  354. {
  355. wpa_printf(MSG_DEBUG, "SME: Authentication timed out");
  356. wpa_supplicant_req_scan(wpa_s, 5, 0);
  357. }
  358. void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
  359. union wpa_event_data *data)
  360. {
  361. wpa_printf(MSG_DEBUG, "SME: Association timed out");
  362. wpa_supplicant_mark_disassoc(wpa_s);
  363. wpa_supplicant_req_scan(wpa_s, 5, 0);
  364. }
  365. void sme_event_disassoc(struct wpa_supplicant *wpa_s,
  366. union wpa_event_data *data)
  367. {
  368. wpa_printf(MSG_DEBUG, "SME: Disassociation event received");
  369. if (!is_zero_ether_addr(wpa_s->bssid) &&
  370. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)) {
  371. /*
  372. * cfg80211/mac80211 can get into somewhat confused state if
  373. * the AP only disassociates us and leaves us in authenticated
  374. * state. For now, force the state to be cleared to avoid
  375. * confusing errors if we try to associate with the AP again.
  376. */
  377. wpa_printf(MSG_DEBUG, "SME: Deauthenticate to clear driver "
  378. "state");
  379. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
  380. WLAN_REASON_DEAUTH_LEAVING);
  381. }
  382. }