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