sme.c 13 KB

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