sme.c 13 KB

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