sme.c 35 KB

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