wnm_sta.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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].tsf_info);
  275. os_free(wpa_s->wnm_neighbor_report_elements[i].con_coun_str);
  276. os_free(wpa_s->wnm_neighbor_report_elements[i].bss_tran_can);
  277. os_free(wpa_s->wnm_neighbor_report_elements[i].bss_term_dur);
  278. os_free(wpa_s->wnm_neighbor_report_elements[i].bearing);
  279. os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
  280. os_free(wpa_s->wnm_neighbor_report_elements[i].rrm_cap);
  281. os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
  282. }
  283. wpa_s->wnm_num_neighbor_report = 0;
  284. os_free(wpa_s->wnm_neighbor_report_elements);
  285. wpa_s->wnm_neighbor_report_elements = NULL;
  286. }
  287. static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
  288. u8 id, u8 elen, const u8 *pos)
  289. {
  290. switch (id) {
  291. case WNM_NEIGHBOR_TSF:
  292. if (elen < 2 + 2) {
  293. wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
  294. break;
  295. }
  296. os_free(rep->tsf_info);
  297. rep->tsf_info = os_zalloc(sizeof(struct tsf_info));
  298. if (rep->tsf_info == NULL)
  299. break;
  300. rep->tsf_info->present = 1;
  301. os_memcpy(rep->tsf_info->tsf_offset, pos, 2);
  302. os_memcpy(rep->tsf_info->beacon_interval, pos + 2, 2);
  303. break;
  304. case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
  305. if (elen < 2) {
  306. wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
  307. "country string");
  308. break;
  309. }
  310. os_free(rep->con_coun_str);
  311. rep->con_coun_str =
  312. os_zalloc(sizeof(struct condensed_country_string));
  313. if (rep->con_coun_str == NULL)
  314. break;
  315. rep->con_coun_str->present = 1;
  316. os_memcpy(rep->con_coun_str->country_string, pos, 2);
  317. break;
  318. case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
  319. if (elen < 1) {
  320. wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
  321. "candidate");
  322. break;
  323. }
  324. os_free(rep->bss_tran_can);
  325. rep->bss_tran_can =
  326. os_zalloc(sizeof(struct bss_transition_candidate));
  327. if (rep->bss_tran_can == NULL)
  328. break;
  329. rep->bss_tran_can->present = 1;
  330. rep->bss_tran_can->preference = pos[0];
  331. break;
  332. case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
  333. if (elen < 12) {
  334. wpa_printf(MSG_DEBUG, "WNM: Too short BSS termination "
  335. "duration");
  336. break;
  337. }
  338. os_free(rep->bss_term_dur);
  339. rep->bss_term_dur =
  340. os_zalloc(sizeof(struct bss_termination_duration));
  341. if (rep->bss_term_dur == NULL)
  342. break;
  343. rep->bss_term_dur->present = 1;
  344. os_memcpy(rep->bss_term_dur->duration, pos, 12);
  345. break;
  346. case WNM_NEIGHBOR_BEARING:
  347. if (elen < 8) {
  348. wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
  349. "bearing");
  350. break;
  351. }
  352. os_free(rep->bearing);
  353. rep->bearing = os_zalloc(sizeof(struct bearing));
  354. if (rep->bearing == NULL)
  355. break;
  356. rep->bearing->present = 1;
  357. os_memcpy(rep->bearing->bearing, pos, 8);
  358. break;
  359. case WNM_NEIGHBOR_MEASUREMENT_PILOT:
  360. if (elen < 2) {
  361. wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
  362. "pilot");
  363. break;
  364. }
  365. os_free(rep->meas_pilot);
  366. rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
  367. if (rep->meas_pilot == NULL)
  368. break;
  369. rep->meas_pilot->present = 1;
  370. rep->meas_pilot->measurement_pilot = pos[0];
  371. rep->meas_pilot->num_vendor_specific = pos[1];
  372. os_memcpy(rep->meas_pilot->vendor_specific, pos + 2, elen - 2);
  373. break;
  374. case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
  375. if (elen < 4) {
  376. wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
  377. "capabilities");
  378. break;
  379. }
  380. os_free(rep->rrm_cap);
  381. rep->rrm_cap =
  382. os_zalloc(sizeof(struct rrm_enabled_capabilities));
  383. if (rep->rrm_cap == NULL)
  384. break;
  385. rep->rrm_cap->present = 1;
  386. os_memcpy(rep->rrm_cap->capabilities, pos, 4);
  387. break;
  388. case WNM_NEIGHBOR_MULTIPLE_BSSID:
  389. if (elen < 2) {
  390. wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
  391. break;
  392. }
  393. os_free(rep->mul_bssid);
  394. rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
  395. if (rep->mul_bssid == NULL)
  396. break;
  397. rep->mul_bssid->present = 1;
  398. rep->mul_bssid->max_bssid_indicator = pos[0];
  399. rep->mul_bssid->num_vendor_specific = pos[1];
  400. os_memcpy(rep->mul_bssid->vendor_specific, pos + 2, elen - 2);
  401. break;
  402. }
  403. }
  404. static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
  405. const u8 *pos, u8 len,
  406. struct neighbor_report *rep)
  407. {
  408. u8 left = len;
  409. if (left < 13) {
  410. wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
  411. return;
  412. }
  413. os_memcpy(rep->bssid, pos, ETH_ALEN);
  414. os_memcpy(rep->bssid_information, pos + ETH_ALEN, 4);
  415. rep->regulatory_class = *(pos + 10);
  416. rep->channel_number = *(pos + 11);
  417. rep->phy_type = *(pos + 12);
  418. pos += 13;
  419. left -= 13;
  420. while (left >= 2) {
  421. u8 id, elen;
  422. id = *pos++;
  423. elen = *pos++;
  424. wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
  425. left -= 2;
  426. if (elen > left) {
  427. wpa_printf(MSG_DEBUG,
  428. "WNM: Truncated neighbor report subelement");
  429. break;
  430. }
  431. wnm_parse_neighbor_report_elem(rep, id, elen, pos);
  432. left -= elen;
  433. pos += elen;
  434. }
  435. }
  436. static int compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
  437. struct wpa_scan_results *scan_res,
  438. struct neighbor_report *neigh_rep,
  439. u8 num_neigh_rep, u8 *bssid_to_connect)
  440. {
  441. u8 i, j;
  442. if (scan_res == NULL || num_neigh_rep == 0 || !wpa_s->current_bss)
  443. return 0;
  444. wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
  445. MAC2STR(wpa_s->bssid), wpa_s->current_bss->level);
  446. for (i = 0; i < num_neigh_rep; i++) {
  447. for (j = 0; j < scan_res->num; j++) {
  448. /* Check for a better RSSI AP */
  449. if (os_memcmp(scan_res->res[j]->bssid,
  450. neigh_rep[i].bssid, ETH_ALEN) == 0 &&
  451. scan_res->res[j]->level >
  452. wpa_s->current_bss->level) {
  453. /* Got a BSSID with better RSSI value */
  454. os_memcpy(bssid_to_connect, neigh_rep[i].bssid,
  455. ETH_ALEN);
  456. wpa_printf(MSG_DEBUG, "Found a BSS " MACSTR
  457. " with better scan RSSI %d",
  458. MAC2STR(scan_res->res[j]->bssid),
  459. scan_res->res[j]->level);
  460. return 1;
  461. }
  462. wpa_printf(MSG_DEBUG, "scan_res[%d] " MACSTR
  463. " RSSI %d", j,
  464. MAC2STR(scan_res->res[j]->bssid),
  465. scan_res->res[j]->level);
  466. }
  467. }
  468. return 0;
  469. }
  470. static void wnm_send_bss_transition_mgmt_resp(
  471. struct wpa_supplicant *wpa_s, u8 dialog_token,
  472. enum bss_trans_mgmt_status_code status, u8 delay,
  473. const u8 *target_bssid)
  474. {
  475. u8 buf[1000], *pos;
  476. struct ieee80211_mgmt *mgmt;
  477. size_t len;
  478. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
  479. "to " MACSTR " dialog_token=%u status=%u delay=%d",
  480. MAC2STR(wpa_s->bssid), dialog_token, status, delay);
  481. mgmt = (struct ieee80211_mgmt *) buf;
  482. os_memset(&buf, 0, sizeof(buf));
  483. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  484. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  485. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  486. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  487. WLAN_FC_STYPE_ACTION);
  488. mgmt->u.action.category = WLAN_ACTION_WNM;
  489. mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
  490. mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
  491. mgmt->u.action.u.bss_tm_resp.status_code = status;
  492. mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
  493. pos = mgmt->u.action.u.bss_tm_resp.variable;
  494. if (target_bssid) {
  495. os_memcpy(pos, target_bssid, ETH_ALEN);
  496. pos += ETH_ALEN;
  497. } else if (status == WNM_BSS_TM_ACCEPT) {
  498. /*
  499. * P802.11-REVmc clarifies that the Target BSSID field is always
  500. * present when status code is zero, so use a fake value here if
  501. * no BSSID is yet known.
  502. */
  503. os_memset(pos, 0, ETH_ALEN);
  504. pos += ETH_ALEN;
  505. }
  506. len = pos - (u8 *) &mgmt->u.action.category;
  507. wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  508. wpa_s->own_addr, wpa_s->bssid,
  509. &mgmt->u.action.category, len, 0);
  510. }
  511. void wnm_scan_response(struct wpa_supplicant *wpa_s,
  512. struct wpa_scan_results *scan_res)
  513. {
  514. u8 bssid[ETH_ALEN];
  515. if (scan_res == NULL) {
  516. wpa_printf(MSG_ERROR, "Scan result is NULL");
  517. goto send_bss_resp_fail;
  518. }
  519. /* Compare the Neighbor Report and scan results */
  520. if (compare_scan_neighbor_results(wpa_s, scan_res,
  521. wpa_s->wnm_neighbor_report_elements,
  522. wpa_s->wnm_num_neighbor_report,
  523. bssid) == 1) {
  524. /* Associate to the network */
  525. struct wpa_bss *bss;
  526. struct wpa_ssid *ssid = wpa_s->current_ssid;
  527. bss = wpa_bss_get_bssid(wpa_s, bssid);
  528. if (!bss) {
  529. wpa_printf(MSG_DEBUG, "WNM: Target AP not found from "
  530. "BSS table");
  531. goto send_bss_resp_fail;
  532. }
  533. /* Send the BSS Management Response - Accept */
  534. if (wpa_s->wnm_reply) {
  535. wnm_send_bss_transition_mgmt_resp(wpa_s,
  536. wpa_s->wnm_dialog_token,
  537. WNM_BSS_TM_ACCEPT,
  538. 0, bssid);
  539. }
  540. wpa_s->reassociate = 1;
  541. wpa_supplicant_connect(wpa_s, bss, ssid);
  542. wnm_deallocate_memory(wpa_s);
  543. return;
  544. }
  545. /* Send reject response for all the failures */
  546. send_bss_resp_fail:
  547. wnm_deallocate_memory(wpa_s);
  548. if (wpa_s->wnm_reply) {
  549. wnm_send_bss_transition_mgmt_resp(wpa_s,
  550. wpa_s->wnm_dialog_token,
  551. WNM_BSS_TM_REJECT_UNSPECIFIED,
  552. 0, NULL);
  553. }
  554. return;
  555. }
  556. static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
  557. const u8 *pos, const u8 *end,
  558. int reply)
  559. {
  560. if (pos + 5 > end)
  561. return;
  562. wpa_s->wnm_dialog_token = pos[0];
  563. wpa_s->wnm_mode = pos[1];
  564. wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
  565. wpa_s->wnm_validity_interval = pos[4];
  566. wpa_s->wnm_reply = reply;
  567. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
  568. "dialog_token=%u request_mode=0x%x "
  569. "disassoc_timer=%u validity_interval=%u",
  570. wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
  571. wpa_s->wnm_dissoc_timer, wpa_s->wnm_validity_interval);
  572. pos += 5;
  573. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  574. if (pos + 12 > end) {
  575. wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
  576. return;
  577. }
  578. os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
  579. pos += 12; /* BSS Termination Duration */
  580. }
  581. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
  582. char url[256];
  583. unsigned int beacon_int;
  584. if (pos + 1 > end || pos + 1 + pos[0] > end) {
  585. wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
  586. "Management Request (URL)");
  587. return;
  588. }
  589. os_memcpy(url, pos + 1, pos[0]);
  590. url[pos[0]] = '\0';
  591. pos += 1 + pos[0];
  592. if (wpa_s->current_bss)
  593. beacon_int = wpa_s->current_bss->beacon_int;
  594. else
  595. beacon_int = 100; /* best guess */
  596. wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
  597. wpa_sm_pmf_enabled(wpa_s->wpa),
  598. wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
  599. }
  600. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  601. wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
  602. "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
  603. if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
  604. /* TODO: mark current BSS less preferred for
  605. * selection */
  606. wpa_printf(MSG_DEBUG, "Trying to find another BSS");
  607. wpa_supplicant_req_scan(wpa_s, 0, 0);
  608. }
  609. }
  610. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
  611. wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
  612. wpa_s->wnm_num_neighbor_report = 0;
  613. os_free(wpa_s->wnm_neighbor_report_elements);
  614. wpa_s->wnm_neighbor_report_elements = os_zalloc(
  615. WNM_MAX_NEIGHBOR_REPORT *
  616. sizeof(struct neighbor_report));
  617. if (wpa_s->wnm_neighbor_report_elements == NULL)
  618. return;
  619. while (pos + 2 <= end &&
  620. wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
  621. {
  622. u8 tag = *pos++;
  623. u8 len = *pos++;
  624. wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
  625. tag);
  626. if (pos + len > end) {
  627. wpa_printf(MSG_DEBUG, "WNM: Truncated request");
  628. return;
  629. }
  630. if (tag == WLAN_EID_NEIGHBOR_REPORT) {
  631. struct neighbor_report *rep;
  632. rep = &wpa_s->wnm_neighbor_report_elements[
  633. wpa_s->wnm_num_neighbor_report];
  634. wnm_parse_neighbor_report(wpa_s, pos, len, rep);
  635. }
  636. pos += len;
  637. wpa_s->wnm_num_neighbor_report++;
  638. }
  639. wpa_s->scan_res_handler = wnm_scan_response;
  640. wpa_supplicant_req_scan(wpa_s, 0, 0);
  641. } else if (reply) {
  642. enum bss_trans_mgmt_status_code status;
  643. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
  644. status = WNM_BSS_TM_ACCEPT;
  645. else {
  646. wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
  647. status = WNM_BSS_TM_REJECT_UNSPECIFIED;
  648. }
  649. wnm_send_bss_transition_mgmt_resp(wpa_s,
  650. wpa_s->wnm_dialog_token,
  651. status, 0, NULL);
  652. }
  653. }
  654. int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
  655. u8 query_reason)
  656. {
  657. u8 buf[1000], *pos;
  658. struct ieee80211_mgmt *mgmt;
  659. size_t len;
  660. int ret;
  661. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
  662. MACSTR " query_reason=%u",
  663. MAC2STR(wpa_s->bssid), query_reason);
  664. mgmt = (struct ieee80211_mgmt *) buf;
  665. os_memset(&buf, 0, sizeof(buf));
  666. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  667. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  668. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  669. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  670. WLAN_FC_STYPE_ACTION);
  671. mgmt->u.action.category = WLAN_ACTION_WNM;
  672. mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
  673. mgmt->u.action.u.bss_tm_query.dialog_token = 1;
  674. mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
  675. pos = mgmt->u.action.u.bss_tm_query.variable;
  676. len = pos - (u8 *) &mgmt->u.action.category;
  677. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  678. wpa_s->own_addr, wpa_s->bssid,
  679. &mgmt->u.action.category, len, 0);
  680. return ret;
  681. }
  682. static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
  683. const u8 *sa, const u8 *data,
  684. int len)
  685. {
  686. const u8 *pos, *end, *next;
  687. u8 ie, ie_len;
  688. pos = data;
  689. end = data + len;
  690. while (pos + 1 < end) {
  691. ie = *pos++;
  692. ie_len = *pos++;
  693. wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
  694. ie, ie_len);
  695. if (ie_len > end - pos) {
  696. wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
  697. "subelement");
  698. break;
  699. }
  700. next = pos + ie_len;
  701. if (ie_len < 4) {
  702. pos = next;
  703. continue;
  704. }
  705. wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
  706. WPA_GET_BE24(pos), pos[3]);
  707. #ifdef CONFIG_HS20
  708. if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
  709. WPA_GET_BE24(pos) == OUI_WFA &&
  710. pos[3] == HS20_WNM_SUB_REM_NEEDED) {
  711. /* Subscription Remediation subelement */
  712. const u8 *ie_end;
  713. u8 url_len;
  714. char *url;
  715. u8 osu_method;
  716. wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
  717. "subelement");
  718. ie_end = pos + ie_len;
  719. pos += 4;
  720. url_len = *pos++;
  721. if (url_len == 0) {
  722. wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
  723. url = NULL;
  724. osu_method = 1;
  725. } else {
  726. if (pos + url_len + 1 > ie_end) {
  727. wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
  728. url_len,
  729. (int) (ie_end - pos));
  730. break;
  731. }
  732. url = os_malloc(url_len + 1);
  733. if (url == NULL)
  734. break;
  735. os_memcpy(url, pos, url_len);
  736. url[url_len] = '\0';
  737. osu_method = pos[url_len];
  738. }
  739. hs20_rx_subscription_remediation(wpa_s, url,
  740. osu_method);
  741. os_free(url);
  742. pos = next;
  743. continue;
  744. }
  745. if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
  746. WPA_GET_BE24(pos) == OUI_WFA &&
  747. pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
  748. const u8 *ie_end;
  749. u8 url_len;
  750. char *url;
  751. u8 code;
  752. u16 reauth_delay;
  753. ie_end = pos + ie_len;
  754. pos += 4;
  755. code = *pos++;
  756. reauth_delay = WPA_GET_LE16(pos);
  757. pos += 2;
  758. url_len = *pos++;
  759. wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
  760. "Imminent - Reason Code %u "
  761. "Re-Auth Delay %u URL Length %u",
  762. code, reauth_delay, url_len);
  763. if (pos + url_len > ie_end)
  764. break;
  765. url = os_malloc(url_len + 1);
  766. if (url == NULL)
  767. break;
  768. os_memcpy(url, pos, url_len);
  769. url[url_len] = '\0';
  770. hs20_rx_deauth_imminent_notice(wpa_s, code,
  771. reauth_delay, url);
  772. os_free(url);
  773. pos = next;
  774. continue;
  775. }
  776. #endif /* CONFIG_HS20 */
  777. pos = next;
  778. }
  779. }
  780. static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
  781. const u8 *sa, const u8 *frm, int len)
  782. {
  783. const u8 *pos, *end;
  784. u8 dialog_token, type;
  785. /* Dialog Token [1] | Type [1] | Subelements */
  786. if (len < 2 || sa == NULL)
  787. return;
  788. end = frm + len;
  789. pos = frm;
  790. dialog_token = *pos++;
  791. type = *pos++;
  792. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
  793. "(dialog_token %u type %u sa " MACSTR ")",
  794. dialog_token, type, MAC2STR(sa));
  795. wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
  796. pos, end - pos);
  797. if (wpa_s->wpa_state != WPA_COMPLETED ||
  798. os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
  799. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
  800. "from our AP - ignore it");
  801. return;
  802. }
  803. switch (type) {
  804. case 1:
  805. ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
  806. break;
  807. default:
  808. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
  809. "WNM-Notification type %u", type);
  810. break;
  811. }
  812. }
  813. void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
  814. const struct ieee80211_mgmt *mgmt, size_t len)
  815. {
  816. const u8 *pos, *end;
  817. u8 act;
  818. if (len < IEEE80211_HDRLEN + 2)
  819. return;
  820. pos = &mgmt->u.action.category;
  821. pos++;
  822. act = *pos++;
  823. end = ((const u8 *) mgmt) + len;
  824. wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
  825. act, MAC2STR(mgmt->sa));
  826. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  827. os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
  828. wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
  829. "frame");
  830. return;
  831. }
  832. switch (act) {
  833. case WNM_BSS_TRANS_MGMT_REQ:
  834. ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
  835. !(mgmt->da[0] & 0x01));
  836. break;
  837. case WNM_SLEEP_MODE_RESP:
  838. ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
  839. break;
  840. case WNM_NOTIFICATION_REQ:
  841. ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
  842. break;
  843. default:
  844. wpa_printf(MSG_ERROR, "WNM: Unknown request");
  845. break;
  846. }
  847. }