sme.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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. const char *password;
  76. #ifdef CONFIG_TESTING_OPTIONS
  77. if (wpa_s->sae_commit_override) {
  78. wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
  79. buf = wpabuf_alloc(4 + wpabuf_len(wpa_s->sae_commit_override));
  80. if (!buf)
  81. return NULL;
  82. wpabuf_put_le16(buf, 1); /* Transaction seq# */
  83. wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
  84. wpabuf_put_buf(buf, wpa_s->sae_commit_override);
  85. return buf;
  86. }
  87. #endif /* CONFIG_TESTING_OPTIONS */
  88. password = ssid->sae_password;
  89. if (!password)
  90. password = ssid->passphrase;
  91. if (!password) {
  92. wpa_printf(MSG_DEBUG, "SAE: No password available");
  93. return NULL;
  94. }
  95. if (sme_set_sae_group(wpa_s) < 0) {
  96. wpa_printf(MSG_DEBUG, "SAE: Failed to select group");
  97. return NULL;
  98. }
  99. if (sae_prepare_commit(wpa_s->own_addr, bssid,
  100. (u8 *) password, os_strlen(password),
  101. &wpa_s->sme.sae) < 0) {
  102. wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
  103. return NULL;
  104. }
  105. len = wpa_s->sme.sae_token ? wpabuf_len(wpa_s->sme.sae_token) : 0;
  106. buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + len);
  107. if (buf == NULL)
  108. return NULL;
  109. wpabuf_put_le16(buf, 1); /* Transaction seq# */
  110. wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
  111. sae_write_commit(&wpa_s->sme.sae, buf, wpa_s->sme.sae_token);
  112. return buf;
  113. }
  114. static struct wpabuf * sme_auth_build_sae_confirm(struct wpa_supplicant *wpa_s)
  115. {
  116. struct wpabuf *buf;
  117. buf = wpabuf_alloc(4 + SAE_CONFIRM_MAX_LEN);
  118. if (buf == NULL)
  119. return NULL;
  120. wpabuf_put_le16(buf, 2); /* Transaction seq# */
  121. wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
  122. sae_write_confirm(&wpa_s->sme.sae, buf);
  123. return buf;
  124. }
  125. #endif /* CONFIG_SAE */
  126. /**
  127. * sme_auth_handle_rrm - Handle RRM aspects of current authentication attempt
  128. * @wpa_s: Pointer to wpa_supplicant data
  129. * @bss: Pointer to the bss which is the target of authentication attempt
  130. */
  131. static void sme_auth_handle_rrm(struct wpa_supplicant *wpa_s,
  132. struct wpa_bss *bss)
  133. {
  134. const u8 rrm_ie_len = 5;
  135. u8 *pos;
  136. const u8 *rrm_ie;
  137. wpa_s->rrm.rrm_used = 0;
  138. wpa_printf(MSG_DEBUG,
  139. "RRM: Determining whether RRM can be used - device support: 0x%x",
  140. wpa_s->drv_rrm_flags);
  141. rrm_ie = wpa_bss_get_ie(bss, WLAN_EID_RRM_ENABLED_CAPABILITIES);
  142. if (!rrm_ie || !(bss->caps & IEEE80211_CAP_RRM)) {
  143. wpa_printf(MSG_DEBUG, "RRM: No RRM in network");
  144. return;
  145. }
  146. if (!((wpa_s->drv_rrm_flags &
  147. WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
  148. (wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
  149. !(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) {
  150. wpa_printf(MSG_DEBUG,
  151. "RRM: Insufficient RRM support in driver - do not use RRM");
  152. return;
  153. }
  154. if (sizeof(wpa_s->sme.assoc_req_ie) <
  155. wpa_s->sme.assoc_req_ie_len + rrm_ie_len + 2) {
  156. wpa_printf(MSG_INFO,
  157. "RRM: Unable to use RRM, no room for RRM IE");
  158. return;
  159. }
  160. wpa_printf(MSG_DEBUG, "RRM: Adding RRM IE to Association Request");
  161. pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
  162. os_memset(pos, 0, 2 + rrm_ie_len);
  163. *pos++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
  164. *pos++ = rrm_ie_len;
  165. /* Set supported capabilites flags */
  166. if (wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)
  167. *pos |= WLAN_RRM_CAPS_LINK_MEASUREMENT;
  168. *pos |= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
  169. WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
  170. WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
  171. if (wpa_s->lci)
  172. pos[1] |= WLAN_RRM_CAPS_LCI_MEASUREMENT;
  173. wpa_s->sme.assoc_req_ie_len += rrm_ie_len + 2;
  174. wpa_s->rrm.rrm_used = 1;
  175. }
  176. static void sme_send_authentication(struct wpa_supplicant *wpa_s,
  177. struct wpa_bss *bss, struct wpa_ssid *ssid,
  178. int start)
  179. {
  180. struct wpa_driver_auth_params params;
  181. struct wpa_ssid *old_ssid;
  182. #ifdef CONFIG_IEEE80211R
  183. const u8 *ie;
  184. #endif /* CONFIG_IEEE80211R */
  185. #if defined(CONFIG_IEEE80211R) || defined(CONFIG_FILS)
  186. const u8 *md = NULL;
  187. #endif /* CONFIG_IEEE80211R || CONFIG_FILS */
  188. int i, bssid_changed;
  189. struct wpabuf *resp = NULL;
  190. u8 ext_capab[18];
  191. int ext_capab_len;
  192. int skip_auth;
  193. if (bss == NULL) {
  194. wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
  195. "the network");
  196. wpas_connect_work_done(wpa_s);
  197. return;
  198. }
  199. skip_auth = wpa_s->conf->reassoc_same_bss_optim &&
  200. wpa_s->reassoc_same_bss;
  201. wpa_s->current_bss = bss;
  202. os_memset(&params, 0, sizeof(params));
  203. wpa_s->reassociate = 0;
  204. params.freq = bss->freq;
  205. params.bssid = bss->bssid;
  206. params.ssid = bss->ssid;
  207. params.ssid_len = bss->ssid_len;
  208. params.p2p = ssid->p2p_group;
  209. if (wpa_s->sme.ssid_len != params.ssid_len ||
  210. os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
  211. wpa_s->sme.prev_bssid_set = 0;
  212. wpa_s->sme.freq = params.freq;
  213. os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
  214. wpa_s->sme.ssid_len = params.ssid_len;
  215. params.auth_alg = WPA_AUTH_ALG_OPEN;
  216. #ifdef IEEE8021X_EAPOL
  217. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  218. if (ssid->leap) {
  219. if (ssid->non_leap == 0)
  220. params.auth_alg = WPA_AUTH_ALG_LEAP;
  221. else
  222. params.auth_alg |= WPA_AUTH_ALG_LEAP;
  223. }
  224. }
  225. #endif /* IEEE8021X_EAPOL */
  226. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
  227. params.auth_alg);
  228. if (ssid->auth_alg) {
  229. params.auth_alg = ssid->auth_alg;
  230. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  231. "0x%x", params.auth_alg);
  232. }
  233. #ifdef CONFIG_SAE
  234. wpa_s->sme.sae_pmksa_caching = 0;
  235. if (wpa_key_mgmt_sae(ssid->key_mgmt)) {
  236. const u8 *rsn;
  237. struct wpa_ie_data ied;
  238. rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  239. if (!rsn) {
  240. wpa_dbg(wpa_s, MSG_DEBUG,
  241. "SAE enabled, but target BSS does not advertise RSN");
  242. } else if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
  243. wpa_key_mgmt_sae(ied.key_mgmt)) {
  244. wpa_dbg(wpa_s, MSG_DEBUG, "Using SAE auth_alg");
  245. params.auth_alg = WPA_AUTH_ALG_SAE;
  246. } else {
  247. wpa_dbg(wpa_s, MSG_DEBUG,
  248. "SAE enabled, but target BSS does not advertise SAE AKM for RSN");
  249. }
  250. }
  251. #endif /* CONFIG_SAE */
  252. for (i = 0; i < NUM_WEP_KEYS; i++) {
  253. if (ssid->wep_key_len[i])
  254. params.wep_key[i] = ssid->wep_key[i];
  255. params.wep_key_len[i] = ssid->wep_key_len[i];
  256. }
  257. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  258. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  259. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  260. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  261. if (bssid_changed)
  262. wpas_notify_bssid_changed(wpa_s);
  263. if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  264. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  265. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  266. int try_opportunistic;
  267. const u8 *cache_id = NULL;
  268. try_opportunistic = (ssid->proactive_key_caching < 0 ?
  269. wpa_s->conf->okc :
  270. ssid->proactive_key_caching) &&
  271. (ssid->proto & WPA_PROTO_RSN);
  272. #ifdef CONFIG_FILS
  273. if (wpa_key_mgmt_fils(ssid->key_mgmt))
  274. cache_id = wpa_bss_get_fils_cache_id(bss);
  275. #endif /* CONFIG_FILS */
  276. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  277. wpa_s->current_ssid,
  278. try_opportunistic, cache_id) == 0)
  279. eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
  280. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  281. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  282. wpa_s->sme.assoc_req_ie,
  283. &wpa_s->sme.assoc_req_ie_len)) {
  284. wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
  285. "key management and encryption suites");
  286. wpas_connect_work_done(wpa_s);
  287. return;
  288. }
  289. } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  290. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
  291. /*
  292. * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
  293. * use non-WPA since the scan results did not indicate that the
  294. * AP is using WPA or WPA2.
  295. */
  296. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  297. wpa_s->sme.assoc_req_ie_len = 0;
  298. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  299. wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
  300. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  301. wpa_s->sme.assoc_req_ie,
  302. &wpa_s->sme.assoc_req_ie_len)) {
  303. wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
  304. "key management and encryption suites (no "
  305. "scan results)");
  306. wpas_connect_work_done(wpa_s);
  307. return;
  308. }
  309. #ifdef CONFIG_WPS
  310. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  311. struct wpabuf *wps_ie;
  312. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  313. if (wps_ie && wpabuf_len(wps_ie) <=
  314. sizeof(wpa_s->sme.assoc_req_ie)) {
  315. wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
  316. os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
  317. wpa_s->sme.assoc_req_ie_len);
  318. } else
  319. wpa_s->sme.assoc_req_ie_len = 0;
  320. wpabuf_free(wps_ie);
  321. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  322. #endif /* CONFIG_WPS */
  323. } else {
  324. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  325. wpa_s->sme.assoc_req_ie_len = 0;
  326. }
  327. #ifdef CONFIG_IEEE80211R
  328. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  329. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  330. md = ie + 2;
  331. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  332. if (md) {
  333. /* Prepare for the next transition */
  334. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  335. }
  336. if (md && !wpa_key_mgmt_ft(ssid->key_mgmt))
  337. md = NULL;
  338. if (md) {
  339. wpa_dbg(wpa_s, MSG_DEBUG, "SME: FT mobility domain %02x%02x",
  340. md[0], md[1]);
  341. if (wpa_s->sme.assoc_req_ie_len + 5 <
  342. sizeof(wpa_s->sme.assoc_req_ie)) {
  343. struct rsn_mdie *mdie;
  344. u8 *pos = wpa_s->sme.assoc_req_ie +
  345. wpa_s->sme.assoc_req_ie_len;
  346. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  347. *pos++ = sizeof(*mdie);
  348. mdie = (struct rsn_mdie *) pos;
  349. os_memcpy(mdie->mobility_domain, md,
  350. MOBILITY_DOMAIN_ID_LEN);
  351. mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
  352. wpa_s->sme.assoc_req_ie_len += 5;
  353. }
  354. if (wpa_s->sme.ft_used &&
  355. os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
  356. wpa_sm_has_ptk(wpa_s->wpa)) {
  357. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
  358. "over-the-air");
  359. params.auth_alg = WPA_AUTH_ALG_FT;
  360. params.ie = wpa_s->sme.ft_ies;
  361. params.ie_len = wpa_s->sme.ft_ies_len;
  362. }
  363. }
  364. #endif /* CONFIG_IEEE80211R */
  365. #ifdef CONFIG_IEEE80211W
  366. wpa_s->sme.mfp = wpas_get_ssid_pmf(wpa_s, ssid);
  367. if (wpa_s->sme.mfp != NO_MGMT_FRAME_PROTECTION) {
  368. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  369. struct wpa_ie_data _ie;
  370. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
  371. _ie.capabilities &
  372. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  373. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
  374. "MFP: require MFP");
  375. wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
  376. }
  377. }
  378. #endif /* CONFIG_IEEE80211W */
  379. #ifdef CONFIG_P2P
  380. if (wpa_s->global->p2p) {
  381. u8 *pos;
  382. size_t len;
  383. int res;
  384. pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
  385. len = sizeof(wpa_s->sme.assoc_req_ie) -
  386. wpa_s->sme.assoc_req_ie_len;
  387. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  388. ssid->p2p_group);
  389. if (res >= 0)
  390. wpa_s->sme.assoc_req_ie_len += res;
  391. }
  392. #endif /* CONFIG_P2P */
  393. #ifdef CONFIG_FST
  394. if (wpa_s->fst_ies) {
  395. int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
  396. if (wpa_s->sme.assoc_req_ie_len + fst_ies_len <=
  397. sizeof(wpa_s->sme.assoc_req_ie)) {
  398. os_memcpy(wpa_s->sme.assoc_req_ie +
  399. wpa_s->sme.assoc_req_ie_len,
  400. wpabuf_head(wpa_s->fst_ies),
  401. fst_ies_len);
  402. wpa_s->sme.assoc_req_ie_len += fst_ies_len;
  403. }
  404. }
  405. #endif /* CONFIG_FST */
  406. sme_auth_handle_rrm(wpa_s, bss);
  407. wpa_s->sme.assoc_req_ie_len += wpas_supp_op_class_ie(
  408. wpa_s, bss->freq,
  409. wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
  410. sizeof(wpa_s->sme.assoc_req_ie) - wpa_s->sme.assoc_req_ie_len);
  411. if (params.p2p)
  412. wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
  413. else
  414. wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
  415. ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
  416. sizeof(ext_capab));
  417. if (ext_capab_len > 0) {
  418. u8 *pos = wpa_s->sme.assoc_req_ie;
  419. if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  420. pos += 2 + pos[1];
  421. os_memmove(pos + ext_capab_len, pos,
  422. wpa_s->sme.assoc_req_ie_len -
  423. (pos - wpa_s->sme.assoc_req_ie));
  424. wpa_s->sme.assoc_req_ie_len += ext_capab_len;
  425. os_memcpy(pos, ext_capab, ext_capab_len);
  426. }
  427. #ifdef CONFIG_HS20
  428. if (is_hs20_network(wpa_s, ssid, bss)) {
  429. struct wpabuf *hs20;
  430. hs20 = wpabuf_alloc(20);
  431. if (hs20) {
  432. int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
  433. size_t len;
  434. wpas_hs20_add_indication(hs20, pps_mo_id);
  435. len = sizeof(wpa_s->sme.assoc_req_ie) -
  436. wpa_s->sme.assoc_req_ie_len;
  437. if (wpabuf_len(hs20) <= len) {
  438. os_memcpy(wpa_s->sme.assoc_req_ie +
  439. wpa_s->sme.assoc_req_ie_len,
  440. wpabuf_head(hs20), wpabuf_len(hs20));
  441. wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
  442. }
  443. wpabuf_free(hs20);
  444. }
  445. }
  446. #endif /* CONFIG_HS20 */
  447. if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
  448. struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
  449. size_t len;
  450. len = sizeof(wpa_s->sme.assoc_req_ie) -
  451. wpa_s->sme.assoc_req_ie_len;
  452. if (wpabuf_len(buf) <= len) {
  453. os_memcpy(wpa_s->sme.assoc_req_ie +
  454. wpa_s->sme.assoc_req_ie_len,
  455. wpabuf_head(buf), wpabuf_len(buf));
  456. wpa_s->sme.assoc_req_ie_len += wpabuf_len(buf);
  457. }
  458. }
  459. #ifdef CONFIG_MBO
  460. if (wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
  461. int len;
  462. len = wpas_mbo_ie(wpa_s, wpa_s->sme.assoc_req_ie +
  463. wpa_s->sme.assoc_req_ie_len,
  464. sizeof(wpa_s->sme.assoc_req_ie) -
  465. wpa_s->sme.assoc_req_ie_len);
  466. if (len >= 0)
  467. wpa_s->sme.assoc_req_ie_len += len;
  468. }
  469. #endif /* CONFIG_MBO */
  470. #ifdef CONFIG_SAE
  471. if (!skip_auth && params.auth_alg == WPA_AUTH_ALG_SAE &&
  472. pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid, ssid, 0,
  473. NULL) == 0) {
  474. wpa_dbg(wpa_s, MSG_DEBUG,
  475. "PMKSA cache entry found - try to use PMKSA caching instead of new SAE authentication");
  476. params.auth_alg = WPA_AUTH_ALG_OPEN;
  477. wpa_s->sme.sae_pmksa_caching = 1;
  478. }
  479. if (!skip_auth && params.auth_alg == WPA_AUTH_ALG_SAE) {
  480. if (start)
  481. resp = sme_auth_build_sae_commit(wpa_s, ssid,
  482. bss->bssid);
  483. else
  484. resp = sme_auth_build_sae_confirm(wpa_s);
  485. if (resp == NULL) {
  486. wpas_connection_failed(wpa_s, bss->bssid);
  487. return;
  488. }
  489. params.auth_data = wpabuf_head(resp);
  490. params.auth_data_len = wpabuf_len(resp);
  491. wpa_s->sme.sae.state = start ? SAE_COMMITTED : SAE_CONFIRMED;
  492. }
  493. #endif /* CONFIG_SAE */
  494. old_ssid = wpa_s->current_ssid;
  495. wpa_s->current_ssid = ssid;
  496. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  497. wpa_supplicant_initiate_eapol(wpa_s);
  498. #ifdef CONFIG_FILS
  499. /* TODO: FILS operations can in some cases be done between different
  500. * network_ctx (i.e., same credentials can be used with multiple
  501. * networks). */
  502. if (params.auth_alg == WPA_AUTH_ALG_OPEN &&
  503. wpa_key_mgmt_fils(ssid->key_mgmt)) {
  504. const u8 *indic;
  505. u16 fils_info;
  506. /*
  507. * Check FILS Indication element (FILS Information field) bits
  508. * indicating supported authentication algorithms against local
  509. * configuration (ssid->fils_dh_group). Try to use FILS
  510. * authentication only if the AP supports the combination in the
  511. * network profile. */
  512. indic = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
  513. if (!indic || indic[1] < 2) {
  514. wpa_printf(MSG_DEBUG, "SME: " MACSTR
  515. " does not include FILS Indication element - cannot use FILS authentication with it",
  516. MAC2STR(bss->bssid));
  517. goto no_fils;
  518. }
  519. fils_info = WPA_GET_LE16(indic + 2);
  520. if (ssid->fils_dh_group == 0 && !(fils_info & BIT(9))) {
  521. wpa_printf(MSG_DEBUG, "SME: " MACSTR
  522. " does not support FILS SK without PFS - cannot use FILS authentication with it",
  523. MAC2STR(bss->bssid));
  524. goto no_fils;
  525. }
  526. if (ssid->fils_dh_group != 0 && !(fils_info & BIT(10))) {
  527. wpa_printf(MSG_DEBUG, "SME: " MACSTR
  528. " does not support FILS SK with PFS - cannot use FILS authentication with it",
  529. MAC2STR(bss->bssid));
  530. goto no_fils;
  531. }
  532. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  533. ssid, 0,
  534. wpa_bss_get_fils_cache_id(bss)) ==
  535. 0)
  536. wpa_printf(MSG_DEBUG,
  537. "SME: Try to use FILS with PMKSA caching");
  538. resp = fils_build_auth(wpa_s->wpa, ssid->fils_dh_group, md);
  539. if (resp) {
  540. int auth_alg;
  541. if (ssid->fils_dh_group)
  542. wpa_printf(MSG_DEBUG,
  543. "SME: Try to use FILS SK authentication with PFS (DH Group %u)",
  544. ssid->fils_dh_group);
  545. else
  546. wpa_printf(MSG_DEBUG,
  547. "SME: Try to use FILS SK authentication without PFS");
  548. auth_alg = ssid->fils_dh_group ?
  549. WPA_AUTH_ALG_FILS_SK_PFS : WPA_AUTH_ALG_FILS;
  550. params.auth_alg = auth_alg;
  551. params.auth_data = wpabuf_head(resp);
  552. params.auth_data_len = wpabuf_len(resp);
  553. wpa_s->sme.auth_alg = auth_alg;
  554. }
  555. }
  556. no_fils:
  557. #endif /* CONFIG_FILS */
  558. wpa_supplicant_cancel_sched_scan(wpa_s);
  559. wpa_supplicant_cancel_scan(wpa_s);
  560. wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
  561. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  562. wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
  563. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  564. wpa_clear_keys(wpa_s, bss->bssid);
  565. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  566. if (old_ssid != wpa_s->current_ssid)
  567. wpas_notify_network_changed(wpa_s);
  568. #ifdef CONFIG_HS20
  569. hs20_configure_frame_filters(wpa_s);
  570. #endif /* CONFIG_HS20 */
  571. #ifdef CONFIG_P2P
  572. /*
  573. * If multi-channel concurrency is not supported, check for any
  574. * frequency conflict. In case of any frequency conflict, remove the
  575. * least prioritized connection.
  576. */
  577. if (wpa_s->num_multichan_concurrent < 2) {
  578. int freq, num;
  579. num = get_shared_radio_freqs(wpa_s, &freq, 1);
  580. if (num > 0 && freq > 0 && freq != params.freq) {
  581. wpa_printf(MSG_DEBUG,
  582. "Conflicting frequency found (%d != %d)",
  583. freq, params.freq);
  584. if (wpas_p2p_handle_frequency_conflicts(wpa_s,
  585. params.freq,
  586. ssid) < 0) {
  587. wpas_connection_failed(wpa_s, bss->bssid);
  588. wpa_supplicant_mark_disassoc(wpa_s);
  589. wpabuf_free(resp);
  590. wpas_connect_work_done(wpa_s);
  591. return;
  592. }
  593. }
  594. }
  595. #endif /* CONFIG_P2P */
  596. if (skip_auth) {
  597. wpa_msg(wpa_s, MSG_DEBUG,
  598. "SME: Skip authentication step on reassoc-to-same-BSS");
  599. wpabuf_free(resp);
  600. sme_associate(wpa_s, ssid->mode, bss->bssid, WLAN_AUTH_OPEN);
  601. return;
  602. }
  603. wpa_s->sme.auth_alg = params.auth_alg;
  604. if (wpa_drv_authenticate(wpa_s, &params) < 0) {
  605. wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
  606. "driver failed");
  607. wpas_connection_failed(wpa_s, bss->bssid);
  608. wpa_supplicant_mark_disassoc(wpa_s);
  609. wpabuf_free(resp);
  610. wpas_connect_work_done(wpa_s);
  611. return;
  612. }
  613. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  614. NULL);
  615. /*
  616. * Association will be started based on the authentication event from
  617. * the driver.
  618. */
  619. wpabuf_free(resp);
  620. }
  621. static void sme_auth_start_cb(struct wpa_radio_work *work, int deinit)
  622. {
  623. struct wpa_connect_work *cwork = work->ctx;
  624. struct wpa_supplicant *wpa_s = work->wpa_s;
  625. if (deinit) {
  626. if (work->started)
  627. wpa_s->connect_work = NULL;
  628. wpas_connect_work_free(cwork);
  629. return;
  630. }
  631. wpa_s->connect_work = work;
  632. if (cwork->bss_removed ||
  633. !wpas_valid_bss_ssid(wpa_s, cwork->bss, cwork->ssid) ||
  634. wpas_network_disabled(wpa_s, cwork->ssid)) {
  635. wpa_dbg(wpa_s, MSG_DEBUG, "SME: BSS/SSID entry for authentication not valid anymore - drop connection attempt");
  636. wpas_connect_work_done(wpa_s);
  637. return;
  638. }
  639. /* Starting new connection, so clear the possibly used WPA IE from the
  640. * previous association. */
  641. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  642. sme_send_authentication(wpa_s, cwork->bss, cwork->ssid, 1);
  643. }
  644. void sme_authenticate(struct wpa_supplicant *wpa_s,
  645. struct wpa_bss *bss, struct wpa_ssid *ssid)
  646. {
  647. struct wpa_connect_work *cwork;
  648. if (bss == NULL || ssid == NULL)
  649. return;
  650. if (wpa_s->connect_work) {
  651. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reject sme_authenticate() call since connect_work exist");
  652. return;
  653. }
  654. if (radio_work_pending(wpa_s, "sme-connect")) {
  655. /*
  656. * The previous sme-connect work might no longer be valid due to
  657. * the fact that the BSS list was updated. In addition, it makes
  658. * sense to adhere to the 'newer' decision.
  659. */
  660. wpa_dbg(wpa_s, MSG_DEBUG,
  661. "SME: Remove previous pending sme-connect");
  662. radio_remove_works(wpa_s, "sme-connect", 0);
  663. }
  664. wpas_abort_ongoing_scan(wpa_s);
  665. cwork = os_zalloc(sizeof(*cwork));
  666. if (cwork == NULL)
  667. return;
  668. cwork->bss = bss;
  669. cwork->ssid = ssid;
  670. cwork->sme = 1;
  671. #ifdef CONFIG_SAE
  672. wpa_s->sme.sae.state = SAE_NOTHING;
  673. wpa_s->sme.sae.send_confirm = 0;
  674. wpa_s->sme.sae_group_index = 0;
  675. #endif /* CONFIG_SAE */
  676. if (radio_add_work(wpa_s, bss->freq, "sme-connect", 1,
  677. sme_auth_start_cb, cwork) < 0)
  678. wpas_connect_work_free(cwork);
  679. }
  680. #ifdef CONFIG_SAE
  681. static int sme_sae_auth(struct wpa_supplicant *wpa_s, u16 auth_transaction,
  682. u16 status_code, const u8 *data, size_t len)
  683. {
  684. int *groups;
  685. wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE authentication transaction %u "
  686. "status code %u", auth_transaction, status_code);
  687. if (auth_transaction == 1 &&
  688. status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
  689. wpa_s->sme.sae.state == SAE_COMMITTED &&
  690. wpa_s->current_bss && wpa_s->current_ssid) {
  691. int default_groups[] = { 19, 20, 21, 25, 26, 0 };
  692. u16 group;
  693. groups = wpa_s->conf->sae_groups;
  694. if (!groups || groups[0] <= 0)
  695. groups = default_groups;
  696. if (len < sizeof(le16)) {
  697. wpa_dbg(wpa_s, MSG_DEBUG,
  698. "SME: Too short SAE anti-clogging token request");
  699. return -1;
  700. }
  701. group = WPA_GET_LE16(data);
  702. wpa_dbg(wpa_s, MSG_DEBUG,
  703. "SME: SAE anti-clogging token requested (group %u)",
  704. group);
  705. if (sae_group_allowed(&wpa_s->sme.sae, groups, group) !=
  706. WLAN_STATUS_SUCCESS) {
  707. wpa_dbg(wpa_s, MSG_ERROR,
  708. "SME: SAE group %u of anti-clogging request is invalid",
  709. group);
  710. return -1;
  711. }
  712. wpabuf_free(wpa_s->sme.sae_token);
  713. wpa_s->sme.sae_token = wpabuf_alloc_copy(data + sizeof(le16),
  714. len - sizeof(le16));
  715. sme_send_authentication(wpa_s, wpa_s->current_bss,
  716. wpa_s->current_ssid, 1);
  717. return 0;
  718. }
  719. if (auth_transaction == 1 &&
  720. status_code == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
  721. wpa_s->sme.sae.state == SAE_COMMITTED &&
  722. wpa_s->current_bss && wpa_s->current_ssid) {
  723. wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE group not supported");
  724. wpa_s->sme.sae_group_index++;
  725. if (sme_set_sae_group(wpa_s) < 0)
  726. return -1; /* no other groups enabled */
  727. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Try next enabled SAE group");
  728. sme_send_authentication(wpa_s, wpa_s->current_bss,
  729. wpa_s->current_ssid, 1);
  730. return 0;
  731. }
  732. if (status_code != WLAN_STATUS_SUCCESS)
  733. return -1;
  734. if (auth_transaction == 1) {
  735. u16 res;
  736. groups = wpa_s->conf->sae_groups;
  737. wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE commit");
  738. if (wpa_s->current_bss == NULL ||
  739. wpa_s->current_ssid == NULL)
  740. return -1;
  741. if (wpa_s->sme.sae.state != SAE_COMMITTED)
  742. return -1;
  743. if (groups && groups[0] <= 0)
  744. groups = NULL;
  745. res = sae_parse_commit(&wpa_s->sme.sae, data, len, NULL, NULL,
  746. groups);
  747. if (res == SAE_SILENTLY_DISCARD) {
  748. wpa_printf(MSG_DEBUG,
  749. "SAE: Drop commit message due to reflection attack");
  750. return 0;
  751. }
  752. if (res != WLAN_STATUS_SUCCESS)
  753. return -1;
  754. if (sae_process_commit(&wpa_s->sme.sae) < 0) {
  755. wpa_printf(MSG_DEBUG, "SAE: Failed to process peer "
  756. "commit");
  757. return -1;
  758. }
  759. wpabuf_free(wpa_s->sme.sae_token);
  760. wpa_s->sme.sae_token = NULL;
  761. sme_send_authentication(wpa_s, wpa_s->current_bss,
  762. wpa_s->current_ssid, 0);
  763. return 0;
  764. } else if (auth_transaction == 2) {
  765. wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE confirm");
  766. if (wpa_s->sme.sae.state != SAE_CONFIRMED)
  767. return -1;
  768. if (sae_check_confirm(&wpa_s->sme.sae, data, len) < 0)
  769. return -1;
  770. wpa_s->sme.sae.state = SAE_ACCEPTED;
  771. sae_clear_temp_data(&wpa_s->sme.sae);
  772. return 1;
  773. }
  774. return -1;
  775. }
  776. #endif /* CONFIG_SAE */
  777. void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
  778. {
  779. struct wpa_ssid *ssid = wpa_s->current_ssid;
  780. if (ssid == NULL) {
  781. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
  782. "when network is not selected");
  783. return;
  784. }
  785. if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
  786. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
  787. "when not in authenticating state");
  788. return;
  789. }
  790. if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
  791. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
  792. "unexpected peer " MACSTR,
  793. MAC2STR(data->auth.peer));
  794. return;
  795. }
  796. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
  797. " auth_type=%d auth_transaction=%d status_code=%d",
  798. MAC2STR(data->auth.peer), data->auth.auth_type,
  799. data->auth.auth_transaction, data->auth.status_code);
  800. wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
  801. data->auth.ies, data->auth.ies_len);
  802. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  803. #ifdef CONFIG_SAE
  804. if (data->auth.auth_type == WLAN_AUTH_SAE) {
  805. int res;
  806. res = sme_sae_auth(wpa_s, data->auth.auth_transaction,
  807. data->auth.status_code, data->auth.ies,
  808. data->auth.ies_len);
  809. if (res < 0) {
  810. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  811. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  812. }
  813. if (res != 1)
  814. return;
  815. wpa_printf(MSG_DEBUG, "SME: SAE completed - setting PMK for "
  816. "4-way handshake");
  817. wpa_sm_set_pmk(wpa_s->wpa, wpa_s->sme.sae.pmk, PMK_LEN,
  818. wpa_s->sme.sae.pmkid, wpa_s->pending_bssid);
  819. }
  820. #endif /* CONFIG_SAE */
  821. if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
  822. char *ie_txt = NULL;
  823. if (data->auth.ies && data->auth.ies_len) {
  824. size_t buflen = 2 * data->auth.ies_len + 1;
  825. ie_txt = os_malloc(buflen);
  826. if (ie_txt) {
  827. wpa_snprintf_hex(ie_txt, buflen, data->auth.ies,
  828. data->auth.ies_len);
  829. }
  830. }
  831. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AUTH_REJECT MACSTR
  832. " auth_type=%u auth_transaction=%u status_code=%u%s%s",
  833. MAC2STR(data->auth.peer), data->auth.auth_type,
  834. data->auth.auth_transaction, data->auth.status_code,
  835. ie_txt ? " ie=" : "",
  836. ie_txt ? ie_txt : "");
  837. os_free(ie_txt);
  838. if (data->auth.status_code !=
  839. WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
  840. wpa_s->sme.auth_alg == data->auth.auth_type ||
  841. wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
  842. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  843. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  844. return;
  845. }
  846. wpas_connect_work_done(wpa_s);
  847. switch (data->auth.auth_type) {
  848. case WLAN_AUTH_OPEN:
  849. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  850. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
  851. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  852. wpa_s->current_ssid);
  853. return;
  854. case WLAN_AUTH_SHARED_KEY:
  855. wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
  856. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
  857. wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
  858. wpa_s->current_ssid);
  859. return;
  860. default:
  861. return;
  862. }
  863. }
  864. #ifdef CONFIG_IEEE80211R
  865. if (data->auth.auth_type == WLAN_AUTH_FT) {
  866. const u8 *ric_ies = NULL;
  867. size_t ric_ies_len = 0;
  868. if (wpa_s->ric_ies) {
  869. ric_ies = wpabuf_head(wpa_s->ric_ies);
  870. ric_ies_len = wpabuf_len(wpa_s->ric_ies);
  871. }
  872. if (wpa_ft_process_response(wpa_s->wpa, data->auth.ies,
  873. data->auth.ies_len, 0,
  874. data->auth.peer,
  875. ric_ies, ric_ies_len) < 0) {
  876. wpa_dbg(wpa_s, MSG_DEBUG,
  877. "SME: FT Authentication response processing failed");
  878. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
  879. MACSTR
  880. " reason=%d locally_generated=1",
  881. MAC2STR(wpa_s->pending_bssid),
  882. WLAN_REASON_DEAUTH_LEAVING);
  883. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  884. wpa_supplicant_mark_disassoc(wpa_s);
  885. return;
  886. }
  887. }
  888. #endif /* CONFIG_IEEE80211R */
  889. #ifdef CONFIG_FILS
  890. if (data->auth.auth_type == WLAN_AUTH_FILS_SK ||
  891. data->auth.auth_type == WLAN_AUTH_FILS_SK_PFS) {
  892. u16 expect_auth_type;
  893. expect_auth_type = wpa_s->sme.auth_alg ==
  894. WPA_AUTH_ALG_FILS_SK_PFS ? WLAN_AUTH_FILS_SK_PFS :
  895. WLAN_AUTH_FILS_SK;
  896. if (data->auth.auth_type != expect_auth_type) {
  897. wpa_dbg(wpa_s, MSG_DEBUG,
  898. "SME: FILS Authentication response used different auth alg (%u; expected %u)",
  899. data->auth.auth_type, expect_auth_type);
  900. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
  901. MACSTR
  902. " reason=%d locally_generated=1",
  903. MAC2STR(wpa_s->pending_bssid),
  904. WLAN_REASON_DEAUTH_LEAVING);
  905. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  906. wpa_supplicant_mark_disassoc(wpa_s);
  907. return;
  908. }
  909. if (fils_process_auth(wpa_s->wpa, wpa_s->pending_bssid,
  910. data->auth.ies, data->auth.ies_len) < 0) {
  911. wpa_dbg(wpa_s, MSG_DEBUG,
  912. "SME: FILS Authentication response processing failed");
  913. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
  914. MACSTR
  915. " reason=%d locally_generated=1",
  916. MAC2STR(wpa_s->pending_bssid),
  917. WLAN_REASON_DEAUTH_LEAVING);
  918. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  919. wpa_supplicant_mark_disassoc(wpa_s);
  920. return;
  921. }
  922. }
  923. #endif /* CONFIG_FILS */
  924. sme_associate(wpa_s, ssid->mode, data->auth.peer,
  925. data->auth.auth_type);
  926. }
  927. #ifdef CONFIG_FILS
  928. #ifdef CONFIG_IEEE80211R
  929. static void remove_ie(u8 *buf, size_t *len, u8 eid)
  930. {
  931. u8 *pos, *next, *end;
  932. pos = (u8 *) get_ie(buf, *len, eid);
  933. if (pos) {
  934. next = pos + 2 + pos[1];
  935. end = buf + *len;
  936. *len -= 2 + pos[1];
  937. os_memmove(pos, next, end - next);
  938. }
  939. }
  940. #endif /* CONFIG_IEEE80211R */
  941. #endif /* CONFIG_FILS */
  942. void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
  943. const u8 *bssid, u16 auth_type)
  944. {
  945. struct wpa_driver_associate_params params;
  946. struct ieee802_11_elems elems;
  947. #ifdef CONFIG_FILS
  948. u8 nonces[2 * FILS_NONCE_LEN];
  949. #endif /* CONFIG_FILS */
  950. #ifdef CONFIG_HT_OVERRIDES
  951. struct ieee80211_ht_capabilities htcaps;
  952. struct ieee80211_ht_capabilities htcaps_mask;
  953. #endif /* CONFIG_HT_OVERRIDES */
  954. #ifdef CONFIG_VHT_OVERRIDES
  955. struct ieee80211_vht_capabilities vhtcaps;
  956. struct ieee80211_vht_capabilities vhtcaps_mask;
  957. #endif /* CONFIG_VHT_OVERRIDES */
  958. os_memset(&params, 0, sizeof(params));
  959. #ifdef CONFIG_FILS
  960. if (auth_type == WLAN_AUTH_FILS_SK ||
  961. auth_type == WLAN_AUTH_FILS_SK_PFS) {
  962. struct wpabuf *buf;
  963. const u8 *snonce, *anonce;
  964. const unsigned int max_hlp = 20;
  965. struct wpabuf *hlp[max_hlp];
  966. unsigned int i, num_hlp = 0;
  967. struct fils_hlp_req *req;
  968. dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
  969. list) {
  970. hlp[num_hlp] = wpabuf_alloc(2 * ETH_ALEN + 6 +
  971. wpabuf_len(req->pkt));
  972. if (!hlp[num_hlp])
  973. break;
  974. wpabuf_put_data(hlp[num_hlp], req->dst, ETH_ALEN);
  975. wpabuf_put_data(hlp[num_hlp], wpa_s->own_addr,
  976. ETH_ALEN);
  977. wpabuf_put_data(hlp[num_hlp],
  978. "\xaa\xaa\x03\x00\x00\x00", 6);
  979. wpabuf_put_buf(hlp[num_hlp], req->pkt);
  980. num_hlp++;
  981. if (num_hlp >= max_hlp)
  982. break;
  983. }
  984. buf = fils_build_assoc_req(wpa_s->wpa, &params.fils_kek,
  985. &params.fils_kek_len, &snonce,
  986. &anonce,
  987. (const struct wpabuf **) hlp,
  988. num_hlp);
  989. for (i = 0; i < num_hlp; i++)
  990. wpabuf_free(hlp[i]);
  991. if (!buf)
  992. return;
  993. wpa_hexdump(MSG_DEBUG, "FILS: assoc_req before FILS elements",
  994. wpa_s->sme.assoc_req_ie,
  995. wpa_s->sme.assoc_req_ie_len);
  996. #ifdef CONFIG_IEEE80211R
  997. if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
  998. /* Remove RSNE and MDE to allow them to be overridden
  999. * with FILS+FT specific values from
  1000. * fils_build_assoc_req(). */
  1001. remove_ie(wpa_s->sme.assoc_req_ie,
  1002. &wpa_s->sme.assoc_req_ie_len,
  1003. WLAN_EID_RSN);
  1004. wpa_hexdump(MSG_DEBUG,
  1005. "FILS: assoc_req after RSNE removal",
  1006. wpa_s->sme.assoc_req_ie,
  1007. wpa_s->sme.assoc_req_ie_len);
  1008. remove_ie(wpa_s->sme.assoc_req_ie,
  1009. &wpa_s->sme.assoc_req_ie_len,
  1010. WLAN_EID_MOBILITY_DOMAIN);
  1011. wpa_hexdump(MSG_DEBUG,
  1012. "FILS: assoc_req after MDE removal",
  1013. wpa_s->sme.assoc_req_ie,
  1014. wpa_s->sme.assoc_req_ie_len);
  1015. }
  1016. #endif /* CONFIG_IEEE80211R */
  1017. /* TODO: Make wpa_s->sme.assoc_req_ie use dynamic allocation */
  1018. if (wpa_s->sme.assoc_req_ie_len + wpabuf_len(buf) >
  1019. sizeof(wpa_s->sme.assoc_req_ie)) {
  1020. wpa_printf(MSG_ERROR,
  1021. "FILS: Not enough buffer room for own AssocReq elements");
  1022. wpabuf_free(buf);
  1023. return;
  1024. }
  1025. os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
  1026. wpabuf_head(buf), wpabuf_len(buf));
  1027. wpa_s->sme.assoc_req_ie_len += wpabuf_len(buf);
  1028. wpabuf_free(buf);
  1029. wpa_hexdump(MSG_DEBUG, "FILS: assoc_req after FILS elements",
  1030. wpa_s->sme.assoc_req_ie,
  1031. wpa_s->sme.assoc_req_ie_len);
  1032. os_memcpy(nonces, snonce, FILS_NONCE_LEN);
  1033. os_memcpy(nonces + FILS_NONCE_LEN, anonce, FILS_NONCE_LEN);
  1034. params.fils_nonces = nonces;
  1035. params.fils_nonces_len = sizeof(nonces);
  1036. }
  1037. #endif /* CONFIG_FILS */
  1038. #ifdef CONFIG_OWE
  1039. #ifdef CONFIG_TESTING_OPTIONS
  1040. if (get_ie_ext(wpa_s->sme.assoc_req_ie, wpa_s->sme.assoc_req_ie_len,
  1041. WLAN_EID_EXT_OWE_DH_PARAM)) {
  1042. wpa_printf(MSG_INFO, "TESTING: Override OWE DH element");
  1043. } else
  1044. #endif /* CONFIG_TESTING_OPTIONS */
  1045. if (auth_type == WLAN_AUTH_OPEN &&
  1046. wpa_s->key_mgmt == WPA_KEY_MGMT_OWE) {
  1047. struct wpabuf *owe_ie;
  1048. u16 group = OWE_DH_GROUP;
  1049. if (wpa_s->current_ssid && wpa_s->current_ssid->owe_group)
  1050. group = wpa_s->current_ssid->owe_group;
  1051. owe_ie = owe_build_assoc_req(wpa_s->wpa, group);
  1052. if (!owe_ie) {
  1053. wpa_printf(MSG_ERROR,
  1054. "OWE: Failed to build IE for Association Request frame");
  1055. return;
  1056. }
  1057. if (wpa_s->sme.assoc_req_ie_len + wpabuf_len(owe_ie) >
  1058. sizeof(wpa_s->sme.assoc_req_ie)) {
  1059. wpa_printf(MSG_ERROR,
  1060. "OWE: Not enough buffer room for own Association Request frame elements");
  1061. wpabuf_free(owe_ie);
  1062. return;
  1063. }
  1064. os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
  1065. wpabuf_head(owe_ie), wpabuf_len(owe_ie));
  1066. wpa_s->sme.assoc_req_ie_len += wpabuf_len(owe_ie);
  1067. wpabuf_free(owe_ie);
  1068. }
  1069. #endif /* CONFIG_OWE */
  1070. params.bssid = bssid;
  1071. params.ssid = wpa_s->sme.ssid;
  1072. params.ssid_len = wpa_s->sme.ssid_len;
  1073. params.freq.freq = wpa_s->sme.freq;
  1074. params.bg_scan_period = wpa_s->current_ssid ?
  1075. wpa_s->current_ssid->bg_scan_period : -1;
  1076. params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
  1077. wpa_s->sme.assoc_req_ie : NULL;
  1078. params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
  1079. params.pairwise_suite = wpa_s->pairwise_cipher;
  1080. params.group_suite = wpa_s->group_cipher;
  1081. params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
  1082. params.key_mgmt_suite = wpa_s->key_mgmt;
  1083. params.wpa_proto = wpa_s->wpa_proto;
  1084. #ifdef CONFIG_HT_OVERRIDES
  1085. os_memset(&htcaps, 0, sizeof(htcaps));
  1086. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  1087. params.htcaps = (u8 *) &htcaps;
  1088. params.htcaps_mask = (u8 *) &htcaps_mask;
  1089. wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
  1090. #endif /* CONFIG_HT_OVERRIDES */
  1091. #ifdef CONFIG_VHT_OVERRIDES
  1092. os_memset(&vhtcaps, 0, sizeof(vhtcaps));
  1093. os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
  1094. params.vhtcaps = &vhtcaps;
  1095. params.vhtcaps_mask = &vhtcaps_mask;
  1096. wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
  1097. #endif /* CONFIG_VHT_OVERRIDES */
  1098. #ifdef CONFIG_IEEE80211R
  1099. if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
  1100. params.wpa_ie = wpa_s->sme.ft_ies;
  1101. params.wpa_ie_len = wpa_s->sme.ft_ies_len;
  1102. }
  1103. #endif /* CONFIG_IEEE80211R */
  1104. params.mode = mode;
  1105. params.mgmt_frame_protection = wpa_s->sme.mfp;
  1106. params.rrm_used = wpa_s->rrm.rrm_used;
  1107. if (wpa_s->sme.prev_bssid_set)
  1108. params.prev_bssid = wpa_s->sme.prev_bssid;
  1109. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1110. " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
  1111. params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
  1112. params.freq.freq);
  1113. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1114. if (params.wpa_ie == NULL ||
  1115. ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
  1116. < 0) {
  1117. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
  1118. os_memset(&elems, 0, sizeof(elems));
  1119. }
  1120. if (elems.rsn_ie) {
  1121. params.wpa_proto = WPA_PROTO_RSN;
  1122. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
  1123. elems.rsn_ie_len + 2);
  1124. } else if (elems.wpa_ie) {
  1125. params.wpa_proto = WPA_PROTO_WPA;
  1126. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
  1127. elems.wpa_ie_len + 2);
  1128. } else if (elems.osen) {
  1129. params.wpa_proto = WPA_PROTO_OSEN;
  1130. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.osen - 2,
  1131. elems.osen_len + 2);
  1132. } else
  1133. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1134. if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
  1135. params.p2p = 1;
  1136. if (wpa_s->p2pdev->set_sta_uapsd)
  1137. params.uapsd = wpa_s->p2pdev->sta_uapsd;
  1138. else
  1139. params.uapsd = -1;
  1140. if (wpa_drv_associate(wpa_s, &params) < 0) {
  1141. wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
  1142. "driver failed");
  1143. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1144. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1145. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1146. return;
  1147. }
  1148. eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
  1149. NULL);
  1150. }
  1151. int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  1152. const u8 *ies, size_t ies_len)
  1153. {
  1154. if (md == NULL || ies == NULL) {
  1155. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
  1156. os_free(wpa_s->sme.ft_ies);
  1157. wpa_s->sme.ft_ies = NULL;
  1158. wpa_s->sme.ft_ies_len = 0;
  1159. wpa_s->sme.ft_used = 0;
  1160. return 0;
  1161. }
  1162. os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  1163. wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
  1164. os_free(wpa_s->sme.ft_ies);
  1165. wpa_s->sme.ft_ies = os_memdup(ies, ies_len);
  1166. if (wpa_s->sme.ft_ies == NULL)
  1167. return -1;
  1168. wpa_s->sme.ft_ies_len = ies_len;
  1169. return 0;
  1170. }
  1171. static void sme_deauth(struct wpa_supplicant *wpa_s)
  1172. {
  1173. int bssid_changed;
  1174. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1175. if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
  1176. WLAN_REASON_DEAUTH_LEAVING) < 0) {
  1177. wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
  1178. "failed");
  1179. }
  1180. wpa_s->sme.prev_bssid_set = 0;
  1181. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1182. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1183. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1184. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1185. if (bssid_changed)
  1186. wpas_notify_bssid_changed(wpa_s);
  1187. }
  1188. void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
  1189. union wpa_event_data *data)
  1190. {
  1191. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
  1192. "status code %d", MAC2STR(wpa_s->pending_bssid),
  1193. data->assoc_reject.status_code);
  1194. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  1195. #ifdef CONFIG_SAE
  1196. if (wpa_s->sme.sae_pmksa_caching && wpa_s->current_ssid &&
  1197. wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt)) {
  1198. wpa_dbg(wpa_s, MSG_DEBUG,
  1199. "PMKSA caching attempt rejected - drop PMKSA cache entry and fall back to SAE authentication");
  1200. wpa_sm_aborted_cached(wpa_s->wpa);
  1201. wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
  1202. if (wpa_s->current_bss) {
  1203. struct wpa_bss *bss = wpa_s->current_bss;
  1204. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1205. wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
  1206. WLAN_REASON_DEAUTH_LEAVING);
  1207. wpas_connect_work_done(wpa_s);
  1208. wpa_supplicant_mark_disassoc(wpa_s);
  1209. wpa_supplicant_connect(wpa_s, bss, ssid);
  1210. return;
  1211. }
  1212. }
  1213. #endif /* CONFIG_SAE */
  1214. /*
  1215. * For now, unconditionally terminate the previous authentication. In
  1216. * theory, this should not be needed, but mac80211 gets quite confused
  1217. * if the authentication is left pending.. Some roaming cases might
  1218. * benefit from using the previous authentication, so this could be
  1219. * optimized in the future.
  1220. */
  1221. sme_deauth(wpa_s);
  1222. }
  1223. void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
  1224. union wpa_event_data *data)
  1225. {
  1226. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
  1227. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1228. wpa_supplicant_mark_disassoc(wpa_s);
  1229. }
  1230. void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
  1231. union wpa_event_data *data)
  1232. {
  1233. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
  1234. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1235. wpa_supplicant_mark_disassoc(wpa_s);
  1236. }
  1237. void sme_event_disassoc(struct wpa_supplicant *wpa_s,
  1238. struct disassoc_info *info)
  1239. {
  1240. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
  1241. if (wpa_s->sme.prev_bssid_set) {
  1242. /*
  1243. * cfg80211/mac80211 can get into somewhat confused state if
  1244. * the AP only disassociates us and leaves us in authenticated
  1245. * state. For now, force the state to be cleared to avoid
  1246. * confusing errors if we try to associate with the AP again.
  1247. */
  1248. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
  1249. "driver state");
  1250. wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
  1251. WLAN_REASON_DEAUTH_LEAVING);
  1252. }
  1253. }
  1254. static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
  1255. {
  1256. struct wpa_supplicant *wpa_s = eloop_ctx;
  1257. if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
  1258. wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
  1259. sme_deauth(wpa_s);
  1260. }
  1261. }
  1262. static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
  1263. {
  1264. struct wpa_supplicant *wpa_s = eloop_ctx;
  1265. if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  1266. wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
  1267. sme_deauth(wpa_s);
  1268. }
  1269. }
  1270. void sme_state_changed(struct wpa_supplicant *wpa_s)
  1271. {
  1272. /* Make sure timers are cleaned up appropriately. */
  1273. if (wpa_s->wpa_state != WPA_ASSOCIATING)
  1274. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  1275. if (wpa_s->wpa_state != WPA_AUTHENTICATING)
  1276. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  1277. }
  1278. void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
  1279. const u8 *prev_pending_bssid)
  1280. {
  1281. /*
  1282. * mac80211-workaround to force deauth on failed auth cmd,
  1283. * requires us to remain in authenticating state to allow the
  1284. * second authentication attempt to be continued properly.
  1285. */
  1286. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
  1287. "to proceed after disconnection event");
  1288. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  1289. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  1290. /*
  1291. * Re-arm authentication timer in case auth fails for whatever reason.
  1292. */
  1293. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  1294. eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
  1295. NULL);
  1296. }
  1297. void sme_clear_on_disassoc(struct wpa_supplicant *wpa_s)
  1298. {
  1299. wpa_s->sme.prev_bssid_set = 0;
  1300. #ifdef CONFIG_SAE
  1301. wpabuf_free(wpa_s->sme.sae_token);
  1302. wpa_s->sme.sae_token = NULL;
  1303. sae_clear_data(&wpa_s->sme.sae);
  1304. #endif /* CONFIG_SAE */
  1305. #ifdef CONFIG_IEEE80211R
  1306. if (wpa_s->sme.ft_ies)
  1307. sme_update_ft_ies(wpa_s, NULL, NULL, 0);
  1308. #endif /* CONFIG_IEEE80211R */
  1309. }
  1310. void sme_deinit(struct wpa_supplicant *wpa_s)
  1311. {
  1312. os_free(wpa_s->sme.ft_ies);
  1313. wpa_s->sme.ft_ies = NULL;
  1314. wpa_s->sme.ft_ies_len = 0;
  1315. #ifdef CONFIG_IEEE80211W
  1316. sme_stop_sa_query(wpa_s);
  1317. #endif /* CONFIG_IEEE80211W */
  1318. sme_clear_on_disassoc(wpa_s);
  1319. eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
  1320. eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
  1321. eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
  1322. }
  1323. static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
  1324. const u8 *chan_list, u8 num_channels,
  1325. u8 num_intol)
  1326. {
  1327. struct ieee80211_2040_bss_coex_ie *bc_ie;
  1328. struct ieee80211_2040_intol_chan_report *ic_report;
  1329. struct wpabuf *buf;
  1330. wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR
  1331. " (num_channels=%u num_intol=%u)",
  1332. MAC2STR(wpa_s->bssid), num_channels, num_intol);
  1333. wpa_hexdump(MSG_DEBUG, "SME: 20/40 BSS Intolerant Channels",
  1334. chan_list, num_channels);
  1335. buf = wpabuf_alloc(2 + /* action.category + action_code */
  1336. sizeof(struct ieee80211_2040_bss_coex_ie) +
  1337. sizeof(struct ieee80211_2040_intol_chan_report) +
  1338. num_channels);
  1339. if (buf == NULL)
  1340. return;
  1341. wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
  1342. wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
  1343. bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
  1344. bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
  1345. bc_ie->length = 1;
  1346. if (num_intol)
  1347. bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
  1348. if (num_channels > 0) {
  1349. ic_report = wpabuf_put(buf, sizeof(*ic_report));
  1350. ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
  1351. ic_report->length = num_channels + 1;
  1352. ic_report->op_class = 0;
  1353. os_memcpy(wpabuf_put(buf, num_channels), chan_list,
  1354. num_channels);
  1355. }
  1356. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  1357. wpa_s->own_addr, wpa_s->bssid,
  1358. wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
  1359. wpa_msg(wpa_s, MSG_INFO,
  1360. "SME: Failed to send 20/40 BSS Coexistence frame");
  1361. }
  1362. wpabuf_free(buf);
  1363. }
  1364. int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
  1365. {
  1366. struct wpa_bss *bss;
  1367. const u8 *ie;
  1368. u16 ht_cap;
  1369. u8 chan_list[P2P_MAX_CHANNELS], channel;
  1370. u8 num_channels = 0, num_intol = 0, i;
  1371. if (!wpa_s->sme.sched_obss_scan)
  1372. return 0;
  1373. wpa_s->sme.sched_obss_scan = 0;
  1374. if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
  1375. return 1;
  1376. /*
  1377. * Check whether AP uses regulatory triplet or channel triplet in
  1378. * country info. Right now the operating class of the BSS channel
  1379. * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
  1380. * based on the assumption that operating class triplet is not used in
  1381. * beacon frame. If the First Channel Number/Operating Extension
  1382. * Identifier octet has a positive integer value of 201 or greater,
  1383. * then its operating class triplet.
  1384. *
  1385. * TODO: If Supported Operating Classes element is present in beacon
  1386. * frame, have to lookup operating class in Annex E and fill them in
  1387. * 2040 coex frame.
  1388. */
  1389. ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
  1390. if (ie && (ie[1] >= 6) && (ie[5] >= 201))
  1391. return 1;
  1392. os_memset(chan_list, 0, sizeof(chan_list));
  1393. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1394. /* Skip other band bss */
  1395. enum hostapd_hw_mode mode;
  1396. mode = ieee80211_freq_to_chan(bss->freq, &channel);
  1397. if (mode != HOSTAPD_MODE_IEEE80211G &&
  1398. mode != HOSTAPD_MODE_IEEE80211B)
  1399. continue;
  1400. ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
  1401. ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
  1402. wpa_printf(MSG_DEBUG, "SME OBSS scan BSS " MACSTR
  1403. " freq=%u chan=%u ht_cap=0x%x",
  1404. MAC2STR(bss->bssid), bss->freq, channel, ht_cap);
  1405. if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
  1406. if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
  1407. num_intol++;
  1408. /* Check whether the channel is already considered */
  1409. for (i = 0; i < num_channels; i++) {
  1410. if (channel == chan_list[i])
  1411. break;
  1412. }
  1413. if (i != num_channels)
  1414. continue;
  1415. chan_list[num_channels++] = channel;
  1416. }
  1417. }
  1418. sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
  1419. return 1;
  1420. }
  1421. static void wpa_obss_scan_freqs_list(struct wpa_supplicant *wpa_s,
  1422. struct wpa_driver_scan_params *params)
  1423. {
  1424. /* Include only affected channels */
  1425. struct hostapd_hw_modes *mode;
  1426. int count, i;
  1427. int start, end;
  1428. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
  1429. HOSTAPD_MODE_IEEE80211G);
  1430. if (mode == NULL) {
  1431. /* No channels supported in this band - use empty list */
  1432. params->freqs = os_zalloc(sizeof(int));
  1433. return;
  1434. }
  1435. if (wpa_s->sme.ht_sec_chan == HT_SEC_CHAN_UNKNOWN &&
  1436. wpa_s->current_bss) {
  1437. const u8 *ie;
  1438. ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_OPERATION);
  1439. if (ie && ie[1] >= 2) {
  1440. u8 o;
  1441. o = ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
  1442. if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
  1443. wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_ABOVE;
  1444. else if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
  1445. wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_BELOW;
  1446. }
  1447. }
  1448. start = wpa_s->assoc_freq - 10;
  1449. end = wpa_s->assoc_freq + 10;
  1450. switch (wpa_s->sme.ht_sec_chan) {
  1451. case HT_SEC_CHAN_UNKNOWN:
  1452. /* HT40+ possible on channels 1..9 */
  1453. if (wpa_s->assoc_freq <= 2452)
  1454. start -= 20;
  1455. /* HT40- possible on channels 5-13 */
  1456. if (wpa_s->assoc_freq >= 2432)
  1457. end += 20;
  1458. break;
  1459. case HT_SEC_CHAN_ABOVE:
  1460. end += 20;
  1461. break;
  1462. case HT_SEC_CHAN_BELOW:
  1463. start -= 20;
  1464. break;
  1465. }
  1466. wpa_printf(MSG_DEBUG,
  1467. "OBSS: assoc_freq %d possible affected range %d-%d",
  1468. wpa_s->assoc_freq, start, end);
  1469. params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
  1470. if (params->freqs == NULL)
  1471. return;
  1472. for (count = 0, i = 0; i < mode->num_channels; i++) {
  1473. int freq;
  1474. if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
  1475. continue;
  1476. freq = mode->channels[i].freq;
  1477. if (freq - 10 >= end || freq + 10 <= start)
  1478. continue; /* not affected */
  1479. params->freqs[count++] = freq;
  1480. }
  1481. }
  1482. static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  1483. {
  1484. struct wpa_supplicant *wpa_s = eloop_ctx;
  1485. struct wpa_driver_scan_params params;
  1486. if (!wpa_s->current_bss) {
  1487. wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
  1488. return;
  1489. }
  1490. os_memset(&params, 0, sizeof(params));
  1491. wpa_obss_scan_freqs_list(wpa_s, &params);
  1492. params.low_priority = 1;
  1493. wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
  1494. if (wpa_supplicant_trigger_scan(wpa_s, &params))
  1495. wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
  1496. else
  1497. wpa_s->sme.sched_obss_scan = 1;
  1498. os_free(params.freqs);
  1499. eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
  1500. sme_obss_scan_timeout, wpa_s, NULL);
  1501. }
  1502. void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
  1503. {
  1504. const u8 *ie;
  1505. struct wpa_bss *bss = wpa_s->current_bss;
  1506. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1507. struct hostapd_hw_modes *hw_mode = NULL;
  1508. int i;
  1509. eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
  1510. wpa_s->sme.sched_obss_scan = 0;
  1511. wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_UNKNOWN;
  1512. if (!enable)
  1513. return;
  1514. /*
  1515. * Schedule OBSS scan if driver is using station SME in wpa_supplicant
  1516. * or it expects OBSS scan to be performed by wpa_supplicant.
  1517. */
  1518. if (!((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
  1519. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OBSS_SCAN)) ||
  1520. ssid == NULL || ssid->mode != IEEE80211_MODE_INFRA)
  1521. return;
  1522. if (!wpa_s->hw.modes)
  1523. return;
  1524. /* only HT caps in 11g mode are relevant */
  1525. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  1526. hw_mode = &wpa_s->hw.modes[i];
  1527. if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
  1528. break;
  1529. }
  1530. /* Driver does not support HT40 for 11g or doesn't have 11g. */
  1531. if (i == wpa_s->hw.num_modes || !hw_mode ||
  1532. !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  1533. return;
  1534. if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
  1535. return; /* Not associated on 2.4 GHz band */
  1536. /* Check whether AP supports HT40 */
  1537. ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
  1538. if (!ie || ie[1] < 2 ||
  1539. !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  1540. return; /* AP does not support HT40 */
  1541. ie = wpa_bss_get_ie(wpa_s->current_bss,
  1542. WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
  1543. if (!ie || ie[1] < 14)
  1544. return; /* AP does not request OBSS scans */
  1545. wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
  1546. if (wpa_s->sme.obss_scan_int < 10) {
  1547. wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
  1548. "replaced with the minimum 10 sec",
  1549. wpa_s->sme.obss_scan_int);
  1550. wpa_s->sme.obss_scan_int = 10;
  1551. }
  1552. wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
  1553. wpa_s->sme.obss_scan_int);
  1554. eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
  1555. sme_obss_scan_timeout, wpa_s, NULL);
  1556. }
  1557. #ifdef CONFIG_IEEE80211W
  1558. static const unsigned int sa_query_max_timeout = 1000;
  1559. static const unsigned int sa_query_retry_timeout = 201;
  1560. static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
  1561. {
  1562. u32 tu;
  1563. struct os_reltime now, passed;
  1564. os_get_reltime(&now);
  1565. os_reltime_sub(&now, &wpa_s->sme.sa_query_start, &passed);
  1566. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  1567. if (sa_query_max_timeout < tu) {
  1568. wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
  1569. sme_stop_sa_query(wpa_s);
  1570. wpa_supplicant_deauthenticate(
  1571. wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
  1572. return 1;
  1573. }
  1574. return 0;
  1575. }
  1576. static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
  1577. const u8 *trans_id)
  1578. {
  1579. u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
  1580. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
  1581. MACSTR, MAC2STR(wpa_s->bssid));
  1582. wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
  1583. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  1584. req[0] = WLAN_ACTION_SA_QUERY;
  1585. req[1] = WLAN_SA_QUERY_REQUEST;
  1586. os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  1587. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  1588. wpa_s->own_addr, wpa_s->bssid,
  1589. req, sizeof(req), 0) < 0)
  1590. wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
  1591. "Request");
  1592. }
  1593. static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  1594. {
  1595. struct wpa_supplicant *wpa_s = eloop_ctx;
  1596. unsigned int timeout, sec, usec;
  1597. u8 *trans_id, *nbuf;
  1598. if (wpa_s->sme.sa_query_count > 0 &&
  1599. sme_check_sa_query_timeout(wpa_s))
  1600. return;
  1601. nbuf = os_realloc_array(wpa_s->sme.sa_query_trans_id,
  1602. wpa_s->sme.sa_query_count + 1,
  1603. WLAN_SA_QUERY_TR_ID_LEN);
  1604. if (nbuf == NULL) {
  1605. sme_stop_sa_query(wpa_s);
  1606. return;
  1607. }
  1608. if (wpa_s->sme.sa_query_count == 0) {
  1609. /* Starting a new SA Query procedure */
  1610. os_get_reltime(&wpa_s->sme.sa_query_start);
  1611. }
  1612. trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  1613. wpa_s->sme.sa_query_trans_id = nbuf;
  1614. wpa_s->sme.sa_query_count++;
  1615. if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
  1616. wpa_printf(MSG_DEBUG, "Could not generate SA Query ID");
  1617. sme_stop_sa_query(wpa_s);
  1618. return;
  1619. }
  1620. timeout = sa_query_retry_timeout;
  1621. sec = ((timeout / 1000) * 1024) / 1000;
  1622. usec = (timeout % 1000) * 1024;
  1623. eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
  1624. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
  1625. wpa_s->sme.sa_query_count);
  1626. sme_send_sa_query_req(wpa_s, trans_id);
  1627. }
  1628. static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
  1629. {
  1630. sme_sa_query_timer(wpa_s, NULL);
  1631. }
  1632. static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
  1633. {
  1634. eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
  1635. os_free(wpa_s->sme.sa_query_trans_id);
  1636. wpa_s->sme.sa_query_trans_id = NULL;
  1637. wpa_s->sme.sa_query_count = 0;
  1638. }
  1639. void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
  1640. const u8 *da, u16 reason_code)
  1641. {
  1642. struct wpa_ssid *ssid;
  1643. struct os_reltime now;
  1644. if (wpa_s->wpa_state != WPA_COMPLETED)
  1645. return;
  1646. ssid = wpa_s->current_ssid;
  1647. if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION)
  1648. return;
  1649. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  1650. return;
  1651. if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
  1652. reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
  1653. return;
  1654. if (wpa_s->sme.sa_query_count > 0)
  1655. return;
  1656. os_get_reltime(&now);
  1657. if (wpa_s->sme.last_unprot_disconnect.sec &&
  1658. !os_reltime_expired(&now, &wpa_s->sme.last_unprot_disconnect, 10))
  1659. return; /* limit SA Query procedure frequency */
  1660. wpa_s->sme.last_unprot_disconnect = now;
  1661. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
  1662. "possible AP/STA state mismatch - trigger SA Query");
  1663. sme_start_sa_query(wpa_s);
  1664. }
  1665. void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
  1666. const u8 *data, size_t len)
  1667. {
  1668. int i;
  1669. if (wpa_s->sme.sa_query_trans_id == NULL ||
  1670. len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
  1671. data[0] != WLAN_SA_QUERY_RESPONSE)
  1672. return;
  1673. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
  1674. MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
  1675. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
  1676. return;
  1677. for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
  1678. if (os_memcmp(wpa_s->sme.sa_query_trans_id +
  1679. i * WLAN_SA_QUERY_TR_ID_LEN,
  1680. data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
  1681. break;
  1682. }
  1683. if (i >= wpa_s->sme.sa_query_count) {
  1684. wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
  1685. "transaction identifier found");
  1686. return;
  1687. }
  1688. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
  1689. "from " MACSTR, MAC2STR(sa));
  1690. sme_stop_sa_query(wpa_s);
  1691. }
  1692. #endif /* CONFIG_IEEE80211W */