]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wnm_sta.c
Document gas_query.c functions
[thirdparty/hostap.git] / wpa_supplicant / wnm_sta.c
CommitLineData
75cad1a0
XC
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
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "common/ieee802_11_defs.h"
13#include "rsn_supp/wpa.h"
61c54976
JM
14#include "wpa_supplicant_i.h"
15#include "driver_i.h"
75cad1a0
XC
16
17#define MAX_TFS_IE_LEN 1024
18
75cad1a0
XC
19
20/* get the TFS IE from driver */
21static 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
26 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
27}
28
29
30/* set the TFS IE to driver */
31static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
32 const u8 *addr, u8 *buf, u16 *buf_len,
33 enum wnm_oper oper)
34{
35 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
36
37 return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
38}
39
40
41/* MLME-SLEEPMODE.request */
42int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
cd0ef657 43 u8 action, u16 intval, struct wpabuf *tfs_req)
75cad1a0
XC
44{
45 struct ieee80211_mgmt *mgmt;
46 int res;
47 size_t len;
48 struct wnm_sleep_element *wnmsleep_ie;
49 u8 *wnmtfs_ie;
50 u8 wnmsleep_ie_len;
51 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
52 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
53 WNM_SLEEP_TFS_REQ_IE_NONE;
54
cd0ef657
JM
55 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
56 "action=%s to " MACSTR,
57 action == 0 ? "enter" : "exit",
58 MAC2STR(wpa_s->bssid));
59
75cad1a0
XC
60 /* WNM-Sleep Mode IE */
61 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
62 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
63 if (wnmsleep_ie == NULL)
64 return -1;
65 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
66 wnmsleep_ie->len = wnmsleep_ie_len - 2;
67 wnmsleep_ie->action_type = action;
68 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
e9199e31 69 wnmsleep_ie->intval = host_to_le16(intval);
cd0ef657
JM
70 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
71 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
75cad1a0
XC
72
73 /* TFS IE(s) */
cd0ef657
JM
74 if (tfs_req) {
75 wnmtfs_ie_len = wpabuf_len(tfs_req);
76 wnmtfs_ie = os_malloc(wnmtfs_ie_len);
77 if (wnmtfs_ie == NULL) {
78 os_free(wnmsleep_ie);
79 return -1;
80 }
81 os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
82 } else {
83 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
84 if (wnmtfs_ie == NULL) {
85 os_free(wnmsleep_ie);
86 return -1;
87 }
88 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
89 tfs_oper)) {
90 wnmtfs_ie_len = 0;
91 os_free(wnmtfs_ie);
92 wnmtfs_ie = NULL;
93 }
75cad1a0 94 }
cd0ef657
JM
95 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
96 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
75cad1a0
XC
97
98 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
99 if (mgmt == NULL) {
100 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
101 "WNM-Sleep Request action frame");
14df897c
JM
102 os_free(wnmsleep_ie);
103 os_free(wnmtfs_ie);
75cad1a0
XC
104 return -1;
105 }
106
107 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
108 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
109 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
110 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
111 WLAN_FC_STYPE_ACTION);
112 mgmt->u.action.category = WLAN_ACTION_WNM;
113 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
e0c54a15 114 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
75cad1a0
XC
115 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
116 wnmsleep_ie_len);
117 /* copy TFS IE here */
118 if (wnmtfs_ie_len > 0) {
119 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
120 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
121 }
122
123 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
124 wnmtfs_ie_len;
125
126 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
127 wpa_s->own_addr, wpa_s->bssid,
128 &mgmt->u.action.category, len, 0);
129 if (res < 0)
130 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
131 "(action=%d, intval=%d)", action, intval);
132
133 os_free(wnmsleep_ie);
134 os_free(wnmtfs_ie);
135 os_free(mgmt);
136
137 return res;
138}
139
140
74b4a360
JM
141static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
142 u8 *tfsresp_ie_start,
143 u8 *tfsresp_ie_end)
144{
145 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
146 wpa_s->bssid, NULL, NULL);
147 /* remove GTK/IGTK ?? */
148
149 /* set the TFS Resp IE(s) */
150 if (tfsresp_ie_start && tfsresp_ie_end &&
151 tfsresp_ie_end - tfsresp_ie_start >= 0) {
152 u16 tfsresp_ie_len;
153 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
154 tfsresp_ie_start;
155 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
156 /* pass the TFS Resp IE(s) to driver for processing */
157 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
158 tfsresp_ie_start,
159 &tfsresp_ie_len,
160 WNM_SLEEP_TFS_RESP_IE_SET))
161 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
162 }
163}
164
165
166static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
167 const u8 *frm, u16 key_len_total)
168{
169 u8 *ptr, *end;
170 u8 gtk_len;
171
172 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
173 NULL, NULL);
174
175 /* Install GTK/IGTK */
176
177 /* point to key data field */
178 ptr = (u8 *) frm + 1 + 1 + 2;
179 end = ptr + key_len_total;
180 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
181
182 while (ptr + 1 < end) {
183 if (ptr + 2 + ptr[1] > end) {
184 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
185 "length");
186 if (end > ptr) {
187 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
188 ptr, end - ptr);
189 }
190 break;
191 }
192 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
193 if (ptr[1] < 11 + 5) {
194 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
195 "subelem");
196 break;
197 }
198 gtk_len = *(ptr + 4);
199 if (ptr[1] < 11 + gtk_len ||
200 gtk_len < 5 || gtk_len > 32) {
201 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
202 "subelem");
203 break;
204 }
205 wpa_wnmsleep_install_key(
206 wpa_s->wpa,
207 WNM_SLEEP_SUBELEM_GTK,
208 ptr);
209 ptr += 13 + gtk_len;
210#ifdef CONFIG_IEEE80211W
211 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
212 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
213 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
214 "subelem");
215 break;
216 }
217 wpa_wnmsleep_install_key(wpa_s->wpa,
218 WNM_SLEEP_SUBELEM_IGTK, ptr);
219 ptr += 10 + WPA_IGTK_LEN;
220#endif /* CONFIG_IEEE80211W */
221 } else
222 break; /* skip the loop */
223 }
224}
225
226
75cad1a0
XC
227static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
228 const u8 *frm, int len)
229{
230 /*
231 * Action [1] | Diaglog Token [1] | Key Data Len [2] | Key Data |
232 * WNM-Sleep Mode IE | TFS Response IE
233 */
234 u8 *pos = (u8 *) frm; /* point to action field */
235 u16 key_len_total = le_to_host16(*((u16 *)(frm+2)));
75cad1a0
XC
236 struct wnm_sleep_element *wnmsleep_ie = NULL;
237 /* multiple TFS Resp IE (assuming consecutive) */
238 u8 *tfsresp_ie_start = NULL;
239 u8 *tfsresp_ie_end = NULL;
75cad1a0
XC
240
241 wpa_printf(MSG_DEBUG, "action=%d token = %d key_len_total = %d",
242 frm[0], frm[1], key_len_total);
243 pos += 4 + key_len_total;
68db9ab0
JM
244 if (pos > frm + len) {
245 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
246 return;
247 }
75cad1a0
XC
248 while (pos - frm < len) {
249 u8 ie_len = *(pos + 1);
68db9ab0
JM
250 if (pos + 2 + ie_len > frm + len) {
251 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
252 break;
253 }
254 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
75cad1a0
XC
255 if (*pos == WLAN_EID_WNMSLEEP)
256 wnmsleep_ie = (struct wnm_sleep_element *) pos;
257 else if (*pos == WLAN_EID_TFS_RESP) {
258 if (!tfsresp_ie_start)
259 tfsresp_ie_start = pos;
260 tfsresp_ie_end = pos;
261 } else
262 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
263 pos += ie_len + 2;
264 }
265
266 if (!wnmsleep_ie) {
267 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
268 return;
269 }
270
62f6fbb4
JM
271 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
272 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
75cad1a0
XC
273 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
274 "frame (action=%d, intval=%d)",
275 wnmsleep_ie->action_type, wnmsleep_ie->intval);
df80a0cc 276 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
74b4a360
JM
277 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
278 tfsresp_ie_end);
df80a0cc 279 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
74b4a360 280 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
75cad1a0
XC
281 }
282 } else {
283 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
284 "(action=%d, intval=%d)",
285 wnmsleep_ie->action_type, wnmsleep_ie->intval);
df80a0cc 286 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
75cad1a0
XC
287 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
288 wpa_s->bssid, NULL, NULL);
df80a0cc 289 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
75cad1a0
XC
290 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
291 wpa_s->bssid, NULL, NULL);
292 }
293}
294
295
296void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
297 struct rx_action *action)
298{
299 u8 *pos = (u8 *) action->data; /* point to action field */
300 u8 act = *pos++;
301 /* u8 dialog_token = *pos++; */
302
303 switch (act) {
304 case WNM_SLEEP_MODE_RESP:
305 ieee802_11_rx_wnmsleep_resp(wpa_s, action->data, action->len);
306 break;
307 default:
308 break;
309 }
310}