sme.c 63 KB

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