sme.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. * wpa_supplicant - SME
  3. * Copyright (c) 2009-2012, 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 "common/sae.h"
  16. #include "rsn_supp/wpa.h"
  17. #include "rsn_supp/pmksa_cache.h"
  18. #include "config.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "driver_i.h"
  21. #include "wpas_glue.h"
  22. #include "wps_supplicant.h"
  23. #include "p2p_supplicant.h"
  24. #include "notify.h"
  25. #include "bss.h"
  26. #include "scan.h"
  27. #include "sme.h"
  28. #include "hs20_supplicant.h"
  29. #define SME_AUTH_TIMEOUT 5
  30. #define SME_ASSOC_TIMEOUT 5
  31. static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
  32. static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
  33. static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
  34. #ifdef CONFIG_IEEE80211W
  35. static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
  36. #endif /* CONFIG_IEEE80211W */
  37. #ifdef CONFIG_SAE
  38. static struct wpabuf * sme_auth_build_sae_commit(struct wpa_supplicant *wpa_s,
  39. struct wpa_ssid *ssid,
  40. const u8 *bssid)
  41. {
  42. struct wpabuf *buf;
  43. if (ssid->passphrase == NULL) {
  44. wpa_printf(MSG_DEBUG, "SAE: No password available");
  45. return NULL;
  46. }
  47. if (sae_prepare_commit(wpa_s->own_addr, bssid,
  48. (u8 *) ssid->passphrase,
  49. os_strlen(ssid->passphrase),
  50. &wpa_s->sme.sae) < 0) {
  51. wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
  52. return NULL;
  53. }
  54. buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN);
  55. if (buf == NULL)
  56. return NULL;
  57. wpabuf_put_le16(buf, 1); /* Transaction seq# */
  58. wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
  59. sae_write_commit(&wpa_s->sme.sae, buf);
  60. return buf;
  61. }
  62. static struct wpabuf * sme_auth_build_sae_confirm(struct wpa_supplicant *wpa_s)
  63. {
  64. struct wpabuf *buf;
  65. buf = wpabuf_alloc(4 + SAE_CONFIRM_MAX_LEN);
  66. if (buf == NULL)
  67. return NULL;
  68. wpabuf_put_le16(buf, 2); /* Transaction seq# */
  69. wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
  70. sae_write_confirm(&wpa_s->sme.sae, buf);
  71. return buf;
  72. }
  73. #endif /* CONFIG_SAE */
  74. static void sme_send_authentication(struct wpa_supplicant *wpa_s,
  75. struct wpa_bss *bss, struct wpa_ssid *ssid,
  76. int start)
  77. {
  78. struct wpa_driver_auth_params params;
  79. struct wpa_ssid *old_ssid;
  80. #ifdef CONFIG_IEEE80211R
  81. const u8 *ie;
  82. #endif /* CONFIG_IEEE80211R */
  83. #ifdef CONFIG_IEEE80211R
  84. const u8 *md = NULL;
  85. #endif /* CONFIG_IEEE80211R */
  86. int i, bssid_changed;
  87. struct wpabuf *resp = NULL;
  88. u8 ext_capab[10];
  89. int ext_capab_len;
  90. if (bss == NULL) {
  91. wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
  92. "the network");
  93. return;
  94. }
  95. wpa_s->current_bss = bss;
  96. os_memset(&params, 0, sizeof(params));
  97. wpa_s->reassociate = 0;
  98. params.freq = bss->freq;
  99. params.bssid = bss->bssid;
  100. params.ssid = bss->ssid;
  101. params.ssid_len = bss->ssid_len;
  102. params.p2p = ssid->p2p_group;
  103. if (wpa_s->sme.ssid_len != params.ssid_len ||
  104. os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
  105. wpa_s->sme.prev_bssid_set = 0;
  106. wpa_s->sme.freq = params.freq;
  107. os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
  108. wpa_s->sme.ssid_len = params.ssid_len;
  109. params.auth_alg = WPA_AUTH_ALG_OPEN;
  110. #ifdef IEEE8021X_EAPOL
  111. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  112. if (ssid->leap) {
  113. if (ssid->non_leap == 0)
  114. params.auth_alg = WPA_AUTH_ALG_LEAP;
  115. else
  116. params.auth_alg |= WPA_AUTH_ALG_LEAP;
  117. }
  118. }
  119. #endif /* IEEE8021X_EAPOL */
  120. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
  121. params.auth_alg);
  122. if (ssid->auth_alg) {
  123. params.auth_alg = ssid->auth_alg;
  124. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  125. "0x%x", params.auth_alg);
  126. }
  127. #ifdef CONFIG_SAE
  128. if (wpa_key_mgmt_sae(ssid->key_mgmt)) {
  129. const u8 *rsn;
  130. struct wpa_ie_data ied;
  131. rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  132. if (rsn &&
  133. wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0) {
  134. if (wpa_key_mgmt_sae(ied.key_mgmt)) {
  135. wpa_dbg(wpa_s, MSG_DEBUG, "Using SAE auth_alg");
  136. params.auth_alg = WPA_AUTH_ALG_SAE;
  137. }
  138. }
  139. }
  140. #endif /* CONFIG_SAE */
  141. for (i = 0; i < NUM_WEP_KEYS; i++) {
  142. if (ssid->wep_key_len[i])
  143. params.wep_key[i] = ssid->wep_key[i];
  144. params.wep_key_len[i] = ssid->wep_key_len[i];
  145. }
  146. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  147. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  148. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  149. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  150. if (bssid_changed)
  151. wpas_notify_bssid_changed(wpa_s);
  152. if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  153. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  154. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  155. int try_opportunistic;
  156. try_opportunistic = (ssid->proactive_key_caching < 0 ?
  157. wpa_s->conf->okc :
  158. ssid->proactive_key_caching) &&
  159. (ssid->proto & WPA_PROTO_RSN);
  160. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  161. wpa_s->current_ssid,
  162. try_opportunistic) == 0)
  163. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  164. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  165. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  166. wpa_s->sme.assoc_req_ie,
  167. &wpa_s->sme.assoc_req_ie_len)) {
  168. wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
  169. "key management and encryption suites");
  170. return;
  171. }
  172. } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  173. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
  174. /*
  175. * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
  176. * use non-WPA since the scan results did not indicate that the
  177. * AP is using WPA or WPA2.
  178. */
  179. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  180. wpa_s->sme.assoc_req_ie_len = 0;
  181. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  182. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  183. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  184. wpa_s->sme.assoc_req_ie,
  185. &wpa_s->sme.assoc_req_ie_len)) {
  186. wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
  187. "key management and encryption suites (no "
  188. "scan results)");
  189. return;
  190. }
  191. #ifdef CONFIG_WPS
  192. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  193. struct wpabuf *wps_ie;
  194. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  195. if (wps_ie && wpabuf_len(wps_ie) <=
  196. sizeof(wpa_s->sme.assoc_req_ie)) {
  197. wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
  198. os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
  199. wpa_s->sme.assoc_req_ie_len);
  200. } else
  201. wpa_s->sme.assoc_req_ie_len = 0;
  202. wpabuf_free(wps_ie);
  203. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  204. #endif /* CONFIG_WPS */
  205. } else {
  206. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  207. wpa_s->sme.assoc_req_ie_len = 0;
  208. }
  209. #ifdef CONFIG_IEEE80211R
  210. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  211. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  212. md = ie + 2;
  213. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  214. if (md) {
  215. /* Prepare for the next transition */
  216. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  217. }
  218. if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
  219. if (wpa_s->sme.assoc_req_ie_len + 5 <
  220. sizeof(wpa_s->sme.assoc_req_ie)) {
  221. struct rsn_mdie *mdie;
  222. u8 *pos = wpa_s->sme.assoc_req_ie +
  223. wpa_s->sme.assoc_req_ie_len;
  224. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  225. *pos++ = sizeof(*mdie);
  226. mdie = (struct rsn_mdie *) pos;
  227. os_memcpy(mdie->mobility_domain, md,
  228. MOBILITY_DOMAIN_ID_LEN);
  229. mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
  230. wpa_s->sme.assoc_req_ie_len += 5;
  231. }
  232. if (wpa_s->sme.ft_used &&
  233. os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
  234. wpa_sm_has_ptk(wpa_s->wpa)) {
  235. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
  236. "over-the-air");
  237. params.auth_alg = WPA_AUTH_ALG_FT;
  238. params.ie = wpa_s->sme.ft_ies;
  239. params.ie_len = wpa_s->sme.ft_ies_len;
  240. }
  241. }
  242. #endif /* CONFIG_IEEE80211R */
  243. #ifdef CONFIG_IEEE80211W
  244. wpa_s->sme.mfp = ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
  245. wpa_s->conf->pmf : ssid->ieee80211w;
  246. if (wpa_s->sme.mfp != NO_MGMT_FRAME_PROTECTION) {
  247. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  248. struct wpa_ie_data _ie;
  249. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
  250. _ie.capabilities &
  251. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  252. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
  253. "MFP: require MFP");
  254. wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
  255. }
  256. }
  257. #endif /* CONFIG_IEEE80211W */
  258. #ifdef CONFIG_P2P
  259. if (wpa_s->global->p2p) {
  260. u8 *pos;
  261. size_t len;
  262. int res;
  263. pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
  264. len = sizeof(wpa_s->sme.assoc_req_ie) -
  265. wpa_s->sme.assoc_req_ie_len;
  266. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  267. ssid->p2p_group);
  268. if (res >= 0)
  269. wpa_s->sme.assoc_req_ie_len += res;
  270. }
  271. #endif /* CONFIG_P2P */
  272. #ifdef CONFIG_HS20
  273. if (wpa_s->conf->hs20) {
  274. struct wpabuf *hs20;
  275. hs20 = wpabuf_alloc(20);
  276. if (hs20) {
  277. wpas_hs20_add_indication(hs20);
  278. os_memcpy(wpa_s->sme.assoc_req_ie +
  279. wpa_s->sme.assoc_req_ie_len,
  280. wpabuf_head(hs20), wpabuf_len(hs20));
  281. wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
  282. wpabuf_free(hs20);
  283. }
  284. }
  285. #endif /* CONFIG_HS20 */
  286. ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab);
  287. if (ext_capab_len > 0) {
  288. u8 *pos = wpa_s->sme.assoc_req_ie;
  289. if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  290. pos += 2 + pos[1];
  291. os_memmove(pos + ext_capab_len, pos,
  292. wpa_s->sme.assoc_req_ie_len -
  293. (pos - wpa_s->sme.assoc_req_ie));
  294. wpa_s->sme.assoc_req_ie_len += ext_capab_len;
  295. os_memcpy(pos, ext_capab, ext_capab_len);
  296. }
  297. #ifdef CONFIG_SAE
  298. if (params.auth_alg == WPA_AUTH_ALG_SAE) {
  299. if (start)
  300. resp = sme_auth_build_sae_commit(wpa_s, ssid,
  301. bss->bssid);
  302. else
  303. resp = sme_auth_build_sae_confirm(wpa_s);
  304. if (resp == NULL)
  305. return;
  306. params.sae_data = wpabuf_head(resp);
  307. params.sae_data_len = wpabuf_len(resp);
  308. wpa_s->sme.sae.state = start ? SAE_COMMIT : SAE_CONFIRM;
  309. }
  310. #endif /* CONFIG_SAE */
  311. wpa_supplicant_cancel_sched_scan(wpa_s);
  312. wpa_supplicant_cancel_scan(wpa_s);
  313. wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
  314. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  315. wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
  316. wpa_clear_keys(wpa_s, bss->bssid);
  317. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  318. old_ssid = wpa_s->current_ssid;
  319. wpa_s->current_ssid = ssid;
  320. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  321. wpa_supplicant_initiate_eapol(wpa_s);
  322. if (old_ssid != wpa_s->current_ssid)
  323. wpas_notify_network_changed(wpa_s);
  324. wpa_s->sme.auth_alg = params.auth_alg;
  325. if (wpa_drv_authenticate(wpa_s, &params) < 0) {
  326. wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
  327. "driver failed");
  328. wpas_connection_failed(wpa_s, bss->bssid);
  329. wpa_supplicant_mark_disassoc(wpa_s);
  330. wpabuf_free(resp);
  331. return;
  332. }
  333. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  334. NULL);
  335. /*
  336. * Association will be started based on the authentication event from
  337. * the driver.
  338. */
  339. wpabuf_free(resp);
  340. }
  341. void sme_authenticate(struct wpa_supplicant *wpa_s,
  342. struct wpa_bss *bss, struct wpa_ssid *ssid)
  343. {
  344. #ifdef CONFIG_SAE
  345. wpa_s->sme.sae.state = SAE_INIT;
  346. wpa_s->sme.sae.send_confirm = 0;
  347. #endif /* CONFIG_SAE */
  348. sme_send_authentication(wpa_s, bss, ssid, 1);
  349. }
  350. #ifdef CONFIG_SAE
  351. static int sme_sae_auth(struct wpa_supplicant *wpa_s, u16 auth_transaction,
  352. u16 status_code, const u8 *data, size_t len)
  353. {
  354. wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE authentication transaction %u "
  355. "status code %u", auth_transaction, status_code);
  356. wpa_hexdump(MSG_DEBUG, "SME: SAE fields", data, len);
  357. if (status_code != WLAN_STATUS_SUCCESS)
  358. return -1;
  359. if (auth_transaction == 1) {
  360. wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE commit");
  361. if (wpa_s->current_bss == NULL ||
  362. wpa_s->current_ssid == NULL)
  363. return -1;
  364. if (wpa_s->sme.sae.state != SAE_COMMIT)
  365. return -1;
  366. if (sae_parse_commit(&wpa_s->sme.sae, data, len) !=
  367. WLAN_STATUS_SUCCESS)
  368. return -1;
  369. if (sae_process_commit(&wpa_s->sme.sae) < 0) {
  370. wpa_printf(MSG_DEBUG, "SAE: Failed to process peer "
  371. "commit");
  372. return -1;
  373. }
  374. sme_send_authentication(wpa_s, wpa_s->current_bss,
  375. wpa_s->current_ssid, 0);
  376. return 0;
  377. } else if (auth_transaction == 2) {
  378. wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE confirm");
  379. if (wpa_s->sme.sae.state != SAE_CONFIRM)
  380. return -1;
  381. if (sae_check_confirm(&wpa_s->sme.sae, data, len) < 0)
  382. return -1;
  383. return 1;
  384. }
  385. return -1;
  386. }
  387. #endif /* CONFIG_SAE */
  388. void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
  389. {
  390. struct wpa_ssid *ssid = wpa_s->current_ssid;
  391. if (ssid == NULL) {
  392. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
  393. "when network is not selected");
  394. return;
  395. }
  396. if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
  397. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
  398. "when not in authenticating state");
  399. return;
  400. }
  401. if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
  402. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
  403. "unexpected peer " MACSTR,
  404. MAC2STR(data->auth.peer));
  405. return;
  406. }
  407. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
  408. " auth_type=%d auth_transaction=%d status_code=%d",
  409. MAC2STR(data->auth.peer), data->auth.auth_type,
  410. data->auth.auth_transaction, data->auth.status_code);
  411. wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
  412. data->auth.ies, data->auth.ies_len);
  413. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  414. #ifdef CONFIG_SAE
  415. if (data->auth.auth_type == WLAN_AUTH_SAE) {
  416. int res;
  417. res = sme_sae_auth(wpa_s, data->auth.auth_transaction,
  418. data->auth.status_code, data->auth.ies,
  419. data->auth.ies_len);
  420. if (res < 0) {
  421. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  422. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  423. }
  424. if (res != 1)
  425. return;
  426. }
  427. #endif /* CONFIG_SAE */
  428. if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
  429. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
  430. "code %d)", data->auth.status_code);
  431. if (data->auth.status_code !=
  432. WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
  433. wpa_s->sme.auth_alg == data->auth.auth_type ||
  434. wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
  435. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  436. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  437. return;
  438. }
  439. switch (data->auth.auth_type) {
  440. case WLAN_AUTH_OPEN:
  441. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  442. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
  443. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  444. wpa_s->current_ssid);
  445. return;
  446. case WLAN_AUTH_SHARED_KEY:
  447. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
  448. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
  449. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  450. wpa_s->current_ssid);
  451. return;
  452. default:
  453. return;
  454. }
  455. }
  456. #ifdef CONFIG_IEEE80211R
  457. if (data->auth.auth_type == WLAN_AUTH_FT) {
  458. union wpa_event_data edata;
  459. os_memset(&edata, 0, sizeof(edata));
  460. edata.ft_ies.ies = data->auth.ies;
  461. edata.ft_ies.ies_len = data->auth.ies_len;
  462. os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
  463. wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
  464. }
  465. #endif /* CONFIG_IEEE80211R */
  466. sme_associate(wpa_s, ssid->mode, data->auth.peer,
  467. data->auth.auth_type);
  468. }
  469. void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
  470. const u8 *bssid, u16 auth_type)
  471. {
  472. struct wpa_driver_associate_params params;
  473. struct ieee802_11_elems elems;
  474. #ifdef CONFIG_HT_OVERRIDES
  475. struct ieee80211_ht_capabilities htcaps;
  476. struct ieee80211_ht_capabilities htcaps_mask;
  477. #endif /* CONFIG_HT_OVERRIDES */
  478. os_memset(&params, 0, sizeof(params));
  479. params.bssid = bssid;
  480. params.ssid = wpa_s->sme.ssid;
  481. params.ssid_len = wpa_s->sme.ssid_len;
  482. params.freq = wpa_s->sme.freq;
  483. params.bg_scan_period = wpa_s->current_ssid ?
  484. wpa_s->current_ssid->bg_scan_period : -1;
  485. params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
  486. wpa_s->sme.assoc_req_ie : NULL;
  487. params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
  488. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  489. params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
  490. #ifdef CONFIG_HT_OVERRIDES
  491. os_memset(&htcaps, 0, sizeof(htcaps));
  492. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  493. params.htcaps = (u8 *) &htcaps;
  494. params.htcaps_mask = (u8 *) &htcaps_mask;
  495. wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
  496. #endif /* CONFIG_HT_OVERRIDES */
  497. #ifdef CONFIG_IEEE80211R
  498. if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
  499. params.wpa_ie = wpa_s->sme.ft_ies;
  500. params.wpa_ie_len = wpa_s->sme.ft_ies_len;
  501. }
  502. #endif /* CONFIG_IEEE80211R */
  503. params.mode = mode;
  504. params.mgmt_frame_protection = wpa_s->sme.mfp;
  505. if (wpa_s->sme.prev_bssid_set)
  506. params.prev_bssid = wpa_s->sme.prev_bssid;
  507. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  508. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  509. params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
  510. params.freq);
  511. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  512. if (params.wpa_ie == NULL ||
  513. ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
  514. < 0) {
  515. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
  516. os_memset(&elems, 0, sizeof(elems));
  517. }
  518. if (elems.rsn_ie) {
  519. params.wpa_proto = WPA_PROTO_RSN;
  520. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
  521. elems.rsn_ie_len + 2);
  522. } else if (elems.wpa_ie) {
  523. params.wpa_proto = WPA_PROTO_WPA;
  524. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
  525. elems.wpa_ie_len + 2);
  526. } else
  527. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  528. if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
  529. params.p2p = 1;
  530. if (wpa_s->parent->set_sta_uapsd)
  531. params.uapsd = wpa_s->parent->sta_uapsd;
  532. else
  533. params.uapsd = -1;
  534. if (wpa_drv_associate(wpa_s, &params) < 0) {
  535. wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
  536. "driver failed");
  537. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  538. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  539. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  540. return;
  541. }
  542. eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
  543. NULL);
  544. }
  545. int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  546. const u8 *ies, size_t ies_len)
  547. {
  548. if (md == NULL || ies == NULL) {
  549. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
  550. os_free(wpa_s->sme.ft_ies);
  551. wpa_s->sme.ft_ies = NULL;
  552. wpa_s->sme.ft_ies_len = 0;
  553. wpa_s->sme.ft_used = 0;
  554. return 0;
  555. }
  556. os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  557. wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
  558. os_free(wpa_s->sme.ft_ies);
  559. wpa_s->sme.ft_ies = os_malloc(ies_len);
  560. if (wpa_s->sme.ft_ies == NULL)
  561. return -1;
  562. os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
  563. wpa_s->sme.ft_ies_len = ies_len;
  564. return 0;
  565. }
  566. static void sme_deauth(struct wpa_supplicant *wpa_s)
  567. {
  568. int bssid_changed;
  569. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  570. if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
  571. WLAN_REASON_DEAUTH_LEAVING) < 0) {
  572. wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
  573. "failed");
  574. }
  575. wpa_s->sme.prev_bssid_set = 0;
  576. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  577. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  578. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  579. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  580. if (bssid_changed)
  581. wpas_notify_bssid_changed(wpa_s);
  582. }
  583. void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
  584. union wpa_event_data *data)
  585. {
  586. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
  587. "status code %d", MAC2STR(wpa_s->pending_bssid),
  588. data->assoc_reject.status_code);
  589. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  590. /*
  591. * For now, unconditionally terminate the previous authentication. In
  592. * theory, this should not be needed, but mac80211 gets quite confused
  593. * if the authentication is left pending.. Some roaming cases might
  594. * benefit from using the previous authentication, so this could be
  595. * optimized in the future.
  596. */
  597. sme_deauth(wpa_s);
  598. }
  599. void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
  600. union wpa_event_data *data)
  601. {
  602. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
  603. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  604. wpa_supplicant_mark_disassoc(wpa_s);
  605. }
  606. void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
  607. union wpa_event_data *data)
  608. {
  609. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
  610. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  611. wpa_supplicant_mark_disassoc(wpa_s);
  612. }
  613. void sme_event_disassoc(struct wpa_supplicant *wpa_s,
  614. union wpa_event_data *data)
  615. {
  616. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
  617. if (wpa_s->sme.prev_bssid_set) {
  618. /*
  619. * cfg80211/mac80211 can get into somewhat confused state if
  620. * the AP only disassociates us and leaves us in authenticated
  621. * state. For now, force the state to be cleared to avoid
  622. * confusing errors if we try to associate with the AP again.
  623. */
  624. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
  625. "driver state");
  626. wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
  627. WLAN_REASON_DEAUTH_LEAVING);
  628. }
  629. }
  630. static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
  631. {
  632. struct wpa_supplicant *wpa_s = eloop_ctx;
  633. if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
  634. wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
  635. sme_deauth(wpa_s);
  636. }
  637. }
  638. static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
  639. {
  640. struct wpa_supplicant *wpa_s = eloop_ctx;
  641. if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  642. wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
  643. sme_deauth(wpa_s);
  644. }
  645. }
  646. void sme_state_changed(struct wpa_supplicant *wpa_s)
  647. {
  648. /* Make sure timers are cleaned up appropriately. */
  649. if (wpa_s->wpa_state != WPA_ASSOCIATING)
  650. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  651. if (wpa_s->wpa_state != WPA_AUTHENTICATING)
  652. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  653. }
  654. void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
  655. const u8 *prev_pending_bssid)
  656. {
  657. /*
  658. * mac80211-workaround to force deauth on failed auth cmd,
  659. * requires us to remain in authenticating state to allow the
  660. * second authentication attempt to be continued properly.
  661. */
  662. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
  663. "to proceed after disconnection event");
  664. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  665. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  666. /*
  667. * Re-arm authentication timer in case auth fails for whatever reason.
  668. */
  669. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  670. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  671. NULL);
  672. }
  673. void sme_deinit(struct wpa_supplicant *wpa_s)
  674. {
  675. os_free(wpa_s->sme.ft_ies);
  676. wpa_s->sme.ft_ies = NULL;
  677. wpa_s->sme.ft_ies_len = 0;
  678. #ifdef CONFIG_IEEE80211W
  679. sme_stop_sa_query(wpa_s);
  680. #endif /* CONFIG_IEEE80211W */
  681. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  682. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  683. eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
  684. }
  685. static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
  686. const u8 *chan_list, u8 num_channels,
  687. u8 num_intol)
  688. {
  689. struct ieee80211_2040_bss_coex_ie *bc_ie;
  690. struct ieee80211_2040_intol_chan_report *ic_report;
  691. struct wpabuf *buf;
  692. wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR,
  693. MAC2STR(wpa_s->bssid));
  694. buf = wpabuf_alloc(2 + /* action.category + action_code */
  695. sizeof(struct ieee80211_2040_bss_coex_ie) +
  696. sizeof(struct ieee80211_2040_intol_chan_report) +
  697. num_channels);
  698. if (buf == NULL)
  699. return;
  700. wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
  701. wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
  702. bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
  703. bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
  704. bc_ie->length = 1;
  705. if (num_intol)
  706. bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
  707. if (num_channels > 0) {
  708. ic_report = wpabuf_put(buf, sizeof(*ic_report));
  709. ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
  710. ic_report->length = num_channels + 1;
  711. ic_report->op_class = 0;
  712. os_memcpy(wpabuf_put(buf, num_channels), chan_list,
  713. num_channels);
  714. }
  715. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  716. wpa_s->own_addr, wpa_s->bssid,
  717. wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
  718. wpa_msg(wpa_s, MSG_INFO,
  719. "SME: Failed to send 20/40 BSS Coexistence frame");
  720. }
  721. wpabuf_free(buf);
  722. }
  723. /**
  724. * enum wpas_band - Frequency band
  725. * @WPAS_BAND_2GHZ: 2.4 GHz ISM band
  726. * @WPAS_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
  727. */
  728. enum wpas_band {
  729. WPAS_BAND_2GHZ,
  730. WPAS_BAND_5GHZ,
  731. WPAS_BAND_INVALID
  732. };
  733. /**
  734. * freq_to_channel - Convert frequency into channel info
  735. * @channel: Buffer for returning channel number
  736. * Returns: Band (2 or 5 GHz)
  737. */
  738. static enum wpas_band freq_to_channel(int freq, u8 *channel)
  739. {
  740. enum wpas_band band = (freq <= 2484) ? WPAS_BAND_2GHZ : WPAS_BAND_5GHZ;
  741. u8 chan = 0;
  742. if (freq >= 2412 && freq <= 2472)
  743. chan = (freq - 2407) / 5;
  744. else if (freq == 2484)
  745. chan = 14;
  746. else if (freq >= 5180 && freq <= 5805)
  747. chan = (freq - 5000) / 5;
  748. *channel = chan;
  749. return band;
  750. }
  751. int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
  752. {
  753. struct wpa_bss *bss;
  754. const u8 *ie;
  755. u16 ht_cap;
  756. u8 chan_list[P2P_MAX_CHANNELS], channel;
  757. u8 num_channels = 0, num_intol = 0, i;
  758. if (!wpa_s->sme.sched_obss_scan)
  759. return 0;
  760. wpa_s->sme.sched_obss_scan = 0;
  761. if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
  762. return 1;
  763. /*
  764. * Check whether AP uses regulatory triplet or channel triplet in
  765. * country info. Right now the operating class of the BSS channel
  766. * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
  767. * based on the assumption that operating class triplet is not used in
  768. * beacon frame. If the First Channel Number/Operating Extension
  769. * Identifier octet has a positive integer value of 201 or greater,
  770. * then its operating class triplet.
  771. *
  772. * TODO: If Supported Operating Classes element is present in beacon
  773. * frame, have to lookup operating class in Annex E and fill them in
  774. * 2040 coex frame.
  775. */
  776. ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
  777. if (ie && (ie[1] >= 6) && (ie[5] >= 201))
  778. return 1;
  779. os_memset(chan_list, 0, sizeof(chan_list));
  780. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  781. /* Skip other band bss */
  782. if (freq_to_channel(bss->freq, &channel) != WPAS_BAND_2GHZ)
  783. continue;
  784. ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
  785. ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
  786. if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
  787. /* Check whether the channel is already considered */
  788. for (i = 0; i < num_channels; i++) {
  789. if (channel == chan_list[i])
  790. break;
  791. }
  792. if (i != num_channels)
  793. continue;
  794. if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
  795. num_intol++;
  796. chan_list[num_channels++] = channel;
  797. }
  798. }
  799. sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
  800. return 1;
  801. }
  802. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  803. u16 num_modes,
  804. enum hostapd_hw_mode mode)
  805. {
  806. u16 i;
  807. for (i = 0; i < num_modes; i++) {
  808. if (modes[i].mode == mode)
  809. return &modes[i];
  810. }
  811. return NULL;
  812. }
  813. static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
  814. enum hostapd_hw_mode band,
  815. struct wpa_driver_scan_params *params)
  816. {
  817. /* Include only supported channels for the specified band */
  818. struct hostapd_hw_modes *mode;
  819. int count, i;
  820. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
  821. if (mode == NULL) {
  822. /* No channels supported in this band - use empty list */
  823. params->freqs = os_zalloc(sizeof(int));
  824. return;
  825. }
  826. params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
  827. if (params->freqs == NULL)
  828. return;
  829. for (count = 0, i = 0; i < mode->num_channels; i++) {
  830. if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
  831. continue;
  832. params->freqs[count++] = mode->channels[i].freq;
  833. }
  834. }
  835. static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  836. {
  837. struct wpa_supplicant *wpa_s = eloop_ctx;
  838. struct wpa_driver_scan_params params;
  839. if (!wpa_s->current_bss) {
  840. wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
  841. return;
  842. }
  843. os_memset(&params, 0, sizeof(params));
  844. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params);
  845. wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
  846. if (wpa_supplicant_trigger_scan(wpa_s, &params))
  847. wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
  848. else
  849. wpa_s->sme.sched_obss_scan = 1;
  850. os_free(params.freqs);
  851. eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
  852. sme_obss_scan_timeout, wpa_s, NULL);
  853. }
  854. void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
  855. {
  856. const u8 *ie;
  857. struct wpa_bss *bss = wpa_s->current_bss;
  858. struct wpa_ssid *ssid = wpa_s->current_ssid;
  859. struct hostapd_hw_modes *hw_mode = NULL;
  860. int i;
  861. eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
  862. wpa_s->sme.sched_obss_scan = 0;
  863. if (!enable)
  864. return;
  865. /*
  866. * Schedule OBSS scan if driver is using station SME in wpa_supplicant
  867. * or it expects OBSS scan to be performed by wpa_supplicant.
  868. */
  869. if (!((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
  870. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OBSS_SCAN)) ||
  871. ssid == NULL || ssid->mode != IEEE80211_MODE_INFRA)
  872. return;
  873. if (!wpa_s->hw.modes)
  874. return;
  875. /* only HT caps in 11g mode are relevant */
  876. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  877. hw_mode = &wpa_s->hw.modes[i];
  878. if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
  879. break;
  880. }
  881. /* Driver does not support HT40 for 11g or doesn't have 11g. */
  882. if (i == wpa_s->hw.num_modes || !hw_mode ||
  883. !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  884. return;
  885. if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
  886. return; /* Not associated on 2.4 GHz band */
  887. /* Check whether AP supports HT40 */
  888. ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
  889. if (!ie || ie[1] < 2 ||
  890. !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  891. return; /* AP does not support HT40 */
  892. ie = wpa_bss_get_ie(wpa_s->current_bss,
  893. WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
  894. if (!ie || ie[1] < 14)
  895. return; /* AP does not request OBSS scans */
  896. wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
  897. if (wpa_s->sme.obss_scan_int < 10) {
  898. wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
  899. "replaced with the minimum 10 sec",
  900. wpa_s->sme.obss_scan_int);
  901. wpa_s->sme.obss_scan_int = 10;
  902. }
  903. wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
  904. wpa_s->sme.obss_scan_int);
  905. eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
  906. sme_obss_scan_timeout, wpa_s, NULL);
  907. }
  908. #ifdef CONFIG_IEEE80211W
  909. static const unsigned int sa_query_max_timeout = 1000;
  910. static const unsigned int sa_query_retry_timeout = 201;
  911. static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
  912. {
  913. u32 tu;
  914. struct os_time now, passed;
  915. os_get_time(&now);
  916. os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
  917. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  918. if (sa_query_max_timeout < tu) {
  919. wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
  920. sme_stop_sa_query(wpa_s);
  921. wpa_supplicant_deauthenticate(
  922. wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
  923. return 1;
  924. }
  925. return 0;
  926. }
  927. static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
  928. const u8 *trans_id)
  929. {
  930. u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
  931. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
  932. MACSTR, MAC2STR(wpa_s->bssid));
  933. wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
  934. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  935. req[0] = WLAN_ACTION_SA_QUERY;
  936. req[1] = WLAN_SA_QUERY_REQUEST;
  937. os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  938. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  939. wpa_s->own_addr, wpa_s->bssid,
  940. req, sizeof(req), 0) < 0)
  941. wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
  942. "Request");
  943. }
  944. static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  945. {
  946. struct wpa_supplicant *wpa_s = eloop_ctx;
  947. unsigned int timeout, sec, usec;
  948. u8 *trans_id, *nbuf;
  949. if (wpa_s->sme.sa_query_count > 0 &&
  950. sme_check_sa_query_timeout(wpa_s))
  951. return;
  952. nbuf = os_realloc_array(wpa_s->sme.sa_query_trans_id,
  953. wpa_s->sme.sa_query_count + 1,
  954. WLAN_SA_QUERY_TR_ID_LEN);
  955. if (nbuf == NULL)
  956. return;
  957. if (wpa_s->sme.sa_query_count == 0) {
  958. /* Starting a new SA Query procedure */
  959. os_get_time(&wpa_s->sme.sa_query_start);
  960. }
  961. trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  962. wpa_s->sme.sa_query_trans_id = nbuf;
  963. wpa_s->sme.sa_query_count++;
  964. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  965. timeout = sa_query_retry_timeout;
  966. sec = ((timeout / 1000) * 1024) / 1000;
  967. usec = (timeout % 1000) * 1024;
  968. eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
  969. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
  970. wpa_s->sme.sa_query_count);
  971. sme_send_sa_query_req(wpa_s, trans_id);
  972. }
  973. static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
  974. {
  975. sme_sa_query_timer(wpa_s, NULL);
  976. }
  977. static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
  978. {
  979. eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
  980. os_free(wpa_s->sme.sa_query_trans_id);
  981. wpa_s->sme.sa_query_trans_id = NULL;
  982. wpa_s->sme.sa_query_count = 0;
  983. }
  984. void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
  985. const u8 *da, u16 reason_code)
  986. {
  987. struct wpa_ssid *ssid;
  988. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  989. return;
  990. if (wpa_s->wpa_state != WPA_COMPLETED)
  991. return;
  992. ssid = wpa_s->current_ssid;
  993. if (ssid == NULL ||
  994. (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
  995. wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION)
  996. return;
  997. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  998. return;
  999. if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
  1000. reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
  1001. return;
  1002. if (wpa_s->sme.sa_query_count > 0)
  1003. return;
  1004. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
  1005. "possible AP/STA state mismatch - trigger SA Query");
  1006. sme_start_sa_query(wpa_s);
  1007. }
  1008. void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
  1009. const u8 *data, size_t len)
  1010. {
  1011. int i;
  1012. if (wpa_s->sme.sa_query_trans_id == NULL ||
  1013. len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
  1014. data[0] != WLAN_SA_QUERY_RESPONSE)
  1015. return;
  1016. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
  1017. MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
  1018. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  1019. return;
  1020. for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
  1021. if (os_memcmp(wpa_s->sme.sa_query_trans_id +
  1022. i * WLAN_SA_QUERY_TR_ID_LEN,
  1023. data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
  1024. break;
  1025. }
  1026. if (i >= wpa_s->sme.sa_query_count) {
  1027. wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
  1028. "transaction identifier found");
  1029. return;
  1030. }
  1031. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
  1032. "from " MACSTR, MAC2STR(sa));
  1033. sme_stop_sa_query(wpa_s);
  1034. }
  1035. #endif /* CONFIG_IEEE80211W */