wnm_sta.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. #define MAX_TFS_IE_LEN 1024
  20. #define WNM_MAX_NEIGHBOR_REPORT 10
  21. /* get the TFS IE from driver */
  22. static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
  23. u16 *buf_len, enum wnm_oper oper)
  24. {
  25. wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
  26. return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
  27. }
  28. /* set the TFS IE to driver */
  29. static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
  30. const u8 *addr, u8 *buf, u16 *buf_len,
  31. enum wnm_oper oper)
  32. {
  33. wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
  34. return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
  35. }
  36. /* MLME-SLEEPMODE.request */
  37. int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
  38. u8 action, u16 intval, struct wpabuf *tfs_req)
  39. {
  40. struct ieee80211_mgmt *mgmt;
  41. int res;
  42. size_t len;
  43. struct wnm_sleep_element *wnmsleep_ie;
  44. u8 *wnmtfs_ie;
  45. u8 wnmsleep_ie_len;
  46. u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
  47. enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
  48. WNM_SLEEP_TFS_REQ_IE_NONE;
  49. wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
  50. "action=%s to " MACSTR,
  51. action == 0 ? "enter" : "exit",
  52. MAC2STR(wpa_s->bssid));
  53. /* WNM-Sleep Mode IE */
  54. wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
  55. wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
  56. if (wnmsleep_ie == NULL)
  57. return -1;
  58. wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
  59. wnmsleep_ie->len = wnmsleep_ie_len - 2;
  60. wnmsleep_ie->action_type = action;
  61. wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
  62. wnmsleep_ie->intval = host_to_le16(intval);
  63. wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
  64. (u8 *) wnmsleep_ie, wnmsleep_ie_len);
  65. /* TFS IE(s) */
  66. if (tfs_req) {
  67. wnmtfs_ie_len = wpabuf_len(tfs_req);
  68. wnmtfs_ie = os_malloc(wnmtfs_ie_len);
  69. if (wnmtfs_ie == NULL) {
  70. os_free(wnmsleep_ie);
  71. return -1;
  72. }
  73. os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
  74. } else {
  75. wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
  76. if (wnmtfs_ie == NULL) {
  77. os_free(wnmsleep_ie);
  78. return -1;
  79. }
  80. if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
  81. tfs_oper)) {
  82. wnmtfs_ie_len = 0;
  83. os_free(wnmtfs_ie);
  84. wnmtfs_ie = NULL;
  85. }
  86. }
  87. wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
  88. (u8 *) wnmtfs_ie, wnmtfs_ie_len);
  89. mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
  90. if (mgmt == NULL) {
  91. wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
  92. "WNM-Sleep Request action frame");
  93. os_free(wnmsleep_ie);
  94. os_free(wnmtfs_ie);
  95. return -1;
  96. }
  97. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  98. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  99. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  100. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  101. WLAN_FC_STYPE_ACTION);
  102. mgmt->u.action.category = WLAN_ACTION_WNM;
  103. mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
  104. mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
  105. os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
  106. wnmsleep_ie_len);
  107. /* copy TFS IE here */
  108. if (wnmtfs_ie_len > 0) {
  109. os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
  110. wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
  111. }
  112. len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
  113. wnmtfs_ie_len;
  114. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  115. wpa_s->own_addr, wpa_s->bssid,
  116. &mgmt->u.action.category, len, 0);
  117. if (res < 0)
  118. wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
  119. "(action=%d, intval=%d)", action, intval);
  120. os_free(wnmsleep_ie);
  121. os_free(wnmtfs_ie);
  122. os_free(mgmt);
  123. return res;
  124. }
  125. static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
  126. u8 *tfsresp_ie_start,
  127. u8 *tfsresp_ie_end)
  128. {
  129. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
  130. wpa_s->bssid, NULL, NULL);
  131. /* remove GTK/IGTK ?? */
  132. /* set the TFS Resp IE(s) */
  133. if (tfsresp_ie_start && tfsresp_ie_end &&
  134. tfsresp_ie_end - tfsresp_ie_start >= 0) {
  135. u16 tfsresp_ie_len;
  136. tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
  137. tfsresp_ie_start;
  138. wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
  139. /* pass the TFS Resp IE(s) to driver for processing */
  140. if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
  141. tfsresp_ie_start,
  142. &tfsresp_ie_len,
  143. WNM_SLEEP_TFS_RESP_IE_SET))
  144. wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
  145. }
  146. }
  147. static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
  148. const u8 *frm, u16 key_len_total)
  149. {
  150. u8 *ptr, *end;
  151. u8 gtk_len;
  152. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
  153. NULL, NULL);
  154. /* Install GTK/IGTK */
  155. /* point to key data field */
  156. ptr = (u8 *) frm + 1 + 1 + 2;
  157. end = ptr + key_len_total;
  158. wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
  159. while (ptr + 1 < end) {
  160. if (ptr + 2 + ptr[1] > end) {
  161. wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
  162. "length");
  163. if (end > ptr) {
  164. wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
  165. ptr, end - ptr);
  166. }
  167. break;
  168. }
  169. if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
  170. if (ptr[1] < 11 + 5) {
  171. wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
  172. "subelem");
  173. break;
  174. }
  175. gtk_len = *(ptr + 4);
  176. if (ptr[1] < 11 + gtk_len ||
  177. gtk_len < 5 || gtk_len > 32) {
  178. wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
  179. "subelem");
  180. break;
  181. }
  182. wpa_wnmsleep_install_key(
  183. wpa_s->wpa,
  184. WNM_SLEEP_SUBELEM_GTK,
  185. ptr);
  186. ptr += 13 + gtk_len;
  187. #ifdef CONFIG_IEEE80211W
  188. } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
  189. if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
  190. wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
  191. "subelem");
  192. break;
  193. }
  194. wpa_wnmsleep_install_key(wpa_s->wpa,
  195. WNM_SLEEP_SUBELEM_IGTK, ptr);
  196. ptr += 10 + WPA_IGTK_LEN;
  197. #endif /* CONFIG_IEEE80211W */
  198. } else
  199. break; /* skip the loop */
  200. }
  201. }
  202. static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
  203. const u8 *frm, int len)
  204. {
  205. /*
  206. * Action [1] | Diaglog Token [1] | Key Data Len [2] | Key Data |
  207. * WNM-Sleep Mode IE | TFS Response IE
  208. */
  209. u8 *pos = (u8 *) frm; /* point to action field */
  210. u16 key_len_total = le_to_host16(*((u16 *)(frm+2)));
  211. struct wnm_sleep_element *wnmsleep_ie = NULL;
  212. /* multiple TFS Resp IE (assuming consecutive) */
  213. u8 *tfsresp_ie_start = NULL;
  214. u8 *tfsresp_ie_end = NULL;
  215. wpa_printf(MSG_DEBUG, "action=%d token = %d key_len_total = %d",
  216. frm[0], frm[1], key_len_total);
  217. pos += 4 + key_len_total;
  218. if (pos > frm + len) {
  219. wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
  220. return;
  221. }
  222. while (pos - frm < len) {
  223. u8 ie_len = *(pos + 1);
  224. if (pos + 2 + ie_len > frm + len) {
  225. wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
  226. break;
  227. }
  228. wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
  229. if (*pos == WLAN_EID_WNMSLEEP)
  230. wnmsleep_ie = (struct wnm_sleep_element *) pos;
  231. else if (*pos == WLAN_EID_TFS_RESP) {
  232. if (!tfsresp_ie_start)
  233. tfsresp_ie_start = pos;
  234. tfsresp_ie_end = pos;
  235. } else
  236. wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
  237. pos += ie_len + 2;
  238. }
  239. if (!wnmsleep_ie) {
  240. wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
  241. return;
  242. }
  243. if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
  244. wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
  245. wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
  246. "frame (action=%d, intval=%d)",
  247. wnmsleep_ie->action_type, wnmsleep_ie->intval);
  248. if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
  249. wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
  250. tfsresp_ie_end);
  251. } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
  252. wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
  253. }
  254. } else {
  255. wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
  256. "(action=%d, intval=%d)",
  257. wnmsleep_ie->action_type, wnmsleep_ie->intval);
  258. if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
  259. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
  260. wpa_s->bssid, NULL, NULL);
  261. else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
  262. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
  263. wpa_s->bssid, NULL, NULL);
  264. }
  265. }
  266. void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
  267. {
  268. int i;
  269. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  270. os_free(wpa_s->wnm_neighbor_report_elements[i].tsf_info);
  271. os_free(wpa_s->wnm_neighbor_report_elements[i].con_coun_str);
  272. os_free(wpa_s->wnm_neighbor_report_elements[i].bss_tran_can);
  273. os_free(wpa_s->wnm_neighbor_report_elements[i].bss_term_dur);
  274. os_free(wpa_s->wnm_neighbor_report_elements[i].bearing);
  275. os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
  276. os_free(wpa_s->wnm_neighbor_report_elements[i].rrm_cap);
  277. os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
  278. }
  279. os_free(wpa_s->wnm_neighbor_report_elements);
  280. wpa_s->wnm_neighbor_report_elements = NULL;
  281. }
  282. static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
  283. u8 id, u8 elen, const u8 *pos)
  284. {
  285. switch (id) {
  286. case WNM_NEIGHBOR_TSF:
  287. if (elen < 2 + 2) {
  288. wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
  289. break;
  290. }
  291. rep->tsf_info = os_zalloc(sizeof(struct tsf_info));
  292. if (rep->tsf_info == NULL)
  293. break;
  294. rep->tsf_info->present = 1;
  295. os_memcpy(rep->tsf_info->tsf_offset, pos, 2);
  296. os_memcpy(rep->tsf_info->beacon_interval, pos + 2, 2);
  297. break;
  298. case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
  299. if (elen < 2) {
  300. wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
  301. "country string");
  302. break;
  303. }
  304. rep->con_coun_str =
  305. os_zalloc(sizeof(struct condensed_country_string));
  306. if (rep->con_coun_str == NULL)
  307. break;
  308. rep->con_coun_str->present = 1;
  309. os_memcpy(rep->con_coun_str->country_string, pos, 2);
  310. break;
  311. case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
  312. if (elen < 1) {
  313. wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
  314. "candidate");
  315. break;
  316. }
  317. rep->bss_tran_can =
  318. os_zalloc(sizeof(struct bss_transition_candidate));
  319. if (rep->bss_tran_can == NULL)
  320. break;
  321. rep->bss_tran_can->present = 1;
  322. rep->bss_tran_can->preference = pos[0];
  323. break;
  324. case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
  325. if (elen < 12) {
  326. wpa_printf(MSG_DEBUG, "WNM: Too short BSS termination "
  327. "duration");
  328. break;
  329. }
  330. rep->bss_term_dur =
  331. os_zalloc(sizeof(struct bss_termination_duration));
  332. if (rep->bss_term_dur == NULL)
  333. break;
  334. rep->bss_term_dur->present = 1;
  335. os_memcpy(rep->bss_term_dur->duration, pos, 12);
  336. break;
  337. case WNM_NEIGHBOR_BEARING:
  338. if (elen < 8) {
  339. wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
  340. "bearing");
  341. break;
  342. }
  343. rep->bearing = os_zalloc(sizeof(struct bearing));
  344. if (rep->bearing == NULL)
  345. break;
  346. rep->bearing->present = 1;
  347. os_memcpy(rep->bearing->bearing, pos, 8);
  348. break;
  349. case WNM_NEIGHBOR_MEASUREMENT_PILOT:
  350. if (elen < 2) {
  351. wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
  352. "pilot");
  353. break;
  354. }
  355. rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
  356. if (rep->meas_pilot == NULL)
  357. break;
  358. rep->meas_pilot->present = 1;
  359. rep->meas_pilot->measurement_pilot = pos[0];
  360. rep->meas_pilot->num_vendor_specific = pos[1];
  361. os_memcpy(rep->meas_pilot->vendor_specific, pos + 2, elen - 2);
  362. break;
  363. case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
  364. if (elen < 4) {
  365. wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
  366. "capabilities");
  367. break;
  368. }
  369. rep->rrm_cap =
  370. os_zalloc(sizeof(struct rrm_enabled_capabilities));
  371. if (rep->rrm_cap == NULL)
  372. break;
  373. rep->rrm_cap->present = 1;
  374. os_memcpy(rep->rrm_cap->capabilities, pos, 4);
  375. break;
  376. case WNM_NEIGHBOR_MULTIPLE_BSSID:
  377. if (elen < 2) {
  378. wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
  379. break;
  380. }
  381. rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
  382. if (rep->mul_bssid == NULL)
  383. break;
  384. rep->mul_bssid->present = 1;
  385. rep->mul_bssid->max_bssid_indicator = pos[0];
  386. rep->mul_bssid->num_vendor_specific = pos[1];
  387. os_memcpy(rep->mul_bssid->vendor_specific, pos + 2, elen - 2);
  388. break;
  389. }
  390. }
  391. static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
  392. const u8 *pos, u8 len,
  393. struct neighbor_report *rep)
  394. {
  395. u8 left = len;
  396. if (left < 13) {
  397. wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
  398. return;
  399. }
  400. os_memcpy(rep->bssid, pos, ETH_ALEN);
  401. os_memcpy(rep->bssid_information, pos + ETH_ALEN, 4);
  402. rep->regulatory_class = *(pos + 10);
  403. rep->channel_number = *(pos + 11);
  404. rep->phy_type = *(pos + 12);
  405. pos += 13;
  406. left -= 13;
  407. while (left >= 2) {
  408. u8 id, elen;
  409. id = *pos++;
  410. elen = *pos++;
  411. wnm_parse_neighbor_report_elem(rep, id, elen, pos);
  412. left -= 2 + elen;
  413. pos += elen;
  414. }
  415. }
  416. static int compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
  417. struct wpa_scan_results *scan_res,
  418. struct neighbor_report *neigh_rep,
  419. u8 num_neigh_rep, u8 *bssid_to_connect)
  420. {
  421. u8 i, j;
  422. if (scan_res == NULL || num_neigh_rep == 0)
  423. return 0;
  424. for (i = 0; i < num_neigh_rep; i++) {
  425. for (j = 0; j < scan_res->num; j++) {
  426. /* Check for a better RSSI AP */
  427. if (os_memcmp(scan_res->res[j]->bssid,
  428. neigh_rep[i].bssid, ETH_ALEN) == 0 &&
  429. scan_res->res[j]->level >
  430. wpa_s->current_bss->level) {
  431. /* Got a BSSID with better RSSI value */
  432. os_memcpy(bssid_to_connect, neigh_rep[i].bssid,
  433. ETH_ALEN);
  434. return 1;
  435. }
  436. }
  437. }
  438. return 0;
  439. }
  440. static void wnm_send_bss_transition_mgmt_resp(
  441. struct wpa_supplicant *wpa_s, u8 dialog_token,
  442. enum bss_trans_mgmt_status_code status, u8 delay,
  443. const u8 *target_bssid)
  444. {
  445. u8 buf[1000], *pos;
  446. struct ieee80211_mgmt *mgmt;
  447. size_t len;
  448. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
  449. "to " MACSTR " dialog_token=%u status=%u delay=%d",
  450. MAC2STR(wpa_s->bssid), dialog_token, status, delay);
  451. mgmt = (struct ieee80211_mgmt *) buf;
  452. os_memset(&buf, 0, sizeof(buf));
  453. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  454. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  455. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  456. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  457. WLAN_FC_STYPE_ACTION);
  458. mgmt->u.action.category = WLAN_ACTION_WNM;
  459. mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
  460. mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
  461. mgmt->u.action.u.bss_tm_resp.status_code = status;
  462. mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
  463. pos = mgmt->u.action.u.bss_tm_resp.variable;
  464. if (target_bssid) {
  465. os_memcpy(pos, target_bssid, ETH_ALEN);
  466. pos += ETH_ALEN;
  467. }
  468. len = pos - (u8 *) &mgmt->u.action.category;
  469. wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  470. wpa_s->own_addr, wpa_s->bssid,
  471. &mgmt->u.action.category, len, 0);
  472. }
  473. void wnm_scan_response(struct wpa_supplicant *wpa_s,
  474. struct wpa_scan_results *scan_res)
  475. {
  476. u8 bssid[ETH_ALEN];
  477. if (scan_res == NULL) {
  478. wpa_printf(MSG_ERROR, "Scan result is NULL");
  479. goto send_bss_resp_fail;
  480. }
  481. /* Compare the Neighbor Report and scan results */
  482. if (compare_scan_neighbor_results(wpa_s, scan_res,
  483. wpa_s->wnm_neighbor_report_elements,
  484. wpa_s->wnm_num_neighbor_report,
  485. bssid) == 1) {
  486. /* Associate to the network */
  487. struct wpa_bss *bss;
  488. struct wpa_ssid *ssid = wpa_s->current_ssid;
  489. bss = wpa_bss_get_bssid(wpa_s, bssid);
  490. if (!bss) {
  491. wpa_printf(MSG_DEBUG, "WNM: Target AP not found from "
  492. "BSS table");
  493. goto send_bss_resp_fail;
  494. }
  495. /* Send the BSS Management Response - Accept */
  496. if (wpa_s->wnm_reply) {
  497. wnm_send_bss_transition_mgmt_resp(wpa_s,
  498. wpa_s->wnm_dialog_token,
  499. WNM_BSS_TM_ACCEPT,
  500. 0, NULL);
  501. }
  502. wpa_s->reassociate = 1;
  503. wpa_supplicant_connect(wpa_s, bss, ssid);
  504. wnm_deallocate_memory(wpa_s);
  505. return;
  506. }
  507. /* Send reject response for all the failures */
  508. send_bss_resp_fail:
  509. wnm_deallocate_memory(wpa_s);
  510. if (wpa_s->wnm_reply) {
  511. wnm_send_bss_transition_mgmt_resp(wpa_s,
  512. wpa_s->wnm_dialog_token,
  513. WNM_BSS_TM_REJECT_UNSPECIFIED,
  514. 0, NULL);
  515. }
  516. return;
  517. }
  518. static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
  519. const u8 *pos, const u8 *end,
  520. int reply)
  521. {
  522. if (pos + 5 > end)
  523. return;
  524. wpa_s->wnm_dialog_token = pos[0];
  525. wpa_s->wnm_mode = pos[1];
  526. wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
  527. wpa_s->wnm_validity_interval = pos[4];
  528. wpa_s->wnm_reply = reply;
  529. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
  530. "dialog_token=%u request_mode=0x%x "
  531. "disassoc_timer=%u validity_interval=%u",
  532. wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
  533. wpa_s->wnm_dissoc_timer, wpa_s->wnm_validity_interval);
  534. pos += 5;
  535. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  536. if (pos + 12 > end) {
  537. wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
  538. return;
  539. }
  540. os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
  541. pos += 12; /* BSS Termination Duration */
  542. }
  543. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
  544. char url[256];
  545. unsigned int beacon_int;
  546. if (pos + 1 > end || pos + 1 + pos[0] > end) {
  547. wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
  548. "Management Request (URL)");
  549. return;
  550. }
  551. os_memcpy(url, pos + 1, pos[0]);
  552. url[pos[0]] = '\0';
  553. pos += 1 + pos[0];
  554. if (wpa_s->current_bss)
  555. beacon_int = wpa_s->current_bss->beacon_int;
  556. else
  557. beacon_int = 100; /* best guess */
  558. wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
  559. wpa_sm_pmf_enabled(wpa_s->wpa),
  560. wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
  561. }
  562. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  563. wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
  564. "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
  565. if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
  566. /* TODO: mark current BSS less preferred for
  567. * selection */
  568. wpa_printf(MSG_DEBUG, "Trying to find another BSS");
  569. wpa_supplicant_req_scan(wpa_s, 0, 0);
  570. }
  571. }
  572. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
  573. wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
  574. wpa_s->wnm_num_neighbor_report = 0;
  575. os_free(wpa_s->wnm_neighbor_report_elements);
  576. wpa_s->wnm_neighbor_report_elements = os_zalloc(
  577. WNM_MAX_NEIGHBOR_REPORT *
  578. sizeof(struct neighbor_report));
  579. if (wpa_s->wnm_neighbor_report_elements == NULL)
  580. return;
  581. while (pos + 2 <= end &&
  582. wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
  583. {
  584. u8 tag = *pos++;
  585. u8 len = *pos++;
  586. wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
  587. tag);
  588. if (pos + len > end) {
  589. wpa_printf(MSG_DEBUG, "WNM: Truncated request");
  590. return;
  591. }
  592. wnm_parse_neighbor_report(
  593. wpa_s, pos, len,
  594. &wpa_s->wnm_neighbor_report_elements[
  595. wpa_s->wnm_num_neighbor_report]);
  596. pos += len;
  597. wpa_s->wnm_num_neighbor_report++;
  598. }
  599. wpa_s->scan_res_handler = wnm_scan_response;
  600. wpa_supplicant_req_scan(wpa_s, 0, 0);
  601. } else if (reply) {
  602. enum bss_trans_mgmt_status_code status;
  603. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
  604. status = WNM_BSS_TM_ACCEPT;
  605. else {
  606. wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
  607. status = WNM_BSS_TM_REJECT_UNSPECIFIED;
  608. }
  609. wnm_send_bss_transition_mgmt_resp(wpa_s,
  610. wpa_s->wnm_dialog_token,
  611. status, 0, NULL);
  612. }
  613. }
  614. int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
  615. u8 query_reason)
  616. {
  617. u8 buf[1000], *pos;
  618. struct ieee80211_mgmt *mgmt;
  619. size_t len;
  620. int ret;
  621. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
  622. MACSTR " query_reason=%u",
  623. MAC2STR(wpa_s->bssid), query_reason);
  624. mgmt = (struct ieee80211_mgmt *) buf;
  625. os_memset(&buf, 0, sizeof(buf));
  626. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  627. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  628. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  629. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  630. WLAN_FC_STYPE_ACTION);
  631. mgmt->u.action.category = WLAN_ACTION_WNM;
  632. mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
  633. mgmt->u.action.u.bss_tm_query.dialog_token = 0;
  634. mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
  635. pos = mgmt->u.action.u.bss_tm_query.variable;
  636. len = pos - (u8 *) &mgmt->u.action.category;
  637. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  638. wpa_s->own_addr, wpa_s->bssid,
  639. &mgmt->u.action.category, len, 0);
  640. return ret;
  641. }
  642. void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
  643. struct rx_action *action)
  644. {
  645. const u8 *pos, *end;
  646. u8 act;
  647. if (action->data == NULL || action->len == 0)
  648. return;
  649. pos = action->data;
  650. end = pos + action->len;
  651. act = *pos++;
  652. wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
  653. act, MAC2STR(action->sa));
  654. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  655. os_memcmp(action->sa, wpa_s->bssid, ETH_ALEN) != 0) {
  656. wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
  657. "frame");
  658. return;
  659. }
  660. switch (act) {
  661. case WNM_BSS_TRANS_MGMT_REQ:
  662. ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
  663. !(action->da[0] & 0x01));
  664. break;
  665. case WNM_SLEEP_MODE_RESP:
  666. ieee802_11_rx_wnmsleep_resp(wpa_s, action->data, action->len);
  667. break;
  668. default:
  669. wpa_printf(MSG_ERROR, "WNM: Unknown request");
  670. break;
  671. }
  672. }