wnm_sta.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #define MAX_TFS_IE_LEN 1024
  15. #ifdef CONFIG_IEEE80211V
  16. /* get the TFS IE from driver */
  17. static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
  18. u16 *buf_len, enum wnm_oper oper)
  19. {
  20. wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
  21. return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
  22. }
  23. /* set the TFS IE to driver */
  24. static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
  25. const u8 *addr, u8 *buf, u16 *buf_len,
  26. enum wnm_oper oper)
  27. {
  28. wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
  29. return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
  30. }
  31. /* MLME-SLEEPMODE.request */
  32. int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
  33. u8 action, u16 intval, struct wpabuf *tfs_req)
  34. {
  35. struct ieee80211_mgmt *mgmt;
  36. int res;
  37. size_t len;
  38. struct wnm_sleep_element *wnmsleep_ie;
  39. u8 *wnmtfs_ie;
  40. u8 wnmsleep_ie_len;
  41. u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
  42. enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
  43. WNM_SLEEP_TFS_REQ_IE_NONE;
  44. wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
  45. "action=%s to " MACSTR,
  46. action == 0 ? "enter" : "exit",
  47. MAC2STR(wpa_s->bssid));
  48. /* WNM-Sleep Mode IE */
  49. wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
  50. wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
  51. if (wnmsleep_ie == NULL)
  52. return -1;
  53. wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
  54. wnmsleep_ie->len = wnmsleep_ie_len - 2;
  55. wnmsleep_ie->action_type = action;
  56. wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
  57. wnmsleep_ie->intval = host_to_le16(intval);
  58. wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
  59. (u8 *) wnmsleep_ie, wnmsleep_ie_len);
  60. /* TFS IE(s) */
  61. if (tfs_req) {
  62. wnmtfs_ie_len = wpabuf_len(tfs_req);
  63. wnmtfs_ie = os_malloc(wnmtfs_ie_len);
  64. if (wnmtfs_ie == NULL) {
  65. os_free(wnmsleep_ie);
  66. return -1;
  67. }
  68. os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
  69. } else {
  70. wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
  71. if (wnmtfs_ie == NULL) {
  72. os_free(wnmsleep_ie);
  73. return -1;
  74. }
  75. if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
  76. tfs_oper)) {
  77. wnmtfs_ie_len = 0;
  78. os_free(wnmtfs_ie);
  79. wnmtfs_ie = NULL;
  80. }
  81. }
  82. wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
  83. (u8 *) wnmtfs_ie, wnmtfs_ie_len);
  84. mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
  85. if (mgmt == NULL) {
  86. wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
  87. "WNM-Sleep Request action frame");
  88. return -1;
  89. }
  90. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  91. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  92. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  93. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  94. WLAN_FC_STYPE_ACTION);
  95. mgmt->u.action.category = WLAN_ACTION_WNM;
  96. mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
  97. mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
  98. os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
  99. wnmsleep_ie_len);
  100. /* copy TFS IE here */
  101. if (wnmtfs_ie_len > 0) {
  102. os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
  103. wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
  104. }
  105. len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
  106. wnmtfs_ie_len;
  107. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  108. wpa_s->own_addr, wpa_s->bssid,
  109. &mgmt->u.action.category, len, 0);
  110. if (res < 0)
  111. wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
  112. "(action=%d, intval=%d)", action, intval);
  113. os_free(wnmsleep_ie);
  114. os_free(wnmtfs_ie);
  115. os_free(mgmt);
  116. return res;
  117. }
  118. static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
  119. const u8 *frm, int len)
  120. {
  121. /*
  122. * Action [1] | Diaglog Token [1] | Key Data Len [2] | Key Data |
  123. * WNM-Sleep Mode IE | TFS Response IE
  124. */
  125. u8 *pos = (u8 *) frm; /* point to action field */
  126. u16 key_len_total = le_to_host16(*((u16 *)(frm+2)));
  127. u8 gtk_len;
  128. struct wnm_sleep_element *wnmsleep_ie = NULL;
  129. /* multiple TFS Resp IE (assuming consecutive) */
  130. u8 *tfsresp_ie_start = NULL;
  131. u8 *tfsresp_ie_end = NULL;
  132. u16 tfsresp_ie_len = 0;
  133. wpa_printf(MSG_DEBUG, "action=%d token = %d key_len_total = %d",
  134. frm[0], frm[1], key_len_total);
  135. pos += 4 + key_len_total;
  136. while (pos - frm < len) {
  137. u8 ie_len = *(pos + 1);
  138. if (*pos == WLAN_EID_WNMSLEEP)
  139. wnmsleep_ie = (struct wnm_sleep_element *) pos;
  140. else if (*pos == WLAN_EID_TFS_RESP) {
  141. if (!tfsresp_ie_start)
  142. tfsresp_ie_start = pos;
  143. tfsresp_ie_end = pos;
  144. } else
  145. wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
  146. pos += ie_len + 2;
  147. }
  148. if (!wnmsleep_ie) {
  149. wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
  150. return;
  151. }
  152. if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT) {
  153. wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
  154. "frame (action=%d, intval=%d)",
  155. wnmsleep_ie->action_type, wnmsleep_ie->intval);
  156. if (wnmsleep_ie->action_type == 0) {
  157. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
  158. wpa_s->bssid, NULL, NULL);
  159. /* remove GTK/IGTK ?? */
  160. /* set the TFS Resp IE(s) */
  161. if (tfsresp_ie_start && tfsresp_ie_end &&
  162. tfsresp_ie_end - tfsresp_ie_start >= 0) {
  163. tfsresp_ie_len = (tfsresp_ie_end +
  164. tfsresp_ie_end[1] + 2) -
  165. tfsresp_ie_start;
  166. wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
  167. /*
  168. * pass the TFS Resp IE(s) to driver for
  169. * processing
  170. */
  171. if (ieee80211_11_set_tfs_ie(
  172. wpa_s, wpa_s->bssid,
  173. tfsresp_ie_start,
  174. &tfsresp_ie_len,
  175. WNM_SLEEP_TFS_RESP_IE_SET))
  176. wpa_printf(MSG_DEBUG, "Fail to set "
  177. "TFS Resp IE");
  178. }
  179. } else if (wnmsleep_ie->action_type == 1) {
  180. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM,
  181. wpa_s->bssid, NULL, NULL);
  182. /* Install GTK/IGTK */
  183. do {
  184. /* point to key data field */
  185. u8 *ptr = (u8 *) frm + 1 + 1 + 2;
  186. while (ptr < (u8 *) frm + 4 + key_len_total) {
  187. if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
  188. gtk_len = *(ptr + 4);
  189. wpa_wnmsleep_install_key(
  190. wpa_s->wpa,
  191. WNM_SLEEP_SUBELEM_GTK,
  192. ptr);
  193. ptr += 13 + gtk_len;
  194. #ifdef CONFIG_IEEE80211W
  195. } else if (*ptr ==
  196. WNM_SLEEP_SUBELEM_IGTK) {
  197. wpa_wnmsleep_install_key(
  198. wpa_s->wpa,
  199. WNM_SLEEP_SUBELEM_IGTK,
  200. ptr);
  201. ptr += 10 + WPA_IGTK_LEN;
  202. #endif /* CONFIG_IEEE80211W */
  203. } else
  204. break; /* skip the loop */
  205. }
  206. } while(0);
  207. }
  208. } else {
  209. wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
  210. "(action=%d, intval=%d)",
  211. wnmsleep_ie->action_type, wnmsleep_ie->intval);
  212. if (wnmsleep_ie->action_type == 0)
  213. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
  214. wpa_s->bssid, NULL, NULL);
  215. else if (wnmsleep_ie->action_type == 1)
  216. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
  217. wpa_s->bssid, NULL, NULL);
  218. }
  219. }
  220. void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
  221. struct rx_action *action)
  222. {
  223. u8 *pos = (u8 *) action->data; /* point to action field */
  224. u8 act = *pos++;
  225. /* u8 dialog_token = *pos++; */
  226. switch (act) {
  227. case WNM_SLEEP_MODE_RESP:
  228. ieee802_11_rx_wnmsleep_resp(wpa_s, action->data, action->len);
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. #endif /* CONFIG_IEEE80211V */