wnm_ap.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * hostapd - 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 "ap/hostapd.h"
  12. #include "ap/sta_info.h"
  13. #include "ap/ap_config.h"
  14. #include "ap/ap_drv_ops.h"
  15. #include "ap/wpa_auth.h"
  16. #include "wnm_ap.h"
  17. #define MAX_TFS_IE_LEN 1024
  18. /* get the TFS IE from driver */
  19. static int ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
  20. u8 *buf, u16 *buf_len, enum wnm_oper oper)
  21. {
  22. wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
  23. return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
  24. }
  25. /* set the TFS IE to driver */
  26. static int ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
  27. u8 *buf, u16 *buf_len, enum wnm_oper oper)
  28. {
  29. wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
  30. return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
  31. }
  32. /* MLME-SLEEPMODE.response */
  33. static int ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd,
  34. const u8 *addr, u8 dialog_token,
  35. u8 action_type, u16 intval)
  36. {
  37. struct ieee80211_mgmt *mgmt;
  38. int res;
  39. size_t len;
  40. size_t gtk_elem_len = 0;
  41. size_t igtk_elem_len = 0;
  42. struct wnm_sleep_element wnmsleep_ie;
  43. u8 *wnmtfs_ie;
  44. u8 wnmsleep_ie_len;
  45. u16 wnmtfs_ie_len;
  46. u8 *pos;
  47. struct sta_info *sta;
  48. enum wnm_oper tfs_oper = action_type == 0 ? WNM_SLEEP_TFS_RESP_IE_ADD :
  49. WNM_SLEEP_TFS_RESP_IE_NONE;
  50. sta = ap_get_sta(hapd, addr);
  51. if (sta == NULL) {
  52. wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
  53. return -EINVAL;
  54. }
  55. /* WNM-Sleep Mode IE */
  56. os_memset(&wnmsleep_ie, 0, sizeof(struct wnm_sleep_element));
  57. wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
  58. wnmsleep_ie.eid = WLAN_EID_WNMSLEEP;
  59. wnmsleep_ie.len = wnmsleep_ie_len - 2;
  60. wnmsleep_ie.action_type = action_type;
  61. wnmsleep_ie.status = WNM_STATUS_SLEEP_ACCEPT;
  62. wnmsleep_ie.intval = intval;
  63. /* TFS IE(s) */
  64. wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
  65. if (wnmtfs_ie == NULL)
  66. return -1;
  67. if (ieee80211_11_get_tfs_ie(hapd, addr, wnmtfs_ie, &wnmtfs_ie_len,
  68. tfs_oper)) {
  69. wnmtfs_ie_len = 0;
  70. os_free(wnmtfs_ie);
  71. wnmtfs_ie = NULL;
  72. }
  73. #define MAX_GTK_SUBELEM_LEN 45
  74. #define MAX_IGTK_SUBELEM_LEN 26
  75. mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len +
  76. MAX_GTK_SUBELEM_LEN + MAX_IGTK_SUBELEM_LEN);
  77. if (mgmt == NULL) {
  78. wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
  79. "WNM-Sleep Response action frame");
  80. return -1;
  81. }
  82. os_memcpy(mgmt->da, addr, ETH_ALEN);
  83. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  84. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  85. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  86. WLAN_FC_STYPE_ACTION);
  87. mgmt->u.action.category = WLAN_ACTION_WNM;
  88. mgmt->u.action.u.wnm_sleep_resp.action = WNM_SLEEP_MODE_RESP;
  89. mgmt->u.action.u.wnm_sleep_resp.dialogtoken = dialog_token;
  90. pos = (u8 *)mgmt->u.action.u.wnm_sleep_resp.variable;
  91. /* add key data if MFP is enabled */
  92. if (wpa_auth_uses_mfp(sta->wpa_sm) || action_type != 1){
  93. mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
  94. } else {
  95. gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
  96. pos += gtk_elem_len;
  97. wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
  98. (int) gtk_elem_len);
  99. #ifdef CONFIG_IEEE80211W
  100. res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
  101. if (res < 0) {
  102. os_free(wnmtfs_ie);
  103. os_free(mgmt);
  104. return -1;
  105. }
  106. igtk_elem_len = res;
  107. pos += igtk_elem_len;
  108. wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
  109. (int) igtk_elem_len);
  110. #endif /* CONFIG_IEEE80211W */
  111. WPA_PUT_LE16((u8 *)
  112. &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
  113. gtk_elem_len + igtk_elem_len);
  114. }
  115. os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
  116. /* copy TFS IE here */
  117. pos += wnmsleep_ie_len;
  118. os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
  119. len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
  120. igtk_elem_len + wnmsleep_ie_len + wnmtfs_ie_len;
  121. /* In driver, response frame should be forced to sent when STA is in
  122. * PS mode */
  123. res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
  124. mgmt->da, &mgmt->u.action.category, len);
  125. if (!res) {
  126. wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
  127. "frame");
  128. /* when entering wnmsleep
  129. * 1. pause the node in driver
  130. * 2. mark the node so that AP won't update GTK/IGTK during
  131. * WNM Sleep
  132. */
  133. if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
  134. wnmsleep_ie.action_type == 0) {
  135. hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
  136. addr, NULL, NULL);
  137. wpa_set_wnmsleep(sta->wpa_sm, 1);
  138. }
  139. /* when exiting wnmsleep
  140. * 1. unmark the node
  141. * 2. start GTK/IGTK update if MFP is not used
  142. * 3. unpause the node in driver
  143. */
  144. if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
  145. wnmsleep_ie.action_type == 1) {
  146. wpa_set_wnmsleep(sta->wpa_sm, 0);
  147. hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
  148. addr, NULL, NULL);
  149. if (wpa_auth_uses_mfp(sta->wpa_sm) && action_type == 1)
  150. wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
  151. }
  152. } else
  153. wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
  154. #undef MAX_GTK_SUBELEM_LEN
  155. #undef MAX_IGTK_SUBELEM_LEN
  156. os_free(wnmtfs_ie);
  157. os_free(mgmt);
  158. return res;
  159. }
  160. static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
  161. const u8 *addr, const u8 *frm, int len)
  162. {
  163. /* Dialog Token [1] | WNM-Sleep Mode IE | TFS Response IE */
  164. const u8 *pos = frm;
  165. u8 dialog_token;
  166. struct wnm_sleep_element *wnmsleep_ie = NULL;
  167. /* multiple TFS Req IE (assuming consecutive) */
  168. u8 *tfsreq_ie_start = NULL;
  169. u8 *tfsreq_ie_end = NULL;
  170. u16 tfsreq_ie_len = 0;
  171. dialog_token = *pos++;
  172. while (pos + 1 < frm + len) {
  173. u8 ie_len = pos[1];
  174. if (pos + 2 + ie_len > frm + len)
  175. break;
  176. if (*pos == WLAN_EID_WNMSLEEP)
  177. wnmsleep_ie = (struct wnm_sleep_element *) pos;
  178. else if (*pos == WLAN_EID_TFS_REQ) {
  179. if (!tfsreq_ie_start)
  180. tfsreq_ie_start = (u8 *) pos;
  181. tfsreq_ie_end = (u8 *) pos;
  182. } else
  183. wpa_printf(MSG_DEBUG, "WNM: EID %d not recognized",
  184. *pos);
  185. pos += ie_len + 2;
  186. }
  187. if (!wnmsleep_ie) {
  188. wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
  189. return;
  190. }
  191. if (wnmsleep_ie->action_type == 0 && tfsreq_ie_start &&
  192. tfsreq_ie_end && tfsreq_ie_end - tfsreq_ie_start >= 0) {
  193. tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
  194. tfsreq_ie_start;
  195. wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
  196. /* pass the TFS Req IE(s) to driver for processing */
  197. if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
  198. &tfsreq_ie_len,
  199. WNM_SLEEP_TFS_REQ_IE_SET))
  200. wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
  201. }
  202. ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
  203. wnmsleep_ie->action_type,
  204. wnmsleep_ie->intval);
  205. if (wnmsleep_ie->action_type == 1) {
  206. /* clear the tfs after sending the resp frame */
  207. ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
  208. &tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
  209. }
  210. }
  211. int ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
  212. struct rx_action *action)
  213. {
  214. if (action->len < 1 || action->data == NULL)
  215. return -1;
  216. switch (action->data[0]) {
  217. case WNM_SLEEP_MODE_REQ:
  218. ieee802_11_rx_wnmsleep_req(hapd, action->sa, action->data + 1,
  219. action->len - 1);
  220. return 0;
  221. }
  222. wpa_printf(MSG_DEBUG, "WNM: Unsupported WNM Action %u from " MACSTR,
  223. action->data[0], MAC2STR(action->sa));
  224. return -1;
  225. }