]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/wnm_ap.c
hostapd: Make STA flags available through ctrl_iface STA command
[thirdparty/hostap.git] / src / ap / wnm_ap.c
CommitLineData
d32d94db
XC
1/*
2 * hostapd - WNM
2025cad9 3 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
d32d94db
XC
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"
2025cad9 12#include "utils/eloop.h"
d32d94db
XC
13#include "common/ieee802_11_defs.h"
14#include "ap/hostapd.h"
15#include "ap/sta_info.h"
16#include "ap/ap_config.h"
17#include "ap/ap_drv_ops.h"
18#include "ap/wpa_auth.h"
19#include "wnm_ap.h"
20
21#define MAX_TFS_IE_LEN 1024
22
d32d94db
XC
23
24/* get the TFS IE from driver */
25static int ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
26 u8 *buf, u16 *buf_len, enum wnm_oper oper)
27{
28 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
29
30 return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
31}
32
33
34/* set the TFS IE to driver */
35static int ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
36 u8 *buf, u16 *buf_len, enum wnm_oper oper)
37{
38 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
39
40 return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
41}
42
43
44/* MLME-SLEEPMODE.response */
45static int ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd,
46 const u8 *addr, u8 dialog_token,
47 u8 action_type, u16 intval)
48{
49 struct ieee80211_mgmt *mgmt;
50 int res;
51 size_t len;
52 size_t gtk_elem_len = 0;
53 size_t igtk_elem_len = 0;
54 struct wnm_sleep_element wnmsleep_ie;
55 u8 *wnmtfs_ie;
56 u8 wnmsleep_ie_len;
57 u16 wnmtfs_ie_len;
58 u8 *pos;
59 struct sta_info *sta;
615a5d55
JM
60 enum wnm_oper tfs_oper = action_type == WNM_SLEEP_MODE_ENTER ?
61 WNM_SLEEP_TFS_RESP_IE_ADD : WNM_SLEEP_TFS_RESP_IE_NONE;
d32d94db
XC
62
63 sta = ap_get_sta(hapd, addr);
64 if (sta == NULL) {
65 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
66 return -EINVAL;
67 }
68
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;
77
78 /* TFS IE(s) */
79 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
80 if (wnmtfs_ie == NULL)
81 return -1;
82 if (ieee80211_11_get_tfs_ie(hapd, addr, wnmtfs_ie, &wnmtfs_ie_len,
83 tfs_oper)) {
84 wnmtfs_ie_len = 0;
85 os_free(wnmtfs_ie);
86 wnmtfs_ie = NULL;
87 }
88
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);
93 if (mgmt == NULL) {
94 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
95 "WNM-Sleep Response action frame");
96 return -1;
97 }
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 */
4da10640 108 if (!wpa_auth_uses_mfp(sta->wpa_sm) ||
615a5d55 109 action_type != WNM_SLEEP_MODE_EXIT) {
d32d94db
XC
110 mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
111 } else {
112 gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
113 pos += gtk_elem_len;
114 wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
115 (int) gtk_elem_len);
116#ifdef CONFIG_IEEE80211W
117 res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
118 if (res < 0) {
119 os_free(wnmtfs_ie);
120 os_free(mgmt);
121 return -1;
122 }
123 igtk_elem_len = res;
124 pos += igtk_elem_len;
125 wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
126 (int) igtk_elem_len);
127#endif /* CONFIG_IEEE80211W */
128
129 WPA_PUT_LE16((u8 *)
130 &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
131 gtk_elem_len + igtk_elem_len);
132 }
133 os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
134 /* copy TFS IE here */
135 pos += wnmsleep_ie_len;
a8e93a1a
JM
136 if (wnmtfs_ie)
137 os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
d32d94db
XC
138
139 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
140 igtk_elem_len + wnmsleep_ie_len + wnmtfs_ie_len;
141
142 /* In driver, response frame should be forced to sent when STA is in
143 * PS mode */
144 res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
145 mgmt->da, &mgmt->u.action.category, len);
146
147 if (!res) {
148 wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
149 "frame");
150
151 /* when entering wnmsleep
152 * 1. pause the node in driver
153 * 2. mark the node so that AP won't update GTK/IGTK during
154 * WNM Sleep
155 */
156 if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
615a5d55 157 wnmsleep_ie.action_type == WNM_SLEEP_MODE_ENTER) {
3578e665 158 sta->flags |= WLAN_STA_WNM_SLEEP_MODE;
d32d94db
XC
159 hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
160 addr, NULL, NULL);
161 wpa_set_wnmsleep(sta->wpa_sm, 1);
162 }
163 /* when exiting wnmsleep
164 * 1. unmark the node
165 * 2. start GTK/IGTK update if MFP is not used
166 * 3. unpause the node in driver
167 */
4da10640
JM
168 if ((wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT ||
169 wnmsleep_ie.status ==
170 WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) &&
615a5d55 171 wnmsleep_ie.action_type == WNM_SLEEP_MODE_EXIT) {
3578e665 172 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
d32d94db
XC
173 wpa_set_wnmsleep(sta->wpa_sm, 0);
174 hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
175 addr, NULL, NULL);
4da10640 176 if (!wpa_auth_uses_mfp(sta->wpa_sm))
d32d94db
XC
177 wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
178 }
179 } else
180 wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
181
182#undef MAX_GTK_SUBELEM_LEN
183#undef MAX_IGTK_SUBELEM_LEN
184 os_free(wnmtfs_ie);
185 os_free(mgmt);
186 return res;
187}
188
189
190static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
191 const u8 *addr, const u8 *frm, int len)
192{
c79938a5
JM
193 /* Dialog Token [1] | WNM-Sleep Mode IE | TFS Response IE */
194 const u8 *pos = frm;
195 u8 dialog_token;
d32d94db
XC
196 struct wnm_sleep_element *wnmsleep_ie = NULL;
197 /* multiple TFS Req IE (assuming consecutive) */
198 u8 *tfsreq_ie_start = NULL;
199 u8 *tfsreq_ie_end = NULL;
200 u16 tfsreq_ie_len = 0;
201
c79938a5
JM
202 dialog_token = *pos++;
203 while (pos + 1 < frm + len) {
204 u8 ie_len = pos[1];
205 if (pos + 2 + ie_len > frm + len)
206 break;
d32d94db 207 if (*pos == WLAN_EID_WNMSLEEP)
c79938a5 208 wnmsleep_ie = (struct wnm_sleep_element *) pos;
d32d94db
XC
209 else if (*pos == WLAN_EID_TFS_REQ) {
210 if (!tfsreq_ie_start)
c79938a5
JM
211 tfsreq_ie_start = (u8 *) pos;
212 tfsreq_ie_end = (u8 *) pos;
d32d94db 213 } else
c79938a5
JM
214 wpa_printf(MSG_DEBUG, "WNM: EID %d not recognized",
215 *pos);
d32d94db
XC
216 pos += ie_len + 2;
217 }
218
219 if (!wnmsleep_ie) {
220 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
221 return;
222 }
223
615a5d55
JM
224 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER &&
225 tfsreq_ie_start && tfsreq_ie_end &&
226 tfsreq_ie_end - tfsreq_ie_start >= 0) {
d32d94db
XC
227 tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
228 tfsreq_ie_start;
229 wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
230 /* pass the TFS Req IE(s) to driver for processing */
231 if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
232 &tfsreq_ie_len,
233 WNM_SLEEP_TFS_REQ_IE_SET))
234 wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
235 }
236
237 ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
238 wnmsleep_ie->action_type,
239 wnmsleep_ie->intval);
240
615a5d55 241 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
d32d94db
XC
242 /* clear the tfs after sending the resp frame */
243 ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
244 &tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
245 }
246}
247
248
28ab64af
JM
249static int ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd,
250 const u8 *addr,
251 u8 dialog_token,
252 const char *url)
253{
254 struct ieee80211_mgmt *mgmt;
255 size_t url_len, len;
256 u8 *pos;
257 int res;
258
259 if (url)
260 url_len = os_strlen(url);
261 else
262 url_len = 0;
263
264 mgmt = os_zalloc(sizeof(*mgmt) + (url_len ? 1 + url_len : 0));
265 if (mgmt == NULL)
266 return -1;
267 os_memcpy(mgmt->da, addr, ETH_ALEN);
268 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
269 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
270 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
271 WLAN_FC_STYPE_ACTION);
272 mgmt->u.action.category = WLAN_ACTION_WNM;
273 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
274 mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
275 mgmt->u.action.u.bss_tm_req.req_mode = 0;
276 mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
277 mgmt->u.action.u.bss_tm_req.validity_interval = 1;
278 pos = mgmt->u.action.u.bss_tm_req.variable;
279 if (url) {
280 *pos++ += url_len;
281 os_memcpy(pos, url, url_len);
282 pos += url_len;
283 }
284
285 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
286 MACSTR " dialog_token=%u req_mode=0x%x disassoc_timer=%u "
287 "validity_interval=%u",
288 MAC2STR(addr), dialog_token,
289 mgmt->u.action.u.bss_tm_req.req_mode,
290 le_to_host16(mgmt->u.action.u.bss_tm_req.disassoc_timer),
291 mgmt->u.action.u.bss_tm_req.validity_interval);
292
293 len = pos - &mgmt->u.action.category;
294 res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
295 mgmt->da, &mgmt->u.action.category, len);
296 os_free(mgmt);
297 return res;
298}
299
300
301static void ieee802_11_rx_bss_trans_mgmt_query(struct hostapd_data *hapd,
302 const u8 *addr, const u8 *frm,
303 size_t len)
304{
305 u8 dialog_token, reason;
306 const u8 *pos, *end;
307
308 if (len < 2) {
309 wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Query from "
310 MACSTR, MAC2STR(addr));
311 return;
312 }
313
314 pos = frm;
315 end = pos + len;
316 dialog_token = *pos++;
317 reason = *pos++;
318
319 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Query from "
320 MACSTR " dialog_token=%u reason=%u",
321 MAC2STR(addr), dialog_token, reason);
322
323 wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
324 pos, end - pos);
325
326 ieee802_11_send_bss_trans_mgmt_request(hapd, addr, dialog_token, NULL);
327}
328
329
330static void ieee802_11_rx_bss_trans_mgmt_resp(struct hostapd_data *hapd,
331 const u8 *addr, const u8 *frm,
332 size_t len)
333{
334 u8 dialog_token, status_code, bss_termination_delay;
335 const u8 *pos, *end;
336
337 if (len < 3) {
338 wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Response from "
339 MACSTR, MAC2STR(addr));
340 return;
341 }
342
343 pos = frm;
344 end = pos + len;
345 dialog_token = *pos++;
346 status_code = *pos++;
347 bss_termination_delay = *pos++;
348
349 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Response from "
350 MACSTR " dialog_token=%u status_code=%u "
351 "bss_termination_delay=%u", MAC2STR(addr), dialog_token,
352 status_code, bss_termination_delay);
353
354 if (status_code == WNM_BSS_TM_ACCEPT) {
355 if (end - pos < ETH_ALEN) {
356 wpa_printf(MSG_DEBUG, "WNM: not enough room for Target BSSID field");
357 return;
358 }
359 wpa_printf(MSG_DEBUG, "WNM: Target BSSID: " MACSTR,
360 MAC2STR(pos));
361 pos += ETH_ALEN;
362 }
363
364 wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
365 pos, end - pos);
366}
367
368
c79938a5
JM
369int ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
370 struct rx_action *action)
d32d94db 371{
c79938a5
JM
372 if (action->len < 1 || action->data == NULL)
373 return -1;
d32d94db 374
c79938a5 375 switch (action->data[0]) {
2049a875 376 case WNM_BSS_TRANS_MGMT_QUERY:
28ab64af
JM
377 ieee802_11_rx_bss_trans_mgmt_query(hapd, action->sa,
378 action->data + 1,
379 action->len - 1);
380 return 0;
2049a875 381 case WNM_BSS_TRANS_MGMT_RESP:
28ab64af
JM
382 ieee802_11_rx_bss_trans_mgmt_resp(hapd, action->sa,
383 action->data + 1,
384 action->len - 1);
385 return 0;
d32d94db
XC
386 case WNM_SLEEP_MODE_REQ:
387 ieee802_11_rx_wnmsleep_req(hapd, action->sa, action->data + 1,
c79938a5
JM
388 action->len - 1);
389 return 0;
d32d94db 390 }
c79938a5
JM
391
392 wpa_printf(MSG_DEBUG, "WNM: Unsupported WNM Action %u from " MACSTR,
393 action->data[0], MAC2STR(action->sa));
394 return -1;
d32d94db 395}
2025cad9
JM
396
397
398int wnm_send_ess_disassoc_imminent(struct hostapd_data *hapd,
399 struct sta_info *sta, const char *url,
400 int disassoc_timer)
401{
402 u8 buf[1000], *pos;
403 struct ieee80211_mgmt *mgmt;
404 size_t url_len;
405
406 os_memset(buf, 0, sizeof(buf));
407 mgmt = (struct ieee80211_mgmt *) buf;
408 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
409 WLAN_FC_STYPE_ACTION);
410 os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
411 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
412 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
413 mgmt->u.action.category = WLAN_ACTION_WNM;
414 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
415 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
416 mgmt->u.action.u.bss_tm_req.req_mode =
417 WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
418 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
419 mgmt->u.action.u.bss_tm_req.disassoc_timer =
420 host_to_le16(disassoc_timer);
421 mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
422
423 pos = mgmt->u.action.u.bss_tm_req.variable;
424
425 /* Session Information URL */
426 url_len = os_strlen(url);
427 if (url_len > 255)
428 return -1;
429 *pos++ = url_len;
430 os_memcpy(pos, url, url_len);
431 pos += url_len;
432
433 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
434 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
435 "Management Request frame");
436 return -1;
437 }
438
439 /* send disassociation frame after time-out */
440 if (disassoc_timer) {
441 int timeout, beacon_int;
442
443 /*
444 * Prevent STA from reconnecting using cached PMKSA to force
445 * full authentication with the authentication server (which may
446 * decide to reject the connection),
447 */
448 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
449
450 beacon_int = hapd->iconf->beacon_int;
451 if (beacon_int < 1)
452 beacon_int = 100; /* best guess */
453 /* Calculate timeout in ms based on beacon_int in TU */
454 timeout = disassoc_timer * beacon_int * 128 / 125;
455 wpa_printf(MSG_DEBUG, "Disassociation timer for " MACSTR
456 " set to %d ms", MAC2STR(sta->addr), timeout);
457
458 sta->timeout_next = STA_DISASSOC_FROM_CLI;
459 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
460 eloop_register_timeout(timeout / 1000,
461 timeout % 1000 * 1000,
462 ap_handle_timer, hapd, sta);
463 }
464
465 return 0;
466}