sme.c 14 KB

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