wnm_sta.c 21 KB

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