wnm_sta.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * wpa_supplicant - WNM
  3. * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "common/ieee802_11_defs.h"
  11. #include "common/wpa_ctrl.h"
  12. #include "rsn_supp/wpa.h"
  13. #include "wpa_supplicant_i.h"
  14. #include "driver_i.h"
  15. #include "scan.h"
  16. #include "ctrl_iface.h"
  17. #include "bss.h"
  18. #include "wnm_sta.h"
  19. #include "hs20_supplicant.h"
  20. #define MAX_TFS_IE_LEN 1024
  21. #define WNM_MAX_NEIGHBOR_REPORT 10
  22. /* get the TFS IE from driver */
  23. static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
  24. u16 *buf_len, enum wnm_oper oper)
  25. {
  26. wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
  27. return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
  28. }
  29. /* set the TFS IE to driver */
  30. static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
  31. const u8 *addr, u8 *buf, u16 *buf_len,
  32. enum wnm_oper oper)
  33. {
  34. wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
  35. return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
  36. }
  37. /* MLME-SLEEPMODE.request */
  38. int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
  39. u8 action, u16 intval, struct wpabuf *tfs_req)
  40. {
  41. struct ieee80211_mgmt *mgmt;
  42. int res;
  43. size_t len;
  44. struct wnm_sleep_element *wnmsleep_ie;
  45. u8 *wnmtfs_ie;
  46. u8 wnmsleep_ie_len;
  47. u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
  48. enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
  49. WNM_SLEEP_TFS_REQ_IE_NONE;
  50. wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
  51. "action=%s to " MACSTR,
  52. action == 0 ? "enter" : "exit",
  53. MAC2STR(wpa_s->bssid));
  54. /* WNM-Sleep Mode IE */
  55. wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
  56. wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
  57. if (wnmsleep_ie == NULL)
  58. return -1;
  59. wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
  60. wnmsleep_ie->len = wnmsleep_ie_len - 2;
  61. wnmsleep_ie->action_type = action;
  62. wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
  63. wnmsleep_ie->intval = host_to_le16(intval);
  64. wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
  65. (u8 *) wnmsleep_ie, wnmsleep_ie_len);
  66. /* TFS IE(s) */
  67. if (tfs_req) {
  68. wnmtfs_ie_len = wpabuf_len(tfs_req);
  69. wnmtfs_ie = os_malloc(wnmtfs_ie_len);
  70. if (wnmtfs_ie == NULL) {
  71. os_free(wnmsleep_ie);
  72. return -1;
  73. }
  74. os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
  75. } else {
  76. wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
  77. if (wnmtfs_ie == NULL) {
  78. os_free(wnmsleep_ie);
  79. return -1;
  80. }
  81. if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
  82. tfs_oper)) {
  83. wnmtfs_ie_len = 0;
  84. os_free(wnmtfs_ie);
  85. wnmtfs_ie = NULL;
  86. }
  87. }
  88. wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
  89. (u8 *) wnmtfs_ie, wnmtfs_ie_len);
  90. mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
  91. if (mgmt == NULL) {
  92. wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
  93. "WNM-Sleep Request action frame");
  94. os_free(wnmsleep_ie);
  95. os_free(wnmtfs_ie);
  96. return -1;
  97. }
  98. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  99. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  100. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  101. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  102. WLAN_FC_STYPE_ACTION);
  103. mgmt->u.action.category = WLAN_ACTION_WNM;
  104. mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
  105. mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
  106. os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
  107. wnmsleep_ie_len);
  108. /* copy TFS IE here */
  109. if (wnmtfs_ie_len > 0) {
  110. os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
  111. wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
  112. }
  113. len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
  114. wnmtfs_ie_len;
  115. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  116. wpa_s->own_addr, wpa_s->bssid,
  117. &mgmt->u.action.category, len, 0);
  118. if (res < 0)
  119. wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
  120. "(action=%d, intval=%d)", action, intval);
  121. os_free(wnmsleep_ie);
  122. os_free(wnmtfs_ie);
  123. os_free(mgmt);
  124. return res;
  125. }
  126. static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
  127. u8 *tfsresp_ie_start,
  128. u8 *tfsresp_ie_end)
  129. {
  130. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
  131. wpa_s->bssid, NULL, NULL);
  132. /* remove GTK/IGTK ?? */
  133. /* set the TFS Resp IE(s) */
  134. if (tfsresp_ie_start && tfsresp_ie_end &&
  135. tfsresp_ie_end - tfsresp_ie_start >= 0) {
  136. u16 tfsresp_ie_len;
  137. tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
  138. tfsresp_ie_start;
  139. wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
  140. /* pass the TFS Resp IE(s) to driver for processing */
  141. if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
  142. tfsresp_ie_start,
  143. &tfsresp_ie_len,
  144. WNM_SLEEP_TFS_RESP_IE_SET))
  145. wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
  146. }
  147. }
  148. static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
  149. const u8 *frm, u16 key_len_total)
  150. {
  151. u8 *ptr, *end;
  152. u8 gtk_len;
  153. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
  154. NULL, NULL);
  155. /* Install GTK/IGTK */
  156. /* point to key data field */
  157. ptr = (u8 *) frm + 1 + 2;
  158. end = ptr + key_len_total;
  159. wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
  160. while (ptr + 1 < end) {
  161. if (ptr + 2 + ptr[1] > end) {
  162. wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
  163. "length");
  164. if (end > ptr) {
  165. wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
  166. ptr, end - ptr);
  167. }
  168. break;
  169. }
  170. if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
  171. if (ptr[1] < 11 + 5) {
  172. wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
  173. "subelem");
  174. break;
  175. }
  176. gtk_len = *(ptr + 4);
  177. if (ptr[1] < 11 + gtk_len ||
  178. gtk_len < 5 || gtk_len > 32) {
  179. wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
  180. "subelem");
  181. break;
  182. }
  183. wpa_wnmsleep_install_key(
  184. wpa_s->wpa,
  185. WNM_SLEEP_SUBELEM_GTK,
  186. ptr);
  187. ptr += 13 + gtk_len;
  188. #ifdef CONFIG_IEEE80211W
  189. } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
  190. if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
  191. wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
  192. "subelem");
  193. break;
  194. }
  195. wpa_wnmsleep_install_key(wpa_s->wpa,
  196. WNM_SLEEP_SUBELEM_IGTK, ptr);
  197. ptr += 10 + WPA_IGTK_LEN;
  198. #endif /* CONFIG_IEEE80211W */
  199. } else
  200. break; /* skip the loop */
  201. }
  202. }
  203. static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
  204. const u8 *frm, int len)
  205. {
  206. /*
  207. * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
  208. * WNM-Sleep Mode IE | TFS Response IE
  209. */
  210. u8 *pos = (u8 *) frm; /* point to payload after the action field */
  211. u16 key_len_total;
  212. struct wnm_sleep_element *wnmsleep_ie = NULL;
  213. /* multiple TFS Resp IE (assuming consecutive) */
  214. u8 *tfsresp_ie_start = NULL;
  215. u8 *tfsresp_ie_end = NULL;
  216. if (len < 3)
  217. return;
  218. key_len_total = WPA_GET_LE16(frm + 1);
  219. wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
  220. frm[0], key_len_total);
  221. pos += 3 + key_len_total;
  222. if (pos > frm + len) {
  223. wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
  224. return;
  225. }
  226. while (pos - frm < len) {
  227. u8 ie_len = *(pos + 1);
  228. if (pos + 2 + ie_len > frm + len) {
  229. wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
  230. break;
  231. }
  232. wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
  233. if (*pos == WLAN_EID_WNMSLEEP)
  234. wnmsleep_ie = (struct wnm_sleep_element *) pos;
  235. else if (*pos == WLAN_EID_TFS_RESP) {
  236. if (!tfsresp_ie_start)
  237. tfsresp_ie_start = pos;
  238. tfsresp_ie_end = pos;
  239. } else
  240. wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
  241. pos += ie_len + 2;
  242. }
  243. if (!wnmsleep_ie) {
  244. wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
  245. return;
  246. }
  247. if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
  248. wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
  249. wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
  250. "frame (action=%d, intval=%d)",
  251. wnmsleep_ie->action_type, wnmsleep_ie->intval);
  252. if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
  253. wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
  254. tfsresp_ie_end);
  255. } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
  256. wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
  257. }
  258. } else {
  259. wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
  260. "(action=%d, intval=%d)",
  261. wnmsleep_ie->action_type, wnmsleep_ie->intval);
  262. if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
  263. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
  264. wpa_s->bssid, NULL, NULL);
  265. else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
  266. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
  267. wpa_s->bssid, NULL, NULL);
  268. }
  269. }
  270. void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
  271. {
  272. int i;
  273. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  274. os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
  275. os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
  276. }
  277. wpa_s->wnm_num_neighbor_report = 0;
  278. os_free(wpa_s->wnm_neighbor_report_elements);
  279. wpa_s->wnm_neighbor_report_elements = NULL;
  280. }
  281. static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
  282. u8 id, u8 elen, const u8 *pos)
  283. {
  284. switch (id) {
  285. case WNM_NEIGHBOR_TSF:
  286. if (elen < 2 + 2) {
  287. wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
  288. break;
  289. }
  290. rep->tsf_offset = WPA_GET_LE16(pos);
  291. rep->beacon_int = WPA_GET_LE16(pos + 2);
  292. rep->tsf_present = 1;
  293. break;
  294. case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
  295. if (elen < 2) {
  296. wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
  297. "country string");
  298. break;
  299. }
  300. os_memcpy(rep->country, pos, 2);
  301. rep->country_present = 1;
  302. break;
  303. case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
  304. if (elen < 1) {
  305. wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
  306. "candidate");
  307. break;
  308. }
  309. rep->preference = pos[0];
  310. rep->preference_present = 1;
  311. break;
  312. case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
  313. rep->bss_term_tsf = WPA_GET_LE64(pos);
  314. rep->bss_term_dur = WPA_GET_LE16(pos + 8);
  315. rep->bss_term_present = 1;
  316. break;
  317. case WNM_NEIGHBOR_BEARING:
  318. if (elen < 8) {
  319. wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
  320. "bearing");
  321. break;
  322. }
  323. rep->bearing = WPA_GET_LE16(pos);
  324. rep->distance = WPA_GET_LE32(pos + 2);
  325. rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
  326. rep->bearing_present = 1;
  327. break;
  328. case WNM_NEIGHBOR_MEASUREMENT_PILOT:
  329. if (elen < 1) {
  330. wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
  331. "pilot");
  332. break;
  333. }
  334. os_free(rep->meas_pilot);
  335. rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
  336. if (rep->meas_pilot == NULL)
  337. break;
  338. rep->meas_pilot->measurement_pilot = pos[0];
  339. rep->meas_pilot->subelem_len = elen - 1;
  340. os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
  341. break;
  342. case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
  343. if (elen < 5) {
  344. wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
  345. "capabilities");
  346. break;
  347. }
  348. os_memcpy(rep->rm_capab, pos, 5);
  349. rep->rm_capab_present = 1;
  350. break;
  351. case WNM_NEIGHBOR_MULTIPLE_BSSID:
  352. if (elen < 1) {
  353. wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
  354. break;
  355. }
  356. os_free(rep->mul_bssid);
  357. rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
  358. if (rep->mul_bssid == NULL)
  359. break;
  360. rep->mul_bssid->max_bssid_indicator = pos[0];
  361. rep->mul_bssid->subelem_len = elen - 1;
  362. os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
  363. break;
  364. }
  365. }
  366. static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
  367. const u8 *pos, u8 len,
  368. struct neighbor_report *rep)
  369. {
  370. u8 left = len;
  371. if (left < 13) {
  372. wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
  373. return;
  374. }
  375. os_memcpy(rep->bssid, pos, ETH_ALEN);
  376. rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
  377. rep->regulatory_class = *(pos + 10);
  378. rep->channel_number = *(pos + 11);
  379. rep->phy_type = *(pos + 12);
  380. pos += 13;
  381. left -= 13;
  382. while (left >= 2) {
  383. u8 id, elen;
  384. id = *pos++;
  385. elen = *pos++;
  386. wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
  387. left -= 2;
  388. if (elen > left) {
  389. wpa_printf(MSG_DEBUG,
  390. "WNM: Truncated neighbor report subelement");
  391. break;
  392. }
  393. wnm_parse_neighbor_report_elem(rep, id, elen, pos);
  394. left -= elen;
  395. pos += elen;
  396. }
  397. }
  398. static int compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
  399. struct wpa_scan_results *scan_res,
  400. struct neighbor_report *neigh_rep,
  401. u8 num_neigh_rep, u8 *bssid_to_connect)
  402. {
  403. u8 i, j;
  404. const u8 *ssid;
  405. struct wpa_bss *bss = wpa_s->current_bss;
  406. if (scan_res == NULL || num_neigh_rep == 0 || !bss)
  407. return 0;
  408. wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
  409. MAC2STR(wpa_s->bssid), bss->level);
  410. for (i = 0; i < num_neigh_rep; i++) {
  411. struct neighbor_report *nei = &neigh_rep[i];
  412. struct wpa_scan_res *res = NULL;
  413. if (nei->preference_present && nei->preference == 0) {
  414. wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
  415. MAC2STR(nei->bssid));
  416. continue;
  417. }
  418. for (j = 0; j < scan_res->num; j++) {
  419. if (os_memcmp(scan_res->res[j]->bssid,
  420. neigh_rep[i].bssid, ETH_ALEN) == 0) {
  421. res = scan_res->res[j];
  422. break;
  423. }
  424. }
  425. if (!res) {
  426. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  427. " (pref %d) not found in scan results",
  428. MAC2STR(nei->bssid),
  429. nei->preference_present ? nei->preference :
  430. -1);
  431. continue;
  432. }
  433. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  434. if (ssid == NULL || bss->ssid_len != ssid[1] ||
  435. os_memcmp(bss->ssid, ssid + 2, ssid[1]) != 0) {
  436. /*
  437. * TODO: Could consider allowing transition to another
  438. * ESS if PMF was enabled for the association.
  439. */
  440. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  441. " (pref %d) in different ESS",
  442. MAC2STR(nei->bssid),
  443. nei->preference_present ? nei->preference :
  444. -1);
  445. continue;
  446. }
  447. if (res->level < bss->level && res->level < -80) {
  448. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  449. " (pref %d) does not have sufficient signal level (%d)",
  450. MAC2STR(nei->bssid),
  451. nei->preference_present ? nei->preference :
  452. -1,
  453. res->level);
  454. continue;
  455. }
  456. wpa_printf(MSG_DEBUG,
  457. "WNM: Found an acceptable prefed transition candidate BSS "
  458. MACSTR " (RSSI %d)",
  459. MAC2STR(nei->bssid), res->level);
  460. os_memcpy(bssid_to_connect, nei->bssid, ETH_ALEN);
  461. return 1;
  462. }
  463. return 0;
  464. }
  465. static void wnm_send_bss_transition_mgmt_resp(
  466. struct wpa_supplicant *wpa_s, u8 dialog_token,
  467. enum bss_trans_mgmt_status_code status, u8 delay,
  468. const u8 *target_bssid)
  469. {
  470. u8 buf[1000], *pos;
  471. struct ieee80211_mgmt *mgmt;
  472. size_t len;
  473. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
  474. "to " MACSTR " dialog_token=%u status=%u delay=%d",
  475. MAC2STR(wpa_s->bssid), dialog_token, status, delay);
  476. mgmt = (struct ieee80211_mgmt *) buf;
  477. os_memset(&buf, 0, sizeof(buf));
  478. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  479. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  480. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  481. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  482. WLAN_FC_STYPE_ACTION);
  483. mgmt->u.action.category = WLAN_ACTION_WNM;
  484. mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
  485. mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
  486. mgmt->u.action.u.bss_tm_resp.status_code = status;
  487. mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
  488. pos = mgmt->u.action.u.bss_tm_resp.variable;
  489. if (target_bssid) {
  490. os_memcpy(pos, target_bssid, ETH_ALEN);
  491. pos += ETH_ALEN;
  492. } else if (status == WNM_BSS_TM_ACCEPT) {
  493. /*
  494. * P802.11-REVmc clarifies that the Target BSSID field is always
  495. * present when status code is zero, so use a fake value here if
  496. * no BSSID is yet known.
  497. */
  498. os_memset(pos, 0, ETH_ALEN);
  499. pos += ETH_ALEN;
  500. }
  501. len = pos - (u8 *) &mgmt->u.action.category;
  502. wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  503. wpa_s->own_addr, wpa_s->bssid,
  504. &mgmt->u.action.category, len, 0);
  505. }
  506. static void wnm_scan_response(struct wpa_supplicant *wpa_s,
  507. struct wpa_scan_results *scan_res)
  508. {
  509. u8 bssid[ETH_ALEN];
  510. if (scan_res == NULL) {
  511. wpa_printf(MSG_ERROR, "Scan result is NULL");
  512. goto send_bss_resp_fail;
  513. }
  514. /* Compare the Neighbor Report and scan results */
  515. if (compare_scan_neighbor_results(wpa_s, scan_res,
  516. wpa_s->wnm_neighbor_report_elements,
  517. wpa_s->wnm_num_neighbor_report,
  518. bssid) == 1) {
  519. /* Associate to the network */
  520. struct wpa_bss *bss;
  521. struct wpa_ssid *ssid = wpa_s->current_ssid;
  522. bss = wpa_bss_get_bssid(wpa_s, bssid);
  523. if (!bss) {
  524. wpa_printf(MSG_DEBUG, "WNM: Target AP not found from "
  525. "BSS table");
  526. goto send_bss_resp_fail;
  527. }
  528. /* Send the BSS Management Response - Accept */
  529. if (wpa_s->wnm_reply) {
  530. wnm_send_bss_transition_mgmt_resp(wpa_s,
  531. wpa_s->wnm_dialog_token,
  532. WNM_BSS_TM_ACCEPT,
  533. 0, bssid);
  534. }
  535. wpa_s->reassociate = 1;
  536. wpa_supplicant_connect(wpa_s, bss, ssid);
  537. wnm_deallocate_memory(wpa_s);
  538. return;
  539. }
  540. /* Send reject response for all the failures */
  541. send_bss_resp_fail:
  542. wnm_deallocate_memory(wpa_s);
  543. if (wpa_s->wnm_reply) {
  544. wnm_send_bss_transition_mgmt_resp(wpa_s,
  545. wpa_s->wnm_dialog_token,
  546. WNM_BSS_TM_REJECT_UNSPECIFIED,
  547. 0, NULL);
  548. }
  549. return;
  550. }
  551. static int cand_pref_compar(const void *a, const void *b)
  552. {
  553. const struct neighbor_report *aa = a;
  554. const struct neighbor_report *bb = b;
  555. if (!aa->preference_present && !bb->preference_present)
  556. return 0;
  557. if (!aa->preference_present)
  558. return 1;
  559. if (!bb->preference_present)
  560. return -1;
  561. if (bb->preference > aa->preference)
  562. return 1;
  563. if (bb->preference < aa->preference)
  564. return -1;
  565. return 0;
  566. }
  567. static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
  568. {
  569. if (!wpa_s->wnm_neighbor_report_elements)
  570. return;
  571. qsort(wpa_s->wnm_neighbor_report_elements,
  572. wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
  573. cand_pref_compar);
  574. }
  575. static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
  576. {
  577. unsigned int i;
  578. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
  579. if (!wpa_s->wnm_neighbor_report_elements)
  580. return;
  581. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  582. struct neighbor_report *nei;
  583. nei = &wpa_s->wnm_neighbor_report_elements[i];
  584. wpa_printf(MSG_DEBUG, "%u: " MACSTR
  585. " info=0x%x op_class=%u chan=%u phy=%u pref=%d",
  586. i, MAC2STR(nei->bssid), nei->bssid_info,
  587. nei->regulatory_class,
  588. nei->channel_number, nei->phy_type,
  589. nei->preference_present ? nei->preference : -1);
  590. }
  591. }
  592. static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
  593. const u8 *pos, const u8 *end,
  594. int reply)
  595. {
  596. unsigned int beacon_int;
  597. u8 valid_int;
  598. if (pos + 5 > end)
  599. return;
  600. if (wpa_s->current_bss)
  601. beacon_int = wpa_s->current_bss->beacon_int;
  602. else
  603. beacon_int = 100; /* best guess */
  604. wpa_s->wnm_dialog_token = pos[0];
  605. wpa_s->wnm_mode = pos[1];
  606. wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
  607. valid_int = pos[4];
  608. wpa_s->wnm_reply = reply;
  609. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
  610. "dialog_token=%u request_mode=0x%x "
  611. "disassoc_timer=%u validity_interval=%u",
  612. wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
  613. wpa_s->wnm_dissoc_timer, valid_int);
  614. pos += 5;
  615. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  616. if (pos + 12 > end) {
  617. wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
  618. return;
  619. }
  620. os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
  621. pos += 12; /* BSS Termination Duration */
  622. }
  623. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
  624. char url[256];
  625. if (pos + 1 > end || pos + 1 + pos[0] > end) {
  626. wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
  627. "Management Request (URL)");
  628. return;
  629. }
  630. os_memcpy(url, pos + 1, pos[0]);
  631. url[pos[0]] = '\0';
  632. pos += 1 + pos[0];
  633. wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
  634. wpa_sm_pmf_enabled(wpa_s->wpa),
  635. wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
  636. }
  637. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  638. wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
  639. "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
  640. if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
  641. /* TODO: mark current BSS less preferred for
  642. * selection */
  643. wpa_printf(MSG_DEBUG, "Trying to find another BSS");
  644. wpa_supplicant_req_scan(wpa_s, 0, 0);
  645. }
  646. }
  647. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
  648. unsigned int valid_ms;
  649. wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
  650. wnm_deallocate_memory(wpa_s);
  651. wpa_s->wnm_neighbor_report_elements = os_zalloc(
  652. WNM_MAX_NEIGHBOR_REPORT *
  653. sizeof(struct neighbor_report));
  654. if (wpa_s->wnm_neighbor_report_elements == NULL)
  655. return;
  656. while (pos + 2 <= end &&
  657. wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
  658. {
  659. u8 tag = *pos++;
  660. u8 len = *pos++;
  661. wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
  662. tag);
  663. if (pos + len > end) {
  664. wpa_printf(MSG_DEBUG, "WNM: Truncated request");
  665. return;
  666. }
  667. if (tag == WLAN_EID_NEIGHBOR_REPORT) {
  668. struct neighbor_report *rep;
  669. rep = &wpa_s->wnm_neighbor_report_elements[
  670. wpa_s->wnm_num_neighbor_report];
  671. wnm_parse_neighbor_report(wpa_s, pos, len, rep);
  672. }
  673. pos += len;
  674. wpa_s->wnm_num_neighbor_report++;
  675. }
  676. wnm_sort_cand_list(wpa_s);
  677. wnm_dump_cand_list(wpa_s);
  678. valid_ms = valid_int * beacon_int * 128 / 125;
  679. wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
  680. valid_ms);
  681. os_get_reltime(&wpa_s->wnm_cand_valid_until);
  682. wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
  683. wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
  684. wpa_s->wnm_cand_valid_until.sec +=
  685. wpa_s->wnm_cand_valid_until.usec / 1000000;
  686. wpa_s->wnm_cand_valid_until.usec %= 1000000;
  687. wpa_s->scan_res_handler = wnm_scan_response;
  688. wpa_supplicant_req_scan(wpa_s, 0, 0);
  689. } else if (reply) {
  690. enum bss_trans_mgmt_status_code status;
  691. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
  692. status = WNM_BSS_TM_ACCEPT;
  693. else {
  694. wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
  695. status = WNM_BSS_TM_REJECT_UNSPECIFIED;
  696. }
  697. wnm_send_bss_transition_mgmt_resp(wpa_s,
  698. wpa_s->wnm_dialog_token,
  699. status, 0, NULL);
  700. }
  701. }
  702. int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
  703. u8 query_reason)
  704. {
  705. u8 buf[1000], *pos;
  706. struct ieee80211_mgmt *mgmt;
  707. size_t len;
  708. int ret;
  709. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
  710. MACSTR " query_reason=%u",
  711. MAC2STR(wpa_s->bssid), query_reason);
  712. mgmt = (struct ieee80211_mgmt *) buf;
  713. os_memset(&buf, 0, sizeof(buf));
  714. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  715. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  716. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  717. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  718. WLAN_FC_STYPE_ACTION);
  719. mgmt->u.action.category = WLAN_ACTION_WNM;
  720. mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
  721. mgmt->u.action.u.bss_tm_query.dialog_token = 1;
  722. mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
  723. pos = mgmt->u.action.u.bss_tm_query.variable;
  724. len = pos - (u8 *) &mgmt->u.action.category;
  725. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  726. wpa_s->own_addr, wpa_s->bssid,
  727. &mgmt->u.action.category, len, 0);
  728. return ret;
  729. }
  730. static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
  731. const u8 *sa, const u8 *data,
  732. int len)
  733. {
  734. const u8 *pos, *end, *next;
  735. u8 ie, ie_len;
  736. pos = data;
  737. end = data + len;
  738. while (pos + 1 < end) {
  739. ie = *pos++;
  740. ie_len = *pos++;
  741. wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
  742. ie, ie_len);
  743. if (ie_len > end - pos) {
  744. wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
  745. "subelement");
  746. break;
  747. }
  748. next = pos + ie_len;
  749. if (ie_len < 4) {
  750. pos = next;
  751. continue;
  752. }
  753. wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
  754. WPA_GET_BE24(pos), pos[3]);
  755. #ifdef CONFIG_HS20
  756. if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
  757. WPA_GET_BE24(pos) == OUI_WFA &&
  758. pos[3] == HS20_WNM_SUB_REM_NEEDED) {
  759. /* Subscription Remediation subelement */
  760. const u8 *ie_end;
  761. u8 url_len;
  762. char *url;
  763. u8 osu_method;
  764. wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
  765. "subelement");
  766. ie_end = pos + ie_len;
  767. pos += 4;
  768. url_len = *pos++;
  769. if (url_len == 0) {
  770. wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
  771. url = NULL;
  772. osu_method = 1;
  773. } else {
  774. if (pos + url_len + 1 > ie_end) {
  775. wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
  776. url_len,
  777. (int) (ie_end - pos));
  778. break;
  779. }
  780. url = os_malloc(url_len + 1);
  781. if (url == NULL)
  782. break;
  783. os_memcpy(url, pos, url_len);
  784. url[url_len] = '\0';
  785. osu_method = pos[url_len];
  786. }
  787. hs20_rx_subscription_remediation(wpa_s, url,
  788. osu_method);
  789. os_free(url);
  790. pos = next;
  791. continue;
  792. }
  793. if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
  794. WPA_GET_BE24(pos) == OUI_WFA &&
  795. pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
  796. const u8 *ie_end;
  797. u8 url_len;
  798. char *url;
  799. u8 code;
  800. u16 reauth_delay;
  801. ie_end = pos + ie_len;
  802. pos += 4;
  803. code = *pos++;
  804. reauth_delay = WPA_GET_LE16(pos);
  805. pos += 2;
  806. url_len = *pos++;
  807. wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
  808. "Imminent - Reason Code %u "
  809. "Re-Auth Delay %u URL Length %u",
  810. code, reauth_delay, url_len);
  811. if (pos + url_len > ie_end)
  812. break;
  813. url = os_malloc(url_len + 1);
  814. if (url == NULL)
  815. break;
  816. os_memcpy(url, pos, url_len);
  817. url[url_len] = '\0';
  818. hs20_rx_deauth_imminent_notice(wpa_s, code,
  819. reauth_delay, url);
  820. os_free(url);
  821. pos = next;
  822. continue;
  823. }
  824. #endif /* CONFIG_HS20 */
  825. pos = next;
  826. }
  827. }
  828. static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
  829. const u8 *sa, const u8 *frm, int len)
  830. {
  831. const u8 *pos, *end;
  832. u8 dialog_token, type;
  833. /* Dialog Token [1] | Type [1] | Subelements */
  834. if (len < 2 || sa == NULL)
  835. return;
  836. end = frm + len;
  837. pos = frm;
  838. dialog_token = *pos++;
  839. type = *pos++;
  840. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
  841. "(dialog_token %u type %u sa " MACSTR ")",
  842. dialog_token, type, MAC2STR(sa));
  843. wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
  844. pos, end - pos);
  845. if (wpa_s->wpa_state != WPA_COMPLETED ||
  846. os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
  847. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
  848. "from our AP - ignore it");
  849. return;
  850. }
  851. switch (type) {
  852. case 1:
  853. ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
  854. break;
  855. default:
  856. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
  857. "WNM-Notification type %u", type);
  858. break;
  859. }
  860. }
  861. void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
  862. const struct ieee80211_mgmt *mgmt, size_t len)
  863. {
  864. const u8 *pos, *end;
  865. u8 act;
  866. if (len < IEEE80211_HDRLEN + 2)
  867. return;
  868. pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
  869. act = *pos++;
  870. end = ((const u8 *) mgmt) + len;
  871. wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
  872. act, MAC2STR(mgmt->sa));
  873. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  874. os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
  875. wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
  876. "frame");
  877. return;
  878. }
  879. switch (act) {
  880. case WNM_BSS_TRANS_MGMT_REQ:
  881. ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
  882. !(mgmt->da[0] & 0x01));
  883. break;
  884. case WNM_SLEEP_MODE_RESP:
  885. ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
  886. break;
  887. case WNM_NOTIFICATION_REQ:
  888. ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
  889. break;
  890. default:
  891. wpa_printf(MSG_ERROR, "WNM: Unknown request");
  892. break;
  893. }
  894. }