sme.c 40 KB

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