sme.c 12 KB

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