sme.c 35 KB

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