sme.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * wpa_supplicant - SME
  3. * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "utils/eloop.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "eapol_supp/eapol_supp_sm.h"
  14. #include "common/wpa_common.h"
  15. #include "rsn_supp/wpa.h"
  16. #include "rsn_supp/pmksa_cache.h"
  17. #include "config.h"
  18. #include "wpa_supplicant_i.h"
  19. #include "driver_i.h"
  20. #include "wpas_glue.h"
  21. #include "wps_supplicant.h"
  22. #include "p2p_supplicant.h"
  23. #include "notify.h"
  24. #include "bss.h"
  25. #include "scan.h"
  26. #include "sme.h"
  27. #define SME_AUTH_TIMEOUT 5
  28. #define SME_ASSOC_TIMEOUT 5
  29. static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
  30. static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
  31. static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
  32. #ifdef CONFIG_IEEE80211W
  33. static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
  34. #endif /* CONFIG_IEEE80211W */
  35. void sme_authenticate(struct wpa_supplicant *wpa_s,
  36. struct wpa_bss *bss, struct wpa_ssid *ssid)
  37. {
  38. struct wpa_driver_auth_params params;
  39. struct wpa_ssid *old_ssid;
  40. #ifdef CONFIG_IEEE80211R
  41. const u8 *ie;
  42. #endif /* CONFIG_IEEE80211R */
  43. #ifdef CONFIG_IEEE80211R
  44. const u8 *md = NULL;
  45. #endif /* CONFIG_IEEE80211R */
  46. int i, bssid_changed;
  47. if (bss == NULL) {
  48. wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
  49. "the network");
  50. return;
  51. }
  52. wpa_s->current_bss = bss;
  53. os_memset(&params, 0, sizeof(params));
  54. wpa_s->reassociate = 0;
  55. params.freq = bss->freq;
  56. params.bssid = bss->bssid;
  57. params.ssid = bss->ssid;
  58. params.ssid_len = bss->ssid_len;
  59. params.p2p = ssid->p2p_group;
  60. if (wpa_s->sme.ssid_len != params.ssid_len ||
  61. os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
  62. wpa_s->sme.prev_bssid_set = 0;
  63. wpa_s->sme.freq = params.freq;
  64. os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
  65. wpa_s->sme.ssid_len = params.ssid_len;
  66. params.auth_alg = WPA_AUTH_ALG_OPEN;
  67. #ifdef IEEE8021X_EAPOL
  68. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  69. if (ssid->leap) {
  70. if (ssid->non_leap == 0)
  71. params.auth_alg = WPA_AUTH_ALG_LEAP;
  72. else
  73. params.auth_alg |= WPA_AUTH_ALG_LEAP;
  74. }
  75. }
  76. #endif /* IEEE8021X_EAPOL */
  77. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
  78. params.auth_alg);
  79. if (ssid->auth_alg) {
  80. params.auth_alg = ssid->auth_alg;
  81. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  82. "0x%x", 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 ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  96. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  97. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  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_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
  110. "key management and encryption suites");
  111. return;
  112. }
  113. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  114. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  115. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  116. wpa_s->sme.assoc_req_ie,
  117. &wpa_s->sme.assoc_req_ie_len)) {
  118. wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
  119. "key management and encryption suites (no "
  120. "scan results)");
  121. return;
  122. }
  123. #ifdef CONFIG_WPS
  124. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  125. struct wpabuf *wps_ie;
  126. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  127. if (wps_ie && wpabuf_len(wps_ie) <=
  128. sizeof(wpa_s->sme.assoc_req_ie)) {
  129. wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
  130. os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
  131. wpa_s->sme.assoc_req_ie_len);
  132. } else
  133. wpa_s->sme.assoc_req_ie_len = 0;
  134. wpabuf_free(wps_ie);
  135. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  136. #endif /* CONFIG_WPS */
  137. } else {
  138. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  139. wpa_s->sme.assoc_req_ie_len = 0;
  140. }
  141. #ifdef CONFIG_IEEE80211R
  142. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  143. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  144. md = ie + 2;
  145. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  146. if (md) {
  147. /* Prepare for the next transition */
  148. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  149. }
  150. if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
  151. if (wpa_s->sme.assoc_req_ie_len + 5 <
  152. sizeof(wpa_s->sme.assoc_req_ie)) {
  153. struct rsn_mdie *mdie;
  154. u8 *pos = wpa_s->sme.assoc_req_ie +
  155. wpa_s->sme.assoc_req_ie_len;
  156. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  157. *pos++ = sizeof(*mdie);
  158. mdie = (struct rsn_mdie *) pos;
  159. os_memcpy(mdie->mobility_domain, md,
  160. MOBILITY_DOMAIN_ID_LEN);
  161. mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
  162. wpa_s->sme.assoc_req_ie_len += 5;
  163. }
  164. if (wpa_s->sme.ft_used &&
  165. os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
  166. wpa_sm_has_ptk(wpa_s->wpa)) {
  167. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
  168. "over-the-air");
  169. params.auth_alg = WPA_AUTH_ALG_FT;
  170. params.ie = wpa_s->sme.ft_ies;
  171. params.ie_len = wpa_s->sme.ft_ies_len;
  172. }
  173. }
  174. #endif /* CONFIG_IEEE80211R */
  175. #ifdef CONFIG_IEEE80211W
  176. wpa_s->sme.mfp = ssid->ieee80211w;
  177. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  178. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  179. struct wpa_ie_data _ie;
  180. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
  181. _ie.capabilities &
  182. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  183. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
  184. "MFP: require MFP");
  185. wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
  186. }
  187. }
  188. #endif /* CONFIG_IEEE80211W */
  189. #ifdef CONFIG_P2P
  190. if (wpa_s->global->p2p) {
  191. u8 *pos;
  192. size_t len;
  193. int res;
  194. pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
  195. len = sizeof(wpa_s->sme.assoc_req_ie) -
  196. wpa_s->sme.assoc_req_ie_len;
  197. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  198. ssid->p2p_group);
  199. if (res >= 0)
  200. wpa_s->sme.assoc_req_ie_len += res;
  201. }
  202. #endif /* CONFIG_P2P */
  203. #ifdef CONFIG_INTERWORKING
  204. if (wpa_s->conf->interworking) {
  205. u8 *pos = wpa_s->sme.assoc_req_ie;
  206. if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  207. pos += 2 + pos[1];
  208. os_memmove(pos + 6, pos,
  209. wpa_s->sme.assoc_req_ie_len -
  210. (pos - wpa_s->sme.assoc_req_ie));
  211. wpa_s->sme.assoc_req_ie_len += 6;
  212. *pos++ = WLAN_EID_EXT_CAPAB;
  213. *pos++ = 4;
  214. *pos++ = 0x00;
  215. *pos++ = 0x00;
  216. *pos++ = 0x00;
  217. *pos++ = 0x80; /* Bit 31 - Interworking */
  218. }
  219. #endif /* CONFIG_INTERWORKING */
  220. wpa_supplicant_cancel_sched_scan(wpa_s);
  221. wpa_supplicant_cancel_scan(wpa_s);
  222. wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
  223. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  224. wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
  225. wpa_clear_keys(wpa_s, bss->bssid);
  226. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  227. old_ssid = wpa_s->current_ssid;
  228. wpa_s->current_ssid = ssid;
  229. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  230. wpa_supplicant_initiate_eapol(wpa_s);
  231. if (old_ssid != wpa_s->current_ssid)
  232. wpas_notify_network_changed(wpa_s);
  233. wpa_s->sme.auth_alg = params.auth_alg;
  234. if (wpa_drv_authenticate(wpa_s, &params) < 0) {
  235. wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
  236. "driver failed");
  237. wpas_connection_failed(wpa_s, bss->bssid);
  238. wpa_supplicant_mark_disassoc(wpa_s);
  239. return;
  240. }
  241. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  242. NULL);
  243. /*
  244. * Association will be started based on the authentication event from
  245. * the driver.
  246. */
  247. }
  248. void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
  249. {
  250. struct wpa_ssid *ssid = wpa_s->current_ssid;
  251. if (ssid == NULL) {
  252. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
  253. "when network is not selected");
  254. return;
  255. }
  256. if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
  257. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
  258. "when not in authenticating state");
  259. return;
  260. }
  261. if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
  262. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
  263. "unexpected peer " MACSTR,
  264. MAC2STR(data->auth.peer));
  265. return;
  266. }
  267. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
  268. " auth_type=%d status_code=%d",
  269. MAC2STR(data->auth.peer), data->auth.auth_type,
  270. data->auth.status_code);
  271. wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
  272. data->auth.ies, data->auth.ies_len);
  273. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  274. if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
  275. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
  276. "code %d)", data->auth.status_code);
  277. if (data->auth.status_code !=
  278. WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
  279. wpa_s->sme.auth_alg == data->auth.auth_type ||
  280. wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
  281. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  282. return;
  283. }
  284. switch (data->auth.auth_type) {
  285. case WLAN_AUTH_OPEN:
  286. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  287. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
  288. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  289. wpa_s->current_ssid);
  290. return;
  291. case WLAN_AUTH_SHARED_KEY:
  292. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
  293. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
  294. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  295. wpa_s->current_ssid);
  296. return;
  297. default:
  298. return;
  299. }
  300. }
  301. #ifdef CONFIG_IEEE80211R
  302. if (data->auth.auth_type == WLAN_AUTH_FT) {
  303. union wpa_event_data edata;
  304. os_memset(&edata, 0, sizeof(edata));
  305. edata.ft_ies.ies = data->auth.ies;
  306. edata.ft_ies.ies_len = data->auth.ies_len;
  307. os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
  308. wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
  309. }
  310. #endif /* CONFIG_IEEE80211R */
  311. sme_associate(wpa_s, ssid->mode, data->auth.peer,
  312. data->auth.auth_type);
  313. }
  314. void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
  315. const u8 *bssid, u16 auth_type)
  316. {
  317. struct wpa_driver_associate_params params;
  318. struct ieee802_11_elems elems;
  319. #ifdef CONFIG_HT_OVERRIDES
  320. struct ieee80211_ht_capabilities htcaps;
  321. struct ieee80211_ht_capabilities htcaps_mask;
  322. #endif /* CONFIG_HT_OVERRIDES */
  323. os_memset(&params, 0, sizeof(params));
  324. params.bssid = bssid;
  325. params.ssid = wpa_s->sme.ssid;
  326. params.ssid_len = wpa_s->sme.ssid_len;
  327. params.freq = wpa_s->sme.freq;
  328. params.bg_scan_period = wpa_s->current_ssid ?
  329. wpa_s->current_ssid->bg_scan_period : -1;
  330. params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
  331. wpa_s->sme.assoc_req_ie : NULL;
  332. params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
  333. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  334. params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
  335. #ifdef CONFIG_HT_OVERRIDES
  336. os_memset(&htcaps, 0, sizeof(htcaps));
  337. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  338. params.htcaps = (u8 *) &htcaps;
  339. params.htcaps_mask = (u8 *) &htcaps_mask;
  340. wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
  341. #endif /* CONFIG_HT_OVERRIDES */
  342. #ifdef CONFIG_IEEE80211R
  343. if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
  344. params.wpa_ie = wpa_s->sme.ft_ies;
  345. params.wpa_ie_len = wpa_s->sme.ft_ies_len;
  346. }
  347. #endif /* CONFIG_IEEE80211R */
  348. params.mode = mode;
  349. params.mgmt_frame_protection = wpa_s->sme.mfp;
  350. if (wpa_s->sme.prev_bssid_set)
  351. params.prev_bssid = wpa_s->sme.prev_bssid;
  352. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  353. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  354. params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
  355. params.freq);
  356. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  357. if (params.wpa_ie == NULL ||
  358. ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
  359. < 0) {
  360. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
  361. os_memset(&elems, 0, sizeof(elems));
  362. }
  363. if (elems.rsn_ie) {
  364. params.wpa_proto = WPA_PROTO_RSN;
  365. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
  366. elems.rsn_ie_len + 2);
  367. } else if (elems.wpa_ie) {
  368. params.wpa_proto = WPA_PROTO_WPA;
  369. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
  370. elems.wpa_ie_len + 2);
  371. } else
  372. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  373. if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
  374. params.p2p = 1;
  375. if (wpa_s->parent->set_sta_uapsd)
  376. params.uapsd = wpa_s->parent->sta_uapsd;
  377. else
  378. params.uapsd = -1;
  379. if (wpa_drv_associate(wpa_s, &params) < 0) {
  380. wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
  381. "driver failed");
  382. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  383. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  384. return;
  385. }
  386. eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
  387. NULL);
  388. }
  389. int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  390. const u8 *ies, size_t ies_len)
  391. {
  392. if (md == NULL || ies == NULL) {
  393. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
  394. os_free(wpa_s->sme.ft_ies);
  395. wpa_s->sme.ft_ies = NULL;
  396. wpa_s->sme.ft_ies_len = 0;
  397. wpa_s->sme.ft_used = 0;
  398. return 0;
  399. }
  400. os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  401. wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
  402. os_free(wpa_s->sme.ft_ies);
  403. wpa_s->sme.ft_ies = os_malloc(ies_len);
  404. if (wpa_s->sme.ft_ies == NULL)
  405. return -1;
  406. os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
  407. wpa_s->sme.ft_ies_len = ies_len;
  408. return 0;
  409. }
  410. static void sme_deauth(struct wpa_supplicant *wpa_s)
  411. {
  412. int bssid_changed;
  413. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  414. if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
  415. WLAN_REASON_DEAUTH_LEAVING) < 0) {
  416. wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
  417. "failed");
  418. }
  419. wpa_s->sme.prev_bssid_set = 0;
  420. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  421. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  422. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  423. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  424. if (bssid_changed)
  425. wpas_notify_bssid_changed(wpa_s);
  426. }
  427. void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
  428. union wpa_event_data *data)
  429. {
  430. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
  431. "status code %d", MAC2STR(wpa_s->pending_bssid),
  432. data->assoc_reject.status_code);
  433. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  434. /*
  435. * For now, unconditionally terminate the previous authentication. In
  436. * theory, this should not be needed, but mac80211 gets quite confused
  437. * if the authentication is left pending.. Some roaming cases might
  438. * benefit from using the previous authentication, so this could be
  439. * optimized in the future.
  440. */
  441. sme_deauth(wpa_s);
  442. }
  443. void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
  444. union wpa_event_data *data)
  445. {
  446. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
  447. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  448. wpa_supplicant_mark_disassoc(wpa_s);
  449. }
  450. void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
  451. union wpa_event_data *data)
  452. {
  453. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
  454. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  455. wpa_supplicant_mark_disassoc(wpa_s);
  456. }
  457. void sme_event_disassoc(struct wpa_supplicant *wpa_s,
  458. union wpa_event_data *data)
  459. {
  460. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
  461. if (wpa_s->sme.prev_bssid_set) {
  462. /*
  463. * cfg80211/mac80211 can get into somewhat confused state if
  464. * the AP only disassociates us and leaves us in authenticated
  465. * state. For now, force the state to be cleared to avoid
  466. * confusing errors if we try to associate with the AP again.
  467. */
  468. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
  469. "driver state");
  470. wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
  471. WLAN_REASON_DEAUTH_LEAVING);
  472. }
  473. }
  474. static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
  475. {
  476. struct wpa_supplicant *wpa_s = eloop_ctx;
  477. if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
  478. wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
  479. sme_deauth(wpa_s);
  480. }
  481. }
  482. static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
  483. {
  484. struct wpa_supplicant *wpa_s = eloop_ctx;
  485. if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  486. wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
  487. sme_deauth(wpa_s);
  488. }
  489. }
  490. void sme_state_changed(struct wpa_supplicant *wpa_s)
  491. {
  492. /* Make sure timers are cleaned up appropriately. */
  493. if (wpa_s->wpa_state != WPA_ASSOCIATING)
  494. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  495. if (wpa_s->wpa_state != WPA_AUTHENTICATING)
  496. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  497. }
  498. void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
  499. const u8 *prev_pending_bssid)
  500. {
  501. /*
  502. * mac80211-workaround to force deauth on failed auth cmd,
  503. * requires us to remain in authenticating state to allow the
  504. * second authentication attempt to be continued properly.
  505. */
  506. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
  507. "to proceed after disconnection event");
  508. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  509. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  510. /*
  511. * Re-arm authentication timer in case auth fails for whatever reason.
  512. */
  513. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  514. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  515. NULL);
  516. }
  517. void sme_deinit(struct wpa_supplicant *wpa_s)
  518. {
  519. os_free(wpa_s->sme.ft_ies);
  520. wpa_s->sme.ft_ies = NULL;
  521. wpa_s->sme.ft_ies_len = 0;
  522. #ifdef CONFIG_IEEE80211W
  523. sme_stop_sa_query(wpa_s);
  524. #endif /* CONFIG_IEEE80211W */
  525. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  526. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  527. eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
  528. }
  529. static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
  530. const u8 *chan_list, u8 num_channels,
  531. u8 num_intol)
  532. {
  533. struct ieee80211_2040_bss_coex_ie *bc_ie;
  534. struct ieee80211_2040_intol_chan_report *ic_report;
  535. struct wpabuf *buf;
  536. wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR,
  537. MAC2STR(wpa_s->bssid));
  538. buf = wpabuf_alloc(2 + /* action.category + action_code */
  539. sizeof(struct ieee80211_2040_bss_coex_ie) +
  540. sizeof(struct ieee80211_2040_intol_chan_report) +
  541. num_channels);
  542. if (buf == NULL)
  543. return;
  544. wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
  545. wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
  546. bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
  547. bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
  548. bc_ie->length = 1;
  549. if (num_intol)
  550. bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
  551. if (num_channels > 0) {
  552. ic_report = wpabuf_put(buf, sizeof(*ic_report));
  553. ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
  554. ic_report->length = num_channels + 1;
  555. ic_report->op_class = 0;
  556. os_memcpy(wpabuf_put(buf, num_channels), chan_list,
  557. num_channels);
  558. }
  559. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  560. wpa_s->own_addr, wpa_s->bssid,
  561. wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
  562. wpa_msg(wpa_s, MSG_INFO,
  563. "SME: Failed to send 20/40 BSS Coexistence frame");
  564. }
  565. wpabuf_free(buf);
  566. }
  567. /**
  568. * enum wpas_band - Frequency band
  569. * @WPAS_BAND_2GHZ: 2.4 GHz ISM band
  570. * @WPAS_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
  571. */
  572. enum wpas_band {
  573. WPAS_BAND_2GHZ,
  574. WPAS_BAND_5GHZ,
  575. WPAS_BAND_INVALID
  576. };
  577. /**
  578. * freq_to_channel - Convert frequency into channel info
  579. * @channel: Buffer for returning channel number
  580. * Returns: Band (2 or 5 GHz)
  581. */
  582. static enum wpas_band freq_to_channel(int freq, u8 *channel)
  583. {
  584. enum wpas_band band = (freq <= 2484) ? WPAS_BAND_2GHZ : WPAS_BAND_5GHZ;
  585. u8 chan = 0;
  586. if (freq >= 2412 && freq <= 2472)
  587. chan = (freq - 2407) / 5;
  588. else if (freq == 2484)
  589. chan = 14;
  590. else if (freq >= 5180 && freq <= 5805)
  591. chan = (freq - 5000) / 5;
  592. *channel = chan;
  593. return band;
  594. }
  595. int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
  596. {
  597. struct wpa_bss *bss;
  598. const u8 *ie;
  599. u16 ht_cap;
  600. u8 chan_list[P2P_MAX_CHANNELS], channel;
  601. u8 num_channels = 0, num_intol = 0, i;
  602. if (!wpa_s->sme.sched_obss_scan)
  603. return 0;
  604. wpa_s->sme.sched_obss_scan = 0;
  605. if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
  606. return 1;
  607. /*
  608. * Check whether AP uses regulatory triplet or channel triplet in
  609. * country info. Right now the operating class of the BSS channel
  610. * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
  611. * based on the assumption that operating class triplet is not used in
  612. * beacon frame. If the First Channel Number/Operating Extension
  613. * Identifier octet has a positive integer value of 201 or greater,
  614. * then its operating class triplet.
  615. *
  616. * TODO: If Supported Operating Classes element is present in beacon
  617. * frame, have to lookup operating class in Annex E and fill them in
  618. * 2040 coex frame.
  619. */
  620. ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
  621. if (ie && (ie[1] >= 6) && (ie[5] >= 201))
  622. return 1;
  623. os_memset(chan_list, 0, sizeof(chan_list));
  624. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  625. /* Skip other band bss */
  626. if (freq_to_channel(bss->freq, &channel) != WPAS_BAND_2GHZ)
  627. continue;
  628. ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
  629. ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
  630. if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
  631. /* Check whether the channel is already considered */
  632. for (i = 0; i < num_channels; i++) {
  633. if (channel == chan_list[i])
  634. break;
  635. }
  636. if (i != num_channels)
  637. continue;
  638. if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
  639. num_intol++;
  640. chan_list[num_channels++] = channel;
  641. }
  642. }
  643. sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
  644. return 1;
  645. }
  646. static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  647. {
  648. struct wpa_supplicant *wpa_s = eloop_ctx;
  649. struct wpa_driver_scan_params params;
  650. if (!wpa_s->current_bss) {
  651. wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
  652. return;
  653. }
  654. os_memset(&params, 0, sizeof(params));
  655. /* TODO: 2.4 GHz channels only */
  656. wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
  657. if (wpa_supplicant_trigger_scan(wpa_s, &params))
  658. wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
  659. else
  660. wpa_s->sme.sched_obss_scan = 1;
  661. eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
  662. sme_obss_scan_timeout, wpa_s, NULL);
  663. }
  664. void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
  665. {
  666. const u8 *ie;
  667. struct wpa_bss *bss = wpa_s->current_bss;
  668. struct wpa_ssid *ssid = wpa_s->current_ssid;
  669. eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
  670. wpa_s->sme.sched_obss_scan = 0;
  671. if (!enable)
  672. return;
  673. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) || ssid == NULL ||
  674. ssid->mode != IEEE80211_MODE_INFRA)
  675. return; /* Not using station SME in wpa_supplicant */
  676. if (!wpa_s->hw.modes ||
  677. !(wpa_s->hw.modes->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  678. return; /* Driver does not support HT40 */
  679. if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
  680. return; /* Not associated on 2.4 GHz band */
  681. /* Check whether AP supports HT40 */
  682. ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
  683. if (!ie || ie[1] < 2 ||
  684. !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  685. return; /* AP does not support HT40 */
  686. ie = wpa_bss_get_ie(wpa_s->current_bss,
  687. WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
  688. if (!ie || ie[1] < 14)
  689. return; /* AP does not request OBSS scans */
  690. wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
  691. if (wpa_s->sme.obss_scan_int < 10) {
  692. wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
  693. "replaced with the minimum 10 sec",
  694. wpa_s->sme.obss_scan_int);
  695. wpa_s->sme.obss_scan_int = 10;
  696. }
  697. wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
  698. wpa_s->sme.obss_scan_int);
  699. eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
  700. sme_obss_scan_timeout, wpa_s, NULL);
  701. }
  702. #ifdef CONFIG_IEEE80211W
  703. static const unsigned int sa_query_max_timeout = 1000;
  704. static const unsigned int sa_query_retry_timeout = 201;
  705. static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
  706. {
  707. u32 tu;
  708. struct os_time now, passed;
  709. os_get_time(&now);
  710. os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
  711. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  712. if (sa_query_max_timeout < tu) {
  713. wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
  714. sme_stop_sa_query(wpa_s);
  715. wpa_supplicant_deauthenticate(
  716. wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
  717. return 1;
  718. }
  719. return 0;
  720. }
  721. static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
  722. const u8 *trans_id)
  723. {
  724. u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
  725. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
  726. MACSTR, MAC2STR(wpa_s->bssid));
  727. wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
  728. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  729. req[0] = WLAN_ACTION_SA_QUERY;
  730. req[1] = WLAN_SA_QUERY_REQUEST;
  731. os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  732. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  733. wpa_s->own_addr, wpa_s->bssid,
  734. req, sizeof(req), 0) < 0)
  735. wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
  736. "Request");
  737. }
  738. static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  739. {
  740. struct wpa_supplicant *wpa_s = eloop_ctx;
  741. unsigned int timeout, sec, usec;
  742. u8 *trans_id, *nbuf;
  743. if (wpa_s->sme.sa_query_count > 0 &&
  744. sme_check_sa_query_timeout(wpa_s))
  745. return;
  746. nbuf = os_realloc(wpa_s->sme.sa_query_trans_id,
  747. (wpa_s->sme.sa_query_count + 1) *
  748. WLAN_SA_QUERY_TR_ID_LEN);
  749. if (nbuf == NULL)
  750. return;
  751. if (wpa_s->sme.sa_query_count == 0) {
  752. /* Starting a new SA Query procedure */
  753. os_get_time(&wpa_s->sme.sa_query_start);
  754. }
  755. trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  756. wpa_s->sme.sa_query_trans_id = nbuf;
  757. wpa_s->sme.sa_query_count++;
  758. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  759. timeout = sa_query_retry_timeout;
  760. sec = ((timeout / 1000) * 1024) / 1000;
  761. usec = (timeout % 1000) * 1024;
  762. eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
  763. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
  764. wpa_s->sme.sa_query_count);
  765. sme_send_sa_query_req(wpa_s, trans_id);
  766. }
  767. static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
  768. {
  769. sme_sa_query_timer(wpa_s, NULL);
  770. }
  771. static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
  772. {
  773. eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
  774. os_free(wpa_s->sme.sa_query_trans_id);
  775. wpa_s->sme.sa_query_trans_id = NULL;
  776. wpa_s->sme.sa_query_count = 0;
  777. }
  778. void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
  779. const u8 *da, u16 reason_code)
  780. {
  781. struct wpa_ssid *ssid;
  782. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  783. return;
  784. if (wpa_s->wpa_state != WPA_COMPLETED)
  785. return;
  786. ssid = wpa_s->current_ssid;
  787. if (ssid == NULL || ssid->ieee80211w == 0)
  788. return;
  789. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  790. return;
  791. if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
  792. reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
  793. return;
  794. if (wpa_s->sme.sa_query_count > 0)
  795. return;
  796. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
  797. "possible AP/STA state mismatch - trigger SA Query");
  798. sme_start_sa_query(wpa_s);
  799. }
  800. void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
  801. const u8 *data, size_t len)
  802. {
  803. int i;
  804. if (wpa_s->sme.sa_query_trans_id == NULL ||
  805. len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
  806. data[0] != WLAN_SA_QUERY_RESPONSE)
  807. return;
  808. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
  809. MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
  810. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  811. return;
  812. for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
  813. if (os_memcmp(wpa_s->sme.sa_query_trans_id +
  814. i * WLAN_SA_QUERY_TR_ID_LEN,
  815. data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
  816. break;
  817. }
  818. if (i >= wpa_s->sme.sa_query_count) {
  819. wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
  820. "transaction identifier found");
  821. return;
  822. }
  823. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
  824. "from " MACSTR, MAC2STR(sa));
  825. sme_stop_sa_query(wpa_s);
  826. }
  827. #endif /* CONFIG_IEEE80211W */