3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "ap/hostapd.h"
14 #include "ap/sta_info.h"
15 #include "ap/ap_config.h"
16 #include "ap/ap_drv_ops.h"
17 #include "ap/wpa_auth.h"
20 #define MAX_TFS_IE_LEN 1024
22 #ifdef CONFIG_IEEE80211V
24 /* get the TFS IE from driver */
25 static int ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
26 u8 *buf, u16 *buf_len, enum wnm_oper oper)
28 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
30 return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
34 /* set the TFS IE to driver */
35 static int ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
36 u8 *buf, u16 *buf_len, enum wnm_oper oper)
38 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
40 return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
44 /* MLME-SLEEPMODE.response */
45 static int ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd,
46 const u8 *addr, u8 dialog_token,
47 u8 action_type, u16 intval)
49 struct ieee80211_mgmt *mgmt;
52 size_t gtk_elem_len = 0;
53 size_t igtk_elem_len = 0;
54 struct wnm_sleep_element wnmsleep_ie;
60 enum wnm_oper tfs_oper = action_type == 0 ? WNM_SLEEP_TFS_RESP_IE_ADD :
61 WNM_SLEEP_TFS_RESP_IE_NONE;
63 sta = ap_get_sta(hapd, addr);
65 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
69 /* WNM-Sleep Mode IE */
70 os_memset(&wnmsleep_ie, 0, sizeof(struct wnm_sleep_element));
71 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
72 wnmsleep_ie.eid = WLAN_EID_WNMSLEEP;
73 wnmsleep_ie.len = wnmsleep_ie_len - 2;
74 wnmsleep_ie.action_type = action_type;
75 wnmsleep_ie.status = WNM_STATUS_SLEEP_ACCEPT;
76 wnmsleep_ie.intval = intval;
79 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
80 if (wnmtfs_ie == NULL)
82 if (ieee80211_11_get_tfs_ie(hapd, addr, wnmtfs_ie, &wnmtfs_ie_len,
89 #define MAX_GTK_SUBELEM_LEN 45
90 #define MAX_IGTK_SUBELEM_LEN 26
91 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len +
92 MAX_GTK_SUBELEM_LEN + MAX_IGTK_SUBELEM_LEN);
94 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
95 "WNM-Sleep Response action frame");
98 os_memcpy(mgmt->da, addr, ETH_ALEN);
99 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
100 os_memcpy(mgmt->bssid, hapd->own_addr, 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_resp.action = WNM_SLEEP_MODE_RESP;
105 mgmt->u.action.u.wnm_sleep_resp.dialogtoken = dialog_token;
106 pos = (u8 *)mgmt->u.action.u.wnm_sleep_resp.variable;
107 /* add key data if MFP is enabled */
108 if (wpa_auth_uses_mfp(sta->wpa_sm) || action_type != 1){
109 mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
111 gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
113 wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
115 #ifdef CONFIG_IEEE80211W
116 res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
123 pos += igtk_elem_len;
124 wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
125 (int) igtk_elem_len);
126 #endif /* CONFIG_IEEE80211W */
129 &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
130 gtk_elem_len + igtk_elem_len);
132 os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
133 /* copy TFS IE here */
134 pos += wnmsleep_ie_len;
135 os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
137 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
138 igtk_elem_len + wnmsleep_ie_len + wnmtfs_ie_len;
140 /* In driver, response frame should be forced to sent when STA is in
142 res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
143 mgmt->da, &mgmt->u.action.category, len);
146 wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
149 /* when entering wnmsleep
150 * 1. pause the node in driver
151 * 2. mark the node so that AP won't update GTK/IGTK during
154 if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
155 wnmsleep_ie.action_type == 0) {
156 hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
158 wpa_set_wnmsleep(sta->wpa_sm, 1);
160 /* when exiting wnmsleep
162 * 2. start GTK/IGTK update if MFP is not used
163 * 3. unpause the node in driver
165 if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
166 wnmsleep_ie.action_type == 1) {
167 wpa_set_wnmsleep(sta->wpa_sm, 0);
168 hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
170 if (wpa_auth_uses_mfp(sta->wpa_sm) && action_type == 1)
171 wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
174 wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
176 #undef MAX_GTK_SUBELEM_LEN
177 #undef MAX_IGTK_SUBELEM_LEN
184 static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
185 const u8 *addr, const u8 *frm, int len)
188 * Action [1] | Dialog Token [1] | WNM-Sleep Mode IE |
191 u8 *pos = (u8 *) frm; /* point to action field */
192 u8 dialog_token = pos[1];
193 struct wnm_sleep_element *wnmsleep_ie = NULL;
194 /* multiple TFS Req IE (assuming consecutive) */
195 u8 *tfsreq_ie_start = NULL;
196 u8 *tfsreq_ie_end = NULL;
197 u16 tfsreq_ie_len = 0;
200 while (pos - frm < len - 1) {
201 u8 ie_len = *(pos+1);
202 if (*pos == WLAN_EID_WNMSLEEP)
203 wnmsleep_ie = (struct wnm_sleep_element *)pos;
204 else if (*pos == WLAN_EID_TFS_REQ) {
205 if (!tfsreq_ie_start)
206 tfsreq_ie_start = pos;
209 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
214 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
218 if (wnmsleep_ie->action_type == 0 && tfsreq_ie_start &&
219 tfsreq_ie_end && tfsreq_ie_end - tfsreq_ie_start >= 0) {
220 tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
222 wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
223 /* pass the TFS Req IE(s) to driver for processing */
224 if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
226 WNM_SLEEP_TFS_REQ_IE_SET))
227 wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
230 ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
231 wnmsleep_ie->action_type,
232 wnmsleep_ie->intval);
234 if (wnmsleep_ie->action_type == 1) {
235 /* clear the tfs after sending the resp frame */
236 ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
237 &tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
242 void ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
243 struct rx_action *action)
245 u8 *pos = (u8 *) action->data + 1; /* point to the action field */
249 case WNM_SLEEP_MODE_REQ:
250 ieee802_11_rx_wnmsleep_req(hapd, action->sa, action->data + 1,
258 #endif /* CONFIG_IEEE80211V */